Java Code Examples for org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector#process()

The following examples show how to use org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector#process() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstructGraphTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testConstructGraph() throws MalformedQueryException, UnsupportedEncodingException {
    String query = "select ?x where { ?x <uri:talksTo> <uri:Bob>. ?y <uri:worksAt> ?z }";

    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
    ConstructGraph graph = new ConstructGraph(patterns);

    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("x", VF.createIRI("uri:Joe"));
    bs.addBinding("y", VF.createIRI("uri:Bob"));
    bs.addBinding("z", VF.createIRI("uri:BurgerShack"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs,"FOUO");
    Set<RyaStatement> statements = graph.createGraphFromBindingSet(vBs);
    
    RyaStatement statement1 = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:talksTo"), new RyaIRI("uri:Bob"));
    RyaStatement statement2 = new RyaStatement(new RyaIRI("uri:Bob"), new RyaIRI("uri:worksAt"), new RyaIRI("uri:BurgerShack"));
    Set<RyaStatement> expected = Sets.newHashSet(Arrays.asList(statement1, statement2));
    expected.forEach(x-> x.setColumnVisibility("FOUO".getBytes()));
    ConstructGraphTestUtils.ryaStatementSetsEqualIgnoresTimestamp(expected, statements);
}
 
Example 2
Source File: ConstructProjectionTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testConstructProjectionProjPred() throws MalformedQueryException {
    String query = "select ?p where { <uri:Joe> ?p <uri:Bob> }";
    
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
    ConstructProjection projection = new ConstructProjection(patterns.get(0));
    
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("p", VF.createIRI("uri:worksWith"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
    RyaStatement statement = projection.projectBindingSet(vBs, new HashMap<>());
    
    RyaStatement expected = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:worksWith"), new RyaIRI("uri:Bob"));
    expected.setTimestamp(statement.getTimestamp());
    expected.setColumnVisibility(new byte[0]);
    
    assertEquals(expected, statement);
}
 
Example 3
Source File: ConstructProjectionTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testConstructProjectionProjectSubj() throws MalformedQueryException, UnsupportedEncodingException {
    String query = "select ?x where { ?x <uri:talksTo> <uri:Bob> }";
    
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
    ConstructProjection projection = new ConstructProjection(patterns.get(0));
    
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("x", VF.createIRI("uri:Joe"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs, "FOUO");
    RyaStatement statement = projection.projectBindingSet(vBs, new HashMap<>());
    
    RyaStatement expected = new RyaStatement(new RyaIRI("uri:Joe"), new RyaIRI("uri:talksTo"), new RyaIRI("uri:Bob"));
    expected.setColumnVisibility("FOUO".getBytes("UTF-8"));
    expected.setTimestamp(statement.getTimestamp());
    
    assertEquals(expected, statement);
}
 
Example 4
Source File: QueryJoinOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected List<Var> getStatementPatternVars(TupleExpr tupleExpr) {
	List<StatementPattern> stPatterns = StatementPatternCollector.process(tupleExpr);
	List<Var> varList = new ArrayList<>(stPatterns.size() * 4);
	for (StatementPattern sp : stPatterns) {
		sp.getVars(varList);
	}
	return varList;
}
 
Example 5
Source File: StatementMetadataOptimizerTest.java    From rya with Apache License 2.0 5 votes vote down vote up
private static Set<StatementMetadataNode<?>> getExpected(String query) throws MalformedQueryException {
    ParsedQuery pq = parser.parseQuery(query, null);
    StatementMetadataExternalSetProvider provider = new StatementMetadataExternalSetProvider(
            getConf(false));
    List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
    JoinSegment<StatementMetadataNode<?>> segment = new JoinSegment<>(
            new HashSet<QueryModelNode>(patterns), new ArrayList<QueryModelNode>(patterns),
            new HashMap<ValueExpr, Filter>());
    return new HashSet<>(provider.getExternalSets(segment));
}
 
Example 6
Source File: MongoStatementMetadataNodeIT.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 */
@Test
public void simpleQueryWithBindingSetJoinOnProperty() throws Exception {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));

        final RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
        dao.add(statement1);

        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery pq = parser.parseQuery(query, null);
        final List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
        final StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);

        final QueryBindingSet bsConstraint = new QueryBindingSet();
        bsConstraint.addBinding("x", VF.createLiteral("CoffeeShop"));
        bsConstraint.addBinding("y", VF.createLiteral("Doug"));

        final CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);

        final List<BindingSet> bsList = new ArrayList<>();
        while (iteration.hasNext()) {
            bsList.add(iteration.next());
        }

        Assert.assertEquals(0, bsList.size());
        dao.delete(statement1, conf);
    } finally {
        dao.destroy();
    }
}
 
