Search in sources :

Example 81 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class VarConstQueryVariableNormalizerTest method queryConstFreeTextFilter.

@Test
public void queryConstFreeTextFilter() throws Exception {
    SPARQLParser parser1 = new SPARQLParser();
    SPARQLParser parser2 = new SPARQLParser();
    ParsedQuery pq1 = parser1.parseQuery(q33, null);
    ParsedQuery pq2 = parser2.parseQuery(q35, null);
    System.out.println(pq1.getTupleExpr());
    List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr());
    Assert.assertEquals(2, normalize.size());
    for (TupleExpr te : normalize) {
        Assert.assertTrue(isTupleSubset(pq1.getTupleExpr(), te));
    }
    FilterCollector fc1 = new FilterCollector();
    pq1.getTupleExpr().visit(fc1);
    List<QueryModelNode> fList1 = fc1.getFilters();
    for (TupleExpr te : normalize) {
        FilterCollector fc2 = new FilterCollector();
        te.visit(fc2);
        List<QueryModelNode> fList2 = fc2.getFilters();
        for (QueryModelNode q : fList2) {
            Assert.assertTrue(fList1.contains(q));
        }
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) Test(org.junit.Test)

Example 82 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class VarConstQueryVariableNormalizerTest method queryCompoundFilterMatch.

@Test
public void queryCompoundFilterMatch() throws Exception {
    String q17 = // 
    "" + // 
    "SELECT ?j ?k ?l ?m ?n ?o " + // 
    "{" + // 
    " GRAPH ?z { " + // 
    "  ?j <uri:talksTo> ?k . " + // 
    "  FILTER ( ?k < ?l && (?m > ?n || ?o = ?j) ). " + // 
    "     }" + // 
    "}";
    // String q18 = ""//
    // + "SELECT ?r ?s ?t ?u " //
    // + "{" //
    // + " GRAPH ?q { " //
    // + "  FILTER(bound(?r) && sameTerm(?s,?t)&&bound(?u)). " //
    // + "  ?t a ?u ."//
    // + "     }"//
    // + "}";//
    String q19 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d ?f ?q ?g ?h " + // 
    "{" + // 
    " GRAPH ?x { " + // 
    "  ?a a ?b ." + // 
    "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ." + // 
    "  ?d <uri:talksTo> \"5\" . " + // 
    "  FILTER ( \"5\" < ?f && (?a > ?b || ?c = ?d) ). " + // 
    "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " + // 
    "  FILTER(?g IN (1,2,3) && ?h NOT IN(5,6,7)). " + // 
    "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?g. " + // 
    "  ?b a ?q ." + // 
    "     }" + // 
    "}";
    // String q20 = ""//
    // + "SELECT ?m ?n ?o " //
    // + "{" //
    // + " GRAPH ?q { " //
    // + "  FILTER(?m IN (1,?o,3) && ?n NOT IN(5,6,7)). " //
    // + "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
    // + "     }"//
    // + "}";//
    SPARQLParser parser1 = new SPARQLParser();
    SPARQLParser parser2 = new SPARQLParser();
    ParsedQuery pq1 = parser1.parseQuery(q19, null);
    ParsedQuery pq2 = parser2.parseQuery(q17, null);
    List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr());
    System.out.println(normalize);
    Assert.assertEquals(1, normalize.size());
    for (TupleExpr te : normalize) {
        Assert.assertTrue(isTupleSubset(pq1.getTupleExpr(), te));
    }
    FilterCollector fc1 = new FilterCollector();
    pq1.getTupleExpr().visit(fc1);
    List<QueryModelNode> fList1 = fc1.getFilters();
    for (TupleExpr te : normalize) {
        FilterCollector fc2 = new FilterCollector();
        te.visit(fc2);
        List<QueryModelNode> fList2 = fc2.getFilters();
        for (QueryModelNode q : fList2) {
            Assert.assertTrue(fList1.contains(q));
        }
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) Test(org.junit.Test)

Example 83 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class VarConstQueryVariableNormalizerTest method queryConstNodeTwoMatch.

@Test
public void queryConstNodeTwoMatch() throws Exception {
    SPARQLParser parser1 = new SPARQLParser();
    SPARQLParser parser2 = new SPARQLParser();
    ParsedQuery pq1 = parser1.parseQuery(q7, null);
    ParsedQuery pq2 = parser2.parseQuery(q4, null);
    List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr());
    Assert.assertEquals(2, normalize.size());
    for (TupleExpr te : normalize) {
        Assert.assertTrue(isTupleSubset(pq1.getTupleExpr(), te));
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) TupleExpr(org.openrdf.query.algebra.TupleExpr) Test(org.junit.Test)

