Search in sources :

Example 1 with EmptyIteration

use of info.aduna.iteration.EmptyIteration in project incubator-rya by apache.

the class ParallelEvaluationStrategyImpl method evaluate.

public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final StatementPattern sp, Collection<BindingSet> bindings) throws QueryEvaluationException {
    final Var subjVar = sp.getSubjectVar();
    final Var predVar = sp.getPredicateVar();
    final Var objVar = sp.getObjectVar();
    final Var cntxtVar = sp.getContextVar();
    List<Map.Entry<Statement, BindingSet>> stmts = new ArrayList<Map.Entry<Statement, BindingSet>>();
    Iteration<? extends Map.Entry<Statement, BindingSet>, QueryEvaluationException> iter;
    if (sp instanceof FixedStatementPattern) {
        Collection<Map.Entry<Statement, BindingSet>> coll = Lists.newArrayList();
        for (BindingSet binding : bindings) {
            Value subjValue = getVarValue(subjVar, binding);
            Value predValue = getVarValue(predVar, binding);
            Value objValue = getVarValue(objVar, binding);
            Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
            for (Statement st : ((FixedStatementPattern) sp).statements) {
                if (!((subjValue != null && !subjValue.equals(st.getSubject())) || (predValue != null && !predValue.equals(st.getPredicate())) || (objValue != null && !objValue.equals(st.getObject())))) {
                    coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
                }
            }
        }
        iter = new IteratorIteration(coll.iterator());
    } else if (sp instanceof TransitivePropertySP && ((subjVar != null && subjVar.getValue() != null) || (objVar != null && objVar.getValue() != null)) && sp.getPredicateVar() != null) {
        // if this is a transitive prop ref, we need to make sure that either the subj or obj is not null
        // TODO: Cannot handle a open ended transitive property where subj and obj are null
        // TODO: Should one day handle filling in the subj or obj with bindings and working this
        // TODO: a lot of assumptions, and might be a large set returned causing an OME
        Set<Statement> sts = null;
        try {
            sts = inferenceEngine.findTransitiveProperty((Resource) getVarValue(subjVar), (URI) getVarValue(predVar), getVarValue(objVar), (Resource) getVarValue(cntxtVar));
        } catch (InferenceEngineException e) {
            throw new QueryEvaluationException(e);
        }
        Collection<Map.Entry<Statement, BindingSet>> coll = new ArrayList();
        for (BindingSet binding : bindings) {
            for (Statement st : sts) {
                coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
            }
        }
        iter = new IteratorIteration(coll.iterator());
    } else {
        for (BindingSet binding : bindings) {
            Value subjValue = getVarValue(subjVar, binding);
            Value predValue = getVarValue(predVar, binding);
            Value objValue = getVarValue(objVar, binding);
            Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
            if ((subjValue != null && !(subjValue instanceof Resource)) || (predValue != null && !(predValue instanceof URI))) {
                continue;
            }
            stmts.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(new NullableStatementImpl((Resource) subjValue, (URI) predValue, objValue, contxtValue), binding));
        }
        if (stmts.size() == 0) {
            return new EmptyIteration();
        }
        iter = ((RdfCloudTripleStoreConnection.StoreTripleSource) tripleSource).getStatements(stmts);
    }
    return new ConvertingIteration<Map.Entry<Statement, BindingSet>, BindingSet, QueryEvaluationException>(iter) {

        @Override
        protected BindingSet convert(Map.Entry<Statement, BindingSet> stbs) throws QueryEvaluationException {
            Statement st = stbs.getKey();
            BindingSet bs = stbs.getValue();
            QueryBindingSet result = new QueryBindingSet(bs);
            // contain a Value for that Var name
            if (subjVar != null && !subjVar.isConstant() && !result.hasBinding(subjVar.getName())) {
                result.addBinding(subjVar.getName(), st.getSubject());
            }
            if (predVar != null && !predVar.isConstant() && !result.hasBinding(predVar.getName())) {
                result.addBinding(predVar.getName(), st.getPredicate());
            }
            if (objVar != null && !objVar.isConstant() && !result.hasBinding(objVar.getName())) {
                result.addBinding(objVar.getName(), st.getObject());
            }
            if (cntxtVar != null && !cntxtVar.isConstant() && !result.hasBinding(cntxtVar.getName()) && st.getContext() != null) {
                result.addBinding(cntxtVar.getName(), st.getContext());
            }
            return result;
        }
    };
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Set(java.util.Set) StoreTripleSource(org.apache.rya.rdftriplestore.RdfCloudTripleStoreConnection.StoreTripleSource) ConvertingIteration(info.aduna.iteration.ConvertingIteration) Var(org.openrdf.query.algebra.Var) ArrayList(java.util.ArrayList) URI(org.openrdf.model.URI) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Statement(org.openrdf.model.Statement) EmptyIteration(info.aduna.iteration.EmptyIteration) Resource(org.openrdf.model.Resource) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) TransitivePropertySP(org.apache.rya.rdftriplestore.utils.TransitivePropertySP) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RdfCloudTripleStoreUtils(org.apache.rya.api.RdfCloudTripleStoreUtils) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) IteratorIteration(info.aduna.iteration.IteratorIteration) Collection(java.util.Collection) Map(java.util.Map)

