org.eclipse.rdf4j.query.parser.QueryParserUtil Java Examples
The following examples show how to use
org.eclipse.rdf4j.query.parser.QueryParserUtil.
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: SPARQLConnection.java From semagrow with Apache License 2.0 | 6 votes |
@Override public Query prepareQuery(QueryLanguage ql, String query, String base) throws RepositoryException, MalformedQueryException { if (QueryLanguage.SPARQL.equals(ql)) { String strippedQuery = QueryParserUtil.removeSPARQLQueryProlog(query).toUpperCase(); if (strippedQuery.startsWith("SELECT")) { return prepareTupleQuery(ql, query, base); } else if (strippedQuery.startsWith("ASK")) { return prepareBooleanQuery(ql, query, base); } else { return prepareGraphQuery(ql, query, base); } } throw new UnsupportedOperationException("Unsupported query language " + ql); }
Example #2
Source File: HBaseSailTest.java From Halyard with Apache License 2.0 | 6 votes |
@Test public void testCardinalityCalculator() throws Exception { HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), "cardinalitytable", true, 0, true, 0, null, null); sail.initialize(); SimpleValueFactory f = SimpleValueFactory.getInstance(); TupleExpr q1 = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, "select * where {?s a ?o}", "http://whatever/").getTupleExpr(); TupleExpr q2 = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, "select * where {graph <http://whatevercontext> {?s a ?o}}", "http://whatever/").getTupleExpr(); TupleExpr q3 = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, "select * where {?s <http://whatever/> ?o}", "http://whatever/").getTupleExpr(); TupleExpr q4 = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, "select * where {?s ?p \"whatever\"^^<" + HALYARD.SEARCH_TYPE.stringValue() + ">}", "http://whatever/").getTupleExpr(); assertEquals(100.0, sail.statistics.getCardinality(q1), 0.01); assertEquals(100.0, sail.statistics.getCardinality(q2), 0.01); assertEquals(100.0, sail.statistics.getCardinality(q3), 0.01); assertEquals(0.0001, sail.statistics.getCardinality(q4), 0.00001); sail.addStatement(HALYARD.STATS_ROOT_NODE, VOID.TRIPLES, f.createLiteral(10000l), HALYARD.STATS_GRAPH_CONTEXT); sail.addStatement(f.createIRI(HALYARD.STATS_ROOT_NODE.stringValue() + "_property_" + HalyardTableUtils.encode(HalyardTableUtils.hashKey(RDF.TYPE))), VOID.TRIPLES, f.createLiteral(5000l), HALYARD.STATS_GRAPH_CONTEXT); sail.addStatement(f.createIRI("http://whatevercontext"), VOID.TRIPLES, f.createLiteral(10000l), HALYARD.STATS_GRAPH_CONTEXT); sail.addStatement(f.createIRI("http://whatevercontext_property_" + HalyardTableUtils.encode(HalyardTableUtils.hashKey(RDF.TYPE))), VOID.TRIPLES, f.createLiteral(20l), HALYARD.STATS_GRAPH_CONTEXT); sail.commit(); assertEquals(5000.0, sail.statistics.getCardinality(q1), 0.01); assertEquals(20.0, sail.statistics.getCardinality(q2), 0.01); assertEquals(100.0, sail.statistics.getCardinality(q3), 0.01); assertEquals(0.0001, sail.statistics.getCardinality(q4), 0.00001); sail.shutDown(); }
Example #3
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 #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: 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 #6
Source File: ParallelSplitFunction.java From Halyard with Apache License 2.0 | 5 votes |
public static int getNumberOfForksFromFunctionArgument(String query, boolean sparqlUpdate, int stage) throws IllegalArgumentException{ ParallelSplitFunctionVisitor psfv = new ParallelSplitFunctionVisitor(); if (sparqlUpdate) { List<UpdateExpr> exprs = QueryParserUtil.parseUpdate(QueryLanguage.SPARQL, query, null).getUpdateExprs(); if (exprs.size() > stage) { exprs.get(stage).visit(psfv); } } else { QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null).getTupleExpr().visit(psfv); } return psfv.forks; }
Example #7
Source File: SPARQLConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public Query prepareQuery(QueryLanguage ql, String query, String base) throws RepositoryException, MalformedQueryException { if (SPARQL.equals(ql)) { String strippedQuery = QueryParserUtil.removeSPARQLQueryProlog(query).toUpperCase(); if (strippedQuery.startsWith("SELECT")) { return prepareTupleQuery(ql, query, base); } else if (strippedQuery.startsWith("ASK")) { return prepareBooleanQuery(ql, query, base); } else { return prepareGraphQuery(ql, query, base); } } throw new UnsupportedOperationException("Unsupported query language " + ql); }
Example #8
Source File: SailRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public SailBooleanQuery prepareBooleanQuery(QueryLanguage ql, String queryString, String baseURI) throws MalformedQueryException { Optional<TupleExpr> sailTupleExpr = sailConnection.prepareQuery(ql, Query.QueryType.BOOLEAN, queryString, baseURI); ParsedBooleanQuery parsedQuery = sailTupleExpr .map(expr -> new ParsedBooleanQuery(queryString, expr)) .orElse(QueryParserUtil.parseBooleanQuery(ql, queryString, baseURI)); return new SailBooleanQuery(parsedQuery, this); }
Example #9
Source File: SailRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public SailGraphQuery prepareGraphQuery(QueryLanguage ql, String queryString, String baseURI) throws MalformedQueryException { Optional<TupleExpr> sailTupleExpr = sailConnection.prepareQuery(ql, Query.QueryType.GRAPH, queryString, baseURI); ParsedGraphQuery parsedQuery = sailTupleExpr .map(expr -> new ParsedGraphQuery(queryString, expr)) .orElse(QueryParserUtil.parseGraphQuery(ql, queryString, baseURI)); return new SailGraphQuery(parsedQuery, this); }
Example #10
Source File: SailRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public SailTupleQuery prepareTupleQuery(QueryLanguage ql, String queryString, String baseURI) throws MalformedQueryException { Optional<TupleExpr> sailTupleExpr = sailConnection.prepareQuery(ql, Query.QueryType.TUPLE, queryString, baseURI); ParsedTupleQuery parsedQuery = sailTupleExpr .map(expr -> new ParsedTupleQuery(queryString, expr)) .orElse(QueryParserUtil.parseTupleQuery(ql, queryString, baseURI)); return new SailTupleQuery(parsedQuery, this); }
Example #11
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 #12
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 #13
Source File: QueryManager.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
/** * Retrieve the query plan for the given query string. */ public String getQueryPlan(String queryString, Object summary) { if (prefixDeclarations.size() > 0) { /* we have to check for prefixes in the query to not add * duplicate entries. In case duplicates are present * Sesame throws a MalformedQueryException */ if (prefixCheck.matcher(queryString).matches()) { queryString = getPrefixDeclarationsCheck(queryString) + queryString; } else { queryString = getPrefixDeclarations() + queryString; } } ParsedOperation query = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, queryString, null); if (!(query instanceof ParsedQuery)) throw new MalformedQueryException("Not a ParsedQuery: " + query.getClass()); // we use a dummy query info object here QueryInfo qInfo = new QueryInfo(conn.getSailConnection(), queryString, QueryType.SELECT, summary); TupleExpr tupleExpr = ((ParsedQuery)query).getTupleExpr(); try { tupleExpr = Optimizer.optimize(tupleExpr, new SimpleDataset(), EmptyBindingSet.getInstance(), conn.getSailConnection().getStrategy(), qInfo); return tupleExpr.toString(); } catch (SailException e) { throw new FedXException("Unable to retrieve query plan: " + e.getMessage()); } }
Example #14
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 #15
Source File: BulkedExternalInnerJoin.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public BulkedExternalInnerJoin(PlanNode leftNode, SailConnection connection, String query, boolean skipBasedOnPreviousConnection, SailConnection previousStateConnection, String... variables) { this.leftNode = leftNode; String completeQuery = "select * where { VALUES (?a) {}" + query + "} order by ?a"; parsedQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, completeQuery, null); this.connection = connection; this.skipBasedOnPreviousConnection = skipBasedOnPreviousConnection; this.variables = variables; this.previousStateConnection = previousStateConnection; }
Example #16
Source File: CustomGraphQueryInferencer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Called in order to set all the fields needed for the inferencer to function. * * @param language language that <tt>queryText</tt> and <tt>matcherText</tt> are expressed in * @param queryText a query that returns an RDF graph of inferred statements to be added to the underlying Sail * @param matcherText a query that returns an RDF graph of existing inferred statements already added previously * @throws MalformedQueryException if there is a problem parsing either of the given queries * @throws SailException if a problem occurs interpreting the rule pattern */ public final void setFields(QueryLanguage language, String queryText, String matcherText) throws MalformedQueryException, SailException { customQuery = QueryParserUtil.parseGraphQuery(language, queryText, null); String matcherQuery = matcherText; if (matcherText.trim().isEmpty()) { matcherQuery = CustomGraphQueryInferencerConfig.buildMatcherQueryFromRuleQuery(language, queryText); } customMatcher = QueryParserUtil.parseGraphQuery(language, matcherQuery, null); customQuery.getTupleExpr().visit(new AbstractQueryModelVisitor<SailException>() { @Override public void meet(StatementPattern statement) throws SailException { Var var = statement.getSubjectVar(); if (var.hasValue()) { watchSubjects.add(var.getValue()); } var = statement.getPredicateVar(); if (var.hasValue()) { watchPredicates.add(var.getValue()); } var = statement.getObjectVar(); if (var.hasValue()) { watchObjects.add(var.getValue()); } } }); hasWatchValues = !(watchSubjects.isEmpty() && watchPredicates.isEmpty() && watchObjects.isEmpty()); }
Example #17
Source File: SpinRendererTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testSpinRenderer() throws IOException, RDF4JException { StatementCollector expected = new StatementCollector(); RDFParser parser = Rio.createParser(RDFFormat.TURTLE); parser.setRDFHandler(expected); try (InputStream rdfStream = testURL.openStream()) { parser.parse(rdfStream, testURL.toString()); } // get query from sp:text String query = null; for (Statement stmt : expected.getStatements()) { if (SP.TEXT_PROPERTY.equals(stmt.getPredicate())) { query = stmt.getObject().stringValue(); break; } } assertNotNull(query); ParsedOperation parsedOp = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, query, testURL.toString()); StatementCollector actual = new StatementCollector(); renderer.render(parsedOp, actual); Object operation = (parsedOp instanceof ParsedQuery) ? ((ParsedQuery) parsedOp).getTupleExpr() : ((ParsedUpdate) parsedOp).getUpdateExprs(); assertTrue("Operation was\n" + operation + "\nExpected\n" + toRDF(expected) + "\nbut was\n" + toRDF(actual), Models.isomorphic(actual.getStatements(), expected.getStatements())); }
Example #18
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 #19
Source File: QueryManager.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Retrieve the query plan for the given query string. * * @param queryString * @return the query plan * @throws MalformedQueryException * @throws FedXException */ public String getQueryPlan(String queryString) throws MalformedQueryException, FedXException { if (prefixDeclarations.size() > 0) { /* * we have to check for prefixes in the query to not add duplicate entries. In case duplicates are present * RDF4J throws a MalformedQueryException */ if (prefixCheck.matcher(queryString).matches()) { queryString = getPrefixDeclarationsCheck(queryString) + queryString; } else { queryString = getPrefixDeclarations() + queryString; } } ParsedOperation query = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, queryString, null); if (!(query instanceof ParsedQuery)) { throw new MalformedQueryException("Not a ParsedQuery: " + query.getClass()); } // we use a dummy query info object here QueryInfo qInfo = new QueryInfo(queryString, QueryType.SELECT, federationContext.getConfig().getIncludeInferredDefault(), federationContext, ((ParsedQuery) query).getDataset()); TupleExpr tupleExpr = ((ParsedQuery) query).getTupleExpr(); try { FederationEvaluationStatistics evaluationStatistics = new FederationEvaluationStatistics(qInfo, new SimpleDataset()); tupleExpr = federationContext.getStrategy() .optimize(tupleExpr, evaluationStatistics, EmptyBindingSet.getInstance()); return tupleExpr.toString(); } catch (SailException e) { throw new FedXException("Unable to retrieve query plan: " + e.getMessage()); } }
Example #20
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testDualConnections() throws Exception { SailConnection con2 = sail.getConnection(); try { Assert.assertEquals(0, countAllElements()); con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter, context1); con.addStatement(guernica, RDF.TYPE, painting, context1); con.commit(); Assert.assertEquals(4, countAllElements()); con2.begin(); con2.addStatement(RDF.NIL, RDF.TYPE, RDF.LIST); String query = "SELECT S, P, O FROM {S} P {O}"; ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, query, null); Assert.assertEquals(5, countElements( con2.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false))); Runnable clearer = () -> { try { con.begin(); con.clear(); con.commit(); } catch (SailException e) { throw new RuntimeException(e); } }; Thread thread = new Thread(clearer); thread.start(); Thread.yield(); Thread.yield(); con2.commit(); thread.join(); } finally { con2.close(); } }
Example #21
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testValueRoundTrip(Resource subj, IRI pred, Value obj) throws Exception { con.begin(); con.addStatement(subj, pred, obj); con.commit(); CloseableIteration<? extends Statement, SailException> stIter = con.getStatements(null, null, null, false); try { Assert.assertTrue(stIter.hasNext()); Statement st = stIter.next(); Assert.assertEquals(subj, st.getSubject()); Assert.assertEquals(pred, st.getPredicate()); Assert.assertEquals(obj, st.getObject()); Assert.assertTrue(!stIter.hasNext()); } finally { stIter.close(); } ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT S, P, O FROM {S} P {O} WHERE P = <" + pred.stringValue() + ">", null); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false); try { Assert.assertTrue(iter.hasNext()); BindingSet bindings = iter.next(); Assert.assertEquals(subj, bindings.getValue("S")); Assert.assertEquals(pred, bindings.getValue("P")); Assert.assertEquals(obj, bindings.getValue("O")); Assert.assertTrue(!iter.hasNext()); } finally { iter.close(); } }
Example #22
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"); }
Example #23
Source File: StoreSerializationTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void testSerialization() throws Exception { MemoryStore store = new MemoryStore(dataDir); store.initialize(); ValueFactory factory = store.getValueFactory(); IRI foo = factory.createIRI("http://www.foo.example/foo"); IRI bar = factory.createIRI("http://www.foo.example/bar"); SailConnection con = store.getConnection(); con.begin(); con.addStatement(foo, RDF.TYPE, bar); con.commit(); ParsedTupleQuery query = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT X, P, Y FROM {X} P {Y}", null); TupleExpr tupleExpr = query.getTupleExpr(); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter = con.evaluate(tupleExpr, null, EmptyBindingSet.getInstance(), false); BindingSet bindingSet = iter.next(); assertEquals(bindingSet.getValue("X"), foo); assertEquals(bindingSet.getValue("P"), RDF.TYPE); assertEquals(bindingSet.getValue("Y"), bar); iter.close(); con.close(); store.shutDown(); store = new MemoryStore(dataDir); store.initialize(); factory = store.getValueFactory(); foo = factory.createIRI("http://www.foo.example/foo"); bar = factory.createIRI("http://www.foo.example/bar"); con = store.getConnection(); iter = con.evaluate(tupleExpr, null, EmptyBindingSet.getInstance(), false); bindingSet = iter.next(); assertEquals(bindingSet.getValue("X"), foo); assertEquals(bindingSet.getValue("P"), RDF.TYPE); assertEquals(bindingSet.getValue("Y"), bar); iter.close(); con.begin(); con.addStatement(bar, RDF.TYPE, foo); con.commit(); con.close(); store.shutDown(); }
Example #24
Source File: SailRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Update prepareUpdate(QueryLanguage ql, String update, String baseURI) throws RepositoryException, MalformedQueryException { ParsedUpdate parsedUpdate = QueryParserUtil.parseUpdate(ql, update, baseURI); return new SailUpdate(parsedUpdate, this); }
Example #25
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected int countQueryResults(String query) throws Exception { ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, query + " using namespace ex = <" + EXAMPLE_NS + ">", null); return countElements(con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false)); }
Example #26
Source File: SPARQLParser.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static void main(String[] args) throws java.io.IOException { System.out.println("Your SPARQL query:"); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuilder buf = new StringBuilder(); String line = null; int emptyLineCount = 0; while ((line = in.readLine()) != null) { if (line.length() > 0) { emptyLineCount = 0; buf.append(' ').append(line).append('\n'); } else { emptyLineCount++; } if (emptyLineCount == 2) { emptyLineCount = 0; String queryStr = buf.toString().trim(); if (queryStr.length() > 0) { try { long start = System.currentTimeMillis(); ParsedOperation parsedQuery = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, queryStr, null); long finish = System.currentTimeMillis(); System.out.println("Parsed query: "); System.out.println(parsedQuery.toString()); System.out.println(); System.out.println("parsed in " + (finish - start) + " ms."); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } buf.setLength(0); } } }
Example #27
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testQueryBindings() throws Exception { // Add some data to the repository con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter, context1); con.addStatement(guernica, RDF.TYPE, painting, context1); con.addStatement(picasso, paints, guernica, context1); con.commit(); // Query 1 ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "select X from {X} rdf:type {Y} rdf:type {rdfs:Class}", null); TupleExpr tupleExpr = tupleQuery.getTupleExpr(); MapBindingSet bindings = new MapBindingSet(2); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; iter = con.evaluate(tupleExpr, null, bindings, false); int resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 2, resultCount); bindings.addBinding("Y", painter); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 1, resultCount); bindings.addBinding("Z", painting); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 1, resultCount); bindings.removeBinding("Y"); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 2, resultCount); // Query 2 tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "select X from {X} rdf:type {Y} rdf:type {rdfs:Class} where Y = Z", null); tupleExpr = tupleQuery.getTupleExpr(); bindings.clear(); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 0, resultCount); bindings.addBinding("Z", painter); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 1, resultCount); }
Example #28
Source File: HalyardBulkUpdate.java From Halyard with Apache License 2.0 | 4 votes |
public int run(CommandLine cmd) throws Exception { String source = cmd.getOptionValue('s'); String queryFiles = cmd.getOptionValue('q'); String workdir = cmd.getOptionValue('w'); getConf().setLong(DEFAULT_TIMESTAMP_PROPERTY, Long.parseLong(cmd.getOptionValue('e', String.valueOf(System.currentTimeMillis())))); if (cmd.hasOption('i')) getConf().set(ELASTIC_INDEX_URL, cmd.getOptionValue('i')); TableMapReduceUtil.addDependencyJars(getConf(), HalyardExport.class, NTriplesUtil.class, Rio.class, AbstractRDFHandler.class, RDFFormat.class, RDFParser.class, HTable.class, HBaseConfiguration.class, AuthenticationProtos.class, Trace.class, Gauge.class); HBaseConfiguration.addHbaseResources(getConf()); getConf().setStrings(TABLE_NAME_PROPERTY, source); getConf().setLong(DEFAULT_TIMESTAMP_PROPERTY, getConf().getLong(DEFAULT_TIMESTAMP_PROPERTY, System.currentTimeMillis())); int stages = 1; for (int stage = 0; stage < stages; stage++) { Job job = Job.getInstance(getConf(), "HalyardBulkUpdate -> " + workdir + " -> " + source + " stage #" + stage); job.getConfiguration().setInt(STAGE_PROPERTY, stage); job.setJarByClass(HalyardBulkUpdate.class); job.setMapperClass(SPARQLUpdateMapper.class); job.setMapOutputKeyClass(ImmutableBytesWritable.class); job.setMapOutputValueClass(KeyValue.class); job.setInputFormatClass(QueryInputFormat.class); job.setSpeculativeExecution(false); job.setReduceSpeculativeExecution(false); try (HTable hTable = HalyardTableUtils.getTable(getConf(), source, false, 0)) { HFileOutputFormat2.configureIncrementalLoad(job, hTable.getTableDescriptor(), hTable.getRegionLocator()); QueryInputFormat.setQueriesFromDirRecursive(job.getConfiguration(), queryFiles, true, stage); Path outPath = new Path(workdir, "stage"+stage); FileOutputFormat.setOutputPath(job, outPath); TableMapReduceUtil.addDependencyJars(job); TableMapReduceUtil.initCredentials(job); if (stage == 0) { //count real number of stages for (InputSplit is : new QueryInputFormat().getSplits(job)) { QueryInputFormat.QueryInputSplit qis = (QueryInputFormat.QueryInputSplit)is; int updates = QueryParserUtil.parseUpdate(QueryLanguage.SPARQL, qis.getQuery(), null).getUpdateExprs().size(); if (updates > stages) { stages = updates; } LOG.log(Level.INFO, "{0} contains {1} stages of the update sequence.", new Object[]{qis.getQueryName(), updates}); } LOG.log(Level.INFO, "Bulk Update will process {0} MapReduce stages.", stages); } if (job.waitForCompletion(true)) { new LoadIncrementalHFiles(getConf()).doBulkLoad(outPath, hTable); LOG.log(Level.INFO, "Stage #{0} of {1} completed..", new Object[]{stage, stages}); } else { return -1; } } } LOG.info("Bulk Update Completed.."); return 0; }
Example #29
Source File: Query.java From mobi with GNU Affero General Public License v3.0 | 4 votes |
@Override public Object execute() throws Exception { // Get Query String String queryString; if (queryFileParam != null) { try { queryString = new String(Files.readAllBytes(Paths.get(queryFileParam))); } catch (IOException e) { throw new MobiException(e); } } else { queryString = queryParam; } // Get Repo if (StringUtils.isEmpty(repositoryParam)) { repositoryParam = "system"; } Optional<Repository> repoOpt = repoManager.getRepository(repositoryParam); if (!repoOpt.isPresent()) { System.out.println("ERROR: Repository not found."); return null; } // Parse Query ParsedOperation parsedOperation = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, queryString, null); // Execute Query if (parsedOperation instanceof ParsedQuery) { if (parsedOperation instanceof ParsedTupleQuery) { executeTupleQuery(repoOpt.get(), queryString); } else if (parsedOperation instanceof ParsedGraphQuery) { executeGraphQuery(repoOpt.get(), queryString); } else { System.out.println("Query type not supported."); } } else { System.out.println("Update queries not supported."); } return null; }
Example #30
Source File: W3CApprovedSPARQL11SyntaxTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override protected ParsedOperation parseOperation(String operation, String fileURL) throws MalformedQueryException { return QueryParserUtil.parseOperation(QueryLanguage.SPARQL, operation, fileURL); }