Package org.tweetyproject.lp.asp.syntax
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 Summary
ConstructorDescriptionASPRule()
Empty constructorASPRule
(List<ASPBodyElement> body) Creates a constraint with the given body.Creates a weak constraint with the given weight and terms.Creates a weak constraint with the given weight, level (priority) and terms.Creates a fact with the given ASPHead.ASPRule
(ASPHead head, List<ASPBodyElement> body) Creates a rule with the given head and body.ASPRule
(ASPHead head, ASPBodyElement b) Creates a rule with the given head and a single-element body.ASPRule
(ASPLiteral literal) Creates a fact with the given literal.ASPRule
(ASPLiteral head, List<ASPBodyElement> body) Creates a rule with the given head and body.ASPRule
(ASPLiteral head, ASPBodyElement b) Creates a rule with the given head and a single-element body.Copy-ConstructorCreates a new rule with the given optimization statement. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBody
(ASPBodyElement premise) Add the given body element to this rule.void
addBodyElements
(Collection<? extends ASPBodyElement> premises) Add the given body elements to this rule.void
addPremise
(ASPBodyElement premise) Add the given premise to this rule.void
addPremises
(Collection<? extends ASPBodyElement> premises) Add the given premises to this rule.void
addToHead
(ASPLiteral literal) Add the given literal to the head of the rule.clone()
Creates a deep copy of this formulaint
boolean
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.getAtoms()
Processes the set of all atoms which appear in this formulagetBody()
Return the body of this ruleReturns the conclusion of this rule.Return the constraint terms of this rule.getHead()
Return head of this ruleTerm
<?> getLevel()
Return the level attribute of this constraint.Collection
<? extends ASPLiteral> Retrieves all the literals contained within this rule.Processes the set of all predicates which appear in this formulaReturns the premise of this rule.Returns the signature of the language of this formula.getTerms()
Processes the set containing all terms of type C.Term
<?> Return the weight of this constraint.int
hashCode()
boolean
isConstraintboolean
isEmpty()
return true if the rule's head and body are both empty, false otherwise.boolean
isFact()
isFactboolean
isGround()
Checks whether this formula is ground, i.e.boolean
boolean
Return true if isOptimizationStatementisSafe()
This method tests a rule for safety.Returns a representation of this ASP element in clingo (potassco) syntax.Returns a representation of this ASP element in DLV syntax.void
setBody
(ASPBodyElement... aspBodyElements) Set this rule's body to the given ASPBodyElements.void
setConclusion
(ASPHead conclusion) Set the conclusion of this rule.void
setConclusion
(ASPLiteral head) Set the conclusion of this rule.void
setConstraintTerms
(List<Term<?>> constraintTerms) Settervoid
Set the head of this rule.void
setHead
(ASPLiteral head) Set the head of this rule.void
Set the level attribute of this constraint.void
Set the weight attribute of this constraint.substitute
(Term<?> v, Term<?> t) Substitutes all occurrences of term "v" in this formula by term "t" and returns the new formula.toString()
Methods inherited from class org.tweetyproject.lp.asp.syntax.ASPElement
containsTermsOfType, getPredicateCls, isWellFormed, substitute
-
Constructor Details
-
ASPRule
public ASPRule()Empty constructor -
ASPRule
-
ASPRule
Creates a fact with the given literal.- Parameters:
literal
- a literal
-
ASPRule
Creates a rule with the given head and body.- Parameters:
head
- an ASPHeadbody
- a list of ASPBodyElement
-
ASPRule
Creates a rule with the given head and a single-element body.- Parameters:
head
- an ASPLiteralb
- a body element
-
ASPRule
Creates a rule with the given head and a single-element body.- Parameters:
head
- an ASPHeadb
- a body element
-
ASPRule
Creates a rule with the given head and body.- Parameters:
head
- an ASPLiteralbody
- a list of ASPBodyElement
-
ASPRule
Creates a constraint with the given body.- Parameters:
body
- a list of ASPBodyElement
-
ASPRule
Creates a weak constraint with the given weight and terms.- Parameters:
nafliterals
- the naf literalsweight
- some weightterms
- a list of terms
-
ASPRule
-
ASPRule
Creates a new rule with the given optimization statement.- Parameters:
opt
- OptimizationStatement
-
ASPRule
-
-
Method Details
-
setConstraintTerms
-
addPremise
Description copied from interface:Rule
Add the given premise to this rule.- Specified by:
addPremise
in interfaceRule<ASPHead,
ASPBodyElement> - Parameters:
premise
- some formula
-
getPremise
Description copied from interface:Rule
Returns the premise of this rule.- Specified by:
getPremise
in interfaceRule<ASPHead,
ASPBodyElement> - Returns:
- the premise of this rule.
-
addPremises
Description copied from interface:Rule
Add the given premises to this rule.- Specified by:
addPremises
in interfaceRule<ASPHead,
ASPBodyElement> - Parameters:
premises
- some formulas
-
addBody
Add the given body element to this rule.- Parameters:
premise
- the premise
-
getBody
-
addBodyElements
Add the given body elements to this rule.- Parameters:
premises
- premise
-
setBody
Set this rule's body to the given ASPBodyElements.- Parameters:
aspBodyElements
- the body elements
-
setConclusion
Description copied from interface:Rule
Set the conclusion of this rule.- Specified by:
setConclusion
in interfaceRule<ASPHead,
ASPBodyElement> - Parameters:
conclusion
- some formula
-
setConclusion
Set the conclusion of this rule.- Parameters:
head
- the head
-
getConclusion
Description copied from interface:Rule
Returns the conclusion of this rule.- Specified by:
getConclusion
in interfaceRule<ASPHead,
ASPBodyElement> - Returns:
- the conclusion of this rule.
-
addToHead
Add the given literal to the head of the rule.- Parameters:
literal
- ASPLiteral
-
getHead
-
setHead
-
setHead
-
getWeight
Return the weight of this constraint.- Returns:
- the weight of this constraint.
-
setWeight
Set the weight attribute of this constraint.- Parameters:
weight
- a term
-
getLevel
Return the level attribute of this constraint.- Returns:
- the level attribute of this constraint.
-
setLevel
Set the level attribute of this constraint.- Parameters:
level
- a term
-
getConstraintTerms
-
isFact
public boolean isFact()Description copied from interface:Rule
isFact- Specified by:
isFact
in interfaceRule<ASPHead,
ASPBodyElement> - Returns:
- whether the rule is a fact
-
isConstraint
public boolean isConstraint()Description copied from interface:Rule
isConstraint- Specified by:
isConstraint
in interfaceRule<ASPHead,
ASPBodyElement> - Returns:
- whether the rule is a constraint
-
isOptimizationStatement
public boolean isOptimizationStatement()Return true if isOptimizationStatement- Returns:
- true if isOptimizationStatement
-
isEmpty
public boolean isEmpty()return true if the rule's head and body are both empty, false otherwise.- Returns:
- true if the rule's head and body are both empty, false otherwise.
-
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 interfaceComplexLogicalFormula
- Overrides:
isGround
in classASPElement
- Returns:
- "true" if this formula is ground.
-
getSignature
Description copied from interface:Formula
Returns the signature of the language of this formula.- Specified by:
getSignature
in interfaceFormula
- Specified by:
getSignature
in interfaceRule<ASPHead,
ASPBodyElement> - Specified by:
getSignature
in classASPElement
- Returns:
- the signature of the language of this formula.
-
isLiteral
public boolean isLiteral()- Specified by:
isLiteral
in interfaceSimpleLogicalFormula
- Returns:
- true if the formula represents a literal in the language or false otherwise
-
getTerms
- Specified by:
getTerms
in interfaceLogicStructure
- Returns:
- a set containing all terms of this logical structure
-
getTerms
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 interfaceLogicStructure
- 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
Description copied from interface:SimpleLogicalFormula
Processes the set of all predicates which appear in this formula- Specified by:
getPredicates
in interfaceSimpleLogicalFormula
- Specified by:
getPredicates
in classASPElement
- Returns:
- all predicates that appear in this formula
-
getAtoms
Description copied from interface:SimpleLogicalFormula
Processes the set of all atoms which appear in this formula- Specified by:
getAtoms
in interfaceSimpleLogicalFormula
- Specified by:
getAtoms
in classASPElement
- Returns:
- The set of all atoms
-
getLiterals
Retrieves all the literals contained within this rule.This method gathers and returns a collection of literals that are present in both the head and the body of the rule. The literals are stored in a sorted set, ensuring that they are returned in a consistent order.
- Returns:
- A sorted collection of literals (
ASPLiteral
) contained in the head and body of the rule. The returned collection is aSortedSet
to maintain a consistent order.
-
substitute
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 interfaceComplexLogicalFormula
- Specified by:
substitute
in classASPElement
- 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
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 interfaceComplexLogicalFormula
- Overrides:
exchange
in classASPElement
- Parameters:
v
- a term.t
- a term.- Returns:
- a new logical formula with both "v" and "t" exchanged.
-
compareTo
- Specified by:
compareTo
in interfaceComparable<ASPRule>
-
clone
Description copied from interface:SimpleLogicalFormula
Creates a deep copy of this formula- Specified by:
clone
in interfaceComplexLogicalFormula
- Specified by:
clone
in interfaceSimpleLogicalFormula
- Specified by:
clone
in classASPElement
- Returns:
- the cloned formula
-
toString
-
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 classASPElement
- Returns:
- String representation in clingo syntax
-
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 classASPElement
- Returns:
- String representation in DLV syntax
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceSimpleLogicalFormula
- Overrides:
hashCode
in classObject
-
equals
- Specified by:
equals
in interfaceSimpleLogicalFormula
- Overrides:
equals
in classObject
-