org.deri.iris.builtins
Class BuiltinHelper

java.lang.Object
  extended by org.deri.iris.builtins.BuiltinHelper

public class BuiltinHelper
extends Object

Some helper methods common to some Builtins.


Field Summary
static ITuple EMPTY_TUPLE
          Empty tuple (with arity 0) to avoid some timeconsuming creation of tuples.
 
Method Summary
static ITerm add(ITerm t0, ITerm t1)
           Produces the sum of two terms.
static int compare(ITerm t0, ITerm t1)
          Compares two terms to each other.
static int[] complement(int[] i, int l)
           Computes the complement of an index array.
static ITerm[] concat(int[] idx0, ITerm[] t0, ITerm[] t1)
           Concats two term arrays taking an index array into account.
static ITerm dayPart(ITerm term)
           
static int[] determineGround(Collection<ITerm> t)
          Determines the indexes of the ground terms in a collection.
static int[] determineUnground(Collection<ITerm> t)
          Determines the indexes of the unground terms in a collection.
static ITerm divide(ITerm t0, ITerm t1)
           Produces the quotient of two terms.
static ITerm[] getIndexes(Collection<ITerm> t, int[] pos)
          Returns the terms at a given position in a collection.
static ITerm hourPart(ITerm term)
           
static ITerm minutePart(ITerm term)
           
static ITerm modulus(ITerm t0, ITerm t1)
           Produces the modulus of two terms.
static ITerm monthPart(ITerm term)
           
static ITerm multiply(ITerm t0, ITerm t1)
           Produces the product of two terms.
static int numbersCompare(INumericTerm n0, INumericTerm n1)
          Compares two numeric terms using the compareTo method of the terms.
static boolean numbersEqual(INumericTerm n0, INumericTerm n1)
          Compares two numerics by casting them to doubles and then comparing them for equality.
static ITerm promote(ITerm t0, ITerm t1)
          Promotes a term from one type to another according to the promotion rules defined in the XPath specification: Numeric type promotion A value of type xs:float (or any type derived by restriction from xs:float) can be promoted to the type xs:double.
static ITerm secondPart(ITerm term)
           
static ITerm subtract(ITerm t0, ITerm t1)
           Produces the difference of two terms.
static ITerm timezonePart(ITerm term)
           
static ITerm yearPart(ITerm term)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_TUPLE

public static final ITuple EMPTY_TUPLE
Empty tuple (with arity 0) to avoid some timeconsuming creation of tuples.

Method Detail

numbersEqual

public static boolean numbersEqual(INumericTerm n0,
                                   INumericTerm n1)
Compares two numerics by casting them to doubles and then comparing them for equality. This method assumes that only numbers are stored in INumericTerm.

Parameters:
n0 - the first number
n1 - the second number
Returns:
whether the numbers are equal
Throws:
NullPointerException - if one of the numbers is null
See Also:
Number

numbersCompare

public static int numbersCompare(INumericTerm n0,
                                 INumericTerm n1)
Compares two numeric terms using the compareTo method of the terms. Respects floating point round off errors.

Parameters:
n0 - the first number
n1 - the second number
Returns:
0 if they are equal, a value < 0 if n0 is smaller than n1 and a value > 0 if n0 is bigger than n1
Throws:
NullPointerException - if one of the numbers is null
See Also:
Number

compare

public static int compare(ITerm t0,
                          ITerm t1)
Compares two terms to each other. A value <0, 0 or >0 will be returned, if the first term is smaller, equal or greater than the second one.

Parameters:
t0 - the first term
t1 - the second term
Returns:
an Integer determining which one of the terms is bigger
Throws:
NullPointerException - if one of the terms is null
IllegalArgumentException - if the two terms couldn't be compared

add

public static ITerm add(ITerm t0,
                        ITerm t1)

Produces the sum of two terms. The resulting term will be of the most accurate type of the submitted ones.

Parameters:
t0 - the first summand
t1 - the second summand
Returns:
the sum
Throws:
NullPointerException - if one of the terms is null
IllegalArgumentException - if the operation couldn't be applied to the terms

subtract

public static ITerm subtract(ITerm t0,
                             ITerm t1)

Produces the difference of two terms. The resulting term will be of the most accurate type of the submitted ones.

Parameters:
t0 - the minuend
t1 - the subtrahend
Returns:
the differnece
Throws:
NullPointerException - if one of the terms is null
IllegalArgumentException - if the operation couldn't be applied to the terms

multiply

public static ITerm multiply(ITerm t0,
                             ITerm t1)