Example 7
Source File: AccumuloSelectivityEvalDAOTest.java    From rya with Apache License 2.0 5 votes vote down vote up
private List<StatementPattern> getSpList(String query) throws MalformedQueryException {

        SPARQLParser sp = new SPARQLParser();
        ParsedQuery pq = sp.parseQuery(query, null);
        TupleExpr te = pq.getTupleExpr();

        return StatementPatternCollector.process(te);
    }
 
Example 8
Source File: AccumuloStatementMetadataNodeTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleQueryWithBindingSet() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {

    StatementMetadata metadata = new StatementMetadata();
    metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaType("Joe"));
    metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));

    RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
            new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
    RyaStatement statement2 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
            new RyaType("HardwareStore"), new RyaIRI("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", VF.createLiteral("CoffeeShop"));
    bsConstraint.addBinding("z", VF.createLiteral("Virginia"));

    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);

    QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("x", VF.createLiteral("CoffeeShop"));
    expected.addBinding("y", VF.createLiteral("Joe"));
    expected.addBinding("z", VF.createLiteral("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);
}
 
Example 9
Source File: AccumuloStatementMetadataNodeTest.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
     * 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 RyaIRI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));

        RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("CoffeeShop"), new RyaIRI("http://context_1"), "", metadata);
        RyaStatement statement2 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("HardwareStore"), new RyaIRI("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", VF.createLiteral("CoffeeShop"));
        bsConstraint1.addBinding("z", VF.createLiteral("Virginia"));
        bsConstraint1.addBinding("c", VF.createIRI("http://context_1"));

        QueryBindingSet bsConstraint2 = new QueryBindingSet();
        bsConstraint2.addBinding("x", VF.createLiteral("CoffeeShop"));
        bsConstraint2.addBinding("z", VF.createLiteral("Maryland"));
        bsConstraint2.addBinding("c", VF.createIRI("http://context_2"));

        QueryBindingSet bsConstraint4 = new QueryBindingSet();
        bsConstraint4.addBinding("x", VF.createLiteral("HardwareStore"));
        bsConstraint4.addBinding("z", VF.createLiteral("WestVirginia"));
        bsConstraint4.addBinding("c", VF.createIRI("http://context_2"));

        QueryBindingSet bsConstraint3 = new QueryBindingSet();
        bsConstraint3.addBinding("x", VF.createLiteral("BurgerShack"));
        bsConstraint3.addBinding("z", VF.createLiteral("Delaware"));
        bsConstraint3.addBinding("c", VF.createIRI("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 RyaIRI("http://Joe"),
////                new RyaIRI("http://worksAt"), new RyaType("HardwareStore"), new RyaIRI("http://context_2")), conf);
//        CloseableIteration<? extends Map.Entry<RyaStatement,BindingSet>, RyaDAOException> iter = engine.queryWithBindingSet(Arrays.asList(new RdfCloudTripleStoreUtils.CustomEntry<RyaStatement, BindingSet>(
//                new RyaStatement(new RyaIRI("http://Joe"),
//                        new RyaIRI("http://worksAt"), new RyaType("HardwareStore"), new RyaIRI("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", VF.createLiteral("CoffeeShop"));
        expected1.addBinding("y", VF.createLiteral("Joe"));
        expected1.addBinding("z", VF.createLiteral("Virginia"));
        expected1.addBinding("c", VF.createIRI("http://context_1"));

        QueryBindingSet expected2 = new QueryBindingSet();
        expected2.addBinding("x", VF.createLiteral("HardwareStore"));
        expected2.addBinding("y", VF.createLiteral("Joe"));
        expected2.addBinding("z", VF.createLiteral("WestVirginia"));
        expected2.addBinding("c", VF.createIRI("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);
    }
 
Example 10
Source File: MongoStatementMetadataNodeIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleQueryWithBindingSet() throws Exception {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));

        final RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
        final RyaStatement statement2 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("HardwareStore"), new RyaIRI("http://context"), "", metadata);
        dao.add(statement1);
        dao.add(statement2);

        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery pq = parser.parseQuery(query, null);
        final List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
        final StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);

        final QueryBindingSet bsConstraint = new QueryBindingSet();
        bsConstraint.addBinding("x", VF.createLiteral("CoffeeShop"));
        bsConstraint.addBinding("z", VF.createLiteral("Virginia"));

        final CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);

        final QueryBindingSet expected = new QueryBindingSet();
        expected.addBinding("x", VF.createLiteral("CoffeeShop"));
        expected.addBinding("y", VF.createLiteral("Joe"));
        expected.addBinding("z", VF.createLiteral("Virginia"));

        final 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);
    } finally {
        dao.destroy();
    }
}
 
