On 02/11/16 02:28, Jason Koh wrote:
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


Thanks a lot!



With regards,
Jason Koh
cseweb.ucsd.edu/~jbkoh

On Tue, Nov 1, 2016 at 5:34 PM, Martynas Jusevičius <[email protected]>
wrote:

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

On Wed, Nov 2, 2016 at 1:30 AM, Jason Koh <[email protected]> wrote:
 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


Reply via email to