Produces the product of two terms. The resulting term will be of the most accurate type of the submitted ones.

At the moment only INumericTerms are supported.

Parameters:
t0 - the first factor
t1 - the second factor
Returns:
the product
Throws:
NullPointerException - if one of the terms is null
IllegalArgumentException - if one of the terms is not a INumericTerm

divide

public static ITerm divide(ITerm t0,
                           ITerm t1)
                    throws EvaluationException

Produces the quotient of two terms. The resulting term will be of the most accurate type of the submitted ones.

At the moment only INumericTerms are supported.

Parameters:
t0 - the dividend
t1 - the divisor
Returns:
the quotient
Throws:
EvaluationException
NullPointerException - if one of the terms is null

modulus

public static ITerm modulus(ITerm t0,
                            ITerm t1)
                     throws EvaluationException

Produces the modulus of two terms. The resulting term will be of the most accurate type of the submitted ones.

At the moment only INumericTerms are supported.

Parameters:
t0 - the first argument
t1 - the second argument
Returns:
the product = t0 % t1
Throws:
EvaluationException
NullPointerException - if one of the terms is null
IllegalArgumentException - if one of the terms is not a INumericTerm

promote

public static ITerm promote(ITerm t0,
                            ITerm t1)
Promotes a term from one type to another according to the promotion rules defined in the XPath specification:
  1. Numeric type promotion
    • A value of type xs:float (or any type derived by restriction from xs:float) can be promoted to the type xs:double. The result is the xs:double value that is the same as the original value.
    • A value of type xs:decimal (or any type derived by restriction from xs:decimal) can be promoted to either of the types xs:float or xs:double. The result of this promotion is created by casting the original value to the required type. This kind of promotion may cause loss of precision.

Parameters:
t0 - The term which should be promoted depending on the term t1.
t1 - The term according to which the term t0 is promoted.
Returns:
The result of the the type promotion.

yearPart

public static ITerm yearPart(ITerm term)

monthPart

public static ITerm monthPart(ITerm term)

dayPart

public static ITerm dayPart(ITerm term)

hourPart

public static ITerm hourPart(ITerm term)

minutePart

public static ITerm minutePart(ITerm term)

secondPart

public static ITerm secondPart(ITerm term)

timezonePart

public static ITerm timezonePart(ITerm term)

determineGround

public static int[] determineGround(Collection<ITerm> t)
Determines the indexes of the ground terms in a collection.

Parameters:
t - the collection where to search for ground terms
Returns:
the indexes of the ground terms (the first term has the index 0)
Throws:
NullPointerException - if the collection is null

getIndexes

public static ITerm[] getIndexes(Collection<ITerm> t,
                                 int[] pos)
Returns the terms at a given position in a collection.

Parameters:
t - the collection from where to retrieve the terms
pos - the positions of the terms to retrieve
Returns:
the retrieved terms
Throws:
NullPointerException - if the collection is null
NullPointerException - if the position array is null
IllegalArgumentException - if ther are more terms requested, than given (pos.length > t.size())

determineUnground

public static int[] determineUnground(Collection<ITerm> t)
Determines the indexes of the unground terms in a collection.

Parameters:
t - the collection where to search for ground terms
Returns:
the indexes of the unground terms (the first term has the index 0)
Throws:
NullPointerException - if the collection is null

complement

public static int[] complement(int[] i,
                               int l)

Computes the complement of an index array.

E.g. we have computed an index array [1, 3, 4] of a collection with length 5 we would get [0, 2] as complement.

Parameters:
i - the array from which to compute the complement
l - the length of the collection whe took the index from
Returns:
the complement
Throws:
NullPointerException - if the index array was null
IllegalArgumentException - if the lenght was nevative
See Also:
#determineGround(Collection), #determineUnground(Collection)

concat

public static ITerm[] concat(int[] idx0,
                             ITerm[] t0,
                             ITerm[] t1)

Concats two term arrays taking an index array into account.

E.g. we got an index of [0, 3, 4], the firs array is ['a', 'b', 'c'] and the second [12, 13, 14] we will get ['a', 12, 13, 'b', 'c', 14].

Parameters:
idx0 - the index array
t0 - the first array (this terms will be put at he positions of the index array)
t1 - the second array (with this terms the gaps will be filled up)
Throws:
NullPointerException - if the index array is null
NullPointerException - if one of the term arrays is null
IllegalArgumentException - if the lenght of the index and the first term array don't match
See Also:
#determineGround(Collection), #determineUnground(Collection)