Hithe rdf model api (https://rdf4j.org/documentation/programming/model/) allows to create IRIs, Literals and Statements, but how can I create a variable (like ?x)? I want to use variables in an insert sparql query. Is there another way to use variables in my queries?
Thanks for your answer. Helped me alot.I have a couple more questions.1. How can I get the type (Literal, Variable, ...) from a Statement? I tried it like this, but I get a NoSuchElementException.SPARQLParser parser = new SPARQLParser();ParsedUpdate parsed = parser.parseUpdate(sparql, "http://example.org/");Iterator<UpdateExpr> expr = parsed.getUpdateExprs().iterator();UpdateExpr updateExpr = expr.next();InsertData insertDataExpr = (InsertData) updateExpr;RDFParser rdfParser = new SPARQLUpdateDataBlockParser();StatementCollector handler = new StatementCollector();parser.setRDFHandler(handler);rdfParser .parse(new StringReader(insertDataExpr.getDataBlock()), "");Iterator<Statement> iterator = handler.getStatements().iterator();System.out.println("Subject: " + iterator.next().getSubject());
2. Can I build a query with a different triple format? Like predicate (subject, object) instead of subject, predicate, object.
So,1. I found the error and solved it. Thanks.
2. My goal is to parse a INSERT DATA/INSERT Sparql query and build a equivalent datalog rule out of it. I use a datalog framework that is only accepting the triple format predicate (subject, object). Hence, to convert the formats/syntaxes I need to get each query statement. With INSERT DATA queries it is pretty simple, but now I have a problem with the INSERT query. How can I iterate through a UpdateExpr (like the UpdateExpr used here https://stackoverflow.com/questions/55118469/how-get-updateexpr-when-i-execute-insert-query-rdf4j) and get each Statement in a StatementPattern? Can I do this without using a MemoryStore?
Thanks in advanceTimJeen Broekstra schrieb am Freitag, 4. Juni 2021 um 08:42:47 UTC+2:On Fri, 4 Jun 2021, at 06:09, Tim M wrote:Thanks for your answer. Helped me alot.I have a couple more questions.1. How can I get the type (Literal, Variable, ...) from a Statement? I tried it like this, but I get a NoSuchElementException.SPARQLParser parser = new SPARQLParser();ParsedUpdate parsed = parser.parseUpdate(sparql, "http://example.org/");Iterator<UpdateExpr> expr = parsed.getUpdateExprs().iterator();UpdateExpr updateExpr = expr.next();InsertData insertDataExpr = (InsertData) updateExpr;RDFParser rdfParser = new SPARQLUpdateDataBlockParser();StatementCollector handler = new StatementCollector();parser.setRDFHandler(handler);rdfParser .parse(new StringReader(insertDataExpr.getDataBlock()), "");Iterator<Statement> iterator = handler.getStatements().iterator();System.out.println("Subject: " + iterator.next().getSubject());Can you give a bit of background on what you're trying to achieve here? Why are you parsing and inspecting a SPARQL update query in this way? If the goal is to get the statements from a SPARQL update, a possibly simpler approach might be to just execute the update (on a MemoryStore, for example) and then query that store for the statements.That said, I am not immediately spotting anything wrong in your code. A NoSuchElementException just means that the iterator is empty, which either means that your query's datablock was empty, or the data block parser somehow failed. You could try setting an error handler on it to see what is going wrong.2. Can I build a query with a different triple format? Like predicate (subject, object) instead of subject, predicate, object.I don't understand the question. What do you mean with "build a query"? And what are you trying to achieve with this "different triple format"?Jeen
--You received this message because you are subscribed to the Google Groups "RDF4J Users" group.To unsubscribe from this group and stop receiving emails from it, send an email to rdf4j-users...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/rdf4j-users/76980889-245b-4b3d-859b-f182ac9c16d3n%40googlegroups.com.