Search in sources :

Example 96 with QueryBindingSet

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

the class AccumuloStatementMetadataNodeTest method simpleQueryWithBindingSetCollection.

/**
 * Tests if StatementMetadataNode joins BindingSet values correctly for
 * variables appearing as the object in one of the StatementPattern
 * statements (in the case ?x appears as the Object in the statement
 * _:blankNode rdf:object ?x). StatementPattern statements have either
 * rdf:subject, rdf:predicate, or rdf:object as the predicate.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetCollection() 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);
    List<BindingSet> bsCollection = new ArrayList<>();
    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    bsConstraint1.addBinding("x", new LiteralImpl("CoffeeShop"));
    bsConstraint1.addBinding("z", new LiteralImpl("Virginia"));
    QueryBindingSet bsConstraint2 = new QueryBindingSet();
    bsConstraint2.addBinding("x", new LiteralImpl("HardwareStore"));
    bsConstraint2.addBinding("z", new LiteralImpl("Maryland"));
    QueryBindingSet bsConstraint3 = new QueryBindingSet();
    bsConstraint3.addBinding("x", new LiteralImpl("BurgerShack"));
    bsConstraint3.addBinding("z", new LiteralImpl("Delaware"));
    bsCollection.add(bsConstraint1);
    bsCollection.add(bsConstraint2);
    bsCollection.add(bsConstraint3);
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);
    QueryBindingSet expected1 = new QueryBindingSet();
    expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
    expected1.addBinding("y", new LiteralImpl("Joe"));
    expected1.addBinding("z", new LiteralImpl("Virginia"));
    QueryBindingSet expected2 = new QueryBindingSet();
    expected2.addBinding("x", new LiteralImpl("HardwareStore"));
    expected2.addBinding("y", new LiteralImpl("Joe"));
    expected2.addBinding("z", new LiteralImpl("Maryland"));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(2, bsList.size());
    Assert.assertEquals(expected1, bsList.get(1));
    Assert.assertEquals(expected2, 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)

Example 97 with QueryBindingSet

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

the class AccumuloStatementMetadataNodeTest method simpleQueryWithVariableContextAndJoinOnContext.

/**
 * Tests if StatementMetadataNode joins BindingSet values correctly for
 * variables appearing as the object in one of the StatementPattern
 * statements (in the case ?x appears as the Object in the statement
 * _:blankNode rdf:object ?x). StatementPattern statements have either
 * rdf:subject, rdf:predicate, or rdf:object as the predicate. Additionally,
 * this test also determines whether node passes back bindings corresponding
 * to a specified context and that a join across variable context is
 * performed properly.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithVariableContextAndJoinOnContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    // query used to create StatementPatternMetadataNode
    String contextQuery = "prefix owl: <http://www.w3.org/2002/07/owl#> prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?x ?y ?c where { graph ?c {_:blankNode rdf:type owl:Annotation; owl:annotatedSource <http://Joe>; " + "owl:annotatedProperty <http://worksAt>; owl:annotatedTarget ?x; <http://createdBy> ?y; <http://createdOn> \'2017-01-04\'^^xsd:date }}";
    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_1"), "", metadata);
    RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context_2"), "", metadata);
    dao.add(statement1);
    dao.add(statement2);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(contextQuery, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
    List<BindingSet> bsCollection = new ArrayList<>();
    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    bsConstraint1.addBinding("x", new LiteralImpl("CoffeeShop"));
    bsConstraint1.addBinding("z", new LiteralImpl("Virginia"));
    bsConstraint1.addBinding("c", new URIImpl("http://context_1"));
    QueryBindingSet bsConstraint2 = new QueryBindingSet();
    bsConstraint2.addBinding("x", new LiteralImpl("CoffeeShop"));
    bsConstraint2.addBinding("z", new LiteralImpl("Maryland"));
    bsConstraint2.addBinding("c", new URIImpl("http://context_2"));
    QueryBindingSet bsConstraint4 = new QueryBindingSet();
    bsConstraint4.addBinding("x", new LiteralImpl("HardwareStore"));
    bsConstraint4.addBinding("z", new LiteralImpl("WestVirginia"));
    bsConstraint4.addBinding("c", new URIImpl("http://context_2"));
    QueryBindingSet bsConstraint3 = new QueryBindingSet();
    bsConstraint3.addBinding("x", new LiteralImpl("BurgerShack"));
    bsConstraint3.addBinding("z", new LiteralImpl("Delaware"));
    bsConstraint3.addBinding("c", new URIImpl("http://context_1"));
    bsCollection.add(bsConstraint1);
    bsCollection.add(bsConstraint2);
    bsCollection.add(bsConstraint3);
    bsCollection.add(bsConstraint4);
    // AccumuloRyaQueryEngine engine = dao.getQueryEngine();
    // //        CloseableIteration<RyaStatement, RyaDAOException> iter = engine.query(new RyaStatement(new RyaURI("http://Joe"),
    // //                new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context_2")), conf);
    // CloseableIteration<? extends Map.Entry<RyaStatement,BindingSet>, RyaDAOException> iter = engine.queryWithBindingSet(Arrays.asList(new RdfCloudTripleStoreUtils.CustomEntry<RyaStatement, BindingSet>(
    // new RyaStatement(new RyaURI("http://Joe"),
    // new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context_2")), bsConstraint4)), conf);
    // while (iter.hasNext()) {
    // System.out.println(iter.next());
    // }
    // 
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);
    QueryBindingSet expected1 = new QueryBindingSet();
    expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
    expected1.addBinding("y", new LiteralImpl("Joe"));
    expected1.addBinding("z", new LiteralImpl("Virginia"));
    expected1.addBinding("c", new URIImpl("http://context_1"));
    QueryBindingSet expected2 = new QueryBindingSet();
    expected2.addBinding("x", new LiteralImpl("HardwareStore"));
    expected2.addBinding("y", new LiteralImpl("Joe"));
    expected2.addBinding("z", new LiteralImpl("WestVirginia"));
    expected2.addBinding("c", new URIImpl("http://context_2"));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(2, bsList.size());
    Assert.assertEquals(expected1, bsList.get(1));
    Assert.assertEquals(expected2, 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) URIImpl(org.openrdf.model.impl.URIImpl) 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 98 with QueryBindingSet

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

the class AccumuloPcjSerializerTest method basicLongMixLiteralBsTest.

@Test
public void basicLongMixLiteralBsTest() throws BindingSetConversionException {
    final QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("X", new LiteralImpl("literal1"));
    bs.addBinding("Y", new LiteralImpl("5", new URIImpl("http://www.w3.org/2001/XMLSchema#integer")));
    bs.addBinding("Z", new LiteralImpl("5.0", new URIImpl("http://www.w3.org/2001/XMLSchema#double")));
    bs.addBinding("W", new LiteralImpl("1000", new URIImpl("http://www.w3.org/2001/XMLSchema#long")));
    final VariableOrder varOrder = new VariableOrder("W", "X", "Y", "Z");
    BindingSetConverter<byte[]> converter = new AccumuloPcjSerializer();
    final byte[] byteVal = converter.convert(bs, varOrder);
    final BindingSet newBs = converter.convert(byteVal, varOrder);
    assertEquals(bs, newBs);
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) AccumuloPcjSerializer(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjSerializer) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) URIImpl(org.openrdf.model.impl.URIImpl) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 99 with QueryBindingSet

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

the class StarQueryTest method testGetContrainedQuery.

@Test
public void testGetContrainedQuery() throws RyaTypeResolverException, MalformedQueryException {
    String q1 = // 
    "" + // 
    "SELECT ?X ?Y1 ?Y2 " + // 
    "{" + // 
    "GRAPH <http://joe> { " + // 
    "?X <uri:cf1> ?Y1 ." + // 
    "?X <uri:cf2> ?Y2 ." + // 
    "?X <uri:cf3> ?Y3 ." + // 
    "}" + "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = null;
    pq1 = parser.parseQuery(q1, null);
    TupleExpr te1 = pq1.getTupleExpr();
    System.out.println(te1);
    List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
    StarQuery sq1 = new StarQuery(spList1);
    QueryBindingSet bs1 = new QueryBindingSet();
    QueryBindingSet bs2 = new QueryBindingSet();
    Value v1 = vf.createURI("uri:hank");
    Value v2 = vf.createURI("uri:bob");
    bs1.addBinding("X", v1);
    bs2.addBinding("X", v1);
    bs2.addBinding("Y3", v2);
    StarQuery sq2 = StarQuery.getConstrainedStarQuery(sq1, bs1);
    StarQuery sq3 = StarQuery.getConstrainedStarQuery(sq1, bs2);
    Assert.assertTrue(sq2.commonVarHasValue());
    Assert.assertEquals(sq2.getCommonVarValue(), "uri:hank");
    Assert.assertTrue(sq3.commonVarHasValue());
    Assert.assertEquals(sq3.getCommonVarValue(), "uri:hank");
    TextColumn[] tc1 = sq1.getColumnCond();
    TextColumn[] tc2 = sq2.getColumnCond();
    TextColumn[] tc3 = sq3.getColumnCond();
    for (int i = 0; i < tc1.length; i++) {
        Assert.assertTrue(tc1[i].equals(tc2[i]));
        if (i != 2) {
            Assert.assertTrue(tc1[i].equals(tc3[i]));
        } else {
            Assert.assertEquals(tc3[i].getColumnFamily(), new Text("uri:cf3"));
            RyaType objType = RdfToRyaConversions.convertValue(v2);
            byte[][] b1 = null;
            b1 = RyaContext.getInstance().serializeType(objType);
            byte[] b2 = Bytes.concat("object".getBytes(), "\u0000".getBytes(), b1[0], b1[1]);
            Assert.assertEquals(tc3[i].getColumnQualifier(), new Text(b2));
            Assert.assertTrue(!tc3[i].isPrefix());
        }
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Text(org.apache.hadoop.io.Text) RyaType(org.apache.rya.api.domain.RyaType) TupleExpr(org.openrdf.query.algebra.TupleExpr) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) Value(org.openrdf.model.Value) TextColumn(org.apache.rya.accumulo.documentIndex.TextColumn) Test(org.junit.Test)

Example 100 with QueryBindingSet

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

the class StarQueryTest method testBasicFunctionality.

@Test
public void testBasicFunctionality() throws MalformedQueryException {
    String q1 = // 
    "" + // 
    "SELECT ?X ?Y1 ?Y2 " + // 
    "{" + // 
    "GRAPH <http://joe> { " + // 
    "?X <uri:cf1> ?Y1 ." + // 
    "?X <uri:cf2> ?Y2 ." + // 
    "?X <uri:cf3> ?Y3 ." + // 
    "}" + "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = null;
    pq1 = parser.parseQuery(q1, null);
    TupleExpr te1 = pq1.getTupleExpr();
    System.out.println(te1);
    List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
    Assert.assertTrue(StarQuery.isValidStarQuery(spList1));
    StarQuery sq1 = new StarQuery(spList1);
    Var v = sq1.getCommonVar();
    Assert.assertEquals("X", v.getName());
    Assert.assertEquals(null, v.getValue());
    Assert.assertEquals(v.getValue(), sq1.getCommonVarValue());
    Assert.assertTrue(!sq1.commonVarHasValue());
    Assert.assertEquals("X", sq1.getCommonVarName());
    Assert.assertTrue(sq1.isCommonVarURI());
    Assert.assertTrue(sq1.hasContext());
    Assert.assertEquals("http://joe", sq1.getContextURI());
    TextColumn[] cond = sq1.getColumnCond();
    for (int i = 0; i < cond.length; i++) {
        Assert.assertEquals(cond[i].getColumnFamily().toString(), "uri:cf" + (i + 1));
        Assert.assertEquals(cond[i].getColumnQualifier().toString(), "object");
    }
    Set<String> unCommonVars = Sets.newHashSet();
    unCommonVars.add("Y1");
    unCommonVars.add("Y2");
    unCommonVars.add("Y3");
    Assert.assertEquals(unCommonVars, sq1.getUnCommonVars());
    Map<String, Integer> varPos = sq1.getVarPos();
    Assert.assertEquals(0, varPos.get("Y1").intValue());
    Assert.assertEquals(1, varPos.get("Y2").intValue());
    Assert.assertEquals(2, varPos.get("Y3").intValue());
    QueryBindingSet bs1 = new QueryBindingSet();
    QueryBindingSet bs2 = new QueryBindingSet();
    Value v1 = vf.createURI("uri:hank");
    Value v2 = vf.createURI("uri:bob");
    bs1.addBinding("X", v1);
    bs2.addBinding("X", v1);
    bs2.addBinding("Y3", v2);
    Set<String> s1 = StarQuery.getCommonVars(sq1, bs1);
    Set<String> s2 = StarQuery.getCommonVars(sq1, bs2);
    Set<String> s3 = Sets.newHashSet();
    Set<String> s4 = Sets.newHashSet();
    s3.add("X");
    s4.add("X");
    s4.add("Y3");
    Assert.assertEquals(s1, s3);
    Assert.assertEquals(s2, s4);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Var(org.openrdf.query.algebra.Var) TupleExpr(org.openrdf.query.algebra.TupleExpr) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) Value(org.openrdf.model.Value) TextColumn(org.apache.rya.accumulo.documentIndex.TextColumn) Test(org.junit.Test)

Aggregations

QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)107 Test (org.junit.Test)84 BindingSet (org.openrdf.query.BindingSet)79 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)52 RyaURI (org.apache.rya.api.domain.RyaURI)46 RyaStatement (org.apache.rya.api.domain.RyaStatement)45 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)42 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 ParsedQuery (org.openrdf.query.parser.ParsedQuery)41 RyaType (org.apache.rya.api.domain.RyaType)39 LiteralImpl (org.openrdf.model.impl.LiteralImpl)32 ArrayList (java.util.ArrayList)30 HashSet (java.util.HashSet)28 URIImpl (org.openrdf.model.impl.URIImpl)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)26 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)20 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 Statement (org.openrdf.model.Statement)15 TupleExpr (org.openrdf.query.algebra.TupleExpr)14 Collection (java.util.Collection)13