org.deri.iris.builtins
Class AbstractBuiltin

java.lang.Object
  extended by org.deri.iris.builtins.AbstractBuiltin
All Implemented Interfaces:
Comparable<IAtom>, IAtom, IBuiltinAtom
Direct Known Subclasses:
ArithmeticBuiltin, BooleanBuiltin, FunctionalBuiltin

public abstract class AbstractBuiltin
extends Object
implements IBuiltinAtom

Serves as skeleton implementation for builtins. If you use this class as superclass, you only have only to implement the evaluate method.

$Id: AbstractBuiltin.java,v 1.11 2007-10-19 07:37:16 poettler_ric Exp $

Version:
$Revision: 1.11 $
Author:
Richard P??ttler (richard dot poettler at deri dot org)

Field Summary
protected static ITerm EMPTY_TERM
          Something to save creating an an empty tuple every time we just need 'any' tuple.
 
Constructor Summary
protected AbstractBuiltin(IPredicate p, ITerm... t)
          Constructs the builtin.
 
Method Summary
 int compareTo(IAtom o)
           
 boolean equals(Object o)
           
 ITuple evaluate(ITuple t)
           Runs the evaluation.
protected  ITerm evaluateTerms(ITerm[] terms, int[] variableIndexes)
          Evaluate the predicate once the terms and variable indexes have been found.
 IPredicate getPredicate()
           Returns the predicate symbol of the atom.
 ITuple getTuple()
           Returns the tuple of the atom.
 int hashCode()
           
 boolean isBuiltin()
          Returns whether this atom is a builtin one, or not.
 boolean isGround()
           Checks whether the atom is grounded (tuple contains no variables).
 int maxUnknownVariables()
          The maximum number of unknown variables allowed such that the predicate can still be evaluated.
 String toString()
           Returns a short description of the inner atom.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_TERM

protected static final ITerm EMPTY_TERM
Something to save creating an an empty tuple every time we just need 'any' tuple.

Constructor Detail

AbstractBuiltin

protected AbstractBuiltin(IPredicate p,
                          ITerm... t)
Constructs the builtin. More precisely it constructs the inner atom. The number of terms submitted to this constructor must match the arity of the predicate.

Parameters:
p - the special predicate for this builtin
t - the terms defining the values and variables for this builtin
Throws:
NullPointerException - if the perdicate or the terms is null
NullPointerException - if the terms contain null
IllegalArgumentException - if the length of the terms and the arity of the perdicate doesn't match
Method Detail

getPredicate

public IPredicate getPredicate()
Description copied from interface: IAtom

Returns the predicate symbol of the atom.

Specified by:
getPredicate in interface IAtom
Returns:
The predicate symbol.

getTuple

public ITuple getTuple()
Description copied from interface: IAtom

Returns the tuple of the atom.

Specified by:
getTuple in interface IAtom
Returns:
The tuple.

isGround

public boolean isGround()
Description copied from interface: IAtom

Checks whether the atom is grounded (tuple contains no variables).

Specified by:
isGround in interface IAtom
Returns:
True if the atom is grounded, otherwise false.

compareTo

public int compareTo(IAtom o)
Specified by:
compareTo in interface Comparable<IAtom>

toString

public String toString()

Returns a short description of the inner atom. The format of the returned String is undocumented and subject to change.

An example String could be: EQUALS(A, B)

Overrides:
toString in class Object
Returns:
the short description

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object o)
Overrides:
equals in class Object

isBuiltin

public boolean isBuiltin()
Description copied from interface: IAtom
Returns whether this atom is a builtin one, or not.

Specified by:
isBuiltin in interface IAtom
Returns:
true if it is builtin, otherwise false

evaluate

public ITuple evaluate(ITuple t)
                throws EvaluationException
Description copied from interface: IBuiltinAtom

Runs the evaluation.

This method takes as input a tuple for which it should run the evaluation. The tuple must contain the substitutes for the variables of this built-in at the corresponding position. Substitutes where this built-in already has a constant might be ignored.

The returned tuple contains the calculated substitutions for the remaining variables (after replacing the variables with the passed in constants) in the built-in in the order their variables appear in the built-in. If there are no variables left to calculate the built-in will check the correctness of the terms and return a tuple containing all constants if it is correct, otherwise null. For example if you evaluate 4 + X = 9 you would get back <5>. The only difference are the binary built-ins: For a binary built-in if you try to evaluate 3 < 4 you will get back <3, 4>, but if you try to evaluate 3 < 2 you will get back null.

Specified by:
evaluate in interface IBuiltinAtom
Parameters:
t - the substitutes for the variables of the builtin
Returns:
the calculated constants or null if the built-in isn't evaluable
Throws:
EvaluationException

evaluateTerms

protected ITerm evaluateTerms(ITerm[] terms,
                              int[] variableIndexes)
                       throws EvaluationException
Evaluate the predicate once the terms and variable indexes have been found.

Parameters:
terms - The array of all terms for this evaluation.
variableIndexes - the indexes of the terms which should be computed (starting at 0)
Returns:
The result of the evaluation.
Throws:
EvaluationException

maxUnknownVariables

public int maxUnknownVariables()
Description copied from interface: IBuiltinAtom
The maximum number of unknown variables allowed such that the predicate can still be evaluated.

Specified by:
maxUnknownVariables in interface IBuiltinAtom
Returns:
The maximum number of unknown variables.