org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.helpers.StatementPatternCollector. 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: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
private static Set<QueryModelNode> getQNodes(QueryModelNode queryNode) {
    Set<QueryModelNode> rtns = new HashSet<QueryModelNode>();

    StatementPatternCollector spc = new StatementPatternCollector();
    queryNode.visit(spc);
    rtns.addAll(spc.getStatementPatterns());

    FilterCollector fvis = new FilterCollector();
    queryNode.visit(fvis);
    rtns.addAll(fvis.getFilters());

    ExternalTupleCollector eVis = new ExternalTupleCollector();
    queryNode.visit(eVis);
    rtns.addAll(eVis.getExtTup());

    return rtns;
}
 
Example #2
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
private static Set<QueryModelNode> getQNodes(String node, QueryModelNode queryNode) {

        if (node.equals("sp")) {
            Set<QueryModelNode> eSet = new HashSet<QueryModelNode>();
            StatementPatternCollector spc = new StatementPatternCollector();
            queryNode.visit(spc);
            List<StatementPattern> spList = spc.getStatementPatterns();
            eSet.addAll(spList);
            // returns empty set if list contains duplicate StatementPatterns
            if (spList.size() > eSet.size()) {
                return Sets.newHashSet();
            } else {
                return eSet;
            }
        } else if (node.equals("filter")) {

            FilterCollector fvis = new FilterCollector();
            queryNode.visit(fvis);

            return Sets.newHashSet(fvis.getFilters());
        } else {

            throw new IllegalArgumentException("Invalid node type.");
        }
    }
 
Example #3
Source File: AccumuloDocIdIndexer.java    From rya with Apache License 2.0 6 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> queryDocIndex(final String sparqlQuery,
        final Collection<BindingSet> constraints) throws TableNotFoundException, QueryEvaluationException {

    final SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = null;
    try {
        pq1 = parser.parseQuery(sparqlQuery, null);
    } catch (final MalformedQueryException e) {
        e.printStackTrace();
        throw new QueryEvaluationException("Malformed query. query=" + sparqlQuery, e);
    }

    final TupleExpr te1 = pq1.getTupleExpr();
    final List<StatementPattern> spList1 = StatementPatternCollector.process(te1);

    if(StarQuery.isValidStarQuery(spList1)) {
        final StarQuery sq1 = new StarQuery(spList1);
        return queryDocIndex(sq1, constraints);
    } else {
        throw new IllegalArgumentException("Invalid star query!");
    }

}
 
Example #4
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 #5
Source File: AccumuloStatementMetadataNodeTest.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Tests if results are filtered correctly using the metadata properties. In
 * this case, the date for the ingested RyaStatement differs from the date
 * specified in the query.
 * 
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithoutBindingSetInvalidProperty()
        throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    StatementMetadata metadata = new StatementMetadata();
    metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaType("Doug"));
    metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-15"));

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

    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());

    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(0, bsList.size());
    dao.delete(statement, conf);
}
 
