Search in sources :

Example 41 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class ThreshholdPlanSelectorTest method twoIndexFilterTest.

@Test
public void twoIndexFilterTest() {
    String q1 = // 
    "" + // 
    "SELECT ?f ?m ?d ?e ?l ?c " + // 
    "{" + // 
    "  Filter(?f > \"5\")." + // 
    "  Filter(?e > \"5\")." + // 
    "  ?f a ?m ." + // 
    "  ?e a ?l ." + // 
    "  ?d <uri:talksTo> ?f . " + // 
    "  ?c <uri:talksTo> ?e . " + // 
    "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + // 
    "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ." + // 
    "}";
    String q2 = // 
    "" + // 
    "SELECT ?s ?t ?u " + // 
    "{" + // 
    "  ?s a ?t ." + // 
    "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + // 
    "  ?u <uri:talksTo> ?s . " + // 
    "}";
    String q3 = // 
    "" + // 
    "SELECT ?s ?t ?u " + // 
    "{" + // 
    " Filter(?s > \"5\") ." + // 
    "  ?s a ?t ." + // 
    "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + // 
    "  ?u <uri:talksTo> ?s . " + // 
    "}";
    String q4 = // 
    "" + // 
    "SELECT ?f ?m ?d " + // 
    "{" + // 
    " Filter(?f > \"5\") ." + // 
    "  ?f a ?m ." + // 
    "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + // 
    "  ?d <uri:talksTo> ?f . " + // 
    "}";
    String q5 = // 
    "" + // 
    "SELECT ?e ?l ?c " + // 
    "{" + // 
    " Filter(?e > \"5\") ." + // 
    "  ?e a ?l ." + // 
    "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ." + // 
    "  ?c <uri:talksTo> ?e . " + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = null;
    ParsedQuery pq2 = null;
    ParsedQuery pq3 = null;
    ParsedQuery pq4 = null;
    ParsedQuery pq5 = null;
    try {
        pq1 = parser.parseQuery(q1, null);
        pq2 = parser.parseQuery(q2, null);
        pq3 = parser.parseQuery(q3, null);
        pq4 = parser.parseQuery(q4, null);
        pq5 = parser.parseQuery(q5, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
    SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
    SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
    SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
    List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup2);
    list.add(extTup1);
    List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
    list2.add(extTup3);
    list2.add(extTup4);
    IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
    Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator().getPlans(iep.getIndexedTuples());
    IndexPlanValidator ipv = new IndexPlanValidator(false);
    Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
    ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
    TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .8, .1, .1);
    NodeCollector nc = new NodeCollector();
    optimalTup.visit(nc);
    Assert.assertEquals(nc.getNodes().size(), list2.size());
    for (QueryModelNode e : nc.getNodes()) {
        Assert.assertTrue(list2.contains(e));
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) Test(org.junit.Test)

Example 42 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class GeneralizedExternalProcessor method getQNodes.

private static Set<QueryModelNode> getQNodes(QueryModelNode queryNode) {
    Set<QueryModelNode> rtns = new HashSet<QueryModelNode>();
    StatementPatternCollector spc = new StatementPatternCollector();
    queryNode.visit(spc);
    rtns.addAll(spc.getStatementPatterns());
    FilterCollector fvis = new FilterCollector();
    queryNode.visit(fvis);
    rtns.addAll(fvis.getFilters());
    ExternalTupleCollector eVis = new ExternalTupleCollector();
    queryNode.visit(eVis);
    rtns.addAll(eVis.getExtTup());
    return rtns;
}
Also used : StatementPatternCollector(org.openrdf.query.algebra.helpers.StatementPatternCollector) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) HashSet(java.util.HashSet)

Example 43 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class ValidIndexCombinationGenerator method indicesDisjoint.

private int indicesDisjoint(List<Integer> combo, List<ExternalTupleSet> indexList) {
    Set<QueryModelNode> indexNodes = Sets.newHashSet();
    Set<QueryModelNode> tempNodes;
    TupleExpr temp;
    int j = 0;
    for (Integer i : combo) {
        temp = indexList.get(i).getTupleExpr();
        SpFilterCollector spf = new SpFilterCollector();
        temp.visit(spf);
        tempNodes = spf.getSpFilterSet();
        if (Sets.intersection(indexNodes, tempNodes).size() == 0) {
            indexNodes = Sets.union(indexNodes, tempNodes);
            if (indexNodes.size() > spFilterSet.size()) {
                return j;
            }
        } else {
            return j;
        }
        j++;
    }
    return -1;
}
Also used : QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 44 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class IteratorFactory method getIterator.