Example 2 with EmptyIteration

use of info.aduna.iteration.EmptyIteration in project incubator-rya by apache.

the class StatementMetadataNode method evaluate.

/**
 * This method pairs each {@link BindingSet} in the specified collection
 * with the StatementPattern constraints and issues a query to Rya using the
 * {@link RyaQueryEngine}.
 */
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Collection<BindingSet> bindingset) throws QueryEvaluationException {
    if (bindingset.size() == 0) {
        return new EmptyIteration<>();
    }
    queryEngine = RyaQueryEngineFactory.getQueryEngine(conf);
    Set<Map.Entry<RyaStatement, BindingSet>> statements = new HashSet<>();
    Iterator<BindingSet> iter = bindingset.iterator();
    while (iter.hasNext()) {
        BindingSet bs = iter.next();
        statements.add(new RdfCloudTripleStoreUtils.CustomEntry<RyaStatement, BindingSet>(getRyaStatementFromBindings(bs), bs));
    }
    final CloseableIteration<? extends Entry<RyaStatement, BindingSet>, RyaDAOException> iteration;
    try {
        iteration = queryEngine.queryWithBindingSet(statements, conf);
    } catch (RyaDAOException e) {
        throw new RuntimeException(e);
    }
    return new PropertyFilterAndBindingSetJoinIteration(iteration, properties, statement);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) EmptyIteration(info.aduna.iteration.EmptyIteration) RyaStatement(org.apache.rya.api.domain.RyaStatement) RdfCloudTripleStoreUtils(org.apache.rya.api.RdfCloudTripleStoreUtils) Entry(java.util.Map.Entry) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) HashSet(java.util.HashSet)

Aggregations

EmptyIteration (info.aduna.iteration.EmptyIteration)2 RdfCloudTripleStoreUtils (org.apache.rya.api.RdfCloudTripleStoreUtils)2 BindingSet (org.openrdf.query.BindingSet)2 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)2 ConvertingIteration (info.aduna.iteration.ConvertingIteration)1 IteratorIteration (info.aduna.iteration.IteratorIteration)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 RyaStatement (org.apache.rya.api.domain.RyaStatement)1 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)1 NullableStatementImpl (org.apache.rya.api.utils.NullableStatementImpl)1 StoreTripleSource (org.apache.rya.rdftriplestore.RdfCloudTripleStoreConnection.StoreTripleSource)1 InferenceEngineException (org.apache.rya.rdftriplestore.inference.InferenceEngineException)1 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)1 TransitivePropertySP (org.apache.rya.rdftriplestore.utils.TransitivePropertySP)1 Resource (org.openrdf.model.Resource)1