RYA-377 Temporal Within Function
diff --git a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/AfterTemporalInstant.java b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/AfterTemporalInstant.java
index 38c02ba..fe58e37 100644
--- a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/AfterTemporalInstant.java
+++ b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/AfterTemporalInstant.java
@@ -29,12 +29,10 @@
* Filter function in a SPARQL query used to filter when a point of time is after another.
*/
@DefaultAnnotation(NonNull.class)
-public class AfterTemporalInstant extends TemporalRelationFunction {
- public static final String URI = BASE_URI + "after";
-
+public class AfterTemporalInstant extends TemporalInstantRelationFunction {
@Override
public String getURI() {
- return URI;
+ return TemporalURIs.AFTER;
}
@Override
diff --git a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/BeforeTemporalInstant.java b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/BeforeTemporalInstant.java
index c9f31c3..4cb12e0 100644
--- a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/BeforeTemporalInstant.java
+++ b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/BeforeTemporalInstant.java
@@ -29,12 +29,10 @@
* Filter function in a SPARQL query used to filter when a point of time is before another.
*/
@DefaultAnnotation(NonNull.class)
-public class BeforeTemporalInstant extends TemporalRelationFunction {
- public static final String URI = BASE_URI + "before";
-
+public class BeforeTemporalInstant extends TemporalInstantRelationFunction {
@Override
public String getURI() {
- return URI;
+ return TemporalURIs.BEFORE;
}
@Override
diff --git a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporal.java b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporalInstant.java
similarity index 84%
rename from common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporal.java
rename to common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporalInstant.java
index 07391c8..10fe40b 100644
--- a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporal.java
+++ b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporalInstant.java
@@ -25,19 +25,14 @@
import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
-import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
-import edu.umd.cs.findbugs.annotations.NonNull;
-
/**
* Filter function in a SPARQL query used to filter equality over time.
*/
@DefaultAnnotation(NonNull.class)
-public class EqualsTemporal extends TemporalRelationFunction {
- private static final String URI = BASE_URI + "equals";
-
+public class EqualsTemporalInstant extends TemporalInstantRelationFunction {
@Override
public String getURI() {
- return URI;
+ return TemporalURIs.EQUALS;
}
@Override
diff --git a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalRelationFunction.java b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalInstantRelationFunction.java
similarity index 94%
rename from common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalRelationFunction.java
rename to common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalInstantRelationFunction.java
index 02710d9..d622329 100644
--- a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalRelationFunction.java
+++ b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalInstantRelationFunction.java
@@ -33,9 +33,7 @@
* Function for comparing 2 {@link ZonedDateTime} objects in a SPARQL filter.
*/
@DefaultAnnotation(NonNull.class)
-abstract class TemporalRelationFunction implements Function {
- public static final String BASE_URI = "http://rya.apache.org/ns/temporal/";
-
+abstract class TemporalInstantRelationFunction implements Function {
@Override
public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException {
if (args.length != 2) {
diff --git a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalRelationFunction.java b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalIntervalRelationFunction.java
similarity index 73%
copy from common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalRelationFunction.java
copy to common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalIntervalRelationFunction.java
index 02710d9..4a0d4bd 100644
--- a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalRelationFunction.java
+++ b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalIntervalRelationFunction.java
@@ -30,22 +30,27 @@
import edu.umd.cs.findbugs.annotations.NonNull;
/**
- * Function for comparing 2 {@link ZonedDateTime} objects in a SPARQL filter.
+ * Function for comparing a {@link ZonedDateTime} with an interval of {@link ZonedDateTime} objects in a SPARQL filter.
*/
@DefaultAnnotation(NonNull.class)
-abstract class TemporalRelationFunction implements Function {
- public static final String BASE_URI = "http://rya.apache.org/ns/temporal/";
-
+abstract class TemporalIntervalRelationFunction implements Function {
@Override
public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException {
if (args.length != 2) {
throw new ValueExprEvaluationException(getURI() + " requires exactly 2 arguments, got " + args.length);
}
+ final String[] strInterval = args[1].stringValue().split("/");
+ if (strInterval.length != 2) {
+ throw new ValueExprEvaluationException(getURI() + " requires the second argument: " + args[1] + " to be 2 dates seperated by a \'/\'");
+ }
try {
final ZonedDateTime date1 = ZonedDateTime.parse(args[0].stringValue());
- final ZonedDateTime date2 = ZonedDateTime.parse(args[1].stringValue());
- final boolean result = relation(date1, date2);
+ final ZonedDateTime[] interval = new ZonedDateTime[] {
+ ZonedDateTime.parse(strInterval[0]),
+ ZonedDateTime.parse(strInterval[1])
+ };
+ final boolean result = relation(date1, interval);
return valueFactory.createLiteral(result);
} catch (final DateTimeParseException e) {
@@ -58,8 +63,8 @@
* objects.
*
* @param d1 first {@link ZonedDateTime} to compare. (not null)
- * @param d2 second {@link ZonedDateTime} to compare. (not null)
+ * @param interval The interval represented by 2 {@link ZonedDateTime}s. (not null)
* @return The result of the comparison between {@link ZonedDateTime}s.
*/
- protected abstract boolean relation(ZonedDateTime d1, ZonedDateTime d2);
+ protected abstract boolean relation(ZonedDateTime d1, ZonedDateTime[] interval);
}
diff --git a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalURIs.java b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalURIs.java
new file mode 100644
index 0000000..7902a5f
--- /dev/null
+++ b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/TemporalURIs.java
@@ -0,0 +1,23 @@
+package org.apache.rya.api.function.temporal;
+
+/**
+ * Constants for the Temporal Functions used in rya.
+ */
+public class TemporalURIs {
+ /**
+ * All temporal functions have the namespace (<tt>http://rya.apache.org/ns/temporal#</tt>).
+ */
+ public static final String NAMESPACE = "http://rya.apache.org/ns/temporal#";
+
+ /** <tt>http://rya.apache.org/ns/temporal#equals</tt> */
+ public final static String EQUALS = NAMESPACE + "equals";
+
+ /** <tt>http://rya.apache.org/ns/temporal#before</tt> */
+ public final static String BEFORE = NAMESPACE + "before";
+
+ /** <tt>http://rya.apache.org/ns/temporal#after</tt> */
+ public final static String AFTER = NAMESPACE + "after";
+
+ /** <tt>http://rya.apache.org/ns/temporal#within</tt> */
+ public final static String WITHIN = NAMESPACE + "within";
+}
diff --git a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporal.java b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/WithinTemporalInterval.java
similarity index 75%
copy from common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporal.java
copy to common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/WithinTemporalInterval.java
index 07391c8..5fc3e03 100644
--- a/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/EqualsTemporal.java
+++ b/common/rya.api.function/src/main/java/org/apache/rya/api/function/temporal/WithinTemporalInterval.java
@@ -25,25 +25,21 @@
import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
-import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
-import edu.umd.cs.findbugs.annotations.NonNull;
-
/**
- * Filter function in a SPARQL query used to filter equality over time.
+ * Filter function in a SPARQL query used to filter when a point of time is
+ * within an interval.
*/
@DefaultAnnotation(NonNull.class)
-public class EqualsTemporal extends TemporalRelationFunction {
- private static final String URI = BASE_URI + "equals";
-
+public class WithinTemporalInterval extends TemporalIntervalRelationFunction {
@Override
public String getURI() {
- return URI;
+ return TemporalURIs.WITHIN;
}
@Override
- protected boolean relation(final ZonedDateTime d1, final ZonedDateTime d2) {
+ protected boolean relation(final ZonedDateTime d1, final ZonedDateTime[] interval) {
Objects.requireNonNull(d1);
- Objects.requireNonNull(d2);
- return d1.isEqual(d2);
+ Objects.requireNonNull(interval);
+ return d1.isAfter(interval[0]) && d1.isBefore(interval[1]);
}
}
diff --git a/common/rya.api.function/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function b/common/rya.api.function/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
index 2ec01d5..1172844 100644
--- a/common/rya.api.function/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
+++ b/common/rya.api.function/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
@@ -16,6 +16,7 @@
# specific language governing permissions and limitations
# under the License.
#
-org.apache.rya.api.function.temporal.EqualsTemporal
+org.apache.rya.api.function.temporal.EqualsTemporalInstant
org.apache.rya.api.function.temporal.BeforeTemporalInstant
org.apache.rya.api.function.temporal.AfterTemporalInstant
+org.apache.rya.api.function.temporal.WithinTemporalInterval
diff --git a/common/rya.api.function/src/main/test/org/apache/rya/api/function/temporal/EqualsTemporalFunctionsTest.java b/common/rya.api.function/src/main/test/org/apache/rya/api/function/temporal/EqualsTemporalFunctionsTest.java
index f32904a..0d14d48 100644
--- a/common/rya.api.function/src/main/test/org/apache/rya/api/function/temporal/EqualsTemporalFunctionsTest.java
+++ b/common/rya.api.function/src/main/test/org/apache/rya/api/function/temporal/EqualsTemporalFunctionsTest.java
@@ -36,7 +36,7 @@
@Test
public void testEquals_equal() throws Exception {
- final EqualsTemporal function = new EqualsTemporal();
+ final EqualsTemporalInstant function = new EqualsTemporalInstant();
// 2 times equal
final Value[] args = new Value[2];
@@ -49,7 +49,7 @@
@Test
public void testEquals_before() throws Exception {
- final EqualsTemporal function = new EqualsTemporal();
+ final EqualsTemporalInstant function = new EqualsTemporalInstant();
// first time is before
final Value[] args = new Value[2];
@@ -62,7 +62,7 @@
@Test
public void testEquals_after() throws Exception {
- final EqualsTemporal function = new EqualsTemporal();
+ final EqualsTemporalInstant function = new EqualsTemporalInstant();
// first time is after
final Value[] args = new Value[2];
diff --git a/common/rya.api.function/src/main/test/org/apache/rya/api/function/temporal/WithinTemporalFunctionsTest.java b/common/rya.api.function/src/main/test/org/apache/rya/api/function/temporal/WithinTemporalFunctionsTest.java
new file mode 100644
index 0000000..6dee711
--- /dev/null
+++ b/common/rya.api.function/src/main/test/org/apache/rya/api/function/temporal/WithinTemporalFunctionsTest.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rya.api.function.temporal;
+
+import static org.junit.Assert.assertEquals;
+
+import java.time.ZonedDateTime;
+
+import org.junit.Test;
+import org.openrdf.model.Value;
+import org.openrdf.model.ValueFactory;
+import org.openrdf.model.impl.ValueFactoryImpl;
+import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
+
+public class WithinTemporalFunctionsTest {
+ private static final ZonedDateTime TIME = ZonedDateTime.parse("2015-12-30T12:00:00Z");
+ private static final ZonedDateTime TIME_10 = ZonedDateTime.parse("2015-12-30T12:00:10Z");
+ private static final ZonedDateTime TIME_20 = ZonedDateTime.parse("2015-12-30T12:00:20Z");
+
+ final ValueFactory VF = ValueFactoryImpl.getInstance();
+
+ @Test(expected = ValueExprEvaluationException.class)
+ public void within_NotInterval() throws Exception {
+ // correct date formats are ensured through other tests
+ final WithinTemporalInterval function = new WithinTemporalInterval();
+
+ // 2 dates are provided
+ final Value[] args = new Value[2];
+ args[0] = VF.createLiteral(TIME.toString());
+ args[1] = VF.createLiteral(TIME.toString());
+ function.evaluate(VF, args);
+ }
+
+ @Test
+ public void testWithin_beginning() throws Exception {
+ final WithinTemporalInterval function = new WithinTemporalInterval();
+
+ // 2 times equal
+ final Value[] args = new Value[2];
+ args[0] = VF.createLiteral(TIME.toString());
+ args[1] = VF.createLiteral(TIME.toString() + "/" + TIME_20.toString());
+ final Value rez = function.evaluate(VF, args);
+
+ assertEquals(VF.createLiteral(false), rez);
+ }
+
+ @Test
+ public void testWithin_within() throws Exception {
+ final WithinTemporalInterval function = new WithinTemporalInterval();
+
+ // first time is before
+ final Value[] args = new Value[2];
+ args[0] = VF.createLiteral(TIME_10.toString());
+ args[1] = VF.createLiteral(TIME.toString() + "/" + TIME_20.toString());
+ final Value rez = function.evaluate(VF, args);
+
+ assertEquals(VF.createLiteral(true), rez);
+ }
+
+ @Test
+ public void testWithin_end() throws Exception {
+ final WithinTemporalInterval function = new WithinTemporalInterval();
+
+ // first time is after
+ final Value[] args = new Value[2];
+ args[0] = VF.createLiteral(TIME_20.toString());
+ args[1] = VF.createLiteral(TIME.toString() + "/" + TIME_20.toString());
+ final Value rez = function.evaluate(VF, args);
+
+ assertEquals(VF.createLiteral(false), rez);
+ }
+}
diff --git a/extras/rya.streams/kafka/src/test/java/org/apache/rya/streams/kafka/processors/filter/TemporalFilterIT.java b/extras/rya.streams/kafka/src/test/java/org/apache/rya/streams/kafka/processors/filter/TemporalFilterIT.java
index 0a2185d..22a883b 100644
--- a/extras/rya.streams/kafka/src/test/java/org/apache/rya/streams/kafka/processors/filter/TemporalFilterIT.java
+++ b/extras/rya.streams/kafka/src/test/java/org/apache/rya/streams/kafka/processors/filter/TemporalFilterIT.java
@@ -74,8 +74,8 @@
}
}
- // There are 3 temporal functions registered, ensure that there are 3.
- assertEquals(3, count);
+ // There are 4 temporal functions registered, ensure that there are 4.
+ assertEquals(4, count);
}
@Test
@@ -89,12 +89,12 @@
// Get the RDF model objects that will be used to build the query.
final String sparql =
"PREFIX time: <http://www.w3.org/2006/time/> \n"
- + "PREFIX tempf: <http://rya.apache.org/ns/temporal/>\n"
- + "SELECT * \n"
- + "WHERE { \n"
- + " <urn:time> time:atTime ?date .\n"
- + " FILTER(tempf:equals(?date, \"" + TIME.toString() + "\")) "
- + "}";
+ + "PREFIX tempf: <http://rya.apache.org/ns/temporal/>\n"
+ + "SELECT * \n"
+ + "WHERE { \n"
+ + " <urn:time> time:atTime ?date .\n"
+ + " FILTER(tempf:equals(?date, \"" + TIME.toString() + "\")) "
+ + "}";
// Setup a topology.
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
@@ -123,12 +123,12 @@
// Get the RDF model objects that will be used to build the query.
final String sparql =
"PREFIX time: <http://www.w3.org/2006/time/> \n"
- + "PREFIX tempf: <http://rya.apache.org/ns/temporal/>\n"
- + "SELECT * \n"
- + "WHERE { \n"
- + " <urn:time> time:atTime ?date .\n"
- + " FILTER(tempf:before(?date, \"" + TIME_10.toString() + "\")) "
- + "}";
+ + "PREFIX tempf: <http://rya.apache.org/ns/temporal/>\n"
+ + "SELECT * \n"
+ + "WHERE { \n"
+ + " <urn:time> time:atTime ?date .\n"
+ + " FILTER(tempf:before(?date, \"" + TIME_10.toString() + "\")) "
+ + "}";
// Setup a topology.
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
@@ -157,12 +157,12 @@
// Get the RDF model objects that will be used to build the query.
final String sparql =
"PREFIX time: <http://www.w3.org/2006/time/> \n"
- + "PREFIX tempf: <http://rya.apache.org/ns/temporal/>\n"
- + "SELECT * \n"
- + "WHERE { \n"
- + " <urn:time> time:atTime ?date .\n"
- + " FILTER(tempf:after(?date, \"" + TIME_10.toString() + "\")) "
- + "}";
+ + "PREFIX tempf: <http://rya.apache.org/ns/temporal/>\n"
+ + "SELECT * \n"
+ + "WHERE { \n"
+ + " <urn:time> time:atTime ?date .\n"
+ + " FILTER(tempf:after(?date, \"" + TIME_10.toString() + "\")) "
+ + "}";
// Setup a topology.
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
@@ -180,6 +180,40 @@
RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
}
+ @Test
+ public void showWithinWorks() throws Exception {
+ // Enumerate some topics that will be re-used
+ final String ryaInstance = UUID.randomUUID().toString();
+ final UUID queryId = UUID.randomUUID();
+ final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
+ final String resultsTopic = KafkaTopics.queryResultsTopic(queryId);
+
+ // Get the RDF model objects that will be used to build the query.
+ final String sparql =
+ "PREFIX time: <http://www.w3.org/2006/time/> \n"
+ + "PREFIX tempf: <http://rya.apache.org/ns/temporal/>\n"
+ + "SELECT * \n"
+ + "WHERE { \n"
+ + " <urn:time> time:atTime ?date .\n"
+ + " FILTER(tempf:within(?date, \"" + TIME.toString() + "/" + TIME_20.toString() + "\")) "
+ + "}";
+ // Setup a topology.
+ final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
+
+ // Create the statements that will be input into the query.
+ final ValueFactory vf = new ValueFactoryImpl();
+ final List<VisibilityStatement> statements = getStatements();
+
+ // Make the expected results.
+ final Set<VisibilityBindingSet> expected = new HashSet<>();
+ final MapBindingSet bs = new MapBindingSet();
+ bs.addBinding("date", vf.createLiteral(TIME_10.toString()));
+ expected.add( new VisibilityBindingSet(bs, "a") );
+
+ // Run the test.
+ RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
+ }
+
private List<VisibilityStatement> getStatements() throws Exception {
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(statement(TIME), "a"));