- java.lang.Object
-
- org.logicng.formulas.Formula
-
- Direct Known Subclasses:
BinaryOperator,Constant,Literal,NAryOperator,Not,PBConstraint
public abstract class Formula extends Object implements Iterable<Formula>
Super class for formulas.- Since:
- 1.0
- Version:
- 2.4.0
-
-
Field Summary
Fields Modifier and Type Field Description protected FormulaFactoryfprotected FTypetype
-
Constructor Summary
Constructors Modifier Constructor Description protectedFormula(FType type, FormulaFactory f)Constructs a new formula.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <T> Tapply(FormulaFunction<T> function)Applies a given function on this formula, caches and returns the result.<T> Tapply(FormulaFunction<T> function, boolean cache)Applies a given function on this formula and returns the result.BDDbdd()Generates a BDD from this formula with no given variable ordering.BDDbdd(VariableOrdering variableOrdering)Generates a BDD from this formula with a given variable ordering.voidclearCaches()Clears the transformation and function cache of the formula.Formulacnf()Returns a copy of this formula which is in CNF.abstract booleancontainsNode(Formula formula)Returnstrueif this formula contains a given node,falseotherwise.booleancontainsVariable(String variable)Returnstrueif a given variable name is found in this formula,falseotherwise.abstract booleancontainsVariable(Variable variable)Returnstrueif a given variable name is found in this formula,falseotherwise.abstract booleanevaluate(Assignment assignment)Evaluates this formula with a given assignment.FormulaFactoryfactory()Returns the factory of this formula.ObjectfunctionCacheEntry(CacheEntry key)Returns an entry of the function cache of this formula.booleanholds(FormulaPredicate predicate)Evaluates a given predicate on this formula, caches the result, and returnstrueif the predicate holds,falseotherwise.booleanholds(FormulaPredicate predicate, boolean cache)Evaluates a given predicate on this formula and returnstrueif the predicate holds,falseotherwise.booleanimplies(Formula other)Returns whether this formula implies the given other formula, i.e.abstract booleanisAtomicFormula()Returns whether this formula is an atomic formula (constant, literal, pseudo Boolean constraint), or not.booleanisCNF()Returnstrueif this formula is in CNF, otherwisefalseabstract booleanisConstantFormula()Returns whether this formula is a constant formula ("True" or "False").booleanisContradiction()Returns whether this formula is a contradiction, hence always false.booleanisDNF()Returnstrueif this formula is in DNF, otherwisefalsebooleanisEquivalentTo(Formula other)Returns whether this formula is equivalent to the given other formula, i.e.booleanisImpliedBy(Formula other)Returns whether this formula is implied by the given other formula, i.e.booleanisNNF()Returnstrueif this formula is in NNF, otherwisefalsebooleanisSatisfiable()Returns whether this formula is satisfiable.booleanisTautology()Returns whether this formula is a tautology, hence always true.SortedSet<Literal>literals()Returns all literals occurring in this formula.abstract Formulanegate()Returns a negated copy of this formula.Formulannf()Returns a copy of this formula which is in NNF.longnumberOfAtoms()Returns the number of atomic formulas of this formula.longnumberOfInternalNodes()Returns the number of internal nodes of this formula.longnumberOfNodes()Returns the number of nodes of this formula.abstract intnumberOfOperands()Returns the number of operands of this formula.TristatepredicateCacheEntry(CacheEntry key)Returns an entry of the predicate cache of this formula.abstract Formularestrict(Assignment assignment)Restricts this formula with a given assignment.voidsetFunctionCacheEntry(CacheEntry key, Object value)Sets an entry in the function cache of this formulavoidsetPredicateCacheEntry(CacheEntry key, boolean value)Sets an entry in the predicate cache of this formulavoidsetPredicateCacheEntry(CacheEntry key, Tristate value)Sets an entry in the predicate cache of this formulavoidsetTransformationCacheEntry(CacheEntry key, Formula value)Sets an entry in the transformation cache of this formulaabstract Stream<Formula>stream()Returns a stream of this formula's operands.abstract Formulasubstitute(Substitution substitution)Performs a given substitution on this formula.Formulasubstitute(Variable variable, Formula formula)Performs a simultaneous substitution on this formula given a single mapping from variable to formula.StringtoString()Formulatransform(FormulaTransformation transformation)Transforms this formula with a given formula transformator and caches the result.Formulatransform(FormulaTransformation transformation, boolean cache)Transforms this formula with a given formula transformator.FormulatransformationCacheEntry(CacheEntry key)Returns an entry of the transformation cache of this formula.FTypetype()Returns the type of this formula.SortedSet<Variable>variables()Returns all variables occurring in this formula.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
-
-
-
Field Detail
-
type
protected final FType type
-
f
protected final FormulaFactory f
-
-
Constructor Detail
-
Formula
protected Formula(FType type, FormulaFactory f)
Constructs a new formula.- Parameters:
type- the type of the formulaf- the factory which created this formula
-
-
Method Detail
-
type
public FType type()
Returns the type of this formula.- Returns:
- the type of this formula
-
factory
public FormulaFactory factory()
Returns the factory of this formula.- Returns:
- the factory of this formula
-
numberOfAtoms
public long numberOfAtoms()
Returns the number of atomic formulas of this formula. An atomic formula is a predicate (constants and literals) or a pseudo-Boolean constraint.- Returns:
- the number of atomic formulas of this formula.
-
numberOfNodes
public long numberOfNodes()
Returns the number of nodes of this formula.- Returns:
- the number of nodes of this formula.
-
numberOfOperands
public abstract int numberOfOperands()
Returns the number of operands of this formula.- Returns:
- the number of operands of this formula
-
numberOfInternalNodes
public long numberOfInternalNodes()
Returns the number of internal nodes of this formula.- Returns:
- the number of internal nodes of this formula.
-
isConstantFormula
public abstract boolean isConstantFormula()
Returns whether this formula is a constant formula ("True" or "False").- Returns:
trueif this formula is a constant formula,falseotherwise
-
isAtomicFormula
public abstract boolean isAtomicFormula()
Returns whether this formula is an atomic formula (constant, literal, pseudo Boolean constraint), or not.- Returns:
trueif this formula is an atomic formula,falseotherwise
-
variables
public SortedSet<Variable> variables()
Returns all variables occurring in this formula. Returns an unmodifiable set, so do not try to change the variable set manually.- Returns:
- all variables occurring in this formula
-
literals
public SortedSet<Literal> literals()
Returns all literals occurring in this formula. Returns an unmodifiable set, so do not try to change the literal set manually.- Returns:
- all literals occurring in this formula
-
containsVariable
public boolean containsVariable(String variable)
Returnstrueif a given variable name is found in this formula,falseotherwise.- Parameters:
variable- the variable to search for- Returns:
trueif a given variable is found in this formula
-
containsVariable
public abstract boolean containsVariable(Variable variable)
Returnstrueif a given variable name is found in this formula,falseotherwise.- Parameters:
variable- the variable to search for- Returns:
trueif a given variable is found in this formula
-
evaluate
public abstract boolean evaluate(Assignment assignment)
Evaluates this formula with a given assignment. A literal not covered by the assignment evaluates tofalseif it is positive, otherwise it evaluates totrue.- Parameters:
assignment- the given assignment- Returns:
- the result of the evaluation,
trueorfalse
-
restrict
public abstract Formula restrict(Assignment assignment)
Restricts this formula with a given assignment.- Parameters:
assignment- the given assignment- Returns:
- a new restricted formula
-
containsNode
public abstract boolean containsNode(Formula formula)
Returnstrueif this formula contains a given node,falseotherwise.In particular, a
Literalnode~adoes NOT contain the nodea.- Parameters:
formula- the node- Returns:
trueif this formula contains a given node
-
isNNF
public boolean isNNF()
Returnstrueif this formula is in NNF, otherwisefalse- Returns:
trueif this formula is in NNF, otherwisefalse- See Also:
the NNF predicate
-
isDNF
public boolean isDNF()
Returnstrueif this formula is in DNF, otherwisefalse- Returns:
trueif this formula is in DNF, otherwisefalse- See Also:
the DNF predicate
-
isCNF
public boolean isCNF()
Returnstrueif this formula is in CNF, otherwisefalse- Returns:
trueif this formula is in CNF, otherwisefalse- See Also:
the CNF predicate
-
substitute
public Formula substitute(Variable variable, Formula formula)
Performs a simultaneous substitution on this formula given a single mapping from variable to formula.- Parameters:
variable- the variableformula- the formula- Returns:
- a new substituted formula
-
substitute
public abstract Formula substitute(Substitution substitution)
Performs a given substitution on this formula.- Parameters:
substitution- the substitution- Returns:
- a new substituted formula
-
negate
public abstract Formula negate()
Returns a negated copy of this formula.- Returns:
- a negated copy of this formula
-
nnf
public Formula nnf()
Returns a copy of this formula which is in NNF.- Returns:
- a copy of this formula which is in NNF
-
cnf
public Formula cnf()
Returns a copy of this formula which is in CNF. The algorithm which is used for the default CNF transformation can be configured in theFormulaFactory.Be aware that the default algorithm for the CNF transformation may result in a CNF containing additional auxiliary variables with prefix "@RESERVED_CNF_". Also, the result may not be a semantically equivalent CNF but an equisatisfiable CNF.
If the introduction of auxiliary variables is unwanted, you can choose one of the algorithms
CNFConfig.Algorithm.FACTORIZATIONandCNFConfig.Algorithm.BDD. Both algorithms provide CNF conversions without the introduction of auxiliary variables and the result is a semantically equivalent CNF.Since CNF is the input for the SAT or MaxSAT solvers, it has a special treatment here. For other conversions, use the according formula functions.
- Returns:
- a copy of this formula which is in CNF
-
isSatisfiable
public boolean isSatisfiable()
Returns whether this formula is satisfiable. A new SAT solver is used to check the satisfiability. This is a convenience method for the predicateSATPredicate. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use aSATSolveron your own.- Returns:
truewhen this formula is satisfiable,falseotherwise
-
isTautology
public boolean isTautology()
Returns whether this formula is a tautology, hence always true. A new SAT solver is used to check the tautology. This is a convenience method for the predicateTautologyPredicate. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use aSATSolveron your own.- Returns:
truewhen this formula is a tautology,falseotherwise
-
isContradiction
public boolean isContradiction()
Returns whether this formula is a contradiction, hence always false. A new SAT solver is used to check the contradiction. This is a convenience method for the predicateContradictionPredicate. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use aSATSolveron your own.- Returns:
truewhen this formula is a contradiction,falseotherwise
-
implies
public boolean implies(Formula other)
Returns whether this formula implies the given other formula, i.e. `this => other` is a tautology. A new SAT solver is used to check this tautology. This is a convenience method. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use aSATSolveron your own.- Parameters:
other- the formula which should be checked if it is implied by this formula- Returns:
truewhen this formula implies the given other formula,falseotherwise
-
isImpliedBy
public boolean isImpliedBy(Formula other)
Returns whether this formula is implied by the given other formula, i.e. `other => this` is a tautology. A new SAT solver is used to check this tautology. This is a convenience method. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use aSATSolveron your own.- Parameters:
other- the formula which should be checked if it implies this formula- Returns:
truewhen this formula is implied by the given other formula,falseotherwise
-
isEquivalentTo
public boolean isEquivalentTo(Formula other)
Returns whether this formula is equivalent to the given other formula, i.e. `other <=> this` is a tautology. A new SAT solver is used to check this tautology. This is a convenience method. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use aSATSolveron your own.- Parameters:
other- the formula which should be checked if it is equivalent with this formula- Returns:
truewhen this formula is equivalent to the given other formula,falseotherwise
-
bdd
public BDD bdd(VariableOrdering variableOrdering)
Generates a BDD from this formula with a given variable ordering. This is done by generating a new BDD factory, generating the variable order for this formula, and building a new BDD. If more sophisticated operations should be performed on the BDD or more than one formula should be constructed on the BDD, an own instance ofBDDFactoryshould be created and used.- Parameters:
variableOrdering- the variable ordering- Returns:
- the BDD for this formula with the given ordering
-
bdd
public BDD bdd()
Generates a BDD from this formula with no given variable ordering. This is done by generating a new BDD factory and building a new BDD. If more sophisticated operations should be performed on the BDD or more than one formula should be constructed on the BDD, an own instance of *BDDFactoryshould be created and used.- Returns:
- the BDD for this formula
-
transform
public Formula transform(FormulaTransformation transformation)
Transforms this formula with a given formula transformator and caches the result.- Parameters:
transformation- the formula transformator- Returns:
- the transformed formula
-
transform
public Formula transform(FormulaTransformation transformation, boolean cache)
Transforms this formula with a given formula transformator.- Parameters:
transformation- the formula transformatorcache- indicates whether the result (and associated predicates) should be cached in this formula's cache.- Returns:
- the transformed formula
-
holds
public boolean holds(FormulaPredicate predicate)
Evaluates a given predicate on this formula, caches the result, and returnstrueif the predicate holds,falseotherwise.- Parameters:
predicate- the predicate- Returns:
trueif the predicate holds,falseotherwise
-
holds
public boolean holds(FormulaPredicate predicate, boolean cache)
Evaluates a given predicate on this formula and returnstrueif the predicate holds,falseotherwise.- Parameters:
predicate- the predicatecache- indicates whether the result should be cached in this formula's cache- Returns:
trueif the predicate holds,falseotherwise
-
apply
public <T> T apply(FormulaFunction<T> function, boolean cache)
Applies a given function on this formula and returns the result.- Type Parameters:
T- the result type of the function- Parameters:
function- the functioncache- indicates whether the result should be cached in this formula's cache- Returns:
- the result of the function application
-
apply
public <T> T apply(FormulaFunction<T> function)
Applies a given function on this formula, caches and returns the result.- Type Parameters:
T- the result type of the function- Parameters:
function- the function- Returns:
- the result of the function application
-
transformationCacheEntry
public Formula transformationCacheEntry(CacheEntry key)
Returns an entry of the transformation cache of this formula.- Parameters:
key- the cache key- Returns:
- the cache value or
nullif the key is not found
-
setTransformationCacheEntry
public void setTransformationCacheEntry(CacheEntry key, Formula value)
Sets an entry in the transformation cache of this formula- Parameters:
key- the cache keyvalue- the cache value
-
predicateCacheEntry
public Tristate predicateCacheEntry(CacheEntry key)
Returns an entry of the predicate cache of this formula.- Parameters:
key- the cache key- Returns:
- the cache value (which is
UNDEFif nothing is present)
-
setPredicateCacheEntry
public void setPredicateCacheEntry(CacheEntry key, boolean value)
Sets an entry in the predicate cache of this formula- Parameters:
key- the cache keyvalue- the cache value
-
setPredicateCacheEntry
public void setPredicateCacheEntry(CacheEntry key, Tristate value)
Sets an entry in the predicate cache of this formula- Parameters:
key- the cache keyvalue- the cache value
-
functionCacheEntry
public Object functionCacheEntry(CacheEntry key)
Returns an entry of the function cache of this formula.- Parameters:
key- the cache key- Returns:
- the cache value or
nullif the key is not found
-
setFunctionCacheEntry
public void setFunctionCacheEntry(CacheEntry key, Object value)
Sets an entry in the function cache of this formula- Parameters:
key- the cache keyvalue- the cache value
-
clearCaches
public void clearCaches()
Clears the transformation and function cache of the formula.
-
stream
public abstract Stream<Formula> stream()
Returns a stream of this formula's operands.Most times streams have worse performance then iterating over the formula per iterator. Since internally formulas store their operands, a costly call to
Arrays.stream()is necessary. So if performance matters - avoid using streams.- Returns:
- the stream
-
-