Search in sources :

Example 31 with TemporalInstantRfc3339

use of org.apache.rya.indexing.TemporalInstantRfc3339 in project incubator-rya by apache.

the class GeoTemporalMongoDBStorageStrategy method getTemporalObjs.

private DBObject[] getTemporalObjs(final Collection<IndexingExpr> temporalFilters) {
    final List<DBObject> objs = new ArrayList<>();
    temporalFilters.forEach(filter -> {
        final TemporalPolicy policy = TemporalPolicy.fromURI(filter.getFunction());
        final String timeStr = ((Value) filter.getArguments()[0]).stringValue();
        final Matcher matcher = TemporalInstantRfc3339.PATTERN.matcher(timeStr);
        if (matcher.find()) {
            final TemporalInterval interval = TemporalInstantRfc3339.parseInterval(timeStr);
            if (policy == TemporalPolicy.INSTANT_AFTER_INSTANT || policy == TemporalPolicy.INSTANT_BEFORE_INSTANT || policy == TemporalPolicy.INSTANT_EQUALS_INSTANT) {
                if (interval == null) {
                    LOG.error("Cannot perform temporal interval based queries on an instant.");
                }
            }
            objs.add(getTemporalObject(interval, policy));
        } else {
            final TemporalInstant instant = new TemporalInstantRfc3339(DateTime.parse(timeStr));
            if (policy != TemporalPolicy.INSTANT_AFTER_INSTANT && policy != TemporalPolicy.INSTANT_BEFORE_INSTANT && policy != TemporalPolicy.INSTANT_EQUALS_INSTANT) {
                LOG.error("Cannot perform temporal instant based queries on an interval.");
            }
            objs.add(getTemporalObject(instant, policy));
        }
    });
    return objs.toArray(new DBObject[] {});
}
Also used : TemporalPolicy(org.apache.rya.indexing.geotemporal.GeoTemporalIndexer.TemporalPolicy) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Value(org.openrdf.model.Value) TemporalInstantRfc3339(org.apache.rya.indexing.TemporalInstantRfc3339) TemporalInstant(org.apache.rya.indexing.TemporalInstant) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) TemporalInterval(org.apache.rya.indexing.TemporalInterval)

Example 32 with TemporalInstantRfc3339

use of org.apache.rya.indexing.TemporalInstantRfc3339 in project incubator-rya by apache.

the class MongoGeoTemporalIndexer method deleteStatement.

@Override
public void deleteStatement(final RyaStatement statement) throws IOException {
    requireNonNull(statement);
    final RyaURI subject = statement.getSubject();
    try {
        final EventStorage eventStore = events.get();
        checkState(events != null, "Must set this indexers configuration before storing statements.");
        new EventUpdater(eventStore).update(subject, old -> {
            final Event.Builder updated;
            if (!old.isPresent()) {
                return Optional.empty();
            } else {
                updated = Event.builder(old.get());
            }
            final Event currentEvent = updated.build();
            final URI pred = statement.getObject().getDataType();
            if ((pred.equals(GeoConstants.GEO_AS_WKT) || pred.equals(GeoConstants.GEO_AS_GML) || pred.equals(GeoConstants.XMLSCHEMA_OGC_WKT) || pred.equals(GeoConstants.XMLSCHEMA_OGC_GML)) && currentEvent.getGeometry().isPresent()) {
                // is geo and needs to be removed.
                try {
                    if (currentEvent.getGeometry().get().equals(GeoParseUtils.getGeometry(RyaToRdfConversions.convertStatement(statement), new GmlParser()))) {
                        updated.setGeometry(null);
                    }
                } catch (final Exception e) {
                    LOG.debug("Unable to parse the stored geometry.");
                }
            } else {
                // is time
                final String dateTime = statement.getObject().getData();
                final Matcher matcher = TemporalInstantRfc3339.PATTERN.matcher(dateTime);
                if (matcher.find()) {
                    final TemporalInterval interval = TemporalInstantRfc3339.parseInterval(dateTime);
                    if (currentEvent.getInterval().get().equals(interval)) {
                        updated.setTemporalInterval(null);
                    }
                } else {
                    final TemporalInstant instant = new TemporalInstantRfc3339(DateTime.parse(dateTime));
                    if (currentEvent.getInstant().get().equals(instant)) {
                        updated.setTemporalInstant(null);
                    }
                }
            }
            return Optional.of(updated.build());
        });
    } catch (final IndexingException e) {
        throw new IOException("Failed to update the Entity index.", e);
    }
}
Also used : Matcher(java.util.regex.Matcher) TemporalInstantRfc3339(org.apache.rya.indexing.TemporalInstantRfc3339) GmlParser(org.apache.rya.indexing.mongodb.geo.GmlParser) IOException(java.io.IOException) TemporalInstant(org.apache.rya.indexing.TemporalInstant) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) IndexingException(org.apache.rya.indexing.mongodb.IndexingException) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) RyaURI(org.apache.rya.api.domain.RyaURI) IndexingException(org.apache.rya.indexing.mongodb.IndexingException) Event(org.apache.rya.indexing.geotemporal.model.Event) TemporalInterval(org.apache.rya.indexing.TemporalInterval) EventStorage(org.apache.rya.indexing.geotemporal.storage.EventStorage)

