Search in sources :

Example 86 with SPARQLParser

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

the class PCJNodeConsolidatorTest method testAlreadyInOrder.

@Test
public void testAlreadyInOrder() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d" + // 
    "{" + // 
    "  ?a <uri:p5> <uri:const3>" + // 
    "  OPTIONAL{?a <uri:p3> ?c} . " + // 
    "  ?a <uri:p4> ?b . " + // 
    "  OPTIONAL{<uri:const2> <uri:p4> ?d } . " + "  ?c <uri:p1> ?d " + // 
    "  OPTIONAL{<uri:const1> <uri:p2> ?b} . " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d" + // 
    "{" + // 
    "  ?a <uri:p4> ?b . " + // 
    "  OPTIONAL{<uri:const2> <uri:p4> ?d } . " + "  ?c <uri:p1> ?d " + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    LeftJoin join1 = (LeftJoin) ((Projection) te1).getArg();
    Join join2 = (Join) ((Projection) te2).getArg();
    QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);
    QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
    List<QueryModelNode> queryNodes = new ArrayList<>(seg1.getOrderedNodes());
    Assert.assertTrue(consolidator.consolidateNodes());
    Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) LeftJoin(org.openrdf.query.algebra.LeftJoin) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) QueryNodeConsolidator(org.apache.rya.indexing.external.matching.QueryNodeConsolidator) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) Test(org.junit.Test)

Example 87 with SPARQLParser

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

the class PCJNodeConsolidatorTest method testSwitchBoundVars.

@Test
public void testSwitchBoundVars() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b " + // 
    "{" + // 
    "  ?a <uri:p0> ?b ." + // 
    " OPTIONAL{ ?a <uri:p1> <uri:o1> } ." + // 
    " ?a <uri:p2> <uri:o2> " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?a ?b " + // 
    "{" + // 
    " ?a <uri:p2> <uri:o2> " + // 
    " OPTIONAL{ ?a <uri:p1> <uri:o1> } ." + // 
    "  ?a <uri:p0> ?b ." + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    Join join1 = (Join) ((Projection) te1).getArg();
    Join join2 = (Join) ((Projection) te2).getArg();
    QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);
    QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
    List<QueryModelNode> queryNodes = new ArrayList<>(seg2.getOrderedNodes());
    Assert.assertTrue(consolidator.consolidateNodes());
    Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) QueryNodeConsolidator(org.apache.rya.indexing.external.matching.QueryNodeConsolidator) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) Test(org.junit.Test)

Example 88 with SPARQLParser

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

the class PCJNodeConsolidatorTest method testUpperLowerBoundOptional.

@Test
public void testUpperLowerBoundOptional() throws Exception {
    String query1 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d" + // 
    "{" + // 
    "  ?a <uri:p5> <uri:const3>" + // 
    "  OPTIONAL{<uri:const2> <uri:p4> ?d } . " + // 
    "  ?a <uri:p4> ?b . " + // 
    "  OPTIONAL{?a <uri:p3> ?c} . " + // 
    "  OPTIONAL{<uri:const1> <uri:p2> ?b} . " + "  ?c <uri:p1> ?d " + // 
    "}";
    String query2 = // 
    "" + // 
    "SELECT ?a ?b ?c ?d" + // 
    "{" + // 
    "  ?a <uri:p4> ?b . " + // 
    "  OPTIONAL{<uri:const2> <uri:p4> ?d } . " + "  ?c <uri:p1> ?d " + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(query1, null);
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    Join join1 = (Join) ((Projection) te1).getArg();
    Join join2 = (Join) ((Projection) te2).getArg();
    QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);
    QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
    List<QueryModelNode> queryNodes = new ArrayList<>(seg1.getOrderedNodes());
    QueryModelNode node = queryNodes.remove(0);
    queryNodes.add(1, node);
    node = queryNodes.remove(3);
    queryNodes.add(2, node);
    node = queryNodes.remove(4);
    queryNodes.add(2, node);
    Assert.assertTrue(consolidator.consolidateNodes());
    Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) LeftJoin(org.openrdf.query.algebra.LeftJoin) Join(org.openrdf.query.algebra.Join) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) QueryNodeConsolidator(org.apache.rya.indexing.external.matching.QueryNodeConsolidator) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) Test(org.junit.Test)

Example 89 with SPARQLParser

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

the class AccumuloStatementMetadataNodeTest method simpleQueryWithBindingSetJoinOnProperty.

/**
 * Tests if the StatementMetadataNode joins BindingSet correctly for
 * variables appearing in metadata statements. In this case, the metadata
 * statements are (_:blankNode <http://createdOn 2017-01-04 ) and
 * (_:blankNode <http://createdBy> ?y). The variable ?y appears as the
 * object in the above metadata statement and its values are joined to the
 * constraint BindingSets in the example below.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetJoinOnProperty() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    StatementMetadata metadata = new StatementMetadata();
    metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
    metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
    RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
    dao.add(statement1);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
    QueryBindingSet bsConstraint = new QueryBindingSet();
    bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop"));
    bsConstraint.addBinding("y", new LiteralImpl("Doug"));
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(0, bsList.size());
    dao.delete(statement1, conf);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) StatementMetadataNode(org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode) LiteralImpl(org.openrdf.model.impl.LiteralImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Test(org.junit.Test)

Example 90 with SPARQLParser

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

the class AccumuloStatementMetadataNodeTest method simpleQueryWithBindingSet.

@Test
public void simpleQueryWithBindingSet() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    StatementMetadata metadata = new StatementMetadata();
    metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
    metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
    RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
    RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
    dao.add(statement1);
    dao.add(statement2);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
    QueryBindingSet bsConstraint = new QueryBindingSet();
    bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop"));
    bsConstraint.addBinding("z", new LiteralImpl("Virginia"));
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);
    QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("x", new LiteralImpl("CoffeeShop"));
    expected.addBinding("y", new LiteralImpl("Joe"));
    expected.addBinding("z", new LiteralImpl("Virginia"));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(1, bsList.size());
    Assert.assertEquals(expected, bsList.get(0));
    dao.delete(statement1, conf);
    dao.delete(statement2, conf);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) StatementMetadataNode(org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode) LiteralImpl(org.openrdf.model.impl.LiteralImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) 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