Example 84 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class OptionalJoinSegmentPCJMatcherTest method testWithUnmatchedNodes.

@Test
public void testWithUnmatchedNodes() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d ?e" + // 
    "{" + // 
    " Filter(?a = <uri:s1>)" + // 
    " Filter(?b = <uri:s2>)" + " {?e <uri:p3> <uri:o2> } UNION {?d <uri:p4> <uri:o3>} ." + " ?a <uri:workAt> <uri:Company1> ." + // 
    "  ?b <uri:talksTo> ?c  . " + // 
    " OPTIONAL { ?b <uri:p1> ?c } . " + // 
    "  ?b <uri:p2> <uri:o1> . " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    "  ?b <uri:talksTo> ?c  . " + // 
    " OPTIONAL { ?b <uri:p1> ?c } . " + // 
    "  ?b <uri:p2> <uri:o1> . " + // 
    " Filter(?b = <uri:s2>)" + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    Projection proj = (Projection) te1;
    Join join = (Join) proj.getArg();
    ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(join));
    SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    Assert.assertEquals(true, jsm.match(pcj));
    TupleExpr te = jsm.getQuery();
    Set<QueryModelNode> qNodes = LeftJoinQueryNodeGatherer.getNodes(te);
    List<QueryModelNode> nodes = jsm.getOrderedNodes();
    Assert.assertEquals(1, jsm.getUnmatchedArgNodes().size());
    Assert.assertEquals(true, jsm.getUnmatchedArgNodes().contains(nodes.get(3)));
    Set<QueryModelNode> nodeSet = new HashSet<>();
    nodeSet.add(nodes.get(0));
    nodeSet.add(nodes.get(2));
    nodeSet.add(nodes.get(3));
    nodeSet.add(pcj);
    Assert.assertEquals(nodeSet, new HashSet<QueryModelNode>(nodes));
    Assert.assertEquals(nodeSet, qNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 85 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class OptionalJoinSegmentPCJMatcherTest method testMultipleFilters.

@Test
public void testMultipleFilters() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    " Filter(?e = <uri:Bob>)" + // 
    " Filter(?c = <uri:Lawyer>)" + " ?l <uri:workAt> <uri:Company1> ." + // 
    " OPTIONAL { ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l } . " + // 
    "  ?e a ?c . " + // 
    "  ?e <uri:talksTo> ?l  . " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?e ?c ?l" + // 
    "{" + // 
    " Filter(?e = <uri:Bob>)" + // 
    " ?e a ?c . " + // 
    " ?e <uri:talksTo> ?l . " + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    Projection proj = (Projection) te1;
    Join join = (Join) proj.getArg();
    ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(join));
    SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
    Assert.assertEquals(true, jsm.match(pcj));
    TupleExpr te = jsm.getQuery();
    Assert.assertEquals(new HashSet<QueryModelNode>(), jsm.getUnmatchedArgNodes());
    Set<QueryModelNode> qNodes = LeftJoinQueryNodeGatherer.getNodes(te);
    List<QueryModelNode> nodes = jsm.getOrderedNodes();
    Set<QueryModelNode> nodeSet = new HashSet<>();
    nodeSet.add(nodes.get(0));
    nodeSet.add(nodes.get(2));
    nodeSet.add(nodes.get(3));
    nodeSet.add(pcj);
    Assert.assertEquals(nodeSet, new HashSet<QueryModelNode>(nodes));
    Assert.assertEquals(nodeSet, qNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)265 ParsedQuery (org.openrdf.query.parser.ParsedQuery)249 Test (org.junit.Test)212 TupleExpr (org.openrdf.query.algebra.TupleExpr)162 ArrayList (java.util.ArrayList)100 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)99 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)86 StatementPattern (org.openrdf.query.algebra.StatementPattern)82 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)62 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)46 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)45 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)43 RyaURI (org.apache.rya.api.domain.RyaURI)42 BindingSet (org.openrdf.query.BindingSet)41 RyaStatement (org.apache.rya.api.domain.RyaStatement)39 Projection (org.openrdf.query.algebra.Projection)37 RyaType (org.apache.rya.api.domain.RyaType)35 HashSet (java.util.HashSet)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)20 LiteralImpl (org.openrdf.model.impl.LiteralImpl)19