Example 11
Source File: ConstructGraphTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testConstructGraphSerializerBlankNode() throws MalformedQueryException {
    
    String query = "select ?x where { _:b <uri:talksTo> ?x. _:b <uri:worksAt> ?y }";

    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
    ConstructGraph graph = new ConstructGraph(patterns);
    
    String constructString = ConstructGraphSerializer.toConstructString(graph);
    ConstructGraph deserialized = ConstructGraphSerializer.toConstructGraph(constructString);
    
    assertEquals(graph, deserialized);
    
}
 
Example 12
Source File: MongoStatementMetadataNodeIT.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Tests to see if correct result is passed back when a metadata statement
 * is joined with a StatementPattern statement (i.e. a common variable
 * appears in a StatementPattern statement and a metadata statement).
 * StatementPattern statements have either rdf:subject, rdf:predicate, or
 * rdf:object as the predicate while a metadata statement is any statement
 * in the reified query whose predicate is not rdf:type and not a
 * StatementPattern predicate.
 */
@Test
public void simpleQueryWithBindingSetJoinPropertyToSubject() throws Exception {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaIRI("http://Joe"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));

        final RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
        final RyaStatement statement2 = new RyaStatement(new RyaIRI("http://Bob"), new RyaIRI("http://worksAt"),
                new RyaType("HardwareStore"), new RyaIRI("http://context"), "", metadata);
        dao.add(statement1);
        dao.add(statement2);

        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery pq = parser.parseQuery(query2, null);
        final List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
        final StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);

        final List<BindingSet> bsCollection = new ArrayList<>();
        final QueryBindingSet bsConstraint1 = new QueryBindingSet();
        bsConstraint1.addBinding("y", VF.createLiteral("CoffeeShop"));
        bsConstraint1.addBinding("z", VF.createLiteral("Virginia"));

        final QueryBindingSet bsConstraint2 = new QueryBindingSet();
        bsConstraint2.addBinding("y", VF.createLiteral("HardwareStore"));
        bsConstraint2.addBinding("z", VF.createLiteral("Maryland"));
        bsCollection.add(bsConstraint1);
        bsCollection.add(bsConstraint2);

        final CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);

        final QueryBindingSet expected = new QueryBindingSet();
        expected.addBinding("y", VF.createLiteral("CoffeeShop"));
        expected.addBinding("x", VF.createIRI("http://Joe"));
        expected.addBinding("z", VF.createLiteral("Virginia"));

        final 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);
    } finally {
        dao.destroy();
    }
}
 
Example 13
Source File: CachedSourceSelector.java    From semagrow with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<SourceMetadata> getSources(TupleExpr expr, Dataset dataset, BindingSet bindings) {
    //FIXME: This is not the case in general but only in a pattern-wise src selector
    Collection<StatementPattern> patterns  = StatementPatternCollector.process( expr );
    return getSources( patterns, dataset, bindings );
}
 
Example 14
Source File: StatementPatternEvalTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 15
Source File: StatementPatternEvalTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleQueryWithBindingSets()
        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:context2"));
    
    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(2, 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"));
    
    Set<BindingSet> expected = new HashSet<>(Arrays.asList(expected1, expected2));
    Set<BindingSet> actual = new HashSet<>(bsList);
    
    Assert.assertEquals(expected, actual);
    
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
 
Example 16
Source File: StatementPatternEvalTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 17
Source File: StarQueryTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@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.createIRI("uri:hank");
    Value v2 = VF.createIRI("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());
       }
   }
    
    
    
}
 