Example 33 with TemporalInstantRfc3339

use of org.apache.rya.indexing.TemporalInstantRfc3339 in project incubator-rya by apache.

the class TemporalInstantTest method zoneTestTest.

@Test
public void zoneTestTest() throws Exception {
    // arbitrary zone, BRST=Brazil, better if not local.
    final String ZONETestDateInBRST = "2014-12-31T23:59:59-02:00";
    final String ZONETestDateInZulu = "2015-01-01T01:59:59Z";
    final String ZONETestDateInET = "2014-12-31T20:59:59-05:00";
    TemporalInstant instant = new TemporalInstantRfc3339(DateTime.parse(ZONETestDateInBRST));
    Assert.assertEquals("Test our test Zulu, ET  strings.", ZONETestDateInET, DateTime.parse(ZONETestDateInZulu).withZone(DateTimeZone.forID("-05:00")).toString(ISODateTimeFormat.dateTimeNoMillis()));
    Assert.assertEquals("Test our test BRST,Zulu strings.", ZONETestDateInZulu, DateTime.parse(ZONETestDateInBRST).withZone(DateTimeZone.UTC).toString(ISODateTimeFormat.dateTimeNoMillis()));
    Assert.assertTrue("Key must be normalized to time zone Zulu: " + instant.getAsKeyString(), instant.getAsKeyString().endsWith("Z"));
    Assert.assertEquals("Key must be normalized from BRST -02:00", ZONETestDateInZulu, instant.getAsKeyString());
    Assert.assertArrayEquals(StringUtils.getBytesUtf8(instant.getAsKeyString()), instant.getAsKeyBytes());
    Assert.assertTrue("Ignore original time zone.", !ZONETestDateInBRST.equals(instant.getAsReadable(DateTimeZone.forID("-07:00"))));
    Assert.assertEquals("Use original time zone.", ZONETestDateInBRST, instant.getAsDateTime().toString(TemporalInstantRfc3339.FORMATTER));
    Assert.assertEquals("Time at specified time zone.", ZONETestDateInET, instant.getAsReadable(DateTimeZone.forID("-05:00")));
    instant = new TemporalInstantRfc3339(DateTime.parse(ZONETestDateInZulu));
    Assert.assertEquals("expect a time with specified time zone.", ZONETestDateInET, instant.getAsReadable(DateTimeZone.forID("-05:00")));
}
Also used : TemporalInstantRfc3339(org.apache.rya.indexing.TemporalInstantRfc3339) TemporalInstant(org.apache.rya.indexing.TemporalInstant) Test(org.junit.Test)