public static CloseableIteration<BindingSet, QueryEvaluationException> getIterator(final StatementPattern match, final BindingSet bindings, final String queryText, final SearchFunction searchFunction) {
    return new CloseableIteration<BindingSet, QueryEvaluationException>() {

        private boolean isClosed = false;

        private CloseableIteration<Statement, QueryEvaluationException> statementIt = null;

        private String subjectBinding = match.getSubjectVar().getName();

        private String predicateBinding = match.getPredicateVar().getName();

        private String objectBinding = match.getObjectVar().getName();

        private String contextBinding = null;

        private void performQuery() throws QueryEvaluationException {
            StatementConstraints contraints = new StatementConstraints();
            // get the context (i.e. named graph) of the statement and use that in the query
            QueryModelNode parentNode = match.getSubjectVar().getParentNode();
            if (parentNode instanceof StatementPattern) {
                StatementPattern parentStatement = (StatementPattern) parentNode;
                Var contextVar = parentStatement.getContextVar();
                if (contextVar != null) {
                    contextBinding = contextVar.getName();
                    Resource context = (Resource) contextVar.getValue();
                    contraints.setContext(context);
                }
            }
            // get the subject constraint
            if (match.getSubjectVar().isConstant()) {
                // get the subject binding from the filter/statement pair
                Resource subject = (Resource) match.getSubjectVar().getValue();
                contraints.setSubject(subject);
            } else if (bindings.hasBinding(subjectBinding)) {
                // get the subject binding from the passed in bindings (eg from other statements/parts of the tree)
                Resource subject = (Resource) bindings.getValue(subjectBinding);
                contraints.setSubject(subject);
            }
            // get the predicate constraint
            if (match.getPredicateVar().isConstant()) {
                // get the predicate binding from the filter/statement pair
                Set<URI> predicates = new HashSet<URI>(getPredicateRestrictions(match.getPredicateVar()));
                contraints.setPredicates(predicates);
            } else if (bindings.hasBinding(predicateBinding)) {
                // get the predicate binding from the passed in bindings (eg from other statements/parts of the tree)
                URI predicateUri = (URI) bindings.getValue(predicateBinding);
                Set<URI> predicates = Collections.singleton(predicateUri);
                contraints.setPredicates(predicates);
            }
            statementIt = searchFunction.performSearch(queryText, contraints);
        }

        @Override
        public boolean hasNext() throws QueryEvaluationException {
            if (statementIt == null) {
                performQuery();
            }
            return statementIt.hasNext();
        }

        @Override
        public BindingSet next() throws QueryEvaluationException {
            if (!hasNext() || isClosed) {
                throw new NoSuchElementException();
            }
            Statement statment = statementIt.next();
            MapBindingSet bset = new MapBindingSet();
            if (!subjectBinding.startsWith("-const"))
                bset.addBinding(subjectBinding, statment.getSubject());
            if (!predicateBinding.startsWith("-const"))
                bset.addBinding(predicateBinding, statment.getPredicate());
            if (!objectBinding.startsWith("-const"))
                bset.addBinding(objectBinding, statment.getObject());
            if (contextBinding != null && !contextBinding.startsWith("-const"))
                bset.addBinding(contextBinding, statment.getContext());
            // merge with other bindings.
            for (String name : bindings.getBindingNames()) {
                bset.addBinding(name, bindings.getValue(name));
            }
            return bset;
        }

        @Override
        public void remove() throws QueryEvaluationException {
            throw new UnsupportedOperationException();
        }

        @Override
        public void close() throws QueryEvaluationException {
            if (statementIt != null) {
                statementIt.close();
            }
            isClosed = true;
        }
    };
}
Also used : Set(java.util.Set) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) URI(org.openrdf.model.URI) CloseableIteration(info.aduna.iteration.CloseableIteration) StatementPattern(org.openrdf.query.algebra.StatementPattern) MapBindingSet(org.openrdf.query.impl.MapBindingSet) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Example 45 with QueryModelNode

use of org.openrdf.query.algebra.QueryModelNode in project incubator-rya by apache.

the class QueryNodeConsolidator method reOrderExtSetNodes.

// assumes nodes are consolidated -- checks if they can be reordered to
// match ExternalSet node list
private boolean reOrderExtSetNodes() {
    int pos = extSetPosSet.last().getPosition();
    for (int j = extSetNodes.size() - 1; j >= 0; j--) {
        QueryModelNode node = extSetNodes.get(j);
        int i = queryNodes.indexOf(node);
        // use ExternalSet node in queryNodes so FlattenedOptional boundVars
        // are consistent with query
        node = queryNodes.get(i);
        if (!moveQueryNode(new PositionNode(node, i), pos)) {
            return false;
        }
        pos--;
    }
    return true;
}
Also used : QueryModelNode(org.openrdf.query.algebra.QueryModelNode)

Aggregations

QueryModelNode (org.openrdf.query.algebra.QueryModelNode)98 TupleExpr (org.openrdf.query.algebra.TupleExpr)74 Test (org.junit.Test)68 ArrayList (java.util.ArrayList)63 ParsedQuery (org.openrdf.query.parser.ParsedQuery)63 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)62 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)56 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)48 StatementPattern (org.openrdf.query.algebra.StatementPattern)33 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)27 HashSet (java.util.HashSet)26 Projection (org.openrdf.query.algebra.Projection)23 Filter (org.openrdf.query.algebra.Filter)15 LeftJoin (org.openrdf.query.algebra.LeftJoin)12 Join (org.openrdf.query.algebra.Join)11 ValueExpr (org.openrdf.query.algebra.ValueExpr)11 QueryNodeConsolidator (org.apache.rya.indexing.external.matching.QueryNodeConsolidator)8 Configuration (org.apache.hadoop.conf.Configuration)7 AccumuloIndexSetProvider (org.apache.rya.indexing.pcj.matching.provider.AccumuloIndexSetProvider)7 BatchWriter (org.apache.accumulo.core.client.BatchWriter)6