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

Add BooleanOperator for convenience.

parent 9f7863bf
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<groupId>de.dfki.lt.hfc</groupId> <groupId>de.dfki.lt.hfc</groupId>
<artifactId>hfc</artifactId> <artifactId>hfc</artifactId>
<version>1.2.3-SNAPSHOT</version> <version>1.2.4-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<inceptionYear>2015</inceptionYear> <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; 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; import de.dfki.lt.hfc.types.XsdDateTime;
public class DTGreater extends FunctionalOperator { public class DTGreater extends BooleanOperator {
@Override @Override
public int apply(int[] args) { protected boolean holds(int[] args) {
XsdDateTime date = ((XsdDateTime) getObject(args[0])); XsdDateTime date = ((XsdDateTime) getObject(args[0]));
return (date.compareTo(args[1]) > 0) ? FunctionalOperator.TRUE : FunctionalOperator.FALSE; return date.compareTo(getObject(args[1])) > 0;
} }
} }
package de.dfki.lt.hfc.operators; 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; import de.dfki.lt.hfc.types.XsdDateTime;
public class DTGreaterEqual extends FunctionalOperator{ public class DTGreaterEqual extends BooleanOperator{
/** /**
* note that apply() does NOT check at the moment whether the int args * note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats; * represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments * note that apply() does NOT check whether it is given exactly two arguments
*/ */
public int apply(int[] args) { protected boolean holds(int[] args) {
XsdDateTime date = ((XsdDateTime) getObject(args[0])); XsdDateTime date = ((XsdDateTime) getObject(args[0]));
return (date.compareTo(args[1]) >= 0) ? FunctionalOperator.TRUE : FunctionalOperator.FALSE; return date.compareTo(getObject(args[1])) >= 0;
} }
} }
package de.dfki.lt.hfc.operators; 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; import de.dfki.lt.hfc.types.XsdDateTime;
/** /**
* checks whether the first argument is less than the second argument; * checks whether the first argument is less than the second argument;
* arguments are assumed to be of type XsdDateTime; * arguments are assumed to be of type XsdDateTime;
* *
* @see FunctionalOperator * @see BooleanOperator
* *
* @author (C) Hans-Ulrich Krieger * @author (C) Hans-Ulrich Krieger
* @since JDK 1.5 * @since JDK 1.5
* @version Fri May 20 15:49:48 CEST 2011 * @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 * 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 { ...@@ -21,40 +21,40 @@ public final class DTLess extends FunctionalOperator {
* note that apply() does NOT check whether it is given exactly two arguments; * note that apply() does NOT check whether it is given exactly two arguments;
* contrary to XsdUnDateTime, a dateTime object is always _fully_ specified * 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 firstArg = (XsdDateTime)getObject(args[0]);
XsdDateTime secondArg = (XsdDateTime)getObject(args[1]); XsdDateTime secondArg = (XsdDateTime)getObject(args[1]);
// year (int) // year (int)
if (firstArg.year < secondArg.year) if (firstArg.year < secondArg.year)
return FunctionalOperator.TRUE; return true;
if (firstArg.year > secondArg.year) if (firstArg.year > secondArg.year)
return FunctionalOperator.FALSE; return false;
//month (int) //month (int)
if (firstArg.month < secondArg.month) if (firstArg.month < secondArg.month)
return FunctionalOperator.TRUE; return true;
if (firstArg.month > secondArg.month) if (firstArg.month > secondArg.month)
return FunctionalOperator.FALSE; return false;
// day (int) // day (int)
if (firstArg.day < secondArg.day) if (firstArg.day < secondArg.day)
return FunctionalOperator.TRUE; return true;
if (firstArg.day > secondArg.day) if (firstArg.day > secondArg.day)
return FunctionalOperator.FALSE; return false;
// hour (int) // hour (int)
if (firstArg.hour < secondArg.hour) if (firstArg.hour < secondArg.hour)
return FunctionalOperator.TRUE; return true;
if (firstArg.hour > secondArg.hour) if (firstArg.hour > secondArg.hour)
return FunctionalOperator.FALSE; return false;
// minute (int) // minute (int)
if (firstArg.minute < secondArg.minute) if (firstArg.minute < secondArg.minute)
return FunctionalOperator.TRUE; return true;
if (firstArg.minute > secondArg.minute) if (firstArg.minute > secondArg.minute)
return FunctionalOperator.FALSE; return false;
// second (float) // second (float)
if (firstArg.second < secondArg.second) if (firstArg.second < secondArg.second)
return FunctionalOperator.TRUE; return true;
return FunctionalOperator.FALSE; return false;
} }
} }
package de.dfki.lt.hfc.operators; 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; import de.dfki.lt.hfc.types.XsdDateTime;
/** /**
* checks whether the first argument is less or equal than the second argument; * checks whether the first argument is less or equal than the second argument;
* arguments are assumed to be of type XsdDateTime; * arguments are assumed to be of type XsdDateTime;
* *
* @see FunctionalOperator * @see BooleanOperator
* *
* @author (C) Hans-Ulrich Krieger * @author (C) Hans-Ulrich Krieger
* @since JDK 1.5 * @since JDK 1.5
* @version Wed Jun 22 18:20:27 CEST 2011 * @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 * 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 { ...@@ -21,40 +21,40 @@ public final class DTLessEqual extends FunctionalOperator {
* note that apply() does NOT check whether it is given exactly two arguments; * note that apply() does NOT check whether it is given exactly two arguments;
* contrary to XsdUnDateTime, a dateTime object is always _fully_ specified * 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 firstArg = (XsdDateTime)getObject(args[0]);
XsdDateTime secondArg = (XsdDateTime)getObject(args[1]); XsdDateTime secondArg = (XsdDateTime)getObject(args[1]);
// year (int) // year (int)
if (firstArg.year < secondArg.year) if (firstArg.year < secondArg.year)
return FunctionalOperator.TRUE; return true;
if (firstArg.year > secondArg.year) if (firstArg.year > secondArg.year)
return FunctionalOperator.FALSE; return false;
//month (int) //month (int)
if (firstArg.month < secondArg.month) if (firstArg.month < secondArg.month)
return FunctionalOperator.TRUE; return true;
if (firstArg.month > secondArg.month) if (firstArg.month > secondArg.month)
return FunctionalOperator.FALSE; return false;
// day (int) // day (int)
if (firstArg.day < secondArg.day) if (firstArg.day < secondArg.day)
return FunctionalOperator.TRUE; return true;
if (firstArg.day > secondArg.day) if (firstArg.day > secondArg.day)
return FunctionalOperator.FALSE; return false;
// hour (int) // hour (int)
if (firstArg.hour < secondArg.hour) if (firstArg.hour < secondArg.hour)
return FunctionalOperator.TRUE; return true;
if (firstArg.hour > secondArg.hour) if (firstArg.hour > secondArg.hour)
return FunctionalOperator.FALSE; return false;
// minute (int) // minute (int)
if (firstArg.minute < secondArg.minute) if (firstArg.minute < secondArg.minute)
return FunctionalOperator.TRUE; return true;
if (firstArg.minute > secondArg.minute) if (firstArg.minute > secondArg.minute)
return FunctionalOperator.FALSE; return false;
// second (float) // second (float)
if (firstArg.second <= secondArg.second) // this is the only difference to DTLess -- at first sight, surprising to me !!! if (firstArg.second <= secondArg.second) // this is the only difference to DTLess -- at first sight, surprising to me !!!
return FunctionalOperator.TRUE; return true;
return FunctionalOperator.FALSE; return false;
} }
} }
package de.dfki.lt.hfc.operators; 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; * 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 * @author (C) Hans-Ulrich Krieger
* @since JDK 1.5 * @since JDK 1.5
* @version Wed Jun 23 11:04:27 CEST 2010 * @version Wed Jun 23 11:04:27 CEST 2010
*/ */
public final class Equal extends FunctionalOperator { public final class Equal extends BooleanOperator {
/** /** Make sure the types for the comparison match
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD ints;
* note that apply() does NOT check whether it is given exactly two arguments * note that apply() does NOT check whether it is given exactly two arguments
*/ */
public int apply(int[] args) { @SuppressWarnings("unchecked")
return getObject(args[0]).compareTo(getObject(args[1])) == 0 protected boolean holds(int[] args) {
? FunctionalOperator.TRUE : FunctionalOperator.FALSE; return getObject(args[0]).compareTo(getObject(args[1])) == 0;
} }
} }
package de.dfki.lt.hfc.operators; 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; * checks whether the first argument is equals to the second argument;
* arguments are assumed to be strings of type xsd:string
* *
* @see FunctionalOperator * @see BooleanOperator
* *
* @author (C) Hans-Ulrich Krieger * @author (C) Hans-Ulrich Krieger
* @since JDK 1.5 * @since JDK 1.5
* @version Wed Jun 23 11:04:27 CEST 2010 * @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 * 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) { protected boolean holds(int[] args) {
if (getObject(args[0]).equals(getObject(args[1]))) return getObject(args[0]).equals(getObject(args[1]));
return FunctionalOperator.TRUE;
else
return FunctionalOperator.FALSE;
} }
} }
package de.dfki.lt.hfc.operators; package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator; import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.Namespace;
/** /**
* an EquivalentClassTest call can be used for replacing an equivalentClass LHS pattern in a rule; * 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; ...@@ -24,7 +23,7 @@ import de.dfki.lt.hfc.Namespace;
* *
* NOTE: the apply() call is always given instantiations to the variables * 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 * @see de.dfki.lt.hfc.operators.EquivalentClassAction
* for handling equivalentClass patterns on the RHS of a rule * for handling equivalentClass patterns on the RHS of a rule
* *
...@@ -32,17 +31,14 @@ import de.dfki.lt.hfc.Namespace; ...@@ -32,17 +31,14 @@ import de.dfki.lt.hfc.Namespace;
* @since JDK 1.5 * @since JDK 1.5
* @version Wed Sep 8 15:21:54 CEST 2010 * @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 * checks whether the first and the second arg have the same proxy
*/ */
public int apply(int[] args) { protected boolean holds(int[] args) {
if (getProxy(args[0]) == getProxy(args[1]) && return getProxy(args[0]) == getProxy(args[1]) &&
getRelation(args[0]) == getRelation(args[1])) getRelation(args[0]) == getRelation(args[1]);
return FunctionalOperator.TRUE;
else
return FunctionalOperator.FALSE;
} }
} }
package de.dfki.lt.hfc.operators; package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator; import de.dfki.lt.hfc.BooleanOperator;
import de.dfki.lt.hfc.Namespace; import de.dfki.lt.hfc.Namespace;
/** /**
...@@ -24,7 +24,7 @@ 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 * 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 * @see de.dfki.lt.hfc.operators.EquivalentPropertyAction
* for handling equivalentProperty patterns on the RHS of a rule * for handling equivalentProperty patterns on the RHS of a rule
* *
...@@ -32,17 +32,14 @@ import de.dfki.lt.hfc.Namespace; ...@@ -32,17 +32,14 @@ import de.dfki.lt.hfc.Namespace;
* @since JDK 1.5 * @since JDK 1.5
* @version Wed Sep 8 15:25:33 CEST 2010 * @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 * checks whether the first and the second arg have the same proxy
*/ */
public int apply(int[] args) { protected boolean holds(int[] args) {
if (getProxy(args[0]) == getProxy(args[1]) && return getProxy(args[0]) == getProxy(args[1]) &&
getRelation(args[0]) == getRelation(args[1])) getRelation(args[0]) == getRelation(args[1]);
return FunctionalOperator.TRUE;
else
return FunctionalOperator.FALSE;
} }
} }
package de.dfki.lt.hfc.operators; 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 de.dfki.lt.hfc.types.XsdFloat;
import static java.lang.Math.abs; import static java.lang.Math.abs;
...@@ -8,22 +8,22 @@ 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; * checks whether the first argument is equal to the second argument;
* arguments are assumed to be numbers of type xsd:float * 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 * @author (C) Hans-Ulrich Krieger
* @since JDK 1.5 * @since JDK 1.5
* @version Wed Jun 23 11:04:27 CEST 2010 * @version Wed Jun 23 11:04:27 CEST 2010
*/ */
public final class FEqual extends FunctionalOperator { public final class FEqual extends BooleanOperator {