Java Code Examples for org.eclipse.rdf4j.query.parser.sparql.SPARQLParser#parseQuery()
The following examples show how to use
org.eclipse.rdf4j.query.parser.sparql.SPARQLParser#parseQuery() .
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: QueryVariableNormalizerTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testSimpleVarGraph() throws Exception { SPARQLParser parser1 = new SPARQLParser(); SPARQLParser parser2 = new SPARQLParser(); ParsedQuery pq1 = parser1.parseQuery(q22, null); ParsedQuery pq2 = parser2.parseQuery(q23, null); List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr()); Assert.assertTrue(normalize.size() == 1); for (TupleExpr s : normalize) { Assert.assertTrue(tupleEquals(s, pq1.getTupleExpr())); } }
Example 2
Source File: QueryJoinOptimizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testSES2116JoinBind() throws Exception { StringBuilder qb = new StringBuilder(); qb.append("SELECT ?subject ?name ?row {\n" + " ?subject <http://localhost/table_1> ?uri .\n" + " BIND(STR(?uri) AS ?name)\n" + " ?table <http://linked.opendata.cz/ontology/odcs/tabular/hasRow> ?row .\n" + " ?table <http://linked.opendata.cz/ontology/odcs/tabular/symbolicName> ?name .\n" + "}"); SPARQLParser parser = new SPARQLParser(); ParsedQuery q = parser.parseQuery(qb.toString(), null); QueryJoinOptimizer opt = new QueryJoinOptimizer(); QueryRoot optRoot = new QueryRoot(q.getTupleExpr()); opt.optimize(optRoot, null, null); TupleExpr leaf = findLeaf(optRoot); Assert.assertTrue("Extension must be evaluated before StatementPattern", leaf.getParentNode() instanceof Extension); }
Example 3
Source File: FlattenedOptionalTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testBasicOptional() throws MalformedQueryException { String query = ""// + "SELECT ?e ?c ?l" // + "{" // + " ?e a ?c . "// + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + " OPTIONAL{?e <uri:talksTo> ?l } . "// + "}";// SPARQLParser parser = new SPARQLParser(); ParsedQuery pq = parser.parseQuery(query, null); List<TupleExpr> joinArgs = getJoinArgs(pq.getTupleExpr(), new ArrayList<TupleExpr>()); FlattenedOptional optional = (FlattenedOptional) joinArgs.get(0); TupleExpr sp1 = joinArgs.get(1); TupleExpr sp2 = joinArgs.get(2); Assert.assertEquals(false, optional.canRemoveTuple(sp2)); Assert.assertEquals(true, optional.canRemoveTuple(sp1)); // System.out.println(joinArgs); }
Example 4
Source File: QueryBenchmark.java From rya with Apache License 2.0 | 6 votes |
public void run() throws MalformedQueryException, QueryEvaluationException, NotEnoughResultsException, SailException { CloseableIteration<? extends BindingSet, QueryEvaluationException> it = null; try { // Execute the query. final SPARQLParser sparqlParser = new SPARQLParser(); final ParsedQuery parsedQuery = sparqlParser.parseQuery(sparql, null); it = sailConn.evaluate(parsedQuery.getTupleExpr(), null, null, false); // Perform the reads. if(numReads.isPresent()) { read(it, numReads.get() ); } else { readAll(it); } } finally { if(it != null) { it.close(); } } }
Example 5
Source File: PeriodicQueryUtilTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void testPeriodicNodeLocation() throws MalformedQueryException { String query = "prefix function: <http://org.apache.rya/function#> " //n + "prefix time: <http://www.w3.org/2006/time#> " //n + "prefix fn: <http://www.w3.org/2006/fn#> " //n + "select ?obs ?time ?lat where {" //n + "Filter(function:periodic(?time, 1,.5,time:hours)) " //n + "Filter(fn:test(?lat, 25)) " //n + "?obs <uri:hasTime> ?time. " //n + "?obs <uri:hasLattitude> ?lat }"; //n SPARQLParser parser = new SPARQLParser(); ParsedQuery pq = parser.parseQuery(query, null); TupleExpr te = pq.getTupleExpr(); te.visit(new PeriodicQueryNodeVisitor()); PeriodicNodeCollector collector = new PeriodicNodeCollector(); te.visit(collector); Assert.assertEquals(2, collector.getPos()); te.visit(new PeriodicQueryNodeRelocator()); collector.resetCount(); te.visit(collector); Assert.assertEquals(1, collector.getPos()); double window = 1*60*60*1000; double period = .5*3600*1000; PeriodicQueryNode node2 = new PeriodicQueryNode((long) window, (long) period, TimeUnit.MILLISECONDS, "time", new Join()); Assert.assertEquals(true, periodicNodesEqualIgnoreArg(node2, collector.getPeriodicQueryNode())); }
Example 6
Source File: GeneralizedExternalProcessorTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void testTwoIndexLargeQuery() throws Exception { SPARQLParser parser = new SPARQLParser(); ParsedQuery pq1 = parser.parseQuery(q15, null); ParsedQuery pq2 = parser.parseQuery(q7, null); ParsedQuery pq3 = parser.parseQuery(q12, null); System.out.println("Query is " + pq1.getTupleExpr()); SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet( new Projection(pq2.getTupleExpr())); SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet( new Projection(pq3.getTupleExpr())); List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); list.add(extTup2); list.add(extTup1); IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator( pq1.getTupleExpr(), list); List<ExternalTupleSet> indexSet = iep.getNormalizedIndices(); Assert.assertEquals(4, indexSet.size()); Set<TupleExpr> processedTups = Sets.newHashSet(iep.getIndexedTuples()); Assert.assertEquals(5, processedTups.size()); }
Example 7
Source File: PrecompJoinOptimizerTest2.java From rya with Apache License 2.0 | 5 votes |
@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 8
Source File: PCJNodeConsolidatorTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void testInvalidOrder() throws Exception { String query1 = ""// + "SELECT ?a ?b ?c ?d" // + "{" // + " ?a <uri:p5> <uri:const3>" // + " OPTIONAL{<uri:const2> <uri:p4> ?d } . "// + " ?a <uri:p4> ?b . "// + " OPTIONAL{?a <uri:p3> ?c} . " // + " OPTIONAL{<uri:const1> <uri:p2> ?b} . "// + " ?c <uri:p1> ?d " + "}";// String query2 = ""// + "SELECT ?a ?b ?c ?d" // + "{" // + " ?a <uri:p4> ?b . "// + " ?c <uri:p1> ?d " + " OPTIONAL{<uri:const2> <uri:p4> ?d } . "// + "}";// SPARQLParser parser = new SPARQLParser(); ParsedQuery pq1 = parser.parseQuery(query1, null); ParsedQuery pq2 = parser.parseQuery(query2, null); TupleExpr te1 = pq1.getTupleExpr(); TupleExpr te2 = pq2.getTupleExpr(); Join join1 = (Join) ((Projection) te1).getArg(); LeftJoin join2 = (LeftJoin) ((Projection) te2).getArg(); QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1); QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2); QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes()); Assert.assertTrue(!consolidator.consolidateNodes()); }
Example 9
Source File: StatementMetadataOptimizerTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void testMongoMatchQuery() throws MalformedQueryException { SPARQLParser parser = new SPARQLParser(); ParsedQuery pq = parser.parseQuery(query, null); TupleExpr te = pq.getTupleExpr(); System.out.println("Parametrized query is : " + te); mongoOptimizer.optimize(te, null, null); System.out.println("Result of optimization is : " + te); assertEquals(expected, StatementMetadataTestUtils.getMetadataNodes(te)); }
Example 10
Source File: ConstructGraphTest.java From rya with Apache License 2.0 | 4 votes |
@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 11
Source File: PrecompJoinOptimizerTest2.java From rya with Apache License 2.0 | 4 votes |
@Test public void testTwoIndexLargeQuery() throws Exception { final SPARQLParser parser1 = new SPARQLParser(); final SPARQLParser parser2 = new SPARQLParser(); final SPARQLParser parser3 = new SPARQLParser(); final ParsedQuery pq1 = parser1.parseQuery(q11, null); final ParsedQuery pq2 = parser2.parseQuery(q7, null); final ParsedQuery pq3 = parser3.parseQuery(q12, null); System.out.println("Query is " + pq1.getTupleExpr()); System.out.println("Indexes are " + pq2.getTupleExpr() + " and " + pq3.getTupleExpr()); final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet( (Projection) pq2.getTupleExpr()); final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet( (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)); }
Example 12
Source File: PrecompJoinOptimizerTest.java From rya with Apache License 2.0 | 4 votes |
@Test public void testThreeIndex2() throws Exception { final 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 . "// + "}";// final String q2 = ""// + "SELECT ?u ?s ?t " // + "{" // + " ?s a ?t ."// + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."// + " ?u <uri:talksTo> ?s . "// + "}";// final String q3 = ""// + "SELECT ?e ?c ?l " // + "{" // + " ?c a ?l ."// + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."// + " ?e <uri:talksTo> ?c . "// + "}";// final String q4 = ""// + "SELECT ?d ?f ?m " // + "{" // + " ?f a ?m ."// + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + " ?d <uri:talksTo> ?f . "// + "}";// final SPARQLParser parser = new SPARQLParser(); final ParsedQuery pq1 = parser.parseQuery(q1, null); final ParsedQuery pq2 = parser.parseQuery(q2, null); final ParsedQuery pq3 = parser.parseQuery(q3, null); final ParsedQuery pq4 = parser.parseQuery(q4, null); 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 List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); list.add(extTup1); final List<StatementPattern> spList = StatementPatternCollector .process(pq1.getTupleExpr()); final List<QueryModelNode> optTupNodes = Lists.newArrayList(); optTupNodes.add(extTup3); optTupNodes.add(spList.get(6)); optTupNodes.add(extTup2); provider.setIndices(list); final PCJOptimizer pcj = new PCJOptimizer(list, true, provider); final TupleExpr te = pq1.getTupleExpr(); pcj.optimize(te, null, null); final NodeCollector nc = new NodeCollector(); te.visit(nc); Assert.assertEquals(nc.qNodes, Sets.newHashSet(optTupNodes)); }
Example 13
Source File: PCJNodeConsolidatorTest.java From rya with Apache License 2.0 | 4 votes |
@Test public void testUpperLowerBoundOptional() throws Exception { String query1 = ""// + "SELECT ?a ?b ?c ?d" // + "{" // + " ?a <uri:p5> <uri:const3>" // + " OPTIONAL{<uri:const2> <uri:p4> ?d } . "// + " ?a <uri:p4> ?b . "// + " OPTIONAL{?a <uri:p3> ?c} . " // + " OPTIONAL{<uri:const1> <uri:p2> ?b} . "// + " ?c <uri:p1> ?d " + "}";// String query2 = ""// + "SELECT ?a ?b ?c ?d" // + "{" // + " ?a <uri:p4> ?b . "// + " OPTIONAL{<uri:const2> <uri:p4> ?d } . "// + " ?c <uri:p1> ?d " + "}";// SPARQLParser parser = new SPARQLParser(); ParsedQuery pq1 = parser.parseQuery(query1, null); ParsedQuery pq2 = parser.parseQuery(query2, null); TupleExpr te1 = pq1.getTupleExpr(); TupleExpr te2 = pq2.getTupleExpr(); Join join1 = (Join) ((Projection) te1).getArg(); Join join2 = (Join) ((Projection) te2).getArg(); QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1); QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2); QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes()); List<QueryModelNode> queryNodes = new ArrayList<>(seg1.getOrderedNodes()); QueryModelNode node = queryNodes.remove(0); queryNodes.add(1,node); node = queryNodes.remove(3); queryNodes.add(2, node); node = queryNodes.remove(4); queryNodes.add(2, node); Assert.assertTrue(consolidator.consolidateNodes()); Assert.assertEquals(consolidator.getQueryNodes(), queryNodes); }
Example 14
Source File: AccumuloIndexSetTest.java From rya with Apache License 2.0 | 4 votes |
@Test public void accumuloIndexSetTestWithTwoDirectProductBindingSetsWithMapping() throws RepositoryException, PcjException, TableNotFoundException, RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, AccumuloException, AccumuloSecurityException { // Load some Triples into Rya. final Set<Statement> triples = new HashSet<>(); triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(14))) ); triples.add( VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) ); triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(16))) ); triples.add( VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) ); triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(12))) ); triples.add( VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")) ); triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://hasAge"), VF.createLiteral(43)) ); triples.add( VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://playsSport"), VF.createLiteral("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 " + "{" + "FILTER(?age < 30) ." + "?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.absent()); final String sparql2 = "SELECT ?x ?y " + "{" + "FILTER(?y < 30) ." + "?x <http://hasAge> ?y." + "?x <http://playsSport> \"Soccer\" " + "}"; final SPARQLParser p = new SPARQLParser(); final ParsedQuery pq = p.parseQuery(sparql2, null); final Map<String,String> map = new HashMap<>(); map.put("x", "name"); map.put("y", "age"); final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName); ais.setProjectionExpr((Projection) pq.getTupleExpr()); ais.setTableVarMap(map); ais.setSupportedVariableOrderMap(Lists.newArrayList("x;y","y;x")); final QueryBindingSet bs = new QueryBindingSet(); bs.addBinding("birthDate",VF.createLiteral("1983-03-17",VF.createIRI("http://www.w3.org/2001/XMLSchema#date"))); bs.addBinding("x",VF.createIRI("http://Alice")); final QueryBindingSet bs2 = new QueryBindingSet(); bs2.addBinding("birthDate",VF.createLiteral("1983-04-18",VF.createIRI("http://www.w3.org/2001/XMLSchema#date"))); bs2.addBinding("x",VF.createIRI("http://Bob")); final Set<BindingSet> bSets = Sets.newHashSet(bs,bs2); final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets); final QueryBindingSet alice = new QueryBindingSet(); alice.addBinding("x", VF.createIRI("http://Alice")); alice.addBinding("y", VF.createLiteral(BigInteger.valueOf(14))); alice.addBinding("birthDate", VF.createLiteral("1983-03-17",VF.createIRI("http://www.w3.org/2001/XMLSchema#date"))); final QueryBindingSet bob = new QueryBindingSet(); bob.addBinding("x", VF.createIRI("http://Bob")); bob.addBinding("y", VF.createLiteral(BigInteger.valueOf(16))); bob.addBinding("birthDate", VF.createLiteral("1983-04-18",VF.createIRI("http://www.w3.org/2001/XMLSchema#date"))); final Set<BindingSet> fetchedResults = new HashSet<>(); while(results.hasNext()) { final BindingSet next = results.next(); System.out.println(next); fetchedResults.add(next); } Assert.assertEquals(Sets.<BindingSet>newHashSet(alice,bob), fetchedResults); }
Example 15
Source File: VarConstQueryVariableNormalizerTest.java From rya with Apache License 2.0 | 4 votes |
@Test public void queryConstNodeSixMatch() throws Exception { SPARQLParser parser1 = new SPARQLParser(); SPARQLParser parser2 = new SPARQLParser(); ParsedQuery pq1 = parser1.parseQuery(q9, null); ParsedQuery pq2 = parser2.parseQuery(q18, null); List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr()); Assert.assertEquals(6, normalize.size()); //System.out.println("tuple expr is " +pq1.getTupleExpr() + " and normalized tuples are " + normalize); for(TupleExpr te: normalize) { Assert.assertTrue(isTupleSubset(pq1.getTupleExpr(), te)); } }
Example 16
Source File: IndexPlanValidatorTest.java From rya with Apache License 2.0 | 4 votes |
@Test public void testEvaluateTwoIndexDiffVars2() throws Exception { final String indexSparqlString = ""// + "SELECT ?dog ?pig ?chicken " // + "{" // + " ?dog a ?chicken . "// + " ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "// + "}";// final String indexSparqlString2 = ""// + "SELECT ?fish ?ant ?turkey " // + "{" // + " ?fish <uri:talksTo> ?turkey . "// + " ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "// + "}";// final String queryString = ""// + "SELECT ?e ?c ?l ?o ?f ?g " // + "{" // + " ?e a ?c . "// + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "// + " ?e <uri:talksTo> ?o . "// + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "// + " ?f <uri:talksTo> ?g . " // + "}";// final SPARQLParser sp = new SPARQLParser(); final ParsedQuery index1 = sp.parseQuery(indexSparqlString, null); final ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null); final List<ExternalTupleSet> index = Lists.newArrayList(); final SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet( (Projection) index1.getTupleExpr()); final SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet( (Projection) index2.getTupleExpr()); index.add(ais1); index.add(ais2); final ParsedQuery pq = sp.parseQuery(queryString, null); final TupleExpr tup = pq.getTupleExpr().clone(); provider.setIndices(index); final PCJOptimizer pcj = new PCJOptimizer(index, false, provider); pcj.optimize(tup, null, null); final IndexPlanValidator ipv = new IndexPlanValidator(false); Assert.assertEquals(true, ipv.isValid(tup)); }
Example 17
Source File: JoinSegmentPCJMatcherTest.java From rya with Apache License 2.0 | 4 votes |
@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); }
Example 18
Source File: VarConstantIndexListPrunerTest.java From rya with Apache License 2.0 | 4 votes |
@Test public void testTwoIndexLargeQuery() throws Exception { SPARQLParser parser = new SPARQLParser(); ParsedQuery pq1 = parser.parseQuery(q11, null); ParsedQuery pq2 = parser.parseQuery(q7, null); ParsedQuery pq3 = parser.parseQuery(q12, null); ParsedQuery pq4 = parser.parseQuery(q13, null); ParsedQuery pq5 = parser.parseQuery(q8, null); ParsedQuery pq6 = parser.parseQuery(q14, null); System.out.println("Query is " + pq1.getTupleExpr()); SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet( (Projection) pq2.getTupleExpr()); SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet( (Projection) pq3.getTupleExpr()); SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet( (Projection) pq4.getTupleExpr()); SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet( (Projection) pq5.getTupleExpr()); SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet( (Projection) pq6.getTupleExpr()); List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); list.add(extTup1); list.add(extTup2); list.add(extTup3); list.add(extTup4); list.add(extTup5); VarConstantIndexListPruner vci = new VarConstantIndexListPruner( pq1.getTupleExpr()); List<ExternalTupleSet> processedIndexSet = vci.getRelevantIndices(list); System.out.println("Relevant indexes are: "); for (ExternalTupleSet e : processedIndexSet) { System.out.println(e); } Set<ExternalTupleSet> indexSet = Sets.newHashSet(); indexSet.add(extTup1); indexSet.add(extTup2); indexSet.add(extTup4); Assert.assertTrue(Sets.intersection(indexSet, Sets.newHashSet(processedIndexSet)) .equals(Sets.newHashSet(processedIndexSet))); }
Example 19
Source File: PrecompJoinOptimizerTest2.java From rya with Apache License 2.0 | 4 votes |
@Test public void testFourIndexGeoFreeCompareFilterMix() throws Exception { final SPARQLParser parser1 = new SPARQLParser(); final SPARQLParser parser2 = new SPARQLParser(); final SPARQLParser parser3 = new SPARQLParser(); final SPARQLParser parser4 = new SPARQLParser(); final SPARQLParser parser5 = new SPARQLParser(); final ParsedQuery pq1 = parser1.parseQuery(q27, null); final ParsedQuery pq2 = parser2.parseQuery(q23, null); final ParsedQuery pq3 = parser3.parseQuery(q26, null); final ParsedQuery pq4 = parser4.parseQuery(q24, null); final ParsedQuery pq5 = parser5.parseQuery(q28, null); System.out.println("Query is " + pq1.getTupleExpr()); System.out.println("Indexes are " + pq2.getTupleExpr() + " , " + pq3.getTupleExpr() + " , " + pq4.getTupleExpr() + " and " + pq5.getTupleExpr()); final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet( new Projection(pq2.getTupleExpr())); final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet( new Projection(pq3.getTupleExpr())); final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet( new Projection(pq4.getTupleExpr())); final SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet( new Projection(pq5.getTupleExpr())); final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); list.add(extTup4); list.add(extTup1); list.add(extTup2); list.add(extTup3); 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 20
Source File: PCJOptimizerTest.java From rya with Apache License 2.0 | 4 votes |
@Test public void testSegmentWithUnion() throws Exception { final String query1 = ""// + "SELECT ?e ?c ?l" // + "{" // + " {?e <uri:p1> <uri:o1>. } UNION { ?e a ?c. OPTIONAL {?e <uri:talksTo> ?l}. ?e <uri:p5> <uri:o4>. ?e <uri:p4> <uri:o3> } . "// + " ?e <uri:p2> ?c . "// + " ?e <uri:p3> <uri:o2> . "// + "}";// final String query2 = ""// + "SELECT ?a ?b ?m" // + "{" // + " ?a <uri:p5> <uri:o4> ." // + " ?a <uri:p4> <uri:o3> ." // + " OPTIONAL {?a <uri:talksTo> ?m} . "// + " ?a a ?b . "// + "}";// final String query3 = ""// + "SELECT ?h ?i" // + "{" // + " ?h <uri:p2> ?i . "// + " ?h <uri:p3> <uri:o2> . "// + "}";// final SPARQLParser parser = new SPARQLParser(); final ParsedQuery pq1 = parser.parseQuery(query1, null); final ParsedQuery pq2 = parser.parseQuery(query2, null); final ParsedQuery pq3 = parser.parseQuery(query3, null); final TupleExpr te1 = pq1.getTupleExpr(); final TupleExpr te2 = pq2.getTupleExpr(); final TupleExpr te3 = pq3.getTupleExpr(); final TupleExpr unOpt = te1.clone(); final List<QueryModelNode> remainingNodes = getNodes(te1); final Set<QueryModelNode> unMatchedNodes = new HashSet<>(); unMatchedNodes.add(remainingNodes.get(0)); final SimpleExternalTupleSet pcj1 = new SimpleExternalTupleSet((Projection) te2); final SimpleExternalTupleSet pcj2 = new SimpleExternalTupleSet((Projection) te3); final List<ExternalTupleSet> externalList = new ArrayList<>(); externalList.add(pcj1); externalList.add(pcj2); provider.setIndices(externalList); final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider); optimizer.optimize(te1, null, null); Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, unMatchedNodes)); }