Example 18
Source File: ThreshholdPlanSelectorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSingleIndex2() throws Exception {

	String q1 = ""//
			+ "SELECT ?f ?m ?d ?e ?l ?c " //
			+ "{" //
			+ "  ?f a ?m ."//
			+ "  ?c a ?l ."//
			+ "  ?d <uri:talksTo> ?f . "//
			+ "  ?e <uri:talksTo> ?c . "//
			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."//
			+ "  ?m <uri:talksTo> ?e . "//
			+ "}";//

	String q2 = ""//
			+ "SELECT ?u ?s ?t " //
			+ "{" //
			+ "  ?s a ?t ."//
			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
			+ "  ?u <uri:talksTo> ?s . "//
			+ "}";//

	String q3 = ""//
			+ "SELECT ?e ?c ?l " //
			+ "{" //
			+ "  ?c a ?l ."//
			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."//
			+ "  ?e <uri:talksTo> ?c . "//
			+ "}";//

	String q4 = ""//
			+ "SELECT ?d ?f ?m " //
			+ "{" //
			+ "  ?f a ?m ."//
			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
			+ "  ?d <uri:talksTo> ?f . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();

	ParsedQuery pq1 = parser.parseQuery(q1, null);
	ParsedQuery pq2 = parser.parseQuery(q2, null);
	ParsedQuery pq3 = parser.parseQuery(q3, null);
	ParsedQuery pq4 = parser.parseQuery(q4, null);

	SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
			(Projection) pq2.getTupleExpr());
	SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
			(Projection) pq3.getTupleExpr());
	SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
			(Projection) pq4.getTupleExpr());

	List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();

	list.add(extTup1);

	List<StatementPattern> spList = StatementPatternCollector.process(pq1
			.getTupleExpr());
	List<QueryModelNode> optTupNodes = Lists.newArrayList();
	optTupNodes.add(extTup3);
	optTupNodes.add(spList.get(6));
	optTupNodes.add(extTup2);

	IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
			pq1.getTupleExpr(), list);

	Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
			.getPlans(iep.getIndexedTuples());

	IndexPlanValidator ipv = new IndexPlanValidator(true);
	Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);

	ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
			pq1.getTupleExpr());

	TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .7,
			.1, .2);

	NodeCollector nc = new NodeCollector();
	optimalTup.visit(nc);

	List<QueryModelNode> qNodes = nc.getNodes();

	Assert.assertTrue(qNodes.equals(optTupNodes));

}
 
Example 19
Source File: AccumuloStatementMetadataNodeTest.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Tests to see if correct result is passed back when a metadata statement
 * is joined with a StatementPattern statement (i.e. a common variable
 * appears in a StatementPattern statement and a metadata statement).
 * StatementPattern statements have either rdf:subject, rdf:predicate, or
 * rdf:object as the predicate while a metadata statement is any statement
 * in the reified query whose predicate is not rdf:type and not a
 * StatementPattern predicate.
 * 
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetJoinPropertyToSubject()
        throws MalformedQueryException, QueryEvaluationException, RyaDAOException {

    StatementMetadata metadata = new StatementMetadata();
    metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaIRI("http://Joe"));
    metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));

    RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
            new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
    RyaStatement statement2 = new RyaStatement(new RyaIRI("http://Bob"), new RyaIRI("http://worksAt"),
            new RyaType("HardwareStore"), new RyaIRI("http://context"), "", metadata);
    dao.add(statement1);
    dao.add(statement2);

    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query2, 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("y", VF.createLiteral("CoffeeShop"));
    bsConstraint1.addBinding("z", VF.createLiteral("Virginia"));

    QueryBindingSet bsConstraint2 = new QueryBindingSet();
    bsConstraint2.addBinding("y", VF.createLiteral("HardwareStore"));
    bsConstraint2.addBinding("z", VF.createLiteral("Maryland"));
    bsCollection.add(bsConstraint1);
    bsCollection.add(bsConstraint2);

    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);

    QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("y", VF.createLiteral("CoffeeShop"));
    expected.addBinding("x", VF.createIRI("http://Joe"));
    expected.addBinding("z", VF.createLiteral("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);
}
 
Example 20
Source File: StarQueryTest.java    From rya with Apache License 2.0 2 votes vote down vote up
@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.createIRI("uri:hank");
    Value v2 = VF.createIRI("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);
    
    
    
}