It's bound to the transaction. You can create an in-memory copy of the
resultset with ResultSetFactory [1] if you really need it outside of the
transaction.

By the way, you don't need the RDF namespace declaration in your query,
rdf:type can be replaced with the keyword "a" because SPARQL syntax is
based on Turtle syntax:

PREFIX kg:<%s>
SELECT ?entityUri ?name ?unit
WHERE
{ ?entityUri      a                         kg:AtomicMetric ;
                        kg:Name            ?name.
  OPTIONAL { ?entityUri      kg:Unit     ?unit } }


[1]
https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/query/ResultSetFactory.html#copyResults-org.apache.jena.query.ResultSet-


On 27.12.2017 05:00, hetong583 wrote:
> Hi,
> It seems ResultSet is not usable after dataset has ended in this case. 
> Invoking `ds.end()` after utilizing ResultSet solved this problem.
> Thanks for your time.
>
>
> Best regards.
>
>
> ------------------
> Wang Tonghe
>
>
>  
>
>
>
>
> ------------------ Original ------------------
> From:  "Marc Agate";<[email protected]>;
> Date:  Wed, Dec 27, 2017 11:34 AM
> To:  "users"<[email protected]>;
>
> Subject:  Re:   RDF namespace is not compatible with OPTIONAL in ARQ
>
>
>
> Well, I use rdf namespace with Optional in almost all my queries and it
> wouldn't make any sense to not being able to use it with OPTIONAL.
>
> Print out your query and check the value of your k: prefix.
>
> Regarding the ResultSet, I am not sure it is still usable once the
> transaction has ended (ds.end()). You should use it in the method,
> eventually calling a utility class to process it and then return the
> processed data instead of the ResultSet itself.
>
> Marc
>
> Le mercredi 27 décembre 2017 à 11:22 +0800, hetong583 a écrit :
>> Hi,
>> 1)  I am sure. Everything works fine if remove OPTIONAL clause. Any
>> change on rdf namespace also works, but you know, resultset is empty.
>> 2) Code failed while modelUri is not null. We always use a named
>> graph.
>>
>>
>> We use `TDBFactory.createDataset(tdbDir)` creating dataset. I think
>> it's a in memory dataset. No .lock file was found in our data
>> directory.
>>
>>
>> Thanks for your reply.
>> Best regards.
>>
>>
>> ------------------
>> Wang Tonghe
>>
>>
>>  
>>
>>
>>
>>
>> ------------------ Original ------------------
>> From:  "Marc Agate";<[email protected]>;
>> Date:  Wed, Dec 27, 2017 11:01 AM
>> To:  "users"<[email protected]>;
>>
>> Subject:  Re:  RDF namespace is not compatible with OPTIONAL in ARQ
>>
>>
>>
>> Hi,
>>
>> 1) Are you sure the placeholder substitution is coorect in : PREFIX
>> kg:<%s> ? (print out the query string after substitution)
>> 2) in which case is your code failing , modelUri null or modelUri not
>> null ?
>>
>> Are you switching between a named graph and the default model ?
>>
>> Is this a in memory dataset or not ? If not check for a .lock file in
>> your data directory.
>>
>>
>>
>> Marc
>>
>>
>> Le mercredi 27 décembre 2017 à 10:42 +0800, hetong583 a écrit :
>>> Yes, this is my execution function
>>> ```
>>>   public ResultSet execSelect(String queryStr) {
>>>     ds.begin(ReadWrite.READ);
>>>     try {
>>>       Query query = QueryFactory.create(queryStr);
>>>       QueryExecution qexec;
>>>       if (modelUri != null) {
>>>         qexec = QueryExecutionFactory.create(query,
>>> ds.getNamedModel(modelUri));
>>>       } else {
>>>         qexec = QueryExecutionFactory.create(query, ds);
>>>       }
>>>       return qexec.execSelect();
>>>     } catch (Exception e) {
>>>       logger.error(e.getMessage(), e);
>>>     } finally {
>>>       ds.end();
>>>     }
>>>     return null;
>>>   }
>>>
>>>
>>>
>>> ```
>>>
>>>
>>> ------------------
>>> Wang Tonghe
>>>
>>>
>>>  
>>>
>>>
>>>
>>>
>>> ------------------ Original ------------------
>>> From:  "Marc Agate";<[email protected]>;
>>> Date:  Wed, Dec 27, 2017 10:35 AM
>>> To:  "users"<[email protected]>;
>>>
>>> Subject:  Re: RDF namespace is not compatible with OPTIONAL in ARQ
>>>
>>>
>>>
>>> Hi!
>>>
>>> Did you first start the transaction with
>>> yourdataset.begin(ReadWrite.READ) ?
>>>
>>> Marc
>>>
>>> Le mercredi 27 décembre 2017 à 10:24 +0800, hetong583 a écrit :
>>>> Hello,
>>>> I tried to access TDB via SPARQL. However following query always
>>>> throws a TDBTransactionException: Not in a transaction.
>>>> ```
>>>> PREFIX kg:<%s>
>>>> PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
>>>> SELECT ?entityUri ?name ?unit 
>>>> WHERE
>>>> { ?entityUri      rdf:type         kg:AtomicMetric. 
>>>>   ?entityUri       kg:Name            ?name. 
>>>>   OPTIONAL { ?entityUri      kg:Unit     ?unit } }
>>>>
>>>> ```
>>>> After several tries, I found that rdf namespace and `OPTIONAL`
>>>> cannot
>>>> appear simultaneously. And any modification on "http://www.w3.org
>>>> /1
>>>> 99
>>>> 9/02/22-rdf-syntax-ns#" will solve the problem. I use Jena 3.5
>>>> and
>>>> I
>>>> found 3.6 also has this problom.
>>>>
>>>>
>>>> Following is the stacktrace.
>>>> ```
>>>> org.apache.jena.tdb.transaction.TDBTransactionException: Not in a
>>>> transaction
>>>>
>>>>
>>>>    at
>>>> org.apache.jena.tdb.transaction.DatasetGraphTransaction.get(Datas
>>>> et
>>>> Gr
>>>> aphTransaction.java:117)
>>>>    at
>>>> org.apache.jena.tdb.transaction.DatasetGraphTransaction.getDatase
>>>> tG
>>>> ra
>>>> phToQuery(DatasetGraphTransaction.java:80)
>>>>    at
>>>> org.apache.jena.tdb.store.GraphTxnTDB.getDatasetGraphTDB(GraphTxn
>>>> TD
>>>> B.
>>>> java:49)
>>>>    at
>>>> org.apache.jena.tdb.store.GraphTDB.getNodeTupleTable(GraphTDB.jav
>>>> a:
>>>> 74
>>>> )
>>>>    at
>>>> org.apache.jena.tdb.solver.SolverLib.execute(SolverLib.java:66)
>>>>    at
>>>> org.apache.jena.tdb.solver.StageGeneratorDirectTDB.execute(StageG
>>>> en
>>>> er
>>>> atorDirectTDB.java:56)
>>>>    at
>>>> org.apache.jena.tdb2.solver.StageGeneratorDirectTDB.execute(Stage
>>>> Ge
>>>> ne
>>>> ratorDirectTDB.java:53)
>>>>    at
>>>> org.apache.jena.sparql.engine.main.OpExecutor.execute(OpExecutor.
>>>> ja
>>>> va
>>>> :128)
>>>>    at
>>>> org.apache.jena.sparql.engine.main.ExecutionDispatch.visit(Execut
>>>> io
>>>> nD
>>>> ispatch.java:58)
>>>>    at org.apache.jena.sparql.algebra.op.OpBGP.visit(OpBGP.java:49)
>>>>    at
>>>> org.apache.jena.sparql.engine.main.ExecutionDispatch.exec(Executi
>>>> on
>>>> Di
>>>> spatch.java:46)
>>>>    at
>>>> org.apache.jena.sparql.engine.main.OpExecutor.exec(OpExecutor.jav
>>>> a:
>>>> 11
>>>> 7)
>>>>    at
>>>> org.apache.jena.sparql.engine.main.OpExecutor.execute(OpExecutor.
>>>> ja
>>>> va
>>>> :88)
>>>>    at org.apache.jena.sparql.engine.main.QC.execute(QC.java:52)
>>>>    at
>>>> org.apache.jena.sparql.engine.main.iterator.QueryIterOptionalInde
>>>> x.
>>>> ne
>>>> xtStage(QueryIterOptionalIndex.java:51)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIterRepeatApply.makeN
>>>> ex
>>>> tS
>>>> tage(QueryIterRepeatApply.java:108)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIterRepeatApply.hasNe
>>>> xt
>>>> Bi
>>>> nding(QueryIterRepeatApply.java:65)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(
>>>> Qu
>>>> er
>>>> yIteratorBase.java:114)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIterConvert.hasNextBi
>>>> nd
>>>> in
>>>> g(QueryIterConvert.java:58)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(
>>>> Qu
>>>> er
>>>> yIteratorBase.java:114)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNe
>>>> xt
>>>> Bi
>>>> nding(QueryIteratorWrapper.java:39)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(
>>>> Qu
>>>> er
>>>> yIteratorBase.java:114)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNe
>>>> xt
>>>> Bi
>>>> nding(QueryIteratorWrapper.java:39)
>>>>    at
>>>> org.apache.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(
>>>> Qu
>>>> er
>>>> yIteratorBase.java:114)
>>>>    at
>>>> org.apache.jena.sparql.engine.ResultSetStream.hasNext(ResultSetSt
>>>> re
>>>> am
>>>> .java:74)
>>>>    at
>>>> org.apache.jena.sparql.engine.ResultSetCheckCondition.hasNext(Res
>>>> ul
>>>> tS
>>>> etCheckCondition.java:55)
>>>>    at
>>>> com.quantchi.intelquery.etl.LtpDict.getEntityFromTdb(LtpDict.java
>>>> :8
>>>> 7)
>>>>
>>>> ```
>>>>
>>>>
>>>> Thanks for your help.  Let me know if you need any other
>>>> infomation.
>>>> Best regards.
>>>>
>>>>
>>>>
>>>>
>>>> ------------------
>>>> Wang Tonghe

Reply via email to