Search in sources :

Example 1 with CollectionIteration

use of org.openrdf.query.algebra.evaluation.iterator.CollectionIteration in project blueprints by tinkerpop.

the class GraphSailConnection method getContextIDsInternal.

// note: iterates over all statements
public CloseableIteration<? extends Resource, SailException> getContextIDsInternal() throws SailException {
    Set<Resource> contexts = new HashSet<Resource>();
    CloseableIteration<? extends Statement, SailException> iter = getStatementsInternal(null, null, null, false);
    try {
        while (iter.hasNext()) {
            Resource context = iter.next().getContext();
            // match the behavior of Sesame stores, which do not include the null context
            if (null != context) {
                contexts.add(context);
            }
        }
    } finally {
        iter.close();
    }
    return new CollectionIteration<Resource, SailException>(contexts);
}
Also used : CollectionIteration(org.openrdf.query.algebra.evaluation.iterator.CollectionIteration) Resource(org.openrdf.model.Resource) SailException(org.openrdf.sail.SailException) HashSet(java.util.HashSet)

Example 2 with CollectionIteration

use of org.openrdf.query.algebra.evaluation.iterator.CollectionIteration in project incubator-rya by apache.

the class EventQueryNode method evaluate.

@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final BindingSet bindings) throws QueryEvaluationException {
    final List<BindingSet> list = new ArrayList<>();
    try {
        final Collection<Event> searchEvents;
        final String subj;
        // if the provided binding set has the subject already, set it to the constant subject.
        if (!subjectConstant.isPresent() && bindings.hasBinding(subjectVar.get())) {
            subjectConstant = Optional.of(bindings.getValue(subjectVar.get()).stringValue());
        } else if (bindings.size() != 0) {
            list.add(bindings);
        }
        // If the subject needs to be filled in, check if the subject variable is in the binding set.
        if (subjectConstant.isPresent()) {
            // if it is, fetch that value and then fetch the entity for the subject.
            subj = subjectConstant.get();
            searchEvents = eventStore.search(Optional.of(new RyaURI(subj)), Optional.of(geoFilters), Optional.of(temporalFilters));
        } else {
            searchEvents = eventStore.search(Optional.empty(), Optional.of(geoFilters), Optional.of(temporalFilters));
        }
        for (final Event event : searchEvents) {
            final MapBindingSet resultSet = new MapBindingSet();
            if (event.getGeometry().isPresent()) {
                final Geometry geo = event.getGeometry().get();
                final Value geoValue = ValueFactoryImpl.getInstance().createLiteral(geo.toText());
                final Var geoObj = geoPattern.getObjectVar();
                resultSet.addBinding(geoObj.getName(), geoValue);
            }
            final Value temporalValue;
            if (event.isInstant() && event.getInstant().isPresent()) {
                final Optional<TemporalInstant> opt = event.getInstant();
                DateTime dt = opt.get().getAsDateTime();
                dt = dt.toDateTime(DateTimeZone.UTC);
                final String str = dt.toString(TemporalInstantRfc3339.FORMATTER);
                temporalValue = ValueFactoryImpl.getInstance().createLiteral(str);
            } else if (event.getInterval().isPresent()) {
                temporalValue = ValueFactoryImpl.getInstance().createLiteral(event.getInterval().get().getAsPair());
            } else {
                temporalValue = null;
            }
            if (temporalValue != null) {
                final Var temporalObj = temporalPattern.getObjectVar();
                resultSet.addBinding(temporalObj.getName(), temporalValue);
            }
            list.add(resultSet);
        }
    } catch (final ObjectStorageException e) {
        throw new QueryEvaluationException("Failed to evaluate the binding set", e);
    }
    return new CollectionIteration<>(list);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) ArrayList(java.util.ArrayList) ObjectStorageException(org.apache.rya.indexing.mongodb.update.RyaObjectStorage.ObjectStorageException) TemporalInstant(org.apache.rya.indexing.TemporalInstant) DateTime(org.joda.time.DateTime) Geometry(com.vividsolutions.jts.geom.Geometry) RyaURI(org.apache.rya.api.domain.RyaURI) CollectionIteration(org.openrdf.query.algebra.evaluation.iterator.CollectionIteration) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) MapBindingSet(org.openrdf.query.impl.MapBindingSet)

Aggregations

CollectionIteration (org.openrdf.query.algebra.evaluation.iterator.CollectionIteration)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 RyaURI (org.apache.rya.api.domain.RyaURI)1 TemporalInstant (org.apache.rya.indexing.TemporalInstant)1 ObjectStorageException (org.apache.rya.indexing.mongodb.update.RyaObjectStorage.ObjectStorageException)1 DateTime (org.joda.time.DateTime)1 Resource (org.openrdf.model.Resource)1 Value (org.openrdf.model.Value)1 BindingSet (org.openrdf.query.BindingSet)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 Var (org.openrdf.query.algebra.Var)1 MapBindingSet (org.openrdf.query.impl.MapBindingSet)1 SailException (org.openrdf.sail.SailException)1