Search in sources :

Example 1 with DateTimeWithinPeriod

use of org.apache.rya.api.functions.DateTimeWithinPeriod in project incubator-rya by apache.

the class QueryIT method dateTimeWithinNow.

@Test
public void dateTimeWithinNow() throws Exception {
    final ValueFactory vf = new ValueFactoryImpl();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    FunctionRegistry.getInstance().add(new DateTimeWithinPeriod());
    final String sparql = "PREFIX fn: <" + FN.NAMESPACE + ">" + "SELECT ?event ?startTime WHERE { ?event <uri:startTime> ?startTime. " + "FILTER(fn:dateTimeWithin(?startTime, NOW(), 30, <" + OWLTime.SECONDS_URI + "> ))}";
    final ZonedDateTime zTime = ZonedDateTime.now();
    final String time = zTime.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime1 = zTime.minusSeconds(30);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
    final Literal lit = vf.createLiteral(dtf.newXMLGregorianCalendar(time));
    final Literal lit1 = vf.createLiteral(dtf.newXMLGregorianCalendar(time1));
    // Create the Statements that will be loaded into Rya.
    final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("uri:event1"), vf.createURI("uri:startTime"), lit), vf.createStatement(vf.createURI("uri:event2"), vf.createURI("uri:startTime"), lit1));
    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("event", vf.createURI("uri:event1"));
    bs.addBinding("startTime", lit);
    expectedResults.add(bs);
    // Verify the end results of the query match the expected results.
    runTest(sparql, statements, expectedResults, ExportStrategy.RYA);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) DatatypeFactory(javax.xml.datatype.DatatypeFactory) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) ZonedDateTime(java.time.ZonedDateTime) Literal(org.openrdf.model.Literal) MapBindingSet(org.openrdf.query.impl.MapBindingSet) DateTimeWithinPeriod(org.apache.rya.api.functions.DateTimeWithinPeriod) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with DateTimeWithinPeriod

use of org.apache.rya.api.functions.DateTimeWithinPeriod in project incubator-rya by apache.

the class QueryIT method dateTimeWithin.

@Test
public void dateTimeWithin() throws Exception {
    final ValueFactory vf = new ValueFactoryImpl();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    FunctionRegistry.getInstance().add(new DateTimeWithinPeriod());
    final String sparql = "PREFIX fn: <" + FN.NAMESPACE + ">" + "SELECT ?event ?startTime ?endTime WHERE { ?event <uri:startTime> ?startTime; <uri:endTime> ?endTime. " + "FILTER(fn:dateTimeWithin(?startTime, ?endTime, 2,<" + OWLTime.HOURS_URI + "> ))}";
    final ZonedDateTime zTime = ZonedDateTime.now();
    final String time = zTime.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime1 = zTime.minusHours(1);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime2 = zTime.minusHours(2);
    final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);
    final Literal lit = vf.createLiteral(dtf.newXMLGregorianCalendar(time));
    final Literal lit1 = vf.createLiteral(dtf.newXMLGregorianCalendar(time1));
    final Literal lit2 = vf.createLiteral(dtf.newXMLGregorianCalendar(time2));
    // Create the Statements that will be loaded into Rya.
    final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("uri:event1"), vf.createURI("uri:startTime"), lit), vf.createStatement(vf.createURI("uri:event1"), vf.createURI("uri:endTime"), lit1), vf.createStatement(vf.createURI("uri:event2"), vf.createURI("uri:startTime"), lit), vf.createStatement(vf.createURI("uri:event2"), vf.createURI("uri:endTime"), lit2));
    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("event", vf.createURI("uri:event1"));
    bs.addBinding("startTime", lit);
    bs.addBinding("endTime", lit1);
    expectedResults.add(bs);
    // Verify the end results of the query match the expected results.
    runTest(sparql, statements, expectedResults, ExportStrategy.RYA);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) DatatypeFactory(javax.xml.datatype.DatatypeFactory) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) ZonedDateTime(java.time.ZonedDateTime) Literal(org.openrdf.model.Literal) MapBindingSet(org.openrdf.query.impl.MapBindingSet) DateTimeWithinPeriod(org.apache.rya.api.functions.DateTimeWithinPeriod) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ZonedDateTime (java.time.ZonedDateTime)2 HashSet (java.util.HashSet)2 DatatypeFactory (javax.xml.datatype.DatatypeFactory)2 DateTimeWithinPeriod (org.apache.rya.api.functions.DateTimeWithinPeriod)2 Test (org.junit.Test)2 Literal (org.openrdf.model.Literal)2 Statement (org.openrdf.model.Statement)2 ValueFactory (org.openrdf.model.ValueFactory)2 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)2 BindingSet (org.openrdf.query.BindingSet)2 MapBindingSet (org.openrdf.query.impl.MapBindingSet)2