Search in sources :

Example 26 with SPARQLParser

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

the class StatementPatternEvalTest method simpleQueryWithoutBindingSets.

@Test
public void simpleQueryWithoutBindingSets() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    // query is used to build statement that will be evaluated
    String query = "select ?x ?c where{ graph ?c  {?x <uri:talksTo> <uri:Bob>. }}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    RyaStatement statement1 = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context1"), "", new StatementMetadata());
    dao.add(statement1);
    RyaStatement statement2 = new RyaStatement(new RyaIRI("uri:Doug"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context2"), "", new StatementMetadata());
    dao.add(statement2);
    RyaStatement statement3 = new RyaStatement(new RyaIRI("uri:Eric"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context3"), "", new StatementMetadata());
    dao.add(statement3);
    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(3, bsList.size());
    QueryBindingSet expected1 = new QueryBindingSet();
    expected1.addBinding("x", VF.createIRI("uri:Joe"));
    expected1.addBinding("c", VF.createIRI("uri:context1"));
    QueryBindingSet expected2 = new QueryBindingSet();
    expected2.addBinding("x", VF.createIRI("uri:Doug"));
    expected2.addBinding("c", VF.createIRI("uri:context2"));
    QueryBindingSet expected3 = new QueryBindingSet();
    expected3.addBinding("x", VF.createIRI("uri:Eric"));
    expected3.addBinding("c", VF.createIRI("uri:context3"));
    Set<BindingSet> expected = new HashSet<>(Arrays.asList(expected1, expected2, expected3));
    Set<BindingSet> actual = new HashSet<>(bsList);
    Assert.assertEquals(expected, actual);
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) RyaIRI(org.apache.rya.api.domain.RyaIRI) 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) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with SPARQLParser

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

the class StatementPatternEvalTest method simpleQueryWithBindingSetSameContext.

@Test
public void simpleQueryWithBindingSetSameContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    // query is used to build statement that will be evaluated
    String query = "select ?x ?c where{ graph ?c  {?x <uri:talksTo> <uri:Bob>. }}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    RyaStatement statement1 = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context1"), "", new StatementMetadata());
    dao.add(statement1);
    RyaStatement statement2 = new RyaStatement(new RyaIRI("uri:Doug"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context2"), "", new StatementMetadata());
    dao.add(statement2);
    RyaStatement statement3 = new RyaStatement(new RyaIRI("uri:Eric"), new RyaIRI("uri:talksTo"), new RyaType("uri:Bob"), new RyaIRI("uri:context3"), "", new StatementMetadata());
    dao.add(statement3);
    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    bsConstraint1.addBinding("c", VF.createIRI("uri:context1"));
    QueryBindingSet bsConstraint2 = new QueryBindingSet();
    bsConstraint2.addBinding("c", VF.createIRI("uri:context1"));
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1, bsConstraint2));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(1, bsList.size());
    QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("x", VF.createIRI("uri:Joe"));
    expected.addBinding("c", VF.createIRI("uri:context1"));
    Assert.assertEquals(expected, bsList.get(0));
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) RyaIRI(org.apache.rya.api.domain.RyaIRI) 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) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Test(org.junit.Test)

Example 28 with SPARQLParser

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

the class RdfTestUtil method getMultiProjection.

/**
 * Get the first {@link MultiProjection} node from a SPARQL query.
 *
 * @param sparql - The query that contains a single Projection node.
 * @return The first {@link MultiProjection} that is encountered.
 * @throws Exception The query could not be parsed.
 */
@Nullable
public static MultiProjection getMultiProjection(final String sparql) throws Exception {
    requireNonNull(sparql);
    final AtomicReference<MultiProjection> multiProjection = new AtomicReference<>();
    final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
    parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {

        @Override
        public void meet(final MultiProjection node) throws Exception {
            multiProjection.set(node);
        }
    });
    return multiProjection.get();
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 29 with SPARQLParser

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

the class RdfTestUtil method getFilter.

/**
 * Get the first {@link Filter} node from a SPARQL query.
 *
 * @param sparql - The query that contains a single Projection node.
 * @return The first {@link Filter} that is encountered.
 * @throws Exception The query could not be parsed.
 */
@Nullable
public static Filter getFilter(final String sparql) throws Exception {
    requireNonNull(sparql);
    final AtomicReference<Filter> filter = new AtomicReference<>();
    final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
    parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {

        @Override
        public void meet(final Filter node) throws Exception {
            filter.set(node);
        }
    });
    return filter.get();
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) Filter(org.eclipse.rdf4j.query.algebra.Filter) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) AtomicReference(java.util.concurrent.atomic.AtomicReference) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 30 with SPARQLParser

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

the class MultiProjectionEvaluatorTest method getMultiProjection.

/**
 * Get the first {@link MultiProjection} node from a SPARQL query.
 *
 * @param sparql - The query that contains a single Projection node.
 * @return The first {@link MultiProjection} that is encountered.
 * @throws Exception The query could not be parsed.
 */
@Nullable
public static MultiProjection getMultiProjection(final String sparql) throws Exception {
    requireNonNull(sparql);
    final AtomicReference<MultiProjection> multiProjection = new AtomicReference<>();
    final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
    parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() {

        @Override
        public void meet(final MultiProjection node) throws Exception {
            multiProjection.set(node);
        }
    });
    return multiProjection.get();
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Aggregations

SPARQLParser (org.eclipse.rdf4j.query.parser.sparql.SPARQLParser)260 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)244 Test (org.junit.Test)208 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)162 ArrayList (java.util.ArrayList)99 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)99 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)86 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)82 QueryModelNode (org.eclipse.rdf4j.query.algebra.QueryModelNode)62 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)46 QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)43 RyaIRI (org.apache.rya.api.domain.RyaIRI)42 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)40 RyaStatement (org.apache.rya.api.domain.RyaStatement)39 BindingSet (org.eclipse.rdf4j.query.BindingSet)37 Projection (org.eclipse.rdf4j.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 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)18