lp.struct
Class LpAtom

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

public class LpAtom
extends LpAbstractStructureUnit

This class represents an atom in logic programming. Such an atom is composed of a predicate symbol (represented by a LpPredicate instance) and its arguments—terms represented by LpTerm instances. The number of arguments must match the predicate's arity (returned by LpPredicate.getArity()). This class is immutable as long as the argument list used to create it is not changed later.

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

Field Summary
private  List<LpTerm> arguments
          The predicate's arguments.
private static LpBuffer keyBuilder
          Used to create construct keys in pool.
private  LpLiteral negLit
          A negative literal bound to this atom.
private static Map<String,LpAtom> pool
          A pool of instances of this immutable class.
private  LpLiteral posLit
          A positive literal bound to this atom.
private  LpPredicate predicate
          The predicate symbol that is used in this atom.
 
Fields inherited from class lp.struct.LpAbstractStructureUnit
TO_STRING_BUFFER
 
Constructor Summary
private LpAtom(LpPredicate predicate, List<LpTerm> arguments)
          Creates a new instance of LpAtom with the given predicate symbol and argument list.
 
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 LpAtom instance, its predicate is equal to this atom's predicate as defined by LpPredicate.equals(Object), its argument list are equal to this atom's argument list.
 List<LpTerm> getArguments()
          Returns the list of arguments of this atom.
static LpAtom getInstance(LpPredicate predicate, List<LpTerm> arguments)
          Returns an instance of LpAtom with the given predicate symbol and argument list.
 LpLiteral getLiteral(boolean positive)
          Returns a literal for this atom represented by a LpLiteral instance.
 LpLiteral getNegativeLiteral()
          Returns a negative literal for this atom represented by a LpLiteral instance.
 LpLiteral getPositiveLiteral()
          Returns a positive literal for this atom represented by a LpLiteral instance.
 LpPredicate getPredicate()
          Returns the predicate symbol of this atom, the same that was given to the constructor.
 int hashCode()
          Overriden in order to maintain the general contract of Object.hashCode().
private static void checkArity(LpPredicate predicate, List<LpTerm> arguments)
          Checks whether predicate.getArity() equals arguments.size().
 
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

pool

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


keyBuilder

private static final LpBuffer keyBuilder
Used to create construct keys in pool.


predicate

private final LpPredicate predicate
The predicate symbol that is used in this atom.


arguments

private final List<LpTerm> arguments
The predicate's arguments.


posLit

private LpLiteral posLit
A positive literal bound to this atom. Is returned lazily when requested.


negLit

private LpLiteral negLit
A negative literal bound to this atom. Is returned lazily when requested.

Constructor Detail

LpAtom

private LpAtom(LpPredicate predicate,
               List<LpTerm> arguments)
Creates a new instance of LpAtom with the given predicate symbol and argument list.

Parameters:
predicate - the requested atom's predicate symbol
arguments - the argument list of the requested LpAtom instance
Throws:
IllegalArgumentException - if predicate is null or the number of arguments doesn't match the predicate's arity, i.e. this condition doesn't hold:
(arguments == null && predicate.getArity() == 0) || arguments.size() == predicate.getArity()
Method Detail

getInstance

public static LpAtom getInstance(LpPredicate predicate,
                                 List<LpTerm> arguments)
Returns an instance of LpAtom with the given predicate symbol and argument list.

Parameters:
predicate - the requested atom's predicate symbol
arguments - the argument list of the requested LpAtom instance
Throws:
IllegalArgumentException - if predicate is null or the number of arguments doesn't match the predicate's arity, i.e. this condition doesn't hold:
(arguments == null && predicate.getArity() == 0) || arguments.size() == predicate.getArity()

checkArity

private static void checkArity(LpPredicate predicate,
                               List<LpTerm> arguments)
Checks whether predicate.getArity() equals arguments.size(). If it doesn't, throws an IllegalArgumentException.

Parameters:
predicate - a predicate, its arity must match the length of the argument list
arguments - an argument list, its length must match the predicate's arity
Throws:
IllegalArgumentException - if predicate.getArity() is not the same as arguments.size()

getPredicate

public LpPredicate getPredicate()
Returns the predicate symbol of this atom, the same that was given to the constructor.

Returns:
the LpPredicate instance given in the constructor

getArguments

public List<LpTerm> getArguments()
Returns the list of arguments of this atom. Doesn't return null, even if it was given to the constructor of this instance (in that case an empty list is returned).

Returns:
an unmodifiable version of the argument list from the constructor

getPositiveLiteral

public LpLiteral getPositiveLiteral()
Returns a positive literal for this atom represented by a LpLiteral instance.

Returns:
as specified above

getNegativeLiteral

public LpLiteral getNegativeLiteral()
Returns a negative literal for this atom represented by a LpLiteral instance.

Returns:
as specified above

getLiteral

public LpLiteral getLiteral(boolean positive)
Returns a literal for this atom represented by a LpLiteral instance. The parameter constrols if the literal will be positive or negative.

Parameters:
positive - if true, the resulting literal will be positive, otherwise it will be negative
Returns:
as specified above

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 LpAtom instance,
  2. its predicate is equal to this atom's predicate as defined by LpPredicate.equals(Object),
  3. its argument list are equal to this atom's argument list.

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:
LpPredicate.equals(Object), LpTerm.equals(Object), List.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:
LpPredicate.hashCode(), List.hashCode()