Search in sources :

Example 6 with Projection

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

the class PrecompJoinOptimizerTest2 method testBindingSetAssignment2.

@Test
public void testBindingSetAssignment2() throws Exception {
    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(q41, null);
    final ParsedQuery pq2 = parser.parseQuery(q42, null);
    final ParsedQuery pq3 = parser.parseQuery(q43, 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.assertEquals(2, eTupSet.size());
    Assert.assertEquals(qSet, set);
    final BindingSetAssignmentCollector bsac1 = new BindingSetAssignmentCollector();
    final BindingSetAssignmentCollector bsac2 = new BindingSetAssignmentCollector();
    pq1.getTupleExpr().visit(bsac1);
    tup.visit(bsac2);
    Assert.assertEquals(bsac1.getBindingSetAssignments(), bsac2.getBindingSetAssignments());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) Projection(org.openrdf.query.algebra.Projection) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) TupleExpr(org.openrdf.query.algebra.TupleExpr) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) BindingSetAssignmentCollector(org.apache.rya.indexing.external.PcjIntegrationTestingUtil.BindingSetAssignmentCollector) StatementPattern(org.openrdf.query.algebra.StatementPattern) SimpleExternalTupleSet(org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) Test(org.junit.Test)

Example 7 with Projection

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

the class ParsedQueryUtilTest method findProjection_projectionIsTopNode.

@Test
public void findProjection_projectionIsTopNode() throws MalformedQueryException {
    // A SPARQL query that will result in the Projection node being the top node.
    String sparql = "SELECT ?a ?b " + "WHERE {" + "?a <http://talksTo> ?b" + "}";
    // Run the test.
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery query = parser.parseQuery(sparql, null);
    Optional<Projection> projection = new ParsedQueryUtil().findProjection(query);
    assertTrue(projection.isPresent());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) Test(org.junit.Test)

Example 8 with Projection

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

the class SimpleExternalTupleSetTest method getSupportedVariableOrderMap.

@Test
public void getSupportedVariableOrderMap() throws MalformedQueryException {
    // Create the PCJ expression.
    final String sparql = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:associatesWith> ?d . " + "}";
    final ParsedQuery query = new SPARQLParser().parseQuery(sparql, null);
    final Projection pcjExpression = (Projection) query.getTupleExpr();
    // Create the object that is being tested.
    final SimpleExternalTupleSet testSet = new SimpleExternalTupleSet(pcjExpression);
    // Verify the correct Supported Variable Order Map is created.
    final Map<String, Set<String>> expected = new HashMap<>();
    String varOrder = "f";
    Set<String> vars = new HashSet<>();
    vars.add("f");
    expected.put(varOrder, vars);
    varOrder = "f;m";
    vars = new HashSet<>();
    vars.add("f");
    vars.add("m");
    expected.put(varOrder, vars);
    varOrder = "f;m;d";
    vars = new HashSet<>();
    vars.add("f");
    vars.add("m");
    vars.add("d");
    expected.put(varOrder, vars);
    assertEquals(expected, testSet.getSupportedVariableOrderMap());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) HashSet(java.util.HashSet) Set(java.util.Set) ParsedQuery(org.openrdf.query.parser.ParsedQuery) HashMap(java.util.HashMap) Projection(org.openrdf.query.algebra.Projection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with Projection

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

the class SimpleExternalTupleSetTest method hashCode_notSame.

public void hashCode_notSame() throws MalformedQueryException {
    // Create the first SimpleExternalTupleSet object.
    final String sparql1 = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:associatesWith> ?d . " + "}";
    final ParsedQuery query1 = new SPARQLParser().parseQuery(sparql1, null);
    final Projection pcjExpression1 = (Projection) query1.getTupleExpr();
    final SimpleExternalTupleSet set1 = new SimpleExternalTupleSet(pcjExpression1);
    // Create another one using a different expression.
    final String sparql2 = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:friendsWith> ?d . " + "}";
    final ParsedQuery query2 = new SPARQLParser().parseQuery(sparql2, null);
    final Projection pcjExpression2 = (Projection) query2.getTupleExpr();
    final SimpleExternalTupleSet set2 = new SimpleExternalTupleSet(pcjExpression2);
    // Show they are not equal.
    assertNotEquals(set1.hashCode(), set2.hashCode());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection)

Example 10 with Projection

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

the class IndexWritingTool method setVarOrders.

public void setVarOrders(final String s, final Configuration conf) throws MalformedQueryException {
    final SPARQLParser parser = new SPARQLParser();
    final TupleExpr query = parser.parseQuery(s, null).getTupleExpr();
    final List<String> projList = Lists.newArrayList(((Projection) query).getProjectionElemList().getTargetNames());
    final String projElems = Joiner.on(";").join(projList);
    conf.set("projElems", projElems);
    final Pattern splitPattern1 = Pattern.compile("\n");
    final Pattern splitPattern2 = Pattern.compile(",");
    final String[] lines = splitPattern1.split(s);
    final List<String> varOrders = Lists.newArrayList();
    final List<String> varOrderPos = Lists.newArrayList();
    int orderNum = 0;
    final int projSizeSq = projList.size() * projList.size();
    for (String t : lines) {
        if (orderNum > projSizeSq) {
            break;
        }
        String[] order = null;
        if (t.startsWith("#prefix")) {
            t = t.substring(7).trim();
            order = splitPattern2.split(t, projList.size());
        }
        String tempVarOrder = "";
        String tempVarOrderPos = "";
        if (order != null) {
            for (final String u : order) {
                if (tempVarOrder.length() == 0) {
                    tempVarOrder = u.trim();
                } else {
                    tempVarOrder = tempVarOrder + ";" + u.trim();
                }
                final int pos = projList.indexOf(u.trim());
                if (pos < 0) {
                    throw new IllegalArgumentException("Invalid variable order!");
                } else {
                    if (tempVarOrderPos.length() == 0) {
                        tempVarOrderPos = tempVarOrderPos + pos;
                    } else {
                        tempVarOrderPos = tempVarOrderPos + ";" + pos;
                    }
                }
            }
            varOrders.add(tempVarOrder);
            varOrderPos.add(tempVarOrderPos);
        }
        if (tempVarOrder.length() > 0) {
            orderNum++;
        }
    }
    if (orderNum == 0) {
        varOrders.add(projElems);
        String tempVarPos = "";
        for (int i = 0; i < projList.size(); i++) {
            if (i == 0) {
                tempVarPos = Integer.toString(0);
            } else {
                tempVarPos = tempVarPos + ";" + i;
            }
        }
        varOrderPos.add(tempVarPos);
    }
    final String[] vOrders = varOrders.toArray(new String[varOrders.size()]);
    final String[] vOrderPos = varOrderPos.toArray(new String[varOrderPos.size()]);
    conf.setStrings("varOrders", vOrders);
    conf.setStrings("varOrderPos", vOrderPos);
}
Also used : Pattern(java.util.regex.Pattern) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) Projection(org.openrdf.query.algebra.Projection) TupleExpr(org.openrdf.query.algebra.TupleExpr)

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