lp.struct
Class LpPredicate

java.lang.Object
  extended by lp.struct.LpAbstractStructureUnit
      extended by lp.struct.LpPredicate
All Implemented Interfaces:
LpStructureUnit

public class LpPredicate
extends LpAbstractStructureUnit

Represents a predicate (symbol) in logic programming. Each predicate has a name and an arity, i.e. a non-negative integer that specifies the number of terms (see LpTerm) that it takes as arguments. LpPredicates are immutable.

Version:
1.0.0
Author:
Martin Slota
See Also:
LpTerm, LpLiteral

Field Summary
private  int arity
          This predicate's arity.
private  String name
          This predicate's name.
private static Map<String,LpPredicate> pool
          A pool of instances of this immutable class.
 
Fields inherited from class lp.struct.LpAbstractStructureUnit
TO_STRING_BUFFER
 
Constructor Summary
protected LpPredicate(String name, int arity)
          Creates a new instance with the given name and arity.
 
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 LpPredicate instance, its name is equal to this predicate's name as defined by String.equals(Object), its arity is equal to this predicate's arity
 int getArity()
          Returns this predicate's arity.
static LpPredicate getInstance(String name, int arity)
          Returns and instance of LpPredicate with the given name and arity.
static LpPredicate getInstance(String name, List<LpTerm> arguments)
          Returns and instance of LpPredicate with the given name and arity implied by the size of the given argument list.
 String getName()
          Returns this predicate's name, the same that was given to the constructor.
 int hashCode()
          Overriden in order to maintain the general contract of Object.hashCode().
 String toString()
          Overrides the behaviour of LpAbstractStructureUnit.toString().
 
Methods inherited from class lp.struct.LpAbstractStructureUnit
toUnmodifiableList, toUnmodifiableSet
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

pool

private static final Map<String,LpPredicate> pool
A pool of instances of this immutable class. The getInstance(String, int) method first looks here for the requested instances and if it is forced to create a new one, it is added to this pool.


name

private final String name
This predicate's name.


arity

private final int arity
This predicate's arity.

Constructor Detail

LpPredicate

protected LpPredicate(String name,
                      int arity)
Creates a new instance with the given name and arity. name is required to be a non-empty string and arity must be non-negative.

Parameters:
name - the name of the predicate
arity - the predicate's arity
Throws:
IllegalArgumentException - if name is null or name is an empty string or arity is negative
Method Detail

getInstance

public static LpPredicate getInstance(String name,
                                      int arity)
Returns and instance of LpPredicate with the given name and arity. name is required to be a non-empty string and arity must be non-negative.

Parameters:
name - the name of the requested predicate
arity - the requested predicate's arity
Throws:
IllegalArgumentException - if name is null or name is an empty string or arity is negative

getInstance

public static LpPredicate getInstance(String name,
                                      List<LpTerm> arguments)
Returns and instance of LpPredicate with the given name and arity implied by the size of the given argument list. name is required to be a non-empty string.

Parameters:
name - the name of the requested predicate
arguments - the sample argument list, its size is the predicate's arity (it can also be null, in that case a predicate with arity 0 is returned)
Throws:
IllegalArgumentException - if name is null or name is an empty string

getName

public String getName()
Returns this predicate's name, the same that was given to the constructor.

Returns:
the name of this predicate

getArity

public int getArity()
Returns this predicate's arity. Guaranteed to be non-negative.

Returns:
this predicate's arity

accept

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

Parameters:
visitor - the visitor to accept

equals

public boolean equals(Object obj)
Returns true if and only if
  1. obj is a LpPredicate instance,
  2. its name is equal to this predicate's name as defined by String.equals(Object),
  3. its arity is equal to this predicate's arity

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:
String.equals(Object)

hashCode

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

Overrides:
hashCode in class Object
Returns:
the hash of this object
See Also:
String.hashCode()

toString

public String toString()
Overrides the behaviour of LpAbstractStructureUnit.toString(). Returns the normal form (i.e. the predicate's name) followed by a "/" and arity.

Overrides:
toString in class LpAbstractStructureUnit