Search in sources :

Example 21 with TemporalInterval

use of org.apache.rya.indexing.TemporalInterval 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 22 with TemporalInterval

use of org.apache.rya.indexing.TemporalInterval 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 23 with TemporalInterval

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

the class MongoTemporalIndexerIT method testQueryInstantHasBeginningInterval.

/**
 * Test instant is the Beginning of the given interval.
 * from the series: Instance {hasBeginning, hasEnd} Interval
 */
@Test
public void testQueryInstantHasBeginningInterval() throws IOException, QueryEvaluationException {
    try (MongoTemporalIndexer tIndexer = new MongoTemporalIndexer()) {
        tIndexer.setConf(conf);
        tIndexer.init();
        // tiB02_E30 read as: Begins 2 seconds, ends at 30 seconds
        // these should not match as they are not instances.
        tIndexer.storeStatement(convertStatement(spo_B03_E20));
        tIndexer.storeStatement(convertStatement(spo_B02_E30));
        tIndexer.storeStatement(convertStatement(spo_B02_E40));
        tIndexer.storeStatement(convertStatement(spo_B02_E31));
        tIndexer.storeStatement(convertStatement(spo_B30_E32));
        // seriesSpo[s] and seriesTs[s] are statements and instants for s seconds after the uniform time.
        // from 2 to 31 seconds
        final TemporalInterval searchInsideInterval = tvB02_E31;
        // <== logic here, and next few lines.
        final int searchSeconds = 2;
        // 2 seconds.
        final int expectedResultCount = 1;
        for (int s = 0; s <= 10; s++) {
            tIndexer.storeStatement(convertStatement(seriesSpo[s]));
        }
        CloseableIteration<Statement, QueryEvaluationException> iter;
        iter = tIndexer.queryInstantHasBeginningInterval(searchInsideInterval, EMPTY_CONSTRAINTS);
        int count = 0;
        while (iter.hasNext()) {
            final Statement s = iter.next();
            // <== logic here
            final Statement nextExpectedStatement = seriesSpo[searchSeconds];
            assertTrue("Should match: " + nextExpectedStatement + " == " + s, nextExpectedStatement.equals(s));
            count++;
        }
        assertEquals("Should find count of rows.", expectedResultCount, count);
    }
}
Also used : MongoTemporalIndexer(org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Statement(org.openrdf.model.Statement) RdfToRyaConversions.convertStatement(org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement) TemporalInterval(org.apache.rya.indexing.TemporalInterval) Test(org.junit.Test)

Example 24 with TemporalInterval

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

the class MongoTemporalIndexerIT method testQueryInstantHasEndInterval.

/**
 * Test instant is the end of the given interval.
 * from the series: Instance {hasBeginning, hasEnd} Interval
 */
@Test
public void testQueryInstantHasEndInterval() throws IOException, QueryEvaluationException {
    try (MongoTemporalIndexer tIndexer = new MongoTemporalIndexer()) {
        tIndexer.setConf(conf);
        tIndexer.init();
        // tiB02_E30 read as: Begins 2 seconds, ends at 30 seconds
        // these should not match as they are not instances.
        tIndexer.storeStatement(convertStatement(spo_B03_E20));
        tIndexer.storeStatement(convertStatement(spo_B02_E30));
        tIndexer.storeStatement(convertStatement(spo_B02_E40));
        tIndexer.storeStatement(convertStatement(spo_B02_E31));
        tIndexer.storeStatement(convertStatement(spo_B30_E32));
        // seriesSpo[s] and seriesTs[s] are statements and instants for s seconds after the uniform time.
        // from 2 to 31 seconds
        final TemporalInterval searchInsideInterval = tvB02_E31;
        // <== logic here, and next few lines.
        final int searchSeconds = 31;
        // 31 seconds.
        final int expectedResultCount = 1;
        for (int s = 0; s <= 40; s++) {
            tIndexer.storeStatement(convertStatement(seriesSpo[s]));
        }
        CloseableIteration<Statement, QueryEvaluationException> iter;
        iter = tIndexer.queryInstantHasEndInterval(searchInsideInterval, EMPTY_CONSTRAINTS);
        int count = 0;
        while (iter.hasNext()) {
            final Statement s = iter.next();
            // <== logic here
            final Statement nextExpectedStatement = seriesSpo[searchSeconds];
            assertTrue("Should match: " + nextExpectedStatement + " == " + s, nextExpectedStatement.equals(s));
            count++;
        }
        assertEquals("Should find count of rows.", expectedResultCount, count);
    }
}
Also used : MongoTemporalIndexer(org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Statement(org.openrdf.model.Statement) RdfToRyaConversions.convertStatement(org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement) TemporalInterval(org.apache.rya.indexing.TemporalInterval) Test(org.junit.Test)

Aggregations

TemporalInterval (org.apache.rya.indexing.TemporalInterval)24 Test (org.junit.Test)16 TemporalInstantRfc3339 (org.apache.rya.indexing.TemporalInstantRfc3339)12 Statement (org.openrdf.model.Statement)11 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)10 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)10 TemporalInstant (org.apache.rya.indexing.TemporalInstant)7 RyaStatement (org.apache.rya.api.domain.RyaStatement)6 MongoTemporalIndexer (org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer)5 DateTime (org.joda.time.DateTime)5 Matcher (java.util.regex.Matcher)4 IOException (java.io.IOException)3 RyaURI (org.apache.rya.api.domain.RyaURI)3 Event (org.apache.rya.indexing.geotemporal.model.Event)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 ParseException (com.vividsolutions.jts.io.ParseException)2 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)2 EventStorage (org.apache.rya.indexing.geotemporal.storage.EventStorage)2 GmlParser (org.apache.rya.indexing.mongodb.geo.GmlParser)2 Literal (org.openrdf.model.Literal)2