Module logicng

Class 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
    • 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 or null if 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 type ConfigurationType.FORMULA_FACTORY was 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 formula
        left - the left-hand side operand
        right - 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 operand
        rightIn - 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 operand
        rightIn - 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 Not object 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 formula
        operands - 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 formula
        operands - 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 name
        phase - 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 constraint
        rhs - the right-hand side of the constraint
        literals - the literals of the constraint
        coefficients - 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 constraint
        rhs - the right-hand side of the constraint
        literals - the literals of the constraint
        coefficients - 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 constraint
        comparator - the comparator of the constraint
        rhs - 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 constraint
        comparator - the comparator of the constraint
        rhs - 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)
        Returns true if the given variable was generated, false otherwise.
        Parameters:
        var - the variable to check
        Returns:
        true if 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 formula
        key - the cache key
        Returns:
        the cache value or null if 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 formula
        key - the cache key
        value - 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 formula
        key - the cache key
        Returns:
        the cache value (which is UNDEF if 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 formula
        key - the cache key
        value - 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 formula
        key - the cache key
        value - 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 formula
        key - the cache key
        Returns:
        the cache value or null if 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 formula
        key - the cache key
        value - 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 formula
        stringRepresentation - 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