lp.struct
Class LpRule

java.lang.Object
  extended by lp.struct.LpAbstractStructureUnit
      extended by lp.struct.LpRule
All Implemented Interfaces:
LpStructureUnit, LpTerm

public class LpRule
extends LpAbstractStructureUnit
implements LpTerm

Represents a logic programming rule. Each rule has a literal (represented by a LpLiteral instance) called its head and a set of such literals (also represented by LpLiteral instances) called its body. A rule can have null as its head (in that case it is an integrity constraint) and it can have null as its body (in that case it is a fact).

Version:
1.0.0
Author:
Martin Slota

Field Summary
private  Set<LpLiteral> body
          The rule's body.
private  LpLiteral head
          The rule's head.
 
Fields inherited from class lp.struct.LpAbstractStructureUnit
TO_STRING_BUFFER
 
Constructor Summary
LpRule(LpLiteral ruleHead, Set<LpLiteral> ruleBody)
          Creates a new instance of LpRule with the given ruleHead and ruleBody.
 
Method Summary
 void accept(LpStructureUnitVisitor visitor)
          Accepts LpStructureUnitVisitor instance, i.e. calls its visitor.visit(this).
 boolean equals(Object obj)
          Returns true if and only if obj is a LpRule instance, its head is equal to this rule's head, i.e. either they are both null or they are equal according to LpLiteral.equals(Object), its body is equal to this rule's body, i.e. either they are both null or one is null and the other has Set.size() equal to 0 or they are equal according to Set.equals(Object).
 Set<LpLiteral> getBody()
          Returns the list literals in the body of this rule.
 int getBodySize()
          Returns the number of literals in this rule's body.
 LpLiteral getHead()
          Returns the head of this rule, the same that was given to the constructor.
 int hashCode()
          Overriden in order to maintain the general contract of Object.hashCode().
 
Methods inherited from class lp.struct.LpAbstractStructureUnit
toString, toUnmodifiableList, toUnmodifiableSet
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

head

private final LpLiteral head
The rule's head.


body

private final Set<LpLiteral> body
The rule's body.

Constructor Detail

LpRule

public LpRule(LpLiteral ruleHead,
              Set<LpLiteral> ruleBody)
Creates a new instance of LpRule with the given ruleHead and ruleBody. Both ruleHead and ruleBody can have a null value, but not at the same time. Arguments of the constructor are not copied (especially the ruleBody Set). This class is immutable as long as ruleBody is not changed after creating an instance.

Parameters:
ruleHead - ruleHead of the new rule
ruleBody - ruleBody of the new rule
Throws:
IllegalArgumentException - if both ruleHead and ruleBody are null
Method Detail

getHead

public LpLiteral getHead()
Returns the head of this rule, the same that was given to the constructor.

Returns:
the LpLiteral given in the constructor as the head of this rule

getBody

public Set<LpLiteral> getBody()
Returns the list literals in the body of this rule.

Returns:
an unmodifiable version of the list of body literals from the constructor

getBodySize

public int getBodySize()
Returns the number of literals in this rule's body.

Returns:
as specified above

accept

public void accept(LpStructureUnitVisitor visitor)
Accepts LpStructureUnitVisitor instance, i.e. calls its visitor.visit(this).

Specified by:
accept in interface LpStructureUnit
Parameters:
visitor - the visitor to accept

equals

public boolean equals(Object obj)
Returns true if and only if
  1. obj is a LpRule instance,
  2. its head is equal to this rule's head, i.e. either they are both null or they are equal according to LpLiteral.equals(Object),
  3. its body is equal to this rule's body, i.e. either they are both null or one is null and the other has Set.size() equal to 0 or they are equal according to Set.equals(Object).

Specified by:
equals in interface LpTerm
Overrides:
equals in class Object
Parameters:
obj - the object to compare with
Returns:
true if this object is equal to obj according to the description above, and false otherwise
See Also:
LpLiteral.equals(Object), Set.equals(Object)

hashCode

public int hashCode()
Overriden in order to maintain the general contract of Object.hashCode().

Specified by:
hashCode in interface LpTerm
Overrides:
hashCode in class Object
Returns:
the hash of this object
See Also:
LpLiteral.hashCode(), Set.hashCode()