Module logicng

Class Literal

  • All Implemented Interfaces:
    Comparable<Literal>, Iterable<Formula>
    Direct Known Subclasses:
    Variable

    public class Literal
    extends Formula
    implements Comparable<Literal>
    Boolean literals.

    Literals are besides the constants true and false and pseudo Boolean constraints the atomic formulas in LogicNG. Each variable is a positive literal.

    A literal consists of its name and its phase (also sign or polarity in the literature). A new positive literal can be constructed with f.literal("a", true) or - as a shortcut - f.variable("a"). A new negative literal can be constructed with f.literal("a", false) or if preferred with f.not(f.variable("a")) or f.variable("a").negate().

    Since:
    1.0
    Version:
    2.2.0
    • Method Detail

      • factory

        public FormulaFactory factory()
        Description copied from class: Formula
        Returns the factory of this formula.
        Overrides:
        factory in class Formula
        Returns:
        the factory of this formula
      • numberOfOperands

        public int numberOfOperands()
        Description copied from class: Formula
        Returns the number of operands of this formula.
        Specified by:
        numberOfOperands in class Formula
        Returns:
        the number of operands of this formula
      • isConstantFormula

        public boolean isConstantFormula()
        Description copied from class: Formula
        Returns whether this formula is a constant formula ("True" or "False").
        Specified by:
        isConstantFormula in class Formula
        Returns:
        true if this formula is a constant formula, false otherwise
      • isAtomicFormula

        public boolean isAtomicFormula()
        Description copied from class: Formula
        Returns whether this formula is an atomic formula (constant, literal, pseudo Boolean constraint), or not.
        Specified by:
        isAtomicFormula in class Formula
        Returns:
        true if this formula is an atomic formula, false otherwise
      • containsVariable

        public boolean containsVariable​(Variable variable)
        Description copied from class: Formula
        Returns true if a given variable name is found in this formula, false otherwise.
        Specified by:
        containsVariable in class Formula
        Parameters:
        variable - the variable to search for
        Returns:
        true if a given variable is found in this formula
      • evaluate

        public boolean evaluate​(Assignment assignment)
        Description copied from class: Formula
        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.
        Specified by:
        evaluate in class Formula
        Parameters:
        assignment - the given assignment
        Returns:
        the result of the evaluation, true or false
      • restrict

        public Formula restrict​(Assignment assignment)
        Description copied from class: Formula
        Restricts this formula with a given assignment.
        Specified by:
        restrict in class Formula
        Parameters:
        assignment - the given assignment
        Returns:
        a new restricted formula
      • containsNode

        public boolean containsNode​(Formula formula)
        Description copied from class: Formula
        Returns true if this formula contains a given node, false otherwise.

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

        Specified by:
        containsNode in class Formula
        Parameters:
        formula - the node
        Returns:
        true if this formula contains a given node
      • substitute

        public Formula substitute​(Substitution substitution)
        Description copied from class: Formula
        Performs a given substitution on this formula.
        Specified by:
        substitute in class Formula
        Parameters:
        substitution - the substitution
        Returns:
        a new substituted formula
      • negate

        public Literal negate()
        Description copied from class: Formula
        Returns a negated copy of this formula.
        Specified by:
        negate in class Formula
        Returns:
        a negated copy of this formula
      • name

        public String name()
        Returns the name of the literal.
        Returns:
        the name of the literal
      • phase

        public boolean phase()
        Returns the phase of the literal.
        Returns:
        the phase of the literal.
      • variable

        public Variable variable()
        Returns a positive version of this literal (aka a variable).
        Returns:
        a positive version of this literal
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • stream

        public Stream<Formula> stream()
        Description copied from class: Formula
        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.

        Specified by:
        stream in class Formula
        Returns:
        the stream