Discussion:
Handling named graphs in Jena.
Jason Koh
2016-11-02 00:30:12 UTC
Permalink
Dear all,

This might be due to my lack of knowledge on SPARQL syntax.

1.
I try to construct a named graph via SPARQL query, but it fails.
so What I did is like following
///////////
Model model = ModelFactory.createDefaultModel();
String queryStr ="CONSTRUCT {GRAPH <http://example.com> {<s1> <p1> <o1>.} }
WHERE{}";
Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, resultModel);
/////////

It fails at the third line while it is parsing with this error message:
Exception in thread "main" org.apache.jena.query.QueryParseException:
Encountered " "graph" "GRAPH "" at line 1, column 335.

I am thinking that I am following
https://jena.apache.org/documentation/query/construct-quad.html correctly.

Could you tell me the problem in the query?


2.
Actually I rather want to use INSERT instead of CONSTRUCT, but QueryFactory
cannot parse INSERT. Why is that?

Thanks!


With regards,
Jason Koh
cseweb.ucsd.edu/~jbkoh
Martynas Jusevičius
2016-11-02 00:34:59 UTC
Permalink
1. You haven't followed the example. You are missing Syntax.syntaxARQ
in QueryFactory.create(), I guess this makes a difference since
CONSTRUCT with quads is not standard SPARQL.

2. Because INSERT is an update and not a query? Se UpdateFactory:
https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/update/UpdateFactory.html
Post by Jason Koh
Dear all,
This might be due to my lack of knowledge on SPARQL syntax.
1.
I try to construct a named graph via SPARQL query, but it fails.
so What I did is like following
///////////
Model model = ModelFactory.createDefaultModel();
String queryStr ="CONSTRUCT {GRAPH <http://example.com> {<s1> <p1> <o1>.} }
WHERE{}";
Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, resultModel);
/////////
Encountered " "graph" "GRAPH "" at line 1, column 335.
I am thinking that I am following
https://jena.apache.org/documentation/query/construct-quad.html correctly.
Could you tell me the problem in the query?
2.
Actually I rather want to use INSERT instead of CONSTRUCT, but QueryFactory
cannot parse INSERT. Why is that?
Thanks!
With regards,
Jason Koh
cseweb.ucsd.edu/~jbkoh
Jason Koh
2016-11-02 02:28:57 UTC
Permalink
Thanks for the answers! It solved the error. But I got a followup
question...

After constructing a named graph with the previous code, I tried to SELECT
all the triples in the graph by the following query, and it gives nothing...

/////////////
String selectQueryStr = "SELECT ?s ?p ?o where{GRAPH <http://example.com>{?s
?p ?o}}";
Query selectQuery = QueryFactory.create(selectQueryStr, Syntax.syntaxARQ);
QueryExecution sqe = QueryExecutionFactory.create(selectQuery,
resultModel);
ResultSet resultset = sqe.execSelect();
//////////

And I found
1) resultset is empty
2) The model generated by CONSTRUCT query is empty. (Only prefixes are
stored.)

Hope I can get another chance to get answers.

Thanks a lot!



