Java Code Examples for org.eclipse.rdf4j.query.parser.QueryParserUtil#parseQuery()
The following examples show how to use
org.eclipse.rdf4j.query.parser.QueryParserUtil#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: StrictEvaluationStrategyTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Verifies if only those input bindings that actually occur in the query are returned in the result. See SES-2373. */ @Test public void testBindings() throws Exception { String query = "SELECT ?a ?b WHERE {}"; ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null); final ValueFactory vf = SimpleValueFactory.getInstance(); QueryBindingSet constants = new QueryBindingSet(); constants.addBinding("a", vf.createLiteral("foo")); constants.addBinding("b", vf.createLiteral("bar")); constants.addBinding("x", vf.createLiteral("X")); constants.addBinding("y", vf.createLiteral("Y")); CloseableIteration<BindingSet, QueryEvaluationException> result = strategy.evaluate(pq.getTupleExpr(), constants); assertNotNull(result); assertTrue(result.hasNext()); BindingSet bs = result.next(); assertTrue(bs.hasBinding("a")); assertTrue(bs.hasBinding("b")); assertFalse(bs.hasBinding("x")); assertFalse(bs.hasBinding("y")); }
Example 2
Source File: SpinParser.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private ParsedOperation parseText(Resource queryResource, IRI queryType, TripleSource store) throws RDF4JException { Value text = TripleSources.singleValue(queryResource, SP.TEXT_PROPERTY, store); if (text != null) { if (QUERY_TYPES.contains(queryType)) { return QueryParserUtil.parseQuery(QueryLanguage.SPARQL, text.stringValue(), null); } else if (UPDATE_TYPES.contains(queryType)) { return QueryParserUtil.parseUpdate(QueryLanguage.SPARQL, text.stringValue(), null); } else { throw new MalformedSpinException(String.format("Unrecognised command type: %s", queryType)); } } else { return null; } }
Example 3
Source File: ConstantOptimizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testAndOptimization() throws RDF4JException { String query = "prefix ex: <ex:>" + "select ?a ?b ?c\n" + "where {\n" + " bind((?a && ?b) as ?c) \n" + "}"; ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null); TupleExpr original = pq.getTupleExpr(); final AlgebraFinder finder = new AlgebraFinder(); original.visit(finder); assertTrue(finder.logicalAndfound); // reset for re-use on optimized query finder.reset(); QueryBindingSet constants = new QueryBindingSet(); constants.addBinding("a", BooleanLiteral.TRUE); constants.addBinding("b", BooleanLiteral.FALSE); EvaluationStrategy strategy = new StrictEvaluationStrategy(new EmptyTripleSource(), null); TupleExpr optimized = optimize(pq.getTupleExpr().clone(), constants, strategy); optimized.visit(finder); assertFalse("optimized query should no longer contain && operator", finder.logicalAndfound); CloseableIteration<BindingSet, QueryEvaluationException> result = strategy.evaluate(optimized, new EmptyBindingSet()); assertNotNull(result); assertTrue(result.hasNext()); BindingSet bindings = result.next(); assertTrue(bindings.hasBinding("a")); assertTrue(bindings.hasBinding("b")); assertTrue(bindings.hasBinding("c")); }
Example 4
Source File: ConstantOptimizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testFunctionOptimization() throws RDF4JException { String query = "prefix ex: <ex:>" + "select ?a ?b ?c \n " + "where {\n" + " bind(concat(?a, ?b) as ?c) \n" + "}"; ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null); EvaluationStrategy strategy = new StrictEvaluationStrategy(new EmptyTripleSource(), null); TupleExpr original = pq.getTupleExpr(); final AlgebraFinder finder = new AlgebraFinder(); original.visit(finder); assertTrue(finder.functionCallFound); // reset for re-use on optimized query finder.reset(); QueryBindingSet constants = new QueryBindingSet(); constants.addBinding("a", ValueFactoryImpl.getInstance().createLiteral("foo")); constants.addBinding("b", ValueFactoryImpl.getInstance().createLiteral("bar")); TupleExpr optimized = optimize(pq.getTupleExpr().clone(), constants, strategy); optimized.visit(finder); assertFalse("optimized query should no longer contain function call", finder.functionCallFound); CloseableIteration<BindingSet, QueryEvaluationException> result = strategy.evaluate(optimized, new EmptyBindingSet()); assertNotNull(result); assertTrue(result.hasNext()); BindingSet bindings = result.next(); assertTrue(bindings.hasBinding("a")); assertTrue(bindings.hasBinding("b")); assertTrue(bindings.hasBinding("c")); }
Example 5
Source File: QueryJoinOptimizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testOptimizer(String expectedQuery, String actualQuery) throws MalformedQueryException, UnsupportedQueryLanguageException { ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, actualQuery, null); QueryJoinOptimizer opt = new QueryJoinOptimizer(); QueryRoot optRoot = new QueryRoot(pq.getTupleExpr()); opt.optimize(optRoot, null, null); ParsedQuery expectedParsedQuery = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, expectedQuery, null); QueryRoot root = new QueryRoot(expectedParsedQuery.getTupleExpr()); assertQueryModelTrees(root, optRoot); }
Example 6
Source File: RegexAsStringFunctionOptimizierTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testOptimizer(String expectedQuery, String actualQuery) throws MalformedQueryException, UnsupportedQueryLanguageException { ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, actualQuery, null); QueryOptimizer opt = new RegexAsStringFunctionOptimizer(SimpleValueFactory.getInstance()); QueryRoot optRoot = new QueryRoot(pq.getTupleExpr()); opt.optimize(optRoot, null, null); ParsedQuery expectedParsedQuery = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, expectedQuery, null); QueryRoot root = new QueryRoot(expectedParsedQuery.getTupleExpr()); assertQueryModelTrees(root, optRoot); }
Example 7
Source File: RDFQueryLogParser.java From semagrow with Apache License 2.0 | 5 votes |
private TupleExpr parseQuery(Value literal, Model model) { if (literal instanceof Literal) { String queryString = ((Literal)literal).stringValue(); try { ParsedQuery query = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, queryString, "http://www.iamabsoluteuri.com"); return query.getTupleExpr(); } catch (MalformedQueryException e) { return null; } } return null; }
Example 8
Source File: StrictEvaluationStrategyTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testEvaluateRegexFlags() throws Exception { String query = "SELECT ?a WHERE { " + "VALUES ?a { \"foo.bar\" \"foo bar\" } \n" + "FILTER REGEX(str(?a), \"foo.bar\")}"; ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null); CloseableIteration<BindingSet, QueryEvaluationException> result = strategy.evaluate(pq.getTupleExpr(), new EmptyBindingSet()); List<BindingSet> bindingSets = QueryResults.asList(result); assertThat(bindingSets).hasSize(2); // match with q flag query = "SELECT ?a WHERE { " + "VALUES ?a { \"foo.bar\" \"foo bar\" } \n" + "FILTER REGEX(str(?a), \"foo.bar\", \"q\")}"; pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null); result = strategy.evaluate(pq.getTupleExpr(), new EmptyBindingSet()); bindingSets = QueryResults.asList(result); assertThat(bindingSets).hasSize(1); assertThat(bindingSets.get(0).getValue("a").stringValue()).isEqualTo("foo.bar"); // match with i and q flag query = "SELECT ?a WHERE { " + "VALUES ?a { \"foo.bar\" \"FOO.BAR\" \"foo bar\" } \n" + "FILTER REGEX(str(?a), \"foo.bar\", \"iq\")}"; pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null); result = strategy.evaluate(pq.getTupleExpr(), new EmptyBindingSet()); bindingSets = QueryResults.asList(result); assertThat(bindingSets).hasSize(2); List<String> values = bindingSets.stream().map(v -> v.getValue("a").stringValue()).collect(Collectors.toList()); assertThat(values).containsExactlyInAnyOrder("foo.bar", "FOO.BAR"); }