Class ASPRule

java.lang.Object
org.tweetyproject.lp.asp.syntax.ASPElement
org.tweetyproject.lp.asp.syntax.ASPRule
All Implemented Interfaces:
Comparable<ASPRule>, Formula, Rule<ASPHead,ASPBodyElement>, ComplexLogicalFormula, LogicStructure, SimpleLogicalFormula

public class ASPRule extends ASPElement implements Rule<ASPHead,ASPBodyElement>, Comparable<ASPRule>
This class models a rule in ASP. A rule consists of a head and a body. The head contains 0+ classical atoms and the body contains 0+ literals. Rules with non-empty heads and empty bodies are called facts, rules with empty heads and non-empty bodies are called constraints.
Author:
Anna Gessler, Tim Janus, Thomas Vengels
  • Constructor Details

    • ASPRule

      public ASPRule()
      Empty constructor
    • ASPRule

      public ASPRule(ASPHead head)
      Creates a fact with the given ASPHead.
      Parameters:
      head - an ASPHead
    • ASPRule

      public ASPRule(ASPLiteral literal)
      Creates a fact with the given literal.
      Parameters:
      literal - a literal
    • ASPRule

      public ASPRule(ASPHead head, List<ASPBodyElement> body)
      Creates a rule with the given head and body.
      Parameters:
      head - an ASPHead
      body - a list of ASPBodyElement
    • ASPRule

      public ASPRule(ASPLiteral head, ASPBodyElement b)
      Creates a rule with the given head and a single-element body.
      Parameters:
      head - an ASPLiteral
      b - a body element
    • ASPRule

      public ASPRule(ASPHead head, ASPBodyElement b)
      Creates a rule with the given head and a single-element body.
      Parameters:
      head - an ASPHead
      b - a body element
    • ASPRule

      public ASPRule(ASPLiteral head, List<ASPBodyElement> body)
      Creates a rule with the given head and body.
      Parameters:
      head - an ASPLiteral
      body - a list of ASPBodyElement
    • ASPRule

      public ASPRule(List<ASPBodyElement> body)
      Creates a constraint with the given body.
      Parameters:
      body - a list of ASPBodyElement
    • ASPRule

      public ASPRule(List<ASPBodyElement> nafliterals, Term<?> weight, List<Term<?>> terms)
      Creates a weak constraint with the given weight and terms.
      Parameters:
      nafliterals - the naf literals
      weight - some weight
      terms - a list of terms
    • ASPRule

      public ASPRule(List<ASPBodyElement> body, Term<?> weight, Term<?> level, List<Term<?>> terms)
      Creates a weak constraint with the given weight, level (priority) and terms.
      Parameters:
      body - a list of ASPBodyElement
      weight - a term
      level - a term
      terms - a list of terms
    • ASPRule

      public ASPRule(OptimizationStatement opt)
      Creates a new rule with the given optimization statement.
      Parameters:
      opt - OptimizationStatement
    • ASPRule

      public ASPRule(ASPRule other)
      Copy-Constructor
      Parameters:
      other - another ASPRule
  • Method Details

    • setConstraintTerms

      public void setConstraintTerms(List<Term<?>> constraintTerms)
    • addPremise

      public void addPremise(ASPBodyElement premise)
      Description copied from interface: Rule
      Add the given premise to this rule.
      Specified by:
      addPremise in interface Rule<ASPHead,ASPBodyElement>
      Parameters:
      premise - some formula
    • getPremise

      public List<ASPBodyElement> getPremise()
      Description copied from interface: Rule
      Returns the premise of this rule.
      Specified by:
      getPremise in interface Rule<ASPHead,ASPBodyElement>
      Returns:
      the premise of this rule.
    • addPremises

      public void addPremises(Collection<? extends ASPBodyElement> premises)
      Description copied from interface: Rule
      Add the given premises to this rule.
      Specified by:
      addPremises in interface Rule<ASPHead,ASPBodyElement>
      Parameters:
      premises - some formulas
    • addBody

      public void addBody(ASPBodyElement premise)
      Add the given body element to this rule.
      Parameters:
      premise -
    • getBody

      public List<ASPBodyElement> getBody()
      Returns:
      the body of this rule
    • addBodyElements

      public void addBodyElements(Collection<? extends ASPBodyElement> premises)
      Add the given body elements to this rule.
      Parameters:
      premises -
    • setBody

      public void setBody(ASPBodyElement... aspBodyElements)
      Set this rule's body to the given ASPBodyElements.
      Parameters:
      aspBodyElements -
    • setConclusion

      public void setConclusion(ASPHead conclusion)
      Description copied from interface: Rule
      Set the conclusion of this rule.
      Specified by:
      setConclusion in interface Rule<ASPHead,ASPBodyElement>
      Parameters:
      conclusion - some formula
    • setConclusion

      public void setConclusion(ASPLiteral head)
      Set the conclusion of this rule.
      Parameters:
      head -
    • getConclusion

      public ASPHead getConclusion()
      Description copied from interface: Rule
      Returns the conclusion of this rule.
      Specified by:
      getConclusion in interface Rule<ASPHead,ASPBodyElement>
      Returns:
      the conclusion of this rule.
    • addToHead

      public void addToHead(ASPLiteral literal)
      Add the given literal to the head of the rule.
      Parameters:
      literal - ASPLiteral
    • getHead

      public ASPHead getHead()
      Returns:
      head of this rule
    • setHead

      public void setHead(ASPHead head)
      Set the head of this rule.
      Parameters:
      head -
    • setHead

      public void setHead(ASPLiteral head)
      Set the head of this rule.
      Parameters:
      head -
    • getWeight

      public Term<?> getWeight()
      Returns:
      the weight of this constraint.
    • setWeight

      public void setWeight(Term<?> weight)
      Set the weight attribute of this constraint.
      Parameters:
      weight - a term
    • getLevel

      public Term<?> getLevel()
      Returns:
      the level attribute of this constraint.
    • setLevel

      public void setLevel(Term<?> level)
      Set the level attribute of this constraint.
      Parameters:
      level - a term
    • getConstraintTerms

      public List<Term<?>> getConstraintTerms()
      Returns:
      the constraint terms of this rule.
    • isFact

      public boolean isFact()
      Description copied from interface: Rule
      isFact
      Specified by:
      isFact in interface Rule<ASPHead,ASPBodyElement>
      Returns:
      whether the rule is a fact
    • isConstraint

      public boolean isConstraint()
      Description copied from interface: Rule
      isConstraint
      Specified by:
      isConstraint in interface Rule<ASPHead,ASPBodyElement>
      Returns:
      whether the rule is a constraint
    • isOptimizationStatement

      public boolean isOptimizationStatement()
    • isEmpty

      public boolean isEmpty()
      Returns:
      true if the rule's head and body are both empty, false otherwise.
    • isSafe

      public Boolean isSafe()
      This method tests a rule for safety. Solvers (such as clingo) usually do not accept unsafe rules. A rule is safe if all of its variables occur in a positive literal (classical atom) in the body of the rule, or as part of one side u of a comparative atom u = t if all variables in t are safe (analogously for switched u and t). A more detailed description can be found in the ASP-2-Core standard description.
      Returns:
      true if the rule is safe, false otherwise
    • isGround

      public boolean isGround()
      Description copied from interface: ComplexLogicalFormula
      Checks whether this formula is ground, i.e. whether there appears no variable in this formula.
      Specified by:
      isGround in interface ComplexLogicalFormula
      Overrides:
      isGround in class ASPElement
      Returns:
      "true" if this formula is ground.
    • getSignature

      public FolSignature getSignature()
      Description copied from interface: Formula
      Returns the signature of the language of this formula.
      Specified by:
      getSignature in interface Formula
      Specified by:
      getSignature in interface Rule<ASPHead,ASPBodyElement>
      Specified by:
      getSignature in class ASPElement
      Returns:
      the signature of the language of this formula.
    • isLiteral

      public boolean isLiteral()
      Specified by:
      isLiteral in interface SimpleLogicalFormula
      Returns:
      true if the formula represents a literal in the language or false otherwise
    • getTerms

      public Set<Term<?>> getTerms()
      Specified by:
      getTerms in interface LogicStructure
      Returns:
      a set containing all terms of this logical structure
    • getTerms

      public <C extends Term<?>> Set<C> getTerms(Class<C> cls)
      Description copied from interface: LogicStructure
      Processes the set containing all terms of type C. This method uses the equals method of the given Class and therefore does not add terms which are sub classes of type C to the set.
      Specified by:
      getTerms in interface LogicStructure
      Type Parameters:
      C - the type of terms
      Parameters:
      cls - The Class structure containing type information about the searched term
      Returns:
      A set containing all terms of type C of this logical structure
    • getPredicates

      public Set<Predicate> getPredicates()
      Description copied from interface: SimpleLogicalFormula
      Processes the set of all predicates which appear in this formula
      Specified by:
      getPredicates in interface SimpleLogicalFormula
      Specified by:
      getPredicates in class ASPElement
      Returns:
      all predicates that appear in this formula
    • getAtoms

      public Set<ASPAtom> getAtoms()
      Description copied from interface: SimpleLogicalFormula
      Processes the set of all atoms which appear in this formula
      Specified by:
      getAtoms in interface SimpleLogicalFormula
      Specified by:
      getAtoms in class ASPElement
      Returns:
      The set of all atoms
    • getLiterals

      public Collection<? extends ASPLiteral> getLiterals()
    • substitute

      public ASPRule substitute(Term<?> v, Term<?> t)
      Description copied from interface: ComplexLogicalFormula
      Substitutes all occurrences of term "v" in this formula by term "t" and returns the new formula.
      Specified by:
      substitute in interface ComplexLogicalFormula
      Specified by:
      substitute in class ASPElement
      Parameters:
      v - the term to be substituted.
      t - the term to substitute.
      Returns:
      a formula where every occurrence of "v" is replaced by "t".
    • exchange

      public ASPRule exchange(Term<?> v, Term<?> t)
      Description copied from interface: ComplexLogicalFormula
      Substitutes all occurrences of term "v" in this formula by term "t" and at the same time replaces all occurrences of term "t" by term "v" and eventually returns the new formula.
      Specified by:
      exchange in interface ComplexLogicalFormula
      Overrides:
      exchange in class ASPElement
      Parameters:
      v - a term.
      t - a term.
      Returns:
      a new logical formula with both "v" and "t" exchanged.
    • compareTo

      public int compareTo(ASPRule arg0)
      Specified by:
      compareTo in interface Comparable<ASPRule>
    • clone

      public ASPRule clone()
      Description copied from interface: SimpleLogicalFormula
      Creates a deep copy of this formula
      Specified by:
      clone in interface ComplexLogicalFormula
      Specified by:
      clone in interface SimpleLogicalFormula
      Specified by:
      clone in class ASPElement
      Returns:
      the cloned formula
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • printToClingo

      public String printToClingo()
      Description copied from class: ASPElement
      Returns a representation of this ASP element in clingo (potassco) syntax. See https://potassco.org/ for more information.
      Overrides:
      printToClingo in class ASPElement
      Returns:
      String representation in clingo syntax
    • printToDLV

      public String printToDLV()
      Description copied from class: ASPElement
      Returns a representation of this ASP element in DLV syntax. See http://www.dlvsystem.com/html/DLV_User_Manual.html for more information.
      Overrides:
      printToDLV in class ASPElement
      Returns:
      String representation in DLV syntax
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface SimpleLogicalFormula
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface SimpleLogicalFormula
      Overrides:
      equals in class Object