org.eclipse.rdf4j.query.parser.ParsedUpdate Java Examples
The following examples show how to use
org.eclipse.rdf4j.query.parser.ParsedUpdate.
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: SPARQLParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Verify that an INSERT with a subselect using a wildcard correctly adds vars to projection * * @see <a href="https://github.com/eclipse/rdf4j/issues/686">#686</a> */ @Test public void testParseWildcardSubselectInUpdate() throws Exception { StringBuilder update = new StringBuilder(); update.append("INSERT { <urn:a> <urn:b> <urn:c> . } WHERE { SELECT * {?s ?p ?o } }"); ParsedUpdate parsedUpdate = parser.parseUpdate(update.toString(), null); List<UpdateExpr> exprs = parsedUpdate.getUpdateExprs(); assertEquals(1, exprs.size()); UpdateExpr expr = exprs.get(0); assertTrue(expr instanceof Modify); Modify m = (Modify) expr; TupleExpr whereClause = m.getWhereExpr(); assertTrue(whereClause instanceof Projection); ProjectionElemList projectionElemList = ((Projection) whereClause).getProjectionElemList(); assertNotNull(projectionElemList); List<ProjectionElem> elements = projectionElemList.getElements(); assertNotNull(elements); assertEquals("projection should contain all three variables", 3, elements.size()); }
Example #2
Source File: AbstractQueryPreparer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void execute() throws UpdateExecutionException { ParsedUpdate parsedUpdate = getParsedUpdate(); List<UpdateExpr> updateExprs = parsedUpdate.getUpdateExprs(); Map<UpdateExpr, Dataset> datasetMapping = parsedUpdate.getDatasetMapping(); for (UpdateExpr updateExpr : updateExprs) { Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr)); try { AbstractQueryPreparer.this.execute(updateExpr, activeDataset, getBindings(), getIncludeInferred(), getMaxExecutionTime()); } catch (UpdateExecutionException e) { if (!updateExpr.isSilent()) { throw e; } } } }
Example #3
Source File: SailConnectionUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void execute() throws UpdateExecutionException { ParsedUpdate parsedUpdate = getParsedUpdate(); List<UpdateExpr> updateExprs = parsedUpdate.getUpdateExprs(); Map<UpdateExpr, Dataset> datasetMapping = parsedUpdate.getDatasetMapping(); SailUpdateExecutor executor = new SailUpdateExecutor(con, vf, parserConfig); for (UpdateExpr updateExpr : updateExprs) { Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr)); try { boolean localTransaction = isLocalTransaction(); if (localTransaction) { beginLocalTransaction(); } executor.executeUpdate(updateExpr, activeDataset, getBindings(), getIncludeInferred(), getMaxExecutionTime()); if (localTransaction) { commitLocalTransaction(); } } catch (RDF4JException | IOException e) { logger.warn("exception during update execution: ", e); if (!updateExpr.isSilent()) { throw new UpdateExecutionException(e); } } } }
Example #4
Source File: SPARQLParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testDeleteDataLineNumberReporting() throws Exception { String deleteDataString = "DELETE DATA {\n incorrect reference }"; try { ParsedUpdate u = parser.parseUpdate(deleteDataString, null); fail("should have resulted in parse exception"); } catch (MalformedQueryException e) { assertTrue(e.getMessage().contains("line 2,")); } }
Example #5
Source File: SPARQLParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testInsertDataLineNumberReporting() throws Exception { String insertDataString = "INSERT DATA {\n incorrect reference }"; try { ParsedUpdate u = parser.parseUpdate(insertDataString, null); fail("should have resulted in parse exception"); } catch (MalformedQueryException e) { assertTrue(e.getMessage().contains("line 2,")); } }
Example #6
Source File: SailUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void execute() throws UpdateExecutionException { ParsedUpdate parsedUpdate = getParsedUpdate(); List<UpdateExpr> updateExprs = parsedUpdate.getUpdateExprs(); Map<UpdateExpr, Dataset> datasetMapping = parsedUpdate.getDatasetMapping(); SailUpdateExecutor executor = new SailUpdateExecutor(con.getSailConnection(), con.getValueFactory(), con.getParserConfig()); boolean localTransaction = false; try { if (!getConnection().isActive()) { localTransaction = true; beginLocalTransaction(); } for (UpdateExpr updateExpr : updateExprs) { Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr)); try { executor.executeUpdate(updateExpr, activeDataset, getBindings(), getIncludeInferred(), getMaxExecutionTime()); } catch (RDF4JException | IOException e) { logger.warn("exception during update execution: ", e); if (!updateExpr.isSilent()) { throw new UpdateExecutionException(e); } } } if (localTransaction) { commitLocalTransaction(); localTransaction = false; } } finally { if (localTransaction) { rollbackLocalTransaction(); } } }
Example #7
Source File: SPARQLParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testLongUnicode() throws Exception { ParsedUpdate ru = parser.parseUpdate("insert data {<urn:test:foo> <urn:test:bar> \"\\U0001F61F\" .}", "urn:test"); InsertData insertData = (InsertData) ru.getUpdateExprs().get(0); String[] lines = insertData.getDataBlock().split("\n"); assertEquals("\uD83D\uDE1F", lines[lines.length - 1].replaceAll(".*\"(.*)\".*", "$1")); }
Example #8
Source File: SailConnectionUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public SailConnectionUpdate(ParsedUpdate parsedUpdate, SailConnection con, ValueFactory vf, ParserConfig parserConfig) { super(parsedUpdate); this.con = con; this.vf = vf; this.parserConfig = parserConfig; }
Example #9
Source File: SpinInferencing.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static int executeRule(Resource subj, Resource rule, QueryPreparer queryPreparer, SpinParser parser, InferencerConnection conn) throws OpenRDFException { int nofInferred; TripleSource tripleSource = queryPreparer.getTripleSource(); ParsedOperation parsedOp = parser.parse(rule, tripleSource); if (parsedOp instanceof ParsedGraphQuery) { ParsedGraphQuery graphQuery = (ParsedGraphQuery) parsedOp; GraphQuery queryOp = queryPreparer.prepare(graphQuery); addBindings(subj, rule, graphQuery, queryOp, tripleSource, parser); CountingRDFInferencerInserter handler = new CountingRDFInferencerInserter(conn, tripleSource.getValueFactory()); queryOp.evaluate(handler); nofInferred = handler.getStatementCount(); } else if (parsedOp instanceof ParsedUpdate) { ParsedUpdate graphUpdate = (ParsedUpdate) parsedOp; Update updateOp = queryPreparer.prepare(graphUpdate); addBindings(subj, rule, graphUpdate, updateOp, tripleSource, parser); UpdateCountListener listener = new UpdateCountListener(); conn.addConnectionListener(listener); updateOp.execute(); conn.removeConnectionListener(listener); // number of statement changes nofInferred = listener.getAddedStatementCount() + listener.getRemovedStatementCount(); } else { throw new MalformedSpinException("Invalid rule: " + rule); } return nofInferred; }
Example #10
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 #11
Source File: SpinParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testSpinParser() 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 resource from sp:text Resource queryResource = null; for (Statement stmt : expected.getStatements()) { if (SP.TEXT_PROPERTY.equals(stmt.getPredicate())) { queryResource = stmt.getSubject(); break; } } assertNotNull(queryResource); TripleSource store = new ModelTripleSource(new TreeModel(expected.getStatements())); ParsedOperation textParsedOp = textParser.parse(queryResource, store); ParsedOperation rdfParsedOp = rdfParser.parse(queryResource, store); if (textParsedOp instanceof ParsedQuery) { assertEquals(((ParsedQuery) textParsedOp).getTupleExpr(), ((ParsedQuery) rdfParsedOp).getTupleExpr()); } else { assertEquals(((ParsedUpdate) textParsedOp).getUpdateExprs(), ((ParsedUpdate) rdfParsedOp).getUpdateExprs()); } }
Example #12
Source File: Template.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public ParsedOperation call(Map<IRI, Value> argValues) throws MalformedSpinException { MapBindingSet args = new MapBindingSet(); for (Argument arg : arguments) { IRI argPred = arg.getPredicate(); Value argValue = argValues.get(argPred); if (argValue == null && !arg.isOptional()) { throw new MalformedSpinException("Missing value for template argument: " + argPred); } if (argValue == null) { argValue = arg.getDefaultValue(); } if (argValue != null) { args.addBinding(argPred.getLocalName(), argValue); } } ParsedOperation callOp; if (parsedOp instanceof ParsedBooleanQuery) { callOp = new ParsedBooleanTemplate(this, args); } else if (parsedOp instanceof ParsedTupleQuery) { callOp = new ParsedTupleTemplate(this, args); } else if (parsedOp instanceof ParsedGraphQuery) { callOp = new ParsedGraphTemplate(this, args); } else if (parsedOp instanceof ParsedUpdate) { callOp = new ParsedUpdateTemplate(this, args); } else { throw new AssertionError("Unrecognised ParsedOperation: " + parsedOp.getClass()); } return callOp; }
Example #13
Source File: ParsedUpdateTemplate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private ParsedUpdateTemplate(Template template, ParsedUpdate update, BindingSet args) { super(update.getSourceString(), update.getNamespaces()); for (UpdateExpr updateExpr : update.getUpdateExprs()) { addUpdateExpr(updateExpr); } for (Map.Entry<UpdateExpr, Dataset> entry : update.getDatasetMapping().entrySet()) { map(entry.getKey(), entry.getValue()); } this.template = template; this.args = args; }
Example #14
Source File: SpinRenderer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void render(ParsedOperation operation, RDFHandler handler) throws RDFHandlerException { if (operation instanceof ParsedQuery) { render((ParsedQuery) operation, handler); } else if (operation instanceof ParsedUpdate) { render((ParsedUpdate) operation, handler); } else { throw new AssertionError("Unrecognised ParsedOperation: " + operation.getClass()); } }
Example #15
Source File: AbstractParserUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected AbstractParserUpdate(ParsedUpdate parsedUpdate) { this.parsedUpdate = parsedUpdate; }
Example #16
Source File: SpinRenderer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void render(ParsedUpdate update, RDFHandler handler) throws RDFHandlerException { handler.startRDF(); for (Map.Entry<String, String> entry : update.getNamespaces().entrySet()) { handler.handleNamespace(entry.getKey(), entry.getValue()); } String[] sourceStrings = update.getSourceString().split("\\s*;\\s*"); List<UpdateExpr> updateExprs = update.getUpdateExprs(); Map<UpdateExpr, Dataset> datasets = update.getDatasetMapping(); for (int i = 0; i < updateExprs.size(); i++) { UpdateExpr updateExpr = updateExprs.get(i); Resource updateSubj = valueFactory.createBNode(); Dataset dataset = datasets.get(updateExpr); IRI updateClass; if (updateExpr instanceof Modify) { Modify modify = (Modify) updateExpr; if (modify.getInsertExpr() == null && modify.getWhereExpr().equals(modify.getDeleteExpr())) { updateClass = SP.DELETE_WHERE_CLASS; } else { updateClass = SP.MODIFY_CLASS; } } else if (updateExpr instanceof InsertData) { updateClass = SP.INSERT_DATA_CLASS; } else if (updateExpr instanceof DeleteData) { updateClass = SP.DELETE_DATA_CLASS; } else if (updateExpr instanceof Load) { updateClass = SP.LOAD_CLASS; } else if (updateExpr instanceof Clear) { updateClass = SP.CLEAR_CLASS; } else if (updateExpr instanceof Create) { updateClass = SP.CREATE_CLASS; } else { throw new RDFHandlerException("Unrecognised UpdateExpr: " + updateExpr.getClass()); } handler.handleStatement(valueFactory.createStatement(updateSubj, RDF.TYPE, updateClass)); if (output.text) { handler.handleStatement(valueFactory.createStatement(updateSubj, SP.TEXT_PROPERTY, valueFactory.createLiteral(sourceStrings[i]))); } if (output.rdf) { SpinVisitor visitor = new SpinVisitor(handler, null, updateSubj, dataset); updateExpr.visit(visitor); visitor.end(); } } handler.endRDF(); }
Example #17
Source File: AbstractParserUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public ParsedUpdate getParsedUpdate() { return parsedUpdate; }
Example #18
Source File: AbstractParserUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected AbstractParserUpdate(ParsedUpdate parsedUpdate) { this.parsedUpdate = parsedUpdate; }
Example #19
Source File: TestSparqlStarParser.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testUseInDeleteFromStatementPattern() throws Exception { String simpleSparqlQuery = "DELETE {<<?s ?p ?o>> <urn:pred> <urn:value>} WHERE {?s ?p ?o}"; ParsedUpdate q = parser.parseUpdate(simpleSparqlQuery, null); assertNotNull(q); assertTrue("expect single UpdateExpr", q.getUpdateExprs().size() == 1); UpdateExpr updateExpr = q.getUpdateExprs().get(0); assertTrue("expect Modify UpdateExpr", updateExpr instanceof Modify); Modify modify = (Modify) updateExpr; assertTrue("expect no INSERT", modify.getInsertExpr() == null); assertTrue("expect DELETE", modify.getDeleteExpr() != null); assertTrue("expect DETELE as statamentPattern", modify.getDeleteExpr() instanceof StatementPattern); StatementPattern insert = (StatementPattern) modify.getDeleteExpr(); String anonVar = insert.getSubjectVar().getName(); assertEquals("expect predicate", "urn:pred", insert.getPredicateVar().getValue().toString()); assertEquals("expect object", "urn:value", insert.getObjectVar().getValue().toString()); assertTrue("expect WHERE", modify.getWhereExpr() != null); assertTrue("expect WHERE as extension", modify.getWhereExpr() instanceof Extension); Extension where = (Extension) modify.getWhereExpr(); Extension ext = (Extension) where; assertTrue("one extention element", ext.getElements().size() == 1); ExtensionElem elem = ext.getElements().get(0); assertEquals("anon name should match first", elem.getName(), anonVar); assertTrue("expect ValueExprTripleRef in extention element", elem.getExpr() instanceof ValueExprTripleRef); ValueExprTripleRef ref = (ValueExprTripleRef) elem.getExpr(); assertEquals("subject var name", "s", ref.getSubjectVar().getName()); assertEquals("predicate var name", "p", ref.getPredicateVar().getName()); assertEquals("object var name", "o", ref.getObjectVar().getName()); assertTrue("expect StatementPattern as extension argument", ext.getArg() instanceof StatementPattern); StatementPattern pattern = (StatementPattern) ext.getArg(); assertEquals("subject var name should match", pattern.getSubjectVar().getName(), ref.getSubjectVar().getName()); assertEquals("predicate var name should match", pattern.getPredicateVar().getName(), ref.getPredicateVar().getName()); assertEquals("object var name should match", pattern.getObjectVar().getName(), ref.getObjectVar().getName()); }
Example #20
Source File: TestSparqlStarParser.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testUseInInsertFromStatementPattern() throws Exception { String simpleSparqlQuery = "Insert {<<?s ?p ?o>> <urn:pred> <urn:value>} WHERE {?s ?p ?o}"; ParsedUpdate q = parser.parseUpdate(simpleSparqlQuery, null); assertNotNull(q); assertTrue("expect single UpdateExpr", q.getUpdateExprs().size() == 1); UpdateExpr updateExpr = q.getUpdateExprs().get(0); assertTrue("expect Modify UpdateExpr", updateExpr instanceof Modify); Modify modify = (Modify) updateExpr; assertTrue("expect no DELETE", modify.getDeleteExpr() == null); assertTrue("expect INSERT", modify.getInsertExpr() != null); assertTrue("expect INSERT as statamentPattern", modify.getInsertExpr() instanceof StatementPattern); StatementPattern insert = (StatementPattern) modify.getInsertExpr(); String anonVar = insert.getSubjectVar().getName(); assertEquals("expect predicate", "urn:pred", insert.getPredicateVar().getValue().toString()); assertEquals("expect object", "urn:value", insert.getObjectVar().getValue().toString()); assertTrue("expect WHERE", modify.getWhereExpr() != null); assertTrue("expect WHERE as extension", modify.getWhereExpr() instanceof Extension); Extension where = (Extension) modify.getWhereExpr(); Extension ext = (Extension) where; assertTrue("one extention element", ext.getElements().size() == 1); ExtensionElem elem = ext.getElements().get(0); assertEquals("anon name should match first", elem.getName(), anonVar); assertTrue("expect ValueExprTripleRef in extention element", elem.getExpr() instanceof ValueExprTripleRef); ValueExprTripleRef ref = (ValueExprTripleRef) elem.getExpr(); assertEquals("subject var name", "s", ref.getSubjectVar().getName()); assertEquals("predicate var name", "p", ref.getPredicateVar().getName()); assertEquals("object var name", "o", ref.getObjectVar().getName()); assertTrue("expect StatementPattern as extension argument", ext.getArg() instanceof StatementPattern); StatementPattern pattern = (StatementPattern) ext.getArg(); assertEquals("subject var name should match", pattern.getSubjectVar().getName(), ref.getSubjectVar().getName()); assertEquals("predicate var name should match", pattern.getPredicateVar().getName(), ref.getPredicateVar().getName()); assertEquals("object var name should match", pattern.getObjectVar().getName(), ref.getObjectVar().getName()); }
Example #21
Source File: AbstractParserUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public ParsedUpdate getParsedUpdate() { return parsedUpdate; }
Example #22
Source File: SeRQLParser.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public ParsedUpdate parseUpdate(String updateStr, String baseURI) throws MalformedQueryException { throw new UnsupportedOperationException("SeRQL does not support update operations"); }
Example #23
Source File: SailUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected SailUpdate(ParsedUpdate parsedUpdate, SailRepositoryConnection con) { super(parsedUpdate); this.con = con; }
Example #24
Source File: SailQueryPreparer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Update prepare(ParsedUpdate graphUpdate) { Update update = new SailUpdate(graphUpdate, con); update.setIncludeInferred(includeInferred); return update; }
Example #25
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 #26
Source File: AbstractQueryPreparer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
UpdateImpl(ParsedUpdate update) { super(update); }
Example #27
Source File: AbstractQueryPreparer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Update prepare(ParsedUpdate u) { return new UpdateImpl(u); }
Example #28
Source File: SailConnectionQueryPreparer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Update prepare(ParsedUpdate graphUpdate) { Update update = new SailConnectionUpdate(graphUpdate, con, vf, parserConfig); update.setIncludeInferred(includeInferred); return update; }
Example #29
Source File: ParsedUpdateTemplate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public ParsedUpdateTemplate(Template template, BindingSet args) { this(template, (ParsedUpdate) template.getParsedOperation(), args); }
Example #30
Source File: SpinParser.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public ParsedUpdate parseUpdate(Resource queryResource, TripleSource store) throws RDF4JException { return (ParsedUpdate) parse(queryResource, SP.UPDATE_CLASS, store); }