use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class PrecompJoinOptimizerTest method twoFourIndexWithFilterTest.
@Test
public void twoFourIndexWithFilterTest() throws Exception {
final String q1 = //
"" + //
"SELECT ?f ?m ?d ?e ?l ?c " + //
"{" + //
" Filter(?f > \"5\")." + //
" Filter(?e > \"5\")." + //
" ?f a ?m ." + //
" ?e a ?l ." + //
" ?d <uri:talksTo> ?f . " + //
" ?c <uri:talksTo> ?e . " + //
" ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + //
" ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ." + //
"}";
final String q2 = //
"" + //
"SELECT ?s ?t ?u " + //
"{" + //
" ?s a ?t ." + //
" ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + //
" ?u <uri:talksTo> ?s . " + //
"}";
final String q3 = //
"" + //
"SELECT ?s ?t ?u " + //
"{" + //
" Filter(?s > \"5\") ." + //
" ?s a ?t ." + //
" ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ." + //
" ?u <uri:talksTo> ?s . " + //
"}";
final String q4 = //
"" + //
"SELECT ?f ?m ?d " + //
"{" + //
" Filter(?f > \"5\") ." + //
" ?f a ?m ." + //
" ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ." + //
" ?d <uri:talksTo> ?f . " + //
"}";
final String q5 = //
"" + //
"SELECT ?e ?l ?c " + //
"{" + //
" Filter(?e > \"5\") ." + //
" ?e a ?l ." + //
" ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ." + //
" ?c <uri:talksTo> ?e . " + //
"}";
final SPARQLParser parser = new SPARQLParser();
ParsedQuery pq1 = null;
ParsedQuery pq2 = null;
ParsedQuery pq3 = null;
ParsedQuery pq4 = null;
ParsedQuery pq5 = 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);
} catch (final Exception e) {
e.printStackTrace();
}
final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
final SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
list.add(extTup2);
list.add(extTup1);
final List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
list2.add(extTup3);
list2.add(extTup4);
provider.setIndices(list);
final PCJOptimizer pcj = new PCJOptimizer(list, true, provider);
final TupleExpr te = pq1.getTupleExpr();
pcj.optimize(te, null, null);
System.out.println(te);
final NodeCollector nc = new NodeCollector();
te.visit(nc);
Assert.assertEquals(nc.qNodes.size(), list2.size());
for (final QueryModelNode e : nc.qNodes) {
Assert.assertTrue(list2.contains(e));
}
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class PrecompJoinOptimizerVarToConstTest method testFreeTestIndexFunction.
@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));
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class PrecompJoinOptimizerVarToConstTest method testContextFilterFourIndex.
@Test
public void testContextFilterFourIndex() throws Exception {
final SPARQLParser parser1 = new SPARQLParser();
final SPARQLParser parser3 = new SPARQLParser();
final SPARQLParser parser4 = new SPARQLParser();
final ParsedQuery pq1 = parser1.parseQuery(q19, null);
final ParsedQuery pq3 = parser3.parseQuery(q17, null);
final ParsedQuery pq4 = parser4.parseQuery(q18, null);
System.out.println("Query is " + pq1.getTupleExpr());
System.out.println("Indexes are " + pq3.getTupleExpr() + " , " + pq4.getTupleExpr());
final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
list.add(extTup3);
list.add(extTup2);
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);
Assert.assertEquals(2, eTupSet.size());
final Set<StatementPattern> set = Sets.newHashSet();
for (final QueryModelNode s : eTupSet) {
final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s).getTupleExpr()));
set.addAll(tempSet);
}
Assert.assertTrue(qSet.containsAll(set));
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class JoinSegmentPCJMatcherTest method testBasicSegment.
@Test
public void testBasicSegment() throws MalformedQueryException {
String query1 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" ?e a ?c . " + //
" ?e <uri:talksTo> ?l . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + //
"}";
String query2 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" ?e a ?c . " + //
" ?e <uri:talksTo> ?l . " + //
"}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq1 = parser.parseQuery(query1, null);
ParsedQuery pq2 = parser.parseQuery(query2, null);
TupleExpr te1 = pq1.getTupleExpr();
TupleExpr te2 = pq2.getTupleExpr();
Projection proj = (Projection) te1;
Join join = (Join) proj.getArg();
ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(join));
SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
Assert.assertEquals(true, jsm.match(pcj));
TupleExpr te = jsm.getQuery();
Assert.assertEquals(new HashSet<QueryModelNode>(), jsm.getUnmatchedArgNodes());
Set<QueryModelNode> qNodes = QueryNodeGatherer.getNodes(te);
List<QueryModelNode> nodes = jsm.getOrderedNodes();
Set<QueryModelNode> nodeSet = new HashSet<>();
nodeSet.add(nodes.get(0));
nodeSet.add(pcj);
Assert.assertEquals(nodeSet, new HashSet<QueryModelNode>(nodes));
Assert.assertEquals(nodeSet, qNodes);
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class AccumuloIndexSetTest method accumuloIndexSetTestWithTwoDirectProductBindingSetsWithConstantMapping.
@Test
public void accumuloIndexSetTestWithTwoDirectProductBindingSetsWithConstantMapping() throws Exception {
// Load some Triples into Rya.
final Set<Statement> triples = new HashSet<>();
triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
for (final Statement triple : triples) {
ryaConn.add(triple);
}
// Create a PCJ table will include those triples in its results.
final String sparql = "SELECT ?name ?age " + "{" + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
// Create and populate the PCJ table.
PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[] { "name", "age" }, Optional.<PcjVarOrderFactory>absent());
final String sparql2 = "SELECT ?x " + "{" + "?x <http://hasAge> 16 ." + "?x <http://playsSport> \"Soccer\" " + "}";
final SPARQLParser p = new SPARQLParser();
final ParsedQuery pq1 = p.parseQuery(sparql, null);
final ParsedQuery pq2 = p.parseQuery(sparql2, null);
final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName);
ais.setProjectionExpr((Projection) QueryVariableNormalizer.getNormalizedIndex(pq2.getTupleExpr(), pq1.getTupleExpr()).get(0));
final QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("birthDate", new LiteralImpl("1983-03-17", new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
bs.addBinding("x", new URIImpl("http://Alice"));
final QueryBindingSet bs2 = new QueryBindingSet();
bs2.addBinding("birthDate", new LiteralImpl("1983-04-18", new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
bs2.addBinding("x", new URIImpl("http://Bob"));
final Set<BindingSet> bSets = Sets.<BindingSet>newHashSet(bs, bs2);
final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);
final Set<BindingSet> fetchedResults = new HashSet<>();
while (results.hasNext()) {
final BindingSet next = results.next();
fetchedResults.add(next);
}
Assert.assertEquals(Sets.<BindingSet>newHashSet(bs2), fetchedResults);
}
Aggregations