Example #6
Source File: ConstructProjectionTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testConstructProjectionBNodes() throws MalformedQueryException {
    String query = "select ?o where { _:b <uri:talksTo> ?o }";
    
    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("o", VF.createIRI("uri:Bob"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
    BNode bNode = VF.createBNode();
    Map<String, BNode> bNodeMap = new HashMap<>();
    bNodeMap.put(VarNameUtils.prependAnonymous("1"), bNode);
    RyaStatement statement = projection.projectBindingSet(vBs,bNodeMap);
    
    RyaStatement expected = new RyaStatement(RdfToRyaConversions.convertResource(bNode), new RyaIRI("uri:talksTo"), new RyaIRI("uri:Bob"));
    expected.setTimestamp(statement.getTimestamp());
    expected.setColumnVisibility(new byte[0]);
    
    assertEquals(expected, statement);
}
 
Example #7
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 #8
Source File: StatementMetadataExternalSetProviderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void createSingleMongoMetadataNode() throws MalformedQueryException {

    MongoDBRdfConfiguration conf = (MongoDBRdfConfiguration) getConf(true);
    Set<RyaIRI> propertySet = new HashSet<>();
    propertySet.add(new RyaIRI("http://createdBy"));
    conf.setStatementMetadataProperties(propertySet);
    StatementMetadataExternalSetProvider metaProvider = new StatementMetadataExternalSetProvider(conf);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);

    List<QueryModelNode> patterns = new ArrayList<>();
    List<StatementMetadataNode<?>> expected = new ArrayList<>();
    Set<StatementPattern> sp = StatementMetadataTestUtils.getMetadataStatementPatterns(pq.getTupleExpr(), propertySet);

    patterns.addAll(StatementPatternCollector.process(pq.getTupleExpr()));
    JoinSegment<StatementMetadataNode<?>> segment = new JoinSegment<>(
            new HashSet<>(patterns), patterns, new HashMap<ValueExpr, Filter>());
    List<StatementMetadataNode<?>> extSets = metaProvider.getExternalSets(segment);

    expected.add(new StatementMetadataNode<>(sp,conf));

    Assert.assertEquals(expected, extSets);

}
 
Example #9
Source File: StatementMetadataExternalSetProviderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void createSingleAccumuloMetadataNode() throws MalformedQueryException {

    AccumuloRdfConfiguration conf = (AccumuloRdfConfiguration) getConf(false);
    Set<RyaIRI> propertySet = new HashSet<>();
    propertySet.add(new RyaIRI("http://createdBy"));
    conf.setStatementMetadataProperties(propertySet);
    StatementMetadataExternalSetProvider metaProvider = new StatementMetadataExternalSetProvider(
            conf);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);

    List<QueryModelNode> patterns = new ArrayList<>();
    List<StatementMetadataNode<?>> expected = new ArrayList<>();
    Set<StatementPattern> sp = StatementMetadataTestUtils.getMetadataStatementPatterns(pq.getTupleExpr(), propertySet);

    patterns.addAll(StatementPatternCollector.process(pq.getTupleExpr()));
    JoinSegment<StatementMetadataNode<?>> segment = new JoinSegment<>(
            new HashSet<>(patterns), patterns, new HashMap<ValueExpr, Filter>());
    List<StatementMetadataNode<?>> extSets = metaProvider.getExternalSets(segment);

    expected.add(new StatementMetadataNode<>(sp, conf));

    Assert.assertEquals(expected, extSets);

}
 
Example #10
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 #11
Source File: VOIDSourceSelector.java    From semagrow with Apache License 2.0 5 votes vote down vote up
@Loggable
public Collection<SourceMetadata> getSources(TupleExpr expr, Dataset dataset, BindingSet bindings) {

    if (expr instanceof StatementPattern)
        return getSources((StatementPattern)expr, dataset, bindings);

    List<StatementPattern> patterns = StatementPatternCollector.process(expr);
    return getSources(patterns, dataset, bindings);
}
 
Example #12
Source File: StatementMetadataExternalSetProviderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void createMultipleMetadataNode() throws MalformedQueryException {

    MongoDBRdfConfiguration conf = (MongoDBRdfConfiguration) getConf(true);
    Set<RyaIRI> propertySet = new HashSet<>();
    propertySet.add(new RyaIRI("http://createdBy"));
    propertySet.add(new RyaIRI("http://createdOn"));
    conf.setStatementMetadataProperties(propertySet);
    StatementMetadataExternalSetProvider metaProvider = new StatementMetadataExternalSetProvider(conf);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq2 = parser.parseQuery(query2, null);
    ParsedQuery pq3 = parser.parseQuery(query3, null);
    ParsedQuery pq1 = parser.parseQuery(query, null);

    List<QueryModelNode> patterns = new ArrayList<>();
    List<StatementMetadataNode<?>> expected = new ArrayList<>();
    Set<StatementPattern> sp1 = StatementMetadataTestUtils.getMetadataStatementPatterns(pq1.getTupleExpr(), propertySet);
    Set<StatementPattern> sp3 = StatementMetadataTestUtils.getMetadataStatementPatterns(pq3.getTupleExpr(), propertySet);
    //added extra blankNode into query3 to make blankNode names line up with query2.  Need to remove it now so that
    //StatementMetadataNode doesn't blow up because all subjects aren't the same.
    removePatternWithGivenSubject(VarNameUtils.prependAnonymous("1"), sp3);

    patterns.addAll(StatementPatternCollector.process(pq2.getTupleExpr()));
    JoinSegment<StatementMetadataNode<?>> segment = new JoinSegment<>(
            new HashSet<>(patterns), patterns, new HashMap<ValueExpr, Filter>());
    List<StatementMetadataNode<?>> extSets = metaProvider.getExternalSets(segment);

    expected.add(new StatementMetadataNode<>(sp1,conf));
    expected.add(new StatementMetadataNode<>(sp3,conf));

    Assert.assertEquals(expected, extSets);
}
 
Example #13
Source File: AskSourceSelector.java    From semagrow with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<SourceMetadata> getSources( TupleExpr expr, Dataset dataset, BindingSet bindings )
{
	if( expr instanceof StatementPattern ) {
		return getSources((StatementPattern)expr, dataset, bindings);
	}

	Collection<StatementPattern> patterns  = StatementPatternCollector.process( expr );
	return getSources( patterns, dataset, bindings );
}
 
Example #14
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 #15
Source File: AccumuloStatementMetadataNodeTest.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.
 * 
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetJoinOnProperty()
        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);
    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", VF.createLiteral("CoffeeShop"));
    bsConstraint.addBinding("y", VF.createLiteral("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);
}
 
Example #16
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 #17
Source File: MongoStatementMetadataNodeIT.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if results are filtered correctly using the metadata properties. In
 * this case, the date for the ingested RyaStatement differs from the date
 * specified in the query.
 */
@Test
public void simpleQueryWithoutBindingSetInvalidProperty() 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("Doug"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-15"));

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

        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 CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());

        final List<BindingSet> bsList = new ArrayList<>();
        while (iteration.hasNext()) {
            bsList.add(iteration.next());
        }
        Assert.assertEquals(0, bsList.size());
        dao.delete(statement, conf);
    } finally {
        dao.destroy();
    }
}
 
Example #18
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 #19
Source File: MongoStatementMetadataNodeIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleQueryWithoutBindingSet() 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 statement = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
                new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
        dao.add(statement);

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

        final StatementMetadataNode<?> node = new StatementMetadataNode<>(spList, conf);
        final CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());

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

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

        Assert.assertEquals(1, bsList.size());
        Assert.assertEquals(bs, bsList.get(0));
        dao.delete(statement, conf);
    } finally {
        dao.destroy();
    }
}
 
Example #20
Source File: PrecompJoinOptimizerVarToConstTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testFreeTestIndexFunction() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q22, null);
    final ParsedQuery pq2 = parser2.parseQuery(q24, null);

    System.out.println("Query is " + pq1.getTupleExpr());
    System.out.println("Index is " + pq2.getTupleExpr());

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    final PCJOptimizer pcj = new PCJOptimizer(list, false, new AccumuloIndexSetProvider(new Configuration(), list));
    pcj.optimize(tup, null, null);

    System.out.println("Processed query is " + tup);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));

    final Set<QueryModelNode> eTupSet =  PcjIntegrationTestingUtil.getTupleSets(tup);
    final Set<StatementPattern> set = Sets.newHashSet();

    Assert.assertEquals(2, eTupSet.size());

    for (final QueryModelNode s : eTupSet) {
        final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
        set.addAll(tempSet);

    }
    Assert.assertTrue(qSet.containsAll(set));
}
 
Example #21
Source File: PatternWiseSourceSelector.java    From semagrow with Apache License 2.0 5 votes vote down vote up
public Collection<SourceMetadata> getSources(TupleExpr expr, Dataset dataset, BindingSet bindingSet) {
    if (expr instanceof StatementPattern)
        return getSources((StatementPattern)expr, dataset, bindingSet);
    else {
        Collection<StatementPattern> patterns = StatementPatternCollector.process(expr);
        return patterns.stream().distinct()
                .flatMap(p -> getSources(p, dataset, bindingSet).stream())
                .collect(Collectors.toList());
    }
}
 
Example #22
Source File: AccumuloStatementMetadataNodeTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleQueryWithoutBindingSet()
        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 statement = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"),
            new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
    dao.add(statement);

    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    // RyaQueryEngine<RdfCloudTripleStoreConfiguration> engine =
    // (RyaQueryEngine<>) dao.getQueryEngine();

    StatementMetadataNode<?> node = new StatementMetadataNode<>(spList, conf);
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());

    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("x", VF.createLiteral("CoffeeShop"));
    bs.addBinding("y", VF.createLiteral("Joe"));

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

    Assert.assertEquals(1, bsList.size());
    Assert.assertEquals(bs, bsList.get(0));
    dao.delete(statement, conf);
}
 
Example #23
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testISNUMERIC() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q29, null);
    final ParsedQuery pq2 = parser2.parseQuery(q30, null);

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
            new Projection(pq2.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
            .process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 1);

}
 
Example #24
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testThreeIndexGeoFreeCompareFilterMix() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();
    final SPARQLParser parser3 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q25, null);
    final ParsedQuery pq2 = parser2.parseQuery(q24, null);
    final ParsedQuery pq3 = parser3.parseQuery(q26, null);

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

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup1);
    list.add(extTup2);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
            .process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 2);

}
 
Example #25
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testFreeTextIndexFunction() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q22, null);
    final ParsedQuery pq2 = parser2.parseQuery(q24, null);

    System.out.println("Query is " + pq1.getTupleExpr());
    System.out.println("Index is " + pq2.getTupleExpr());

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
            (Projection) pq2.getTupleExpr());

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
            .process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);

}
 
Example #26
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeoIndexFunction() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q21, null);
    final ParsedQuery pq2 = parser2.parseQuery(q23, null);

    System.out.println("Query is " + pq1.getTupleExpr());
    System.out.println("Index is " + pq2.getTupleExpr());

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
            new Projection(pq2.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
            .process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);

}
 
Example #27
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleIndexLargeQuery() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q8, null);
    final ParsedQuery pq2 = parser2.parseQuery(q7, null);

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
            new Projection(pq2.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
            .process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(set.equals(qSet));

}
 
Example #28
Source File: BGPGroupGenerator.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
public static List<List<StatementPattern>> generateBgpGroups(ParsedQuery parsedQuery)
{
	List<List<StatementPattern>> bgpGrps = new ArrayList<List<StatementPattern>>();
	TupleExpr query = parsedQuery.getTupleExpr();
	// collect all basic graph patterns
	for (TupleExpr bgp : BasicGraphPatternExtractor.process(query)) {
		List<StatementPattern> patterns = StatementPatternCollector.process(bgp);	
		bgpGrps.add(patterns);
	}
	
	return bgpGrps;
}
 
Example #29
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testVarRelableIndexSmaller() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(queryString, null);
    final ParsedQuery pq2 = parser2.parseQuery(indexSparqlString, null);

    System.out.println("Query is " + pq1.getTupleExpr());
    System.out.println("Index is " + pq2.getTupleExpr());

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
            new Projection(pq2.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets
            .newHashSet(StatementPatternCollector.process(pq1
                    .getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);
}
 
Example #30
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testVarRelableIndexSameSize() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q1, null);
    final ParsedQuery pq2 = parser2.parseQuery(q2, null);

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
            new Projection(pq2.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets
            .newHashSet(StatementPatternCollector.process(pq1
                    .getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(set.equals(qSet));

}