Example 34 with TemporalInstantRfc3339

use of org.apache.rya.indexing.TemporalInstantRfc3339 in project incubator-rya by apache.

the class TemporalInstantTest method constructorTest.

@Test
public void constructorTest() throws Exception {
    TemporalInstant instant = new // 
    TemporalInstantRfc3339(// 
    2014, // 
    12, // 
    30, 12, 59, 59);
    // YYYY-MM-DDThh:mm:ssZ
    String stringTestDate01 = "2014-12-30T12:59:59Z";
    Date dateTestDate01 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(stringTestDate01);
    Assert.assertEquals(stringTestDate01, instant.getAsKeyString());
    Assert.assertArrayEquals(StringUtils.getBytesUtf8(instant.getAsKeyString()), instant.getAsKeyBytes());
    Assert.assertTrue("Key must be normalized to time zone Zulu", instant.getAsKeyString().endsWith("Z"));
    // show the local time us.
    // Warning, if test is run in the London, or Zulu time zone, this test will be same as above, with the Z.
    // TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // this does not affect the library, don't use.
    String stringLocalTestDate01 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(dateTestDate01);
    // for ET, will be: "2014-12-30T07:59:59-05:00"
    // instant.getAsDateTime().withZone(null);
    // System.out.println("===System.getProperty(user.timezone)="+System.getProperty("user.timezone")); //=ET
    // System.out.println("===============TimeZone.getDefault()="+TimeZone.getDefault());				//=ET
    // System.out.println("===========DateTimeZone.getDefault()="+DateTimeZone.getDefault());			//=UTC (wrong!)
    // the timezone default gets set to UTC by some prior test, fix it here.
    DateTimeZone newTimeZone = null;
    try {
        String id = System.getProperty("user.timezone");
        if (id != null) {
            newTimeZone = DateTimeZone.forID(id);
        }
    } catch (RuntimeException ex) {
    // ignored
    }
    if (newTimeZone == null) {
        newTimeZone = DateTimeZone.forTimeZone(TimeZone.getDefault());
    }
    DateTimeZone.setDefault(newTimeZone);
    // null timezone means use the default:
    Assert.assertEquals("Joda time library (actual) should use same local timezone as Java date (expected).", stringLocalTestDate01, instant.getAsReadable(null));
}
Also used : TemporalInstantRfc3339(org.apache.rya.indexing.TemporalInstantRfc3339) TemporalInstant(org.apache.rya.indexing.TemporalInstant) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) DateTimeZone(org.joda.time.DateTimeZone) Test(org.junit.Test)

Aggregations

TemporalInstantRfc3339 (org.apache.rya.indexing.TemporalInstantRfc3339)34 Test (org.junit.Test)26 TemporalInstant (org.apache.rya.indexing.TemporalInstant)21 RyaURI (org.apache.rya.api.domain.RyaURI)15 Geometry (com.vividsolutions.jts.geom.Geometry)13 Coordinate (com.vividsolutions.jts.geom.Coordinate)12 TemporalInterval (org.apache.rya.indexing.TemporalInterval)12 EventStorage (org.apache.rya.indexing.geotemporal.storage.EventStorage)11 URI (org.openrdf.model.URI)11 Event (org.apache.rya.indexing.geotemporal.model.Event)9 Value (org.openrdf.model.Value)9 ValueFactory (org.openrdf.model.ValueFactory)9 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)9 EventQueryNode (org.apache.rya.indexing.geotemporal.model.EventQueryNode)7 DateTime (org.joda.time.DateTime)5 MongoEventStorage (org.apache.rya.indexing.geotemporal.mongo.MongoEventStorage)4 BindingSet (org.openrdf.query.BindingSet)4 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)4 MapBindingSet (org.openrdf.query.impl.MapBindingSet)4 IOException (java.io.IOException)3