Commit d3ea52fd authored by Bernd Kiefer's avatar Bernd Kiefer
Browse files

Add BooleanOperator for convenience.

parent 9f7863bf
......@@ -7,7 +7,7 @@
<groupId>de.dfki.lt.hfc</groupId>
<artifactId>hfc</artifactId>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
<packaging>jar</packaging>
<inceptionYear>2015</inceptionYear>
......
package de.dfki.lt.hfc;
/**
* FunctionalOperator (FO for short) is assumed to be the abstract superclass of all
* functional operators (functions and predicates) used in the forward chainer;
* every subclass of FunctionalOperator MUST implement exactly one unary method,
* called apply(int[] args), together with a package specification and (at least)
* one import statement:
*
* // put your FO in _this_ package:
* package de.dfki.lt.hfc.operators;
*
* // give access to methods from FunctionalOperator which safely access the tuple
* // store via class Operator
* import de.dfki.lt.hfc.FunctionalOperator;
*
* // put your code for the FO in here with exactly the same signature
* public int apply(int[] args) {
* ...
* }
*
* the args (an int array) given to apply() are exactly the arguments given to the FO
* specified in the rules of the forward chainer;
* the return value is usually a positive int, encoding the the application of the FO
* to its arguments;
*
* note that we reserve 0, -1, and -2 as special return values:
* @see FunctionalOperator.UNBOUND
* @see FunctionalOperator.TRUE
* @see FunctionalOperator.FALSE
* @see FunctionalOperator.DONT_KNOW
*
* even a prdicate must return a value (either FunctionalOperator.TRUE or FunctionalOperator.FALSE),
* that is why a predicate is subsumed by the term _Functional_Operator
*
* NOTE: a functional operator is required _not_ to implement any constructor;
*
* @see Operator for the abstract superclass providing useful implemented methods
* @see de.dfki.lt.hfc.Operator for a description of _relational_ operators
*
* @see de.dfki.lt.hfc.operators.NoValue for an example (predicate)
* @see de.dfki.lt.hfc.operators.Concatenate for an example (function)
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Fri Mar 8 12:36:22 CET 2013
*/
public abstract class BooleanOperator extends FunctionalOperator {
public int apply(int[] args) { return holds(args) ? TRUE : FALSE; }
/**
* !!! this is the one and only method that you MUST implement !!!
*/
protected abstract boolean holds(int[] args);
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdDateTime;
public class DTGreater extends FunctionalOperator {
public class DTGreater extends BooleanOperator {
@Override
public int apply(int[] args) {
XsdDateTime date = ((XsdDateTime) getObject(args[0]));
return (date.compareTo(args[1]) > 0) ? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
@Override
protected boolean holds(int[] args) {
XsdDateTime date = ((XsdDateTime) getObject(args[0]));
return date.compareTo(getObject(args[1])) > 0;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdDateTime;
public class DTGreaterEqual extends FunctionalOperator{
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
XsdDateTime date = ((XsdDateTime) getObject(args[0]));
return (date.compareTo(args[1]) >= 0) ? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
public class DTGreaterEqual extends BooleanOperator{
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
protected boolean holds(int[] args) {
XsdDateTime date = ((XsdDateTime) getObject(args[0]));
return date.compareTo(getObject(args[1])) >= 0;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdDateTime;
/**
* checks whether the first argument is less than the second argument;
* arguments are assumed to be of type XsdDateTime;
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Fri May 20 15:49:48 CEST 2011
*/
public final class DTLess extends FunctionalOperator {
public final class DTLess extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args are in fact
......@@ -21,40 +21,40 @@ public final class DTLess extends FunctionalOperator {
* note that apply() does NOT check whether it is given exactly two arguments;
* contrary to XsdUnDateTime, a dateTime object is always _fully_ specified
*
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*/
public int apply(int[] args) {
protected boolean holds(int[] args) {
XsdDateTime firstArg = (XsdDateTime)getObject(args[0]);
XsdDateTime secondArg = (XsdDateTime)getObject(args[1]);
// year (int)
if (firstArg.year < secondArg.year)
return FunctionalOperator.TRUE;
return true;
if (firstArg.year > secondArg.year)
return FunctionalOperator.FALSE;
return false;
//month (int)
if (firstArg.month < secondArg.month)
return FunctionalOperator.TRUE;
return true;
if (firstArg.month > secondArg.month)
return FunctionalOperator.FALSE;
return false;
// day (int)
if (firstArg.day < secondArg.day)
return FunctionalOperator.TRUE;
return true;
if (firstArg.day > secondArg.day)
return FunctionalOperator.FALSE;
return false;
// hour (int)
if (firstArg.hour < secondArg.hour)
return FunctionalOperator.TRUE;
return true;
if (firstArg.hour > secondArg.hour)
return FunctionalOperator.FALSE;
return false;
// minute (int)
if (firstArg.minute < secondArg.minute)
return FunctionalOperator.TRUE;
return true;
if (firstArg.minute > secondArg.minute)
return FunctionalOperator.FALSE;
return false;
// second (float)
if (firstArg.second < secondArg.second)
return FunctionalOperator.TRUE;
return FunctionalOperator.FALSE;
return true;
return false;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdDateTime;
/**
* checks whether the first argument is less or equal than the second argument;
* arguments are assumed to be of type XsdDateTime;
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Wed Jun 22 18:20:27 CEST 2011
*/
public final class DTLessEqual extends FunctionalOperator {
public final class DTLessEqual extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args are in fact
......@@ -21,40 +21,40 @@ public final class DTLessEqual extends FunctionalOperator {
* note that apply() does NOT check whether it is given exactly two arguments;
* contrary to XsdUnDateTime, a dateTime object is always _fully_ specified
*
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*/
public int apply(int[] args) {
protected boolean holds(int[] args) {
XsdDateTime firstArg = (XsdDateTime)getObject(args[0]);
XsdDateTime secondArg = (XsdDateTime)getObject(args[1]);
// year (int)
if (firstArg.year < secondArg.year)
return FunctionalOperator.TRUE;
return true;
if (firstArg.year > secondArg.year)
return FunctionalOperator.FALSE;
return false;
//month (int)
if (firstArg.month < secondArg.month)
return FunctionalOperator.TRUE;
return true;
if (firstArg.month > secondArg.month)
return FunctionalOperator.FALSE;
return false;
// day (int)
if (firstArg.day < secondArg.day)
return FunctionalOperator.TRUE;
return true;
if (firstArg.day > secondArg.day)
return FunctionalOperator.FALSE;
return false;
// hour (int)
if (firstArg.hour < secondArg.hour)
return FunctionalOperator.TRUE;
return true;
if (firstArg.hour > secondArg.hour)
return FunctionalOperator.FALSE;
return false;
// minute (int)
if (firstArg.minute < secondArg.minute)
return FunctionalOperator.TRUE;
return true;
if (firstArg.minute > secondArg.minute)
return FunctionalOperator.FALSE;
return false;
// second (float)
if (firstArg.second <= secondArg.second) // this is the only difference to DTLess -- at first sight, surprising to me !!!
return FunctionalOperator.TRUE;
return FunctionalOperator.FALSE;
return true;
return false;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
/**
* checks whether the first argument is equal to the second argument;
* arguments are assumed to be numbers of type xsd:int
*
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Wed Jun 23 11:04:27 CEST 2010
*/
public final class Equal extends FunctionalOperator {
public final class Equal extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD ints;
/** Make sure the types for the comparison match
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return getObject(args[0]).compareTo(getObject(args[1])) == 0
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
@SuppressWarnings("unchecked")
protected boolean holds(int[] args) {
return getObject(args[0]).compareTo(getObject(args[1])) == 0;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
/**
* checks whether the first argument contains the second argument;
* arguments are assumed to be strings of type xsd:string
* checks whether the first argument is equals to the second argument;
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Wed Jun 23 11:04:27 CEST 2010
*/
public final class Equals extends FunctionalOperator {
public final class Equals extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD strings (and not URIs, XSD ints, etc.);
* note that apply() does NOT check whether it is given exactly two arguments
*
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*/
public int apply(int[] args) {
if (getObject(args[0]).equals(getObject(args[1])))
return FunctionalOperator.TRUE;
else
return FunctionalOperator.FALSE;
protected boolean holds(int[] args) {
return getObject(args[0]).equals(getObject(args[1]));
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.Namespace;
import de.dfki.lt.hfc.BooleanOperator;
/**
* an EquivalentClassTest call can be used for replacing an equivalentClass LHS pattern in a rule;
......@@ -24,7 +23,7 @@ import de.dfki.lt.hfc.Namespace;
*
* NOTE: the apply() call is always given instantiations to the variables
*
* @see de.dfki.lt.hfc.FunctionalOperator
* @see de.dfki.lt.hfc.BooleanOperator
* @see de.dfki.lt.hfc.operators.EquivalentClassAction
* for handling equivalentClass patterns on the RHS of a rule
*
......@@ -32,17 +31,14 @@ import de.dfki.lt.hfc.Namespace;
* @since JDK 1.5
* @version Wed Sep 8 15:21:54 CEST 2010
*/
public final class EquivalentClassTest extends FunctionalOperator {
public final class EquivalentClassTest extends BooleanOperator {
/**
* checks whether the first and the second arg have the same proxy
*/
public int apply(int[] args) {
if (getProxy(args[0]) == getProxy(args[1]) &&
getRelation(args[0]) == getRelation(args[1]))
return FunctionalOperator.TRUE;
else
return FunctionalOperator.FALSE;
protected boolean holds(int[] args) {
return getProxy(args[0]) == getProxy(args[1]) &&
getRelation(args[0]) == getRelation(args[1]);
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.Namespace;
/**
......@@ -24,7 +24,7 @@ import de.dfki.lt.hfc.Namespace;
*
* NOTE: the apply() call is always given instantiations to the variables
*
* @see de.dfki.lt.hfc.FunctionalOperator
* @see de.dfki.lt.hfc.BooleanOperator
* @see de.dfki.lt.hfc.operators.EquivalentPropertyAction
* for handling equivalentProperty patterns on the RHS of a rule
*
......@@ -32,17 +32,14 @@ import de.dfki.lt.hfc.Namespace;
* @since JDK 1.5
* @version Wed Sep 8 15:25:33 CEST 2010
*/
public final class EquivalentPropertyTest extends FunctionalOperator {
public final class EquivalentPropertyTest extends BooleanOperator {
/**
* checks whether the first and the second arg have the same proxy
*/
public int apply(int[] args) {
if (getProxy(args[0]) == getProxy(args[1]) &&
getRelation(args[0]) == getRelation(args[1]))
return FunctionalOperator.TRUE;
else
return FunctionalOperator.FALSE;
protected boolean holds(int[] args) {
return getProxy(args[0]) == getProxy(args[1]) &&
getRelation(args[0]) == getRelation(args[1]);
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdFloat;
import static java.lang.Math.abs;
......@@ -8,22 +8,22 @@ import static java.lang.Math.abs;
* checks whether the first argument is equal to the second argument;
* arguments are assumed to be numbers of type xsd:float
*
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Wed Jun 23 11:04:27 CEST 2010
*/
public final class FEqual extends FunctionalOperator {
public final class FEqual extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
protected boolean holds(int[] args) {
// http://www.ibm.com/developerworks/java/library/j-jtp0114/#N10255
// https://en.wikipedia.org/wiki/Machine_epsilon#Values_for_standard_hardware_floating_point_arithmetics
......@@ -31,10 +31,7 @@ public final class FEqual extends FunctionalOperator {
float two = ((XsdFloat)getObject(args[1])).value;
// recommended value was to small, reason for *100
double epsilon = 5.96e-08 * 100;
if (abs(one/two -1) < epsilon)
return FunctionalOperator.TRUE;
else
return FunctionalOperator.FALSE;
return (abs(one/two -1) < epsilon);
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdFloat;
/**
* checks whether the first argument is greater than the second argument;
* arguments are assumed to be xsd:floats;
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Tue Sep 29 11:11:19 CEST 2009
*/
public final class FGreater extends FunctionalOperator {
public final class FGreater extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return (Float.compare(
((XsdFloat)getObject(args[0])).value,
((XsdFloat)getObject(args[1])).value) > 0)
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
protected boolean holds(int[] args) {
return Float.compare(((XsdFloat)getObject(args[0])).value,
((XsdFloat)getObject(args[1])).value) > 0;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdFloat;
/**
* checks whether the first argument is greater or equal than the second argument;
* arguments are assumed to be xsd:floats;
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Tue Sep 29 11:11:19 CEST 2009
*/
public final class FGreaterEqual extends FunctionalOperator {
public final class FGreaterEqual extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return (Float.compare(
((XsdFloat)getObject(args[0])).value,
((XsdFloat)getObject(args[1])).value) >= 0)
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
protected boolean holds(int[] args) {
return (Float.compare(((XsdFloat)getObject(args[0])).value,
((XsdFloat)getObject(args[1])).value) >= 0);
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.types.XsdFloat;
/**
* checks whether the first argument is less than the second argument;
* arguments are assumed to be xsd:floats;
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
* @return true or false
*
* @see FunctionalOperator
* @see BooleanOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Tue Sep 29 11:11:19 CEST 2009
*/
public final class FLess extends FunctionalOperator {
public final class FLess extends BooleanOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
protected boolean holds(int[] args) {
return (Float.compare(
((XsdFloat)getObject(args[0])).value,
((XsdFloat)getObject(args[1])).value) < 0)
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
((XsdFloat)getObject(args[1])).value) < 0);