Search in sources :

Example 11 with Projection

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

the class ValidIndexCombinationGeneratorTest method medQueryEightOverlapIndex.

@Test
public void medQueryEightOverlapIndex() {
    String q1 = // 
    "" + // 
    "SELECT ?f ?m ?d " + // 
    "{" + // 
    "  ?f a ?m ." + // 
    "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + // 
    "  ?d <uri:talksTo> ?f . " + // 
    "  ?f <uri:hangOutWith> ?m ." + // 
    "  ?m <uri:hangOutWith> ?d ." + // 
    "  ?f <uri:associatesWith> ?m ." + // 
    "  ?m <uri:associatesWith> ?d ." + // 
    "}";
    String q2 = // 
    "" + // 
    "SELECT ?t ?s ?u " + // 
    "{" + // 
    "  ?s a ?t ." + // 
    "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + // 
    "  ?u <uri:talksTo> ?s . " + // 
    "}";
    String q3 = // 
    "" + // 
    "SELECT ?s ?t ?u " + // 
    "{" + // 
    "  ?s <uri:hangOutWith> ?t ." + // 
    "  ?t <uri:hangOutWith> ?u ." + // 
    "}";
    String q4 = // 
    "" + // 
    "SELECT ?s ?t ?u " + // 
    "{" + // 
    "  ?s <uri:associatesWith> ?t ." + // 
    "  ?t <uri:associatesWith> ?u ." + // 
    "}";
    String q5 = // 
    "" + // 
    "SELECT ?t ?s ?u " + // 
    "{" + // 
    "  ?s a ?t ." + // 
    "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + // 
    "  ?u <uri:talksTo> ?s . " + // 
    "  ?s <uri:hangOutWith> ?t ." + // 
    "  ?t <uri:hangOutWith> ?u ." + // 
    "}";
    String q6 = // 
    "" + // 
    "SELECT ?s ?t ?u " + // 
    "{" + // 
    "  ?s <uri:associatesWith> ?t ." + // 
    "  ?t <uri:associatesWith> ?u ." + // 
    "  ?s <uri:hangOutWith> ?t ." + // 
    "  ?t <uri:hangOutWith> ?u ." + // 
    "}";
    String q7 = // 
    "" + // 
    "SELECT ?s ?t ?u " + // 
    "{" + // 
    "  ?s <uri:associatesWith> ?t ." + // 
    "  ?t <uri:associatesWith> ?u ." + // 
    "  ?t <uri:hangOutWith> ?u ." + // 
    "}";
    String q8 = // 
    "" + // 
    "SELECT ?t ?s ?u " + // 
    "{" + // 
    "  ?s a ?t ." + // 
    "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + // 
    "  ?u <uri:talksTo> ?s . " + // 
    "  ?s <uri:associatesWith> ?t ." + // 
    "}";
    String q9 = // 
    "" + // 
    "SELECT ?t ?s ?u " + // 
    "{" + // 
    "  ?s a ?t ." + // 
    "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + // 
    "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = null;
    ParsedQuery pq2 = null;
    ParsedQuery pq3 = null;
    ParsedQuery pq4 = null;
    ParsedQuery pq5 = null;
    ParsedQuery pq6 = null;
    ParsedQuery pq7 = null;
    ParsedQuery pq8 = null;
    ParsedQuery pq9 = null;
    SimpleExternalTupleSet extTup1 = null;
    SimpleExternalTupleSet extTup2 = null;
    SimpleExternalTupleSet extTup3 = null;
    SimpleExternalTupleSet extTup4 = null;
    SimpleExternalTupleSet extTup5 = null;
    SimpleExternalTupleSet extTup6 = null;
    SimpleExternalTupleSet extTup7 = null;
    SimpleExternalTupleSet extTup8 = null;
    try {
        pq1 = parser.parseQuery(q1, null);
        pq2 = parser.parseQuery(q2, null);
        pq3 = parser.parseQuery(q3, null);
        pq4 = parser.parseQuery(q4, null);
        pq5 = parser.parseQuery(q5, null);
        pq6 = parser.parseQuery(q6, null);
        pq7 = parser.parseQuery(q7, null);
        pq8 = parser.parseQuery(q8, null);
        pq9 = parser.parseQuery(q9, null);
        extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
        extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
        extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
        extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
        extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
        extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
        extTup7 = new SimpleExternalTupleSet((Projection) pq8.getTupleExpr());
        extTup8 = new SimpleExternalTupleSet((Projection) pq9.getTupleExpr());
    } catch (MalformedQueryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    List<ExternalTupleSet> indexList = Lists.newArrayList();
    indexList.add(extTup1);
    indexList.add(extTup2);
    indexList.add(extTup3);
    indexList.add(extTup4);
    indexList.add(extTup5);
    indexList.add(extTup6);
    indexList.add(extTup7);
    indexList.add(extTup8);
    ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(pq1.getTupleExpr());
    Iterator<List<ExternalTupleSet>> combos = vic.getValidIndexCombos(indexList);
    int size = 0;
    while (combos.hasNext()) {
        combos.hasNext();
        size++;
        combos.next();
        combos.hasNext();
    }
    Assert.assertTrue(!combos.hasNext());
    Assert.assertEquals(21, size);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) MalformedQueryException(org.openrdf.query.MalformedQueryException) List(java.util.List) ArrayList(java.util.ArrayList) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) Test(org.junit.Test)

Example 12 with Projection

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

the class TopOfQueryFilterRelocator method moveFiltersToTop.

/**
 * This method moves the Filters of a specified {@link TupleExpr} to the top
 * of the TupleExpr.
 *
 * @param query
 *            - query whose filters will be relocated
 * @return - TupleExpr with filters relocated to top
 */
public static TupleExpr moveFiltersToTop(TupleExpr query) {
    ProjectionAndFilterGatherer fg = new ProjectionAndFilterGatherer();
    query.visit(fg);
    List<ValueExpr> filterCond = new ArrayList<>(fg.filterCond);
    Projection projection = fg.projection;
    if (filterCond.size() == 0) {
        return query;
    }
    Filter first = new Filter();
    first.setCondition(filterCond.remove(0));
    Filter current = first;
    for (ValueExpr cond : filterCond) {
        Filter filter = new Filter(null, cond);
        current.setArg(filter);
        current = filter;
    }
    TupleExpr te = projection.getArg();
    projection.setArg(first);
    current.setArg(te);
    return query;
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) Filter(org.openrdf.query.algebra.Filter) ArrayList(java.util.ArrayList) Projection(org.openrdf.query.algebra.Projection) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 13 with Projection

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

the class TopologyFactoryTest method projectionStatementPattern.

@Test
public void projectionStatementPattern() throws Exception {
    final String query = "SELECT * WHERE { " + "?person <urn:talksTo> ?otherPerson . " + "}";
    FACTORY.build(query, "source", "sink", new RandomUUIDFactory());
    final List<ProcessorEntry> entries = FACTORY.getProcessorEntry();
    assertTrue(entries.get(0).getNode() instanceof Projection);
    assertTrue(entries.get(1).getNode() instanceof StatementPattern);
    final StatementPattern expected = new StatementPattern(new Var("person"), TALKS_TO, new Var("otherPerson"));
    assertEquals(expected, entries.get(1).getNode());
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) Var(org.openrdf.query.algebra.Var) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) Projection(org.openrdf.query.algebra.Projection) ProcessorEntry(org.apache.rya.streams.kafka.topology.TopologyFactory.ProcessorEntry) Test(org.junit.Test)

Example 14 with Projection

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

the class TopologyFactoryTest method projectionJoinJoinStatementPattern.

@Test
public void projectionJoinJoinStatementPattern() throws Exception {
    final String query = "SELECT * WHERE { " + "?person <urn:talksTo> ?otherPerson . " + "?otherPerson <urn:talksTo> ?dog . " + "?dog <urn:chews> ?toy . " + "}";
    FACTORY.build(query, "source", "sink", new RandomUUIDFactory());
    final List<ProcessorEntry> entries = FACTORY.getProcessorEntry();
    assertTrue(entries.get(0).getNode() instanceof Projection);
    assertTrue(entries.get(1).getNode() instanceof Join);
    assertTrue(entries.get(2).getNode() instanceof Join);
    StatementPattern expected = new StatementPattern(new Var("person"), TALKS_TO, new Var("otherPerson"));
    assertEquals(expected, entries.get(3).getNode());
    expected = new StatementPattern(new Var("otherPerson"), TALKS_TO, new Var("dog"));
    assertEquals(expected, entries.get(4).getNode());
    expected = new StatementPattern(new Var("dog"), CHEWS, new Var("toy"));
    assertEquals(expected, entries.get(5).getNode());
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) Var(org.openrdf.query.algebra.Var) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) ProcessorEntry(org.apache.rya.streams.kafka.topology.TopologyFactory.ProcessorEntry) Test(org.junit.Test)

Example 15 with Projection

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

the class TopologyFactoryTest method projectionStatementPattern_rebind.

@Test
public void projectionStatementPattern_rebind() throws Exception {
    final String query = "CONSTRUCT { ?person <urn:mightKnow> ?otherPerson } WHERE { " + "?person <urn:talksTo> ?otherPerson . " + "}";
    FACTORY.build(query, "source", "sink", new RandomUUIDFactory());
    final List<ProcessorEntry> entries = FACTORY.getProcessorEntry();
    assertTrue(entries.get(0).getNode() instanceof Projection);
    final StatementPattern expected = new StatementPattern(new Var("person"), TALKS_TO, new Var("otherPerson"));
    assertEquals(expected, entries.get(1).getNode());
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) Var(org.openrdf.query.algebra.Var) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) Projection(org.openrdf.query.algebra.Projection) ProcessorEntry(org.apache.rya.streams.kafka.topology.TopologyFactory.ProcessorEntry) Test(org.junit.Test)

Aggregations

Projection (org.openrdf.query.algebra.Projection)76 Test (org.junit.Test)64 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)37 ParsedQuery (org.openrdf.query.parser.ParsedQuery)34 TupleExpr (org.openrdf.query.algebra.TupleExpr)33 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)27 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)27 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)24 Var (org.openrdf.query.algebra.Var)24 ArrayList (java.util.ArrayList)23 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)23 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)22 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)17 Join (org.openrdf.query.algebra.Join)15 HashSet (java.util.HashSet)14 Union (org.openrdf.query.algebra.Union)10 Resource (org.openrdf.model.Resource)9 MultiProjection (org.openrdf.query.algebra.MultiProjection)9 HashMap (java.util.HashMap)8