org.eclipse.rdf4j.query.parser.sparql.SPARQLParser Java Examples
The following examples show how to use
org.eclipse.rdf4j.query.parser.sparql.SPARQLParser.
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: ConstructProjectionTest.java From rya with Apache License 2.0 | 6 votes |
@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 #2
Source File: SparqlQueryPigEngine.java From rya with Apache License 2.0 | 6 votes |
public String generatePigScript(final String sparql) throws Exception { Preconditions.checkNotNull(sparql, "Sparql query cannot be null"); final QueryParser parser = new SPARQLParser(); final ParsedQuery parsedQuery = parser.parseQuery(sparql, null); final QueryRoot tupleExpr = new QueryRoot(parsedQuery.getTupleExpr()); // SimilarVarJoinOptimizer similarVarJoinOptimizer = new SimilarVarJoinOptimizer(); // similarVarJoinOptimizer.optimize(tupleExpr, null, null); if (inference || stats) { if (inference) { tupleExpr.visit(new TransitivePropertyVisitor(conf, inferenceEngine)); tupleExpr.visit(new SymmetricPropertyVisitor(conf, inferenceEngine)); tupleExpr.visit(new InverseOfVisitor(conf, inferenceEngine)); } if (stats) { (new QueryJoinOptimizer(rdfCloudTripleStoreEvaluationStatistics)).optimize(tupleExpr, null, null); } } sparqlToPigTransformVisitor.meet(tupleExpr); return sparqlToPigTransformVisitor.getPigScript(); }
Example #3
Source File: SparqlToPigTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
public void testStatementPattern() throws Exception { String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + " PREFIX ub: <urn:lubm:rdfts#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + " SELECT * WHERE\n" + " {\n" + "\t?x rdf:type ub:UndergraduateStudent\n" + " }\n" + ""; QueryParser parser = new SPARQLParser(); ParsedQuery parsedQuery = parser.parseQuery(query, null); // System.out.println(parsedQuery); SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor(); visitor.setTablePrefix(tablePrefix); visitor.setInstance(instance); visitor.setZk(zk); visitor.setUser(user); visitor.setPassword(password); visitor.meet(new QueryRoot(parsedQuery.getTupleExpr())); // System.out.println(visitor.getPigScript()); }
Example #4
Source File: SparqlToPigTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
public void testStatementPatternContext() throws Exception { String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + " PREFIX ub: <urn:lubm:rdfts#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + " SELECT * WHERE\n" + " {\n" + " GRAPH ub:g1 {\n" + "\t?x rdf:type ub:UndergraduateStudent\n" + " }\n" + " }\n" + ""; QueryParser parser = new SPARQLParser(); ParsedQuery parsedQuery = parser.parseQuery(query, null); // System.out.println(parsedQuery); SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor(); visitor.setTablePrefix(tablePrefix); visitor.setInstance(instance); visitor.setZk(zk); visitor.setUser(user); visitor.setPassword(password); visitor.meet(new QueryRoot(parsedQuery.getTupleExpr())); // System.out.println(visitor.getPigScript()); }
Example #5
Source File: AntecedentVisitorTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testSelectQuery() throws Exception { String text = "PREFIX foaf: <" + FOAF.NAMESPACE + ">\n" + "SELECT * WHERE {\n" + " ?x a foaf:Person .\n" + " ?y a foaf:Person .\n" + " ?x foaf:knows ?y .\n" + "}"; ParsedQuery query = new SPARQLParser().parseQuery(text, null); AntecedentVisitor visitor = new AntecedentVisitor(); query.getTupleExpr().visit(visitor); Set<StatementPattern> expected = Sets.newHashSet( new StatementPattern(new Var("x"), c(RDF.TYPE), c(FOAF.PERSON)), new StatementPattern(new Var("y"), c(RDF.TYPE), c(FOAF.PERSON)), new StatementPattern(new Var("x"), c(FOAF.KNOWS), new Var("y"))); Assert.assertEquals(expected, visitor.getAntecedents()); }
Example #6
Source File: QueryVariableNormalizerTest.java From rya with Apache License 2.0 | 6 votes |
/** * @throws Exception * Tests QueryVariableNormalizerContext with two queries whose * StatementPattern nodes contain no constant Vars. */ @Test public void testNoConstants() throws Exception { SPARQLParser parser1 = new SPARQLParser(); SPARQLParser parser2 = new SPARQLParser(); ParsedQuery pq1 = parser1.parseQuery(q15, null); ParsedQuery pq2 = parser2.parseQuery(q16, null); List<TupleExpr> normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr()); Assert.assertTrue(normalize.size() == 2); for (TupleExpr s : normalize) { Assert.assertTrue(isTupleSubset(pq1.getTupleExpr(), s)); } pq1 = parser1.parseQuery(q16, null); pq2 = parser2.parseQuery(q17, null); normalize = QueryVariableNormalizer.getNormalizedIndex(pq1.getTupleExpr(), pq2.getTupleExpr()); Assert.assertTrue(normalize.size() == 0); }
Example #7
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 #8
Source File: SparqlToPigTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
public void testStatementPatternContextVar() throws Exception { String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + " PREFIX ub: <urn:lubm:rdfts#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + " SELECT * WHERE\n" + " {\n" + " GRAPH ?g {\n" + "\t?x rdf:type ub:UndergraduateStudent\n" + " }\n" + " ?x ub:pred ?g." + " }\n" + ""; QueryParser parser = new SPARQLParser(); ParsedQuery parsedQuery = parser.parseQuery(query, null); // System.out.println(parsedQuery); SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor(); visitor.setTablePrefix(tablePrefix); visitor.setInstance(instance); visitor.setZk(zk); visitor.setUser(user); visitor.setPassword(password); visitor.meet(new QueryRoot(parsedQuery.getTupleExpr())); // System.out.println(visitor.getPigScript()); }
Example #9
Source File: FlattenedOptionalTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testReOrderedBasicOptional() throws MalformedQueryException { String query = ""// + "SELECT ?e ?c ?l" // + "{" // + " ?e a ?c . "// + " OPTIONAL{?e <uri:talksTo> ?l } . "// + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + "}";// SPARQLParser parser = new SPARQLParser(); ParsedQuery pq = parser.parseQuery(query, null); System.out.println(pq.getTupleExpr()); List<TupleExpr> joinArgs = getJoinArgs(pq.getTupleExpr(), new ArrayList<TupleExpr>()); // System.out.println(joinArgs); FlattenedOptional optional = (FlattenedOptional) joinArgs.get(0); TupleExpr sp1 = joinArgs.get(1); TupleExpr sp2 = joinArgs.get(2); Assert.assertEquals(false, optional.canRemoveTuple(sp1)); Assert.assertEquals(false, optional.canAddTuple(sp2)); }
Example #10
Source File: SimpleExternalTupleSetTest.java From rya with Apache License 2.0 | 6 votes |
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()); }
Example #11
Source File: QueryVariableNormalizerTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testVarGraph() throws Exception { SPARQLParser parser1 = new SPARQLParser(); SPARQLParser parser2 = new SPARQLParser(); ParsedQuery pq1 = parser1.parseQuery(q19, null); ParsedQuery pq2 = parser2.parseQuery(q22, 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 #12
Source File: StatementMetadataExternalSetProviderTest.java From rya with Apache License 2.0 | 6 votes |
@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 #13
Source File: StatementMetadataExternalSetProviderTest.java From rya with Apache License 2.0 | 6 votes |
@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 #14
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 #15
Source File: ThreshholdPlanSelectorTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testCost2() throws Exception { String q1 = ""// + "SELECT ?f ?m ?d ?e ?l ?c " // + "{" // + " ?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 ."// + "}";// SPARQLParser parser = new SPARQLParser(); ParsedQuery pq1 = parser.parseQuery(q1, null); ThreshholdPlanSelector tps = new ThreshholdPlanSelector( pq1.getTupleExpr()); double cost = tps.getCost(pq1.getTupleExpr(), .4, .3, .3); Assert.assertEquals(.58, cost, .000000001); }
Example #16
Source File: ThreshholdPlanSelectorTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testCost1() throws Exception { String q1 = ""// + "SELECT ?f ?m ?d ?h ?i " // + "{" // + " ?f a ?m ."// + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + " ?d <uri:talksTo> ?f . "// + " ?d <uri:hangOutWith> ?f ." // + " ?f <uri:hangOutWith> ?h ." // + " ?f <uri:associatesWith> ?i ." // + " ?i <uri:associatesWith> ?h ." // + "}";// SPARQLParser parser = new SPARQLParser(); ParsedQuery pq1 = parser.parseQuery(q1, null); ThreshholdPlanSelector tps = new ThreshholdPlanSelector( pq1.getTupleExpr()); double cost = tps.getCost(pq1.getTupleExpr(), .6, .4, 0); Assert.assertEquals(.7, cost, .01); }
Example #17
Source File: QueryJoinOptimizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testSES2306AggregateOrderBy() throws Exception { String select = "PREFIX ex: <ex:>\n" + "SELECT ((MIN(?x+1) + MAX(?y-1))/2 AS ?r) {\n" + " ?this ex:name ?n . ?this ex:id ?id . ?this ex:prop1 ?x . ?this ex:prop2 ?y .\n" + "} GROUP BY concat(?n, ?id) HAVING (SUM(?x) + SUM(?y) < 5) ORDER BY (COUNT(?x) + COUNT(?y))"; SPARQLParser parser = new SPARQLParser(); ParsedQuery q = parser.parseQuery(select, null); q.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() { @Override protected void meetUnaryTupleOperator(UnaryTupleOperator node) throws Exception { assertNotEquals(node, node.getArg()); super.meetUnaryTupleOperator(node); } }); }
Example #18
Source File: ConstructProjectionTest.java From rya with Apache License 2.0 | 6 votes |
@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 #19
Source File: QueryVariableNormalizerTest.java From rya with Apache License 2.0 | 6 votes |
/** * @throws Exception * Tests QueryVariable normalizer on queries q9 and q18, where * q18 is obtained from q9 by reordering lines. */ @Test public void testOrderEq() 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.assertTrue(normalize.size() == 24); for (TupleExpr s : normalize) { Assert.assertTrue(isTupleSubset(s, pq1.getTupleExpr())&&isTupleSubset(pq1.getTupleExpr(),s)); } }
Example #20
Source File: StatementPatternMatcherTest.java From rya with Apache License 2.0 | 5 votes |
/** * Fetch the {@link StatementPattern} from a SPARQL string. * * @param sparql - A SPARQL query that contains only a single Statement Patern. (not nul) * @return The {@link StatementPattern} that was in the query, if it could be found. Otherwise {@code null} * @throws Exception The statement pattern could not be found in the parsed SPARQL query. */ public static @Nullable StatementPattern getSp(final String sparql) throws Exception { requireNonNull(sparql); final AtomicReference<StatementPattern> statementPattern = new AtomicReference<>(); final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null); parsed.getTupleExpr().visitChildren(new AbstractQueryModelVisitor<Exception>() { @Override public void meet(final StatementPattern node) throws Exception { statementPattern.set(node); } }); return statementPattern.get(); }
Example #21
Source File: MultiProjectionEvaluatorTest.java From rya with Apache License 2.0 | 5 votes |
/** * Get the first {@link MultiProjection} node from a SPARQL query. * * @param sparql - The query that contains a single Projection node. * @return The first {@link MultiProjection} that is encountered. * @throws Exception The query could not be parsed. */ public static @Nullable MultiProjection getMultiProjection(final String sparql) throws Exception { requireNonNull(sparql); final AtomicReference<MultiProjection> multiProjection = new AtomicReference<>(); final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null); parsed.getTupleExpr().visit(new AbstractQueryModelVisitor<Exception>() { @Override public void meet(final MultiProjection node) throws Exception { multiProjection.set(node); } }); return multiProjection.get(); }
Example #22
Source File: PrecompJoinOptimizerVarToConstTest.java From rya with Apache License 2.0 | 5 votes |
@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(); 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(1, 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 #23
Source File: BGPGroupGenerator.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
/** * Generate BGP groups from a SPARQL query * @param parsedQuery TupleExpr of the SPARQL query * @return DNFGrps Map of DNF groups */ public static List<List<StatementPattern>> generateBgpGroups(String strQuery) { SPARQLParser parser = new SPARQLParser(); ParsedQuery parsedQuery = parser.parseQuery(strQuery, null); return generateBgpGroups(parsedQuery); }
Example #24
Source File: PCJOptionalTestIT.java From rya with Apache License 2.0 | 5 votes |
@Test public void testSimpleOptionalTest1() throws Exception { final String query = ""// + "SELECT ?u ?s ?t " // + "{" // + " ?s a ?t ."// + " OPTIONAL{?t <http://www.w3.org/2000/01/rdf-schema#label> ?u } ."// + " ?u <uri:talksTo> ?s . "// + "}";// final SPARQLParser parser = new SPARQLParser(); final ParsedQuery pq1 = parser.parseQuery(query, null); final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet( (Projection) pq1.getTupleExpr().clone()); final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); list.add(extTup1); final List<QueryModelNode> optTupNodes = Lists.newArrayList(); optTupNodes.add(extTup1); final PCJOptimizer pcj = new PCJOptimizer(list, true, new AccumuloIndexSetProvider(new Configuration(), list)); final TupleExpr te = pq1.getTupleExpr(); pcj.optimize(te, null, null); final NodeCollector nc = new NodeCollector(); te.visit(nc); final List<QueryModelNode> qNodes = nc.getNodes(); Assert.assertEquals(qNodes.size(), optTupNodes.size()); for (final QueryModelNode node : qNodes) { Assert.assertTrue(optTupNodes.contains(node)); } }
Example #25
Source File: TupleExecutionPlanGeneratorTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void testTwoNodeOrder() { SPARQLParser parser = new SPARQLParser(); ParsedQuery pq1 = null; ParsedQuery pq2 = null; try { pq1 = parser.parseQuery(q7, null); pq2 = parser.parseQuery(q8, null); } catch (MalformedQueryException e) { // TODO Auto-generated catch block e.printStackTrace(); } Set<TupleExpr> tupSet = Sets.newHashSet(); tupSet.add(pq1.getTupleExpr()); TupleExecutionPlanGenerator tep = new TupleExecutionPlanGenerator(); Iterator<TupleExpr> processedTups = tep.getPlans(tupSet.iterator()); List<TupleExpr> processedTupList = Lists.newArrayList(); int size = 0; while(processedTups.hasNext()) { Assert.assertTrue(processedTups.hasNext()); processedTupList.add(processedTups.next()); size++; } Assert.assertEquals(2, size); Assert.assertEquals(pq1.getTupleExpr(), processedTupList.get(0)); Assert.assertEquals(pq2.getTupleExpr(), processedTupList.get(1)); }
Example #26
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 #27
Source File: AccumuloStatementMetadataNodeTest.java From rya with Apache License 2.0 | 5 votes |
/** * 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 #28
Source File: ListFluoQueries.java From rya with Apache License 2.0 | 5 votes |
private static String getPrettyPrintSparql(String sparql, int indent) throws Exception { SPARQLParser parser = new SPARQLParser(); ParsedQuery pq = parser.parseQuery(sparql, null); SPARQLQueryRenderer render = new SPARQLQueryRenderer(); String renderedQuery = render.render(pq); //remove extra quotes generated by query renderer String[] splitRender = renderedQuery.split("\"\"\""); StringBuilder builder = new StringBuilder(); for(String s: splitRender) { builder.append(s).append("\""); } builder.replace(builder.length() - 1, builder.length(), ""); //add indent to all lines following newline char String[] newLineRender = builder.toString().split("\n"); builder = new StringBuilder(); String prefix = getVariableIndent(indent); for(int i = 0; i < newLineRender.length; i++) { if(i != 0) { builder.append(prefix); } builder.append(newLineRender[i]).append("\n"); } return builder.toString(); }
Example #29
Source File: PrecompJoinOptimizerTest2.java From rya with Apache License 2.0 | 5 votes |
@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 #30
Source File: RdfTestUtil.java From rya with Apache License 2.0 | 5 votes |
/** * Fetch the {@link StatementPattern} from a SPARQL string. * * @param sparql - A SPARQL query that contains only a single Statement Pattern. (not null) * @return The {@link StatementPattern} that was in the query, if it could be found. Otherwise {@code null} * @throws Exception The statement pattern could not be found in the parsed SPARQL query. */ public static @Nullable StatementPattern getSp(final String sparql) throws Exception { requireNonNull(sparql); final AtomicReference<StatementPattern> statementPattern = new AtomicReference<>(); final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null); parsed.getTupleExpr().visitChildren(new AbstractQueryModelVisitor<Exception>() { @Override public void meet(final StatementPattern node) throws Exception { statementPattern.set(node); } }); return statementPattern.get(); }