Search in sources :

Example 16 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class StatementPatternEvalTest method simpleQueryNoBindingSetConstantContext.

@Test
public void simpleQueryNoBindingSetConstantContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    // query is used to build statement that will be evaluated
    String query = "select ?x ?c where{ graph <uri:context1>  {?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(1, bsList.size());
    QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("x", VF.createIRI("uri:Joe"));
    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 17 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet 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 18 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet 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 19 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class StatementPatternMatcher method match.

/**
 * Matches a {@link Statement} against the provided {@link StatementPattern} and returns a {@link BindingSet}
 * if the statement matched the pattern.
 *
 * @param statement - The statement that will be matched against the pattern. (not null)
 * @return A {@link BinidngSet} containing the statement's values filled in for the pattern's variables if
 *   the statement's values match the pattern's constants; otherwise empty.
 */
public Optional<BindingSet> match(final Statement statement) {
    requireNonNull(statement);
    // Setup the resulting binding set that could be built from this Statement.
    final QueryBindingSet bs = new QueryBindingSet();
    if (matchesValue(pattern.getSubjectVar(), statement.getSubject(), bs) && matchesValue(pattern.getPredicateVar(), statement.getPredicate(), bs) && matchesValue(pattern.getObjectVar(), statement.getObject(), bs) && matchesContext(pattern.getContextVar(), statement.getContext(), bs)) {
        return Optional.of(bs);
    } else {
        return Optional.empty();
    }
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Example 20 with QueryBindingSet

use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class StatementPatternMatcherTest method matchesContext.

@Test
public void matchesContext() throws Exception {
    // Create a matcher against a pattern that matches a specific context.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "GRAPH <urn:testGraph> {" + "?s ?p ?o ." + "}" + "}"));
    // Create a statement that matches the pattern.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Statement statement = vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"), vf.createIRI("urn:testGraph"));
    // Create the expected resulting Binding Set.
    final QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("s", vf.createIRI("urn:Alice"));
    expected.addBinding("p", vf.createIRI("urn:talksTo"));
    expected.addBinding("o", vf.createIRI("urn:Bob"));
    // Show the expected Binding Set matches the resulting Binding Set.
    final Optional<BindingSet> bs = matcher.match(statement);
    assertEquals(expected, bs.get());
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Statement(org.eclipse.rdf4j.model.Statement) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Aggregations

QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)111 Test (org.junit.Test)84 BindingSet (org.eclipse.rdf4j.query.BindingSet)83 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)56 RyaIRI (org.apache.rya.api.domain.RyaIRI)46 RyaStatement (org.apache.rya.api.domain.RyaStatement)45 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)42 SPARQLParser (org.eclipse.rdf4j.query.parser.sparql.SPARQLParser)42 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)41 RyaType (org.apache.rya.api.domain.RyaType)39 ArrayList (java.util.ArrayList)30 HashSet (java.util.HashSet)28 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)26 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)20 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)16 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 Statement (org.eclipse.rdf4j.model.Statement)15 Value (org.eclipse.rdf4j.model.Value)14 Collection (java.util.Collection)13 BatchWriter (org.apache.accumulo.core.client.BatchWriter)12