Module logicng

Class Formula

    • Constructor Detail

      • Formula

        protected Formula​(FType type,
                          FormulaFactory f)
        Constructs a new formula.
        Parameters:
        type - the type of the formula
        f - 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:
        true if this formula is a constant formula, false otherwise
      • isAtomicFormula

        public abstract boolean isAtomicFormula()
        Returns whether this formula is an atomic formula (constant, literal, pseudo Boolean constraint), or not.
        Returns:
        true if this formula is an atomic formula, false otherwise
      • 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)
        Returns true if a given variable name is found in this formula, false otherwise.
        Parameters:
        variable - the variable to search for
        Returns:
        true if a given variable is found in this formula
      • containsVariable

        public abstract boolean containsVariable​(Variable variable)
        Returns true if a given variable name is found in this formula, false otherwise.
        Parameters:
        variable - the variable to search for
        Returns:
        true if 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 to false if it is positive, otherwise it evaluates to true.
        Parameters:
        assignment - the given assignment
        Returns:
        the result of the evaluation, true or false
      • 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)
        Returns true if this formula contains a given node, false otherwise.

        In particular, a Literal node ~a does NOT contain the node a.

        Parameters:
        formula - the node
        Returns:
        true if this formula contains a given node
      • isNNF

        public boolean isNNF()
        Returns true if this formula is in NNF, otherwise false
        Returns:
        true if this formula is in NNF, otherwise false
        See Also:
        the NNF predicate
      • isDNF

        public boolean isDNF()
        Returns true if this formula is in DNF, otherwise false
        Returns:
        true if this formula is in DNF, otherwise false
        See Also:
        the DNF predicate
      • isCNF

        public boolean isCNF()
        Returns true if this formula is in CNF, otherwise false
        Returns:
        true if this formula is in CNF, otherwise false
        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 variable
        formula - 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 the FormulaFactory.

        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.FACTORIZATION and CNFConfig.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 predicate SATPredicate. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use a SATSolver on your own.
        Returns:
        true when this formula is satisfiable, false otherwise
      • 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 predicate TautologyPredicate. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use a SATSolver on your own.
        Returns:
        true when this formula is a tautology, false otherwise
      • 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 predicate ContradictionPredicate. If you want to have more influence on the solver (e.g. which solver type or configuration) you must create and use a SATSolver on your own.
        Returns:
        true when this formula is a contradiction, false otherwise
      • 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 a SATSolver on your own.
        Parameters:
        other - the formula which should be checked if it is implied by this formula
        Returns:
        true when this formula implies the given other formula, false otherwise
      • 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 a SATSolver on your own.
        Parameters:
        other - the formula which should be checked if it implies this formula
        Returns:
        true when this formula is implied by the given other formula, false otherwise
      • 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 a SATSolver on your own.
        Parameters:
        other - the formula which should be checked if it is equivalent with this formula
        Returns:
        true when this formula is equivalent to the given other formula, false otherwise
      • 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 of BDDFactory should 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 * BDDFactory should 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 transformator
        cache - 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 returns true if the predicate holds, false otherwise.
        Parameters:
        predicate - the predicate
        Returns:
        true if the predicate holds, false otherwise
      • holds

        public boolean holds​(FormulaPredicate predicate,
                             boolean cache)
        Evaluates a given predicate on this formula and returns true if the predicate holds, false otherwise.
        Parameters:
        predicate - the predicate
        cache - indicates whether the result should be cached in this formula's cache
        Returns:
        true if the predicate holds, false otherwise
      • 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 function
        cache - 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 null if 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 key
        value - 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 UNDEF if 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 key
        value - 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 key
        value - 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 null if 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 key
        value - 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