- java.lang.Object
-
- org.logicng.formulas.FormulaFactory
-
- Direct Known Subclasses:
ExtendedFormulaFactory
public class FormulaFactory extends Object
The formula factory for LogicNG.New formulas can only be generated by a formula factory. It is implemented s.t. it is guaranteed that equivalent formulas (in terms of associativity and commutativity) are hold exactly once in memory.
A formula factory is NOT thread-safe. If you generate formulas from more than one thread you either need to synchronize the formula factory yourself or you use a formula factory for each single thread.
- Since:
- 1.0
- Version:
- 2.4.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFormulaFactory.FormulaFactoryStatisticsA class for statistics of the formula factory.
-
Constructor Summary
Constructors Constructor Description FormulaFactory()Constructor for a new formula factory with the default configuration.FormulaFactory(FormulaFactoryConfig config)Constructor for a new formula factory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Formulaamo(Collection<Variable> variables)Creates a new at-most-one cardinality constraint.Formulaamo(Variable... variables)Creates a new at-most-one cardinality constraint.Formulaand(Collection<? extends Formula> operands)Creates a new conjunction from a collection of formulas.Formulaand(Formula... operands)Creates a new conjunction from an array of formulas.FormulabinaryOperator(FType type, Formula left, Formula right)Creates a new binary operator with a given type and two operands.Formulacc(CType comparator, int rhs, Collection<Variable> variables)Creates a new cardinality constraint.Formulacc(CType comparator, int rhs, Variable... variables)Creates a new cardinality constraint.Formulaclause(Collection<? extends Literal> literals)Creates a new clause from a collection of literals.Formulaclause(Literal... literals)Creates a new clause from an array of literals.voidclear()Removes all formulas from the factory cache.protected voidclearCaches(Formula formula)Clears the transformation, function, and PB encoding cache for the given formula.Formulacnf(Collection<? extends Formula> clauses)Creates a new CNF from a collection of clauses.Formulacnf(Formula... clauses)Creates a new CNF from an array of clauses.CNFEncodercnfEncoder()Returns the default CNF encoder of this formula factory.ConfigurationconfigurationFor(ConfigurationType cType)Returns the configuration for a given configuration type ornullif there isn't any.Constantconstant(boolean value)Returns the constant "True" or "False" depending on the given value.Formulaequivalence(Formula leftIn, Formula rightIn)Creates a new equivalence.Formulaexo(Collection<Variable> variables)Creates a new exactly-one cardinality constraint.Formulaexo(Variable... variables)Creates a new exactly-one cardinality constraint.CFalsefalsum()Returns a (singleton) object for the constant "False".protected ObjectfunctionCacheEntry(Formula formula, CacheEntry key)Returns an entry of the function cache for the given formula.Formulaimplication(Formula leftIn, Formula rightIn)Creates a new implication.FormulaimportFormula(Formula formula)Imports a formula from another formula factory into this factory and returns it.booleanisGeneratedVariable(Variable var)Returnstrueif the given variable was generated,falseotherwise.Literalliteral(String name, boolean phase)Creates a new literal instance with a given name and phase.Stringname()Returns the name of this formula factory.FormulanaryOperator(FType type, Collection<? extends Formula> operands)Creates a new n-ary operator with a given type and a list of operands.FormulanaryOperator(FType type, Formula... operands)Creates a new n-ary operator with a given type and a list of operands.VariablenewCCVariable()Returns a new cardinality constraint auxiliary literal.VariablenewCNFVariable()Returns a new CNF auxiliary literal.VariablenewPBVariable()Returns a new pseudo Boolean auxiliary literal.Formulanot(Formula formula)Creates the negation of a given formula.longnumberOfNodes(Formula formula)Returns the number of internal nodes of a given formula.Formulaor(Collection<? extends Formula> operands)Creates a new disjunction from a collection of formulas.Formulaor(Formula... operands)Creates a new disjunction from an array of formulas.Formulaparse(String string)Parses a given string to a formula using a pseudo boolean parser.Formulapbc(CType comparator, int rhs, List<? extends Literal> literals, List<Integer> coefficients)Creates a new pseudo-Boolean constraint.Formulapbc(CType comparator, int rhs, Literal[] literals, int[] coefficients)Creates a new pseudo-Boolean constraint.PBEncoderpbEncoder()Returns the default pseudo-Boolean encoder of this formula factory.protected TristatepredicateCacheEntry(Formula formula, CacheEntry key)Returns an entry of the predicate cache for the given formula.voidputConfiguration(Configuration configuration)Puts a new configuration into the configuration database.protected voidsetFunctionCacheEntry(Formula formula, CacheEntry key, Object value)Sets an entry in the function cache for the given formula.protected voidsetPredicateCacheEntry(Formula formula, CacheEntry key, boolean value)Sets an entry in the predicate cache for the given formula.protected voidsetPredicateCacheEntry(Formula formula, CacheEntry key, Tristate value)Sets an entry in the predicate cache for the given formula.protected voidsetTransformationCacheEntry(Formula formula, CacheEntry key, Formula value)Sets an entry in the transformation cache for the given formula.FormulaFactory.FormulaFactoryStatisticsstatistics()Returns the statistics for this formula factory.Stringstring(Formula formula)Returns a string representation of a formula with this factories string representationStringstring(Formula formula, FormulaStringRepresentation stringRepresentation)Returns a string representation of a formula with this factories string representationFormulaStringRepresentationstringRepresentation()Returns the formula formatter of this factory.SubNodeFunctionsubformulaFunction()Returns a function to compute the sub-formulas.StringtoString()protected FormulatransformationCacheEntry(Formula formula, CacheEntry key)Returns an entry of the transformation cache for the given formula.Variablevariable(String name)Creates a new literal instance with a given name and positive phase.List<Variable>variables(String... names)Creates a list of literals with the given names and positive phase.List<Variable>variables(Collection<String> names)Creates a list of literals with the given names and positive phase.CTrueverum()Returns a (singleton) object for the constant "True".
-
-
-
Field Detail
-
CC_PREFIX
public static final String CC_PREFIX
- See Also:
- Constant Field Values
-
PB_PREFIX
public static final String PB_PREFIX
- See Also:
- Constant Field Values
-
CNF_PREFIX
public static final String CNF_PREFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FormulaFactory
public FormulaFactory(FormulaFactoryConfig config)
Constructor for a new formula factory.- Parameters:
config- the configuration for this formula factory
-
FormulaFactory
public FormulaFactory()
Constructor for a new formula factory with the default configuration.
-
-
Method Detail
-
clear
public void clear()
Removes all formulas from the factory cache.
-
name
public String name()
Returns the name of this formula factory.- Returns:
- the name of this formula factory
-
configurationFor
public Configuration configurationFor(ConfigurationType cType)
Returns the configuration for a given configuration type ornullif there isn't any.- Parameters:
cType- the configuration type- Returns:
- the configuration for a given configuration type
-
putConfiguration
public void putConfiguration(Configuration configuration)
Puts a new configuration into the configuration database. If there is already a configuration present for this type, it will be overwritten.Note that is not allowed to pass configurations of type
ConfigurationType.FORMULA_FACTORY. Such configurations can only be passed to the constructor and never be changed thereafter.- Parameters:
configuration- the configuration- Throws:
IllegalArgumentException- if a configuration of typeConfigurationType.FORMULA_FACTORYwas passed
-
subformulaFunction
public SubNodeFunction subformulaFunction()
Returns a function to compute the sub-formulas.- Returns:
- a function to compute the sub-formulas
-
pbEncoder
public PBEncoder pbEncoder()
Returns the default pseudo-Boolean encoder of this formula factory.- Returns:
- the default pseudo-Boolean encoder of this formula factory
-
cnfEncoder
public CNFEncoder cnfEncoder()
Returns the default CNF encoder of this formula factory.- Returns:
- the default CNF encoder of this formula factory
-
binaryOperator
public Formula binaryOperator(FType type, Formula left, Formula right)
Creates a new binary operator with a given type and two operands.- Parameters:
type- the type of the formulaleft- the left-hand side operandright- the right-hand side operand- Returns:
- the newly generated formula
- Throws:
IllegalArgumentException- if a wrong formula type is passed
-
implication
public Formula implication(Formula leftIn, Formula rightIn)
Creates a new implication.- Parameters:
leftIn- the left-hand side operandrightIn- the right-hand side operand- Returns:
- a new implication
-
equivalence
public Formula equivalence(Formula leftIn, Formula rightIn)
Creates a new equivalence.- Parameters:
leftIn- the left-hand side operandrightIn- the right-hand side operand- Returns:
- a new equivalence
-
verum
public CTrue verum()
Returns a (singleton) object for the constant "True".- Returns:
- an object for the constant "True"
-
not
public Formula not(Formula formula)
Creates the negation of a given formula.Constants, literals and negations are negated directly and returned. For all other formulas a new
Notobject is returned.- Parameters:
formula- the given formula- Returns:
- the negated formula
-
falsum
public CFalse falsum()
Returns a (singleton) object for the constant "False".- Returns:
- an object for the constant "False"
-
constant
public Constant constant(boolean value)
Returns the constant "True" or "False" depending on the given value.- Parameters:
value- the given value- Returns:
- the constant
-
naryOperator
public Formula naryOperator(FType type, Collection<? extends Formula> operands)
Creates a new n-ary operator with a given type and a list of operands.- Parameters:
type- the type of the formulaoperands- the list of operands- Returns:
- the newly generated formula
- Throws:
IllegalArgumentException- if a wrong formula type is passed
-
naryOperator
public Formula naryOperator(FType type, Formula... operands)
Creates a new n-ary operator with a given type and a list of operands.- Parameters:
type- the type of the formulaoperands- the list of operands- Returns:
- the newly generated formula
- Throws:
IllegalArgumentException- if a wrong formula type is passed
-
and
public Formula and(Formula... operands)
Creates a new conjunction from an array of formulas.- Parameters:
operands- the vector of formulas- Returns:
- a new conjunction
-
and
public Formula and(Collection<? extends Formula> operands)
Creates a new conjunction from a collection of formulas.Note: The LinkedHashSet is used to eliminate duplicate sub-formulas and to respect the commutativity of operands.
- Parameters:
operands- the array of formulas- Returns:
- a new conjunction
-
cnf
public Formula cnf(Formula... clauses)
Creates a new CNF from an array of clauses.ATTENTION: it is assumed that the operands are really clauses - this is not checked for performance reasons. Also, no reduction of operands is performed - this method should only be used if you are sure that the CNF is free of redundant clauses.
- Parameters:
clauses- the array of clauses- Returns:
- a new CNF
-
cnf
public Formula cnf(Collection<? extends Formula> clauses)
Creates a new CNF from a collection of clauses.ATTENTION: it is assumed that the operands are really clauses - this is not checked for performance reasons. Also, no reduction of operands is performed - this method should only be used if you are sure that the CNF is free of redundant clauses.
- Parameters:
clauses- the collection of clauses- Returns:
- a new CNF
-
or
public Formula or(Formula... operands)
Creates a new disjunction from an array of formulas.- Parameters:
operands- the list of formulas- Returns:
- a new disjunction
-
or
public Formula or(Collection<? extends Formula> operands)
Creates a new disjunction from a collection of formulas.Note: The LinkedHashSet is used to eliminate duplicate sub-formulas and to respect the commutativity of operands.
- Parameters:
operands- the collection of formulas- Returns:
- a new disjunction
-
clause
public Formula clause(Literal... literals)
Creates a new clause from an array of literals.ATTENTION: No reduction of operands is performed - this method should only be used if you are sure that the clause is free of redundant or contradicting literals.
- Parameters:
literals- the collection of literals- Returns:
- a new clause
-
clause
public Formula clause(Collection<? extends Literal> literals)
Creates a new clause from a collection of literals.ATTENTION: No reduction of operands is performed - this method should only be used if you are sure that the clause is free of contradicting literals.
- Parameters:
literals- the collection of literals- Returns:
- a new clause
-
literal
public Literal literal(String name, boolean phase)
Creates a new literal instance with a given name and phase.Literal names should not start with
@RESERVED- these are reserved for internal literals.- Parameters:
name- the literal namephase- the literal phase- Returns:
- a new literal with the given name and phase
-
variable
public Variable variable(String name)
Creates a new literal instance with a given name and positive phase.- Parameters:
name- the variable name- Returns:
- a new literal with the given name and positive phase
-
variables
public List<Variable> variables(Collection<String> names)
Creates a list of literals with the given names and positive phase.- Parameters:
names- the variable names- Returns:
- a new list of literals with the given names and positive phase
-
variables
public List<Variable> variables(String... names)
Creates a list of literals with the given names and positive phase.- Parameters:
names- the variable names- Returns:
- a new list of literals with the given names and positive phase
-
pbc
public Formula pbc(CType comparator, int rhs, List<? extends Literal> literals, List<Integer> coefficients)
Creates a new pseudo-Boolean constraint.- Parameters:
comparator- the comparator of the constraintrhs- the right-hand side of the constraintliterals- the literals of the constraintcoefficients- the coefficients of the constraint- Returns:
- the pseudo-Boolean constraint
- Throws:
IllegalArgumentException- if the number of literals and coefficients do not correspond
-
pbc
public Formula pbc(CType comparator, int rhs, Literal[] literals, int[] coefficients)
Creates a new pseudo-Boolean constraint.- Parameters:
comparator- the comparator of the constraintrhs- the right-hand side of the constraintliterals- the literals of the constraintcoefficients- the coefficients of the constraint- Returns:
- the pseudo-Boolean constraint
- Throws:
IllegalArgumentException- if the number of literals and coefficients do not correspond
-
cc
public Formula cc(CType comparator, int rhs, Collection<Variable> variables)
Creates a new cardinality constraint.- Parameters:
variables- the variables of the constraintcomparator- the comparator of the constraintrhs- the right-hand side of the constraint- Returns:
- the cardinality constraint
- Throws:
IllegalArgumentException- if there are negative variables
-
cc
public Formula cc(CType comparator, int rhs, Variable... variables)
Creates a new cardinality constraint.- Parameters:
variables- the variables of the constraintcomparator- the comparator of the constraintrhs- the right-hand side of the constraint- Returns:
- the cardinality constraint
- Throws:
IllegalArgumentException- if there are negative variables
-
amo
public Formula amo(Collection<Variable> variables)
Creates a new at-most-one cardinality constraint.- Parameters:
variables- the variables of the constraint- Returns:
- the at-most-one constraint
- Throws:
IllegalArgumentException- if there are negative variables
-
amo
public Formula amo(Variable... variables)
Creates a new at-most-one cardinality constraint.- Parameters:
variables- the variables of the constraint- Returns:
- the at-most-one constraint
- Throws:
IllegalArgumentException- if there are negative variables
-
exo
public Formula exo(Collection<Variable> variables)
Creates a new exactly-one cardinality constraint.- Parameters:
variables- the variables of the constraint- Returns:
- the exactly-one constraint
- Throws:
IllegalArgumentException- if there are negative variables
-
exo
public Formula exo(Variable... variables)
Creates a new exactly-one cardinality constraint.- Parameters:
variables- the variables of the constraint- Returns:
- the exactly-one constraint
- Throws:
IllegalArgumentException- if there are negative variables
-
newCCVariable
public Variable newCCVariable()
Returns a new cardinality constraint auxiliary literal.Remark: currently only the counter is increased - there is no check if the literal is already present.
- Returns:
- the new cardinality constraint auxiliary literal
-
newPBVariable
public Variable newPBVariable()
Returns a new pseudo Boolean auxiliary literal.Remark: currently only the counter is increased - there is no check if the literal is already present.
- Returns:
- the new pseudo Boolean auxiliary literal
-
newCNFVariable
public Variable newCNFVariable()
Returns a new CNF auxiliary literal.Remark: currently only the counter is increased - there is no check if the literal is already present.
- Returns:
- the new CNF auxiliary literal
-
isGeneratedVariable
public boolean isGeneratedVariable(Variable var)
Returnstrueif the given variable was generated,falseotherwise.- Parameters:
var- the variable to check- Returns:
trueif the given variable was generated
-
numberOfNodes
public long numberOfNodes(Formula formula)
Returns the number of internal nodes of a given formula.- Parameters:
formula- the formula- Returns:
- the number of internal nodes
-
parse
public Formula parse(String string) throws ParserException
Parses a given string to a formula using a pseudo boolean parser.- Parameters:
string- a string representing the formula- Returns:
- the formula
- Throws:
ParserException- if the parser throws an exception
-
importFormula
public Formula importFormula(Formula formula)
Imports a formula from another formula factory into this factory and returns it. If the current factory of the formula is already this formula factory, the same instance will be returned.- Parameters:
formula- the formula to import- Returns:
- the imported formula on this factory
-
transformationCacheEntry
protected Formula transformationCacheEntry(Formula formula, CacheEntry key)
Returns an entry of the transformation cache for the given formula.- Parameters:
formula- the formulakey- the cache key- Returns:
- the cache value or
nullif the key is not found
-
setTransformationCacheEntry
protected void setTransformationCacheEntry(Formula formula, CacheEntry key, Formula value)
Sets an entry in the transformation cache for the given formula.- Parameters:
formula- the formulakey- the cache keyvalue- the cache value
-
predicateCacheEntry
protected Tristate predicateCacheEntry(Formula formula, CacheEntry key)
Returns an entry of the predicate cache for the given formula.- Parameters:
formula- the formulakey- the cache key- Returns:
- the cache value (which is
UNDEFif nothing is present)
-
setPredicateCacheEntry
protected void setPredicateCacheEntry(Formula formula, CacheEntry key, boolean value)
Sets an entry in the predicate cache for the given formula.- Parameters:
formula- the formulakey- the cache keyvalue- the cache value
-
setPredicateCacheEntry
protected void setPredicateCacheEntry(Formula formula, CacheEntry key, Tristate value)
Sets an entry in the predicate cache for the given formula.- Parameters:
formula- the formulakey- the cache keyvalue- the cache value
-
functionCacheEntry
protected Object functionCacheEntry(Formula formula, CacheEntry key)
Returns an entry of the function cache for the given formula.- Parameters:
formula- the formulakey- the cache key- Returns:
- the cache value or
nullif the key is not found
-
setFunctionCacheEntry
protected void setFunctionCacheEntry(Formula formula, CacheEntry key, Object value)
Sets an entry in the function cache for the given formula.- Parameters:
formula- the formulakey- the cache keyvalue- the cache value
-
clearCaches
protected void clearCaches(Formula formula)
Clears the transformation, function, and PB encoding cache for the given formula.- Parameters:
formula- the formula
-
string
public String string(Formula formula)
Returns a string representation of a formula with this factories string representation- Parameters:
formula- the formula- Returns:
- the string representation
-
string
public String string(Formula formula, FormulaStringRepresentation stringRepresentation)
Returns a string representation of a formula with this factories string representation- Parameters:
formula- the formulastringRepresentation- the string representation- Returns:
- the string representation
-
stringRepresentation
public FormulaStringRepresentation stringRepresentation()
Returns the formula formatter of this factory.- Returns:
- the formula formatter of this factory
-
statistics
public FormulaFactory.FormulaFactoryStatistics statistics()
Returns the statistics for this formula factory.- Returns:
- the statistics for this formula factory
-
-