With regards,
Jason Koh
cseweb.ucsd.edu/~jbkoh
Post by Martynas Jusevičius
1. You haven't followed the example. You are missing Syntax.syntaxARQ
in QueryFactory.create(), I guess this makes a difference since
CONSTRUCT with quads is not standard SPARQL.
https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/update/
UpdateFactory.html
Post by Jason Koh
Dear all,
This might be due to my lack of knowledge on SPARQL syntax.
1.
I try to construct a named graph via SPARQL query, but it fails.
so What I did is like following
///////////
Model model = ModelFactory.createDefaultModel();
String queryStr ="CONSTRUCT {GRAPH <http://example.com> {<s1> <p1>
<o1>.} }
Post by Jason Koh
WHERE{}";
Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, resultModel);
/////////
Encountered " "graph" "GRAPH "" at line 1, column 335.
I am thinking that I am following
https://jena.apache.org/documentation/query/construct-quad.html
correctly.
Post by Jason Koh
Could you tell me the problem in the query?
2.
Actually I rather want to use INSERT instead of CONSTRUCT, but
QueryFactory
Post by Jason Koh
cannot parse INSERT. Why is that?
Thanks!
With regards,
Jason Koh
cseweb.ucsd.edu/~jbkoh
Andy Seaborne
2016-11-02 09:14:41 UTC
Permalink
Post by Jason Koh
Thanks for the answers! It solved the error. But I got a followup
question...
After constructing a named graph with the previous code, I tried to SELECT
all the triples in the graph by the following query, and it gives nothing...
/////////////
String selectQueryStr = "SELECT ?s ?p ?o where{GRAPH <http://example.com>{?s
?p ?o}}";
Query selectQuery = QueryFactory.create(selectQueryStr, Syntax.syntaxARQ);
QueryExecution sqe = QueryExecutionFactory.create(selectQuery,
resultModel);
ResultSet resultset = sqe.execSelect();
//////////
And I found
1) resultset is empty
2) The model generated by CONSTRUCT query is empty. (Only prefixes are
stored.)
Hope I can get another chance to get answers.
1/ CONSTRUCT QUAD returns a dataset. The code above queries
"resultModel" the thing being queried ("resultModel" in both this
message and your initial one) which does not change in a query.

2/ You are querying a model so only the default graph has any data in it.

SELECT * { {?s ?p ?o} UNION { GRAPH ?g { ?s ?p ?o } } }


If you want to change the target,
you need SPARQL UPDATE.

If you want to query the result of the CONSTRUCT quads,
you want something like:

# email 1:
Model model = ModelFactory.createDefaultModel();
String queryStr ="CONSTRUCT {GRAPH <http://example.com> {<s1> <p1> <o1>.} }
WHERE{}";
Query query = QueryFactory.create(queryStr);
QueryExecution qe1 = QueryExecutionFactory.create(query, resultModel);

Dataset dataset = qe.execConstructDataset();
^^^^^^^^^^^^^^^^^^^^^^^^^^^

// Query resulting dataset
QueryExecution qe2 = QueryExecutionFactory.create(query2, dataset);
^^^^^^^^^^^^^^


In SPARQL Update there is also

COPY DEFAULT TO GRAPH <http://example.com> ;

and

String queryStr ="CONSTRUCT {GRAPH <http://example.com> {<s1> <p1> <o1>.} }
WHERE{}";

in terms of update is

INSERT DATA {GRAPH <http://example.com> {<s1> <p1> <o1>.} }

Be careful about the lack of trailing / on <http://example.com>.

Andy
Post by Jason Koh
Thanks a lot!
With regards,
Jason Koh
cseweb.ucsd.edu/~jbkoh
Post by Martynas Jusevičius
1. You haven't followed the example. You are missing Syntax.syntaxARQ
in QueryFactory.create(), I guess this makes a difference since
CONSTRUCT with quads is not standard SPARQL.
https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/update/
UpdateFactory.html
Post by Jason Koh
Dear all,
This might be due to my lack of knowledge on SPARQL syntax.
1.
I try to construct a named graph via SPARQL query, but it fails.
so What I did is like following
///////////
Model model = ModelFactory.createDefaultModel();
String queryStr ="CONSTRUCT {GRAPH <http://example.com> {<s1> <p1>
<o1>.} }
Post by Jason Koh
WHERE{}";
Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, resultModel);
/////////
Encountered " "graph" "GRAPH "" at line 1, column 335.
I am thinking that I am following
https://jena.apache.org/documentation/query/construct-quad.html
correctly.
Post by Jason Koh
Could you tell me the problem in the query?
2.
Actually I rather want to use INSERT instead of CONSTRUCT, but
QueryFactory
Post by Jason Koh
cannot parse INSERT. Why is that?
Thanks!
With regards,
Jason Koh
cseweb.ucsd.edu/~jbkoh
Loading...