You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Milorad Tosic <mb...@yahoo.com> on 2012/03/19 11:18:35 UTC

is GraphStore deepcopy or a facade?

Hi,

I am trying to understand how NamedGraphs work in Jena+TDB and how to use SPARQL for the purpose. The API that I found as an eventual candidate that would fit the task is GraphStore. My initial understanding was that GraphStore is a kind of facade on top of the TDB store that should be used in a case of incoming UPDATE query if there is a chance that the query eventually deals with NamedGraphs. However, the test gives somewhat unexpected results (suggesting that the GraphStore may actually be deep copy of the store?).

What am I missing here? I am afraid that my understanding of the conceptual model of NamedGraphs in Jena+TDB is not correct. How should the SPARQL query processing be organized in the case of TDB store working with named graphs?


Regards,
Milorad


public class rdfStoreNamedGraphsTesting {

    /**
     * @param args
     */
    public static void main(String[] args) {
        File m_baseDir;
        Model m_triplestore;
        Dataset m_dataset;
        
        String baseDirPath = "C:\\Temp";
        m_baseDir = new File(baseDirPath+File.separator+"tdb");
        boolean succ = m_baseDir.exists() && m_baseDir.isDirectory();
        if(!succ) succ = m_baseDir.mkdir();
        if(!succ){
            m_baseDir = null;
            m_triplestore = null;
            m_dataset = null;
            System.err.print("Error opening/creating new folder: "+baseDirPath);
        }else{

            m_dataset = TDBFactory.createDataset(m_baseDir.getPath()) ;
            m_triplestore = m_dataset.getDefaultModel();
            
        }
        
        String querystr;
        
        /**
         * Select distinct NamedGraphs in the store
         */
        querystr = "SELECT DISTINCT ?g WHERE { GRAPH ?g { } }";
        System.out.println("Running "+querystr);

        Query query = QueryFactory.create(querystr);  
        query.setPrefixMapping(m_triplestore);
        m_triplestore.enterCriticalSection(Lock.READ) ;
        try {
            QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
            ResultSet results = qexec.execSelect() ;
            
            System.out.println("============= Query result ============");
            ResultSetFormatter.output(System.out, results, ResultSetFormat.syntaxText);
            System.out.println("============= Query result ============");

            GraphStore graphStore = GraphStoreFactory.create(m_triplestore) ;
            System.out.println("GRAPHS number in store is "+graphStore.size());

            qexec.close();
        } finally { 
            m_triplestore.leaveCriticalSection(); 
        }

        /**
         * Create new NamedGraph in the store
         */
        querystr = "CREATE GRAPH <http://example.com/graphs/test1/>";
        System.out.println("Running "+querystr);

        m_triplestore.enterCriticalSection(Lock.WRITE) ;
        try {
            UpdateRequest updateRequest = UpdateFactory.create(querystr); 
            updateRequest.setPrefixMapping(m_triplestore);

            GraphStore graphStore = GraphStoreFactory.create(m_triplestore) ;
            UpdateAction.execute(updateRequest, graphStore);

            System.out.println("GRAPHS number in GraphStore is "+graphStore.size());
        }catch (Exception e){
            System.err.println(e);
        } finally {
            m_triplestore.commit();
            m_triplestore.getLock().leaveCriticalSection();
            TDB.sync(m_triplestore);
        }


        /**
         * Select distinct NamedGraphs in the store
         */
        querystr = "SELECT DISTINCT ?g WHERE { GRAPH ?g { } }";
        System.out.println("Running "+querystr);

        query = QueryFactory.create(querystr);  
        query.setPrefixMapping(m_triplestore);
        m_triplestore.enterCriticalSection(Lock.READ) ;
        try {
            QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
            ResultSet results = qexec.execSelect() ;
            
            System.out.println("============= Query result ============");
            ResultSetFormatter.output(System.out, results, ResultSetFormat.syntaxText);
            System.out.println("============= Query result ============");

            GraphStore graphStore = GraphStoreFactory.create(m_triplestore) ;
            System.out.println("GRAPHS number in store is "+graphStore.size());

            qexec.close();
        } finally { 
            m_triplestore.leaveCriticalSection(); 
        }
        System.out.println("==== finished ====");
        
        m_triplestore.close();
    }
    
}

Re: is GraphStore deepcopy or a facade?

Posted by Andy Seaborne <an...@apache.org>.
Your query says:

SELECT * WHERE
{GRAPH <http://example.com/graphs/test1/> { ?s ?p ?o . }}

but you are not querying the TDB dataset:

QueryExecutionFactory.create(query, m_triplestore) ;

You are querying one model as the default graph of a temporary dataset. 
  It is not called <http://example.com/graphs/test1/> in that dataset.

The fact the model is populated by the TDB union of named graphs does 
not give the triples any graph names.  A model is a set of triples; a 
model may be named within a dataset.

Remove the GRAPH part.

SELECT * WHERE { ?s ?p ?o . }

	Andy


On 20/03/12 20:36, Milorad Tosic wrote:
> I am re-posting the message to the list for the sake of completeness since I did reply on the personal address by mistake.
>
> Sorry,
> Milorad
>
>
>
>
>> ________________________________
>> From: Milorad Tosic<mb...@yahoo.com>
>> To: Dave Reynolds<da...@gmail.com>
>> Sent: Tuesday, March 20, 2012 2:27 PM
>> Subject: Re: is GraphStore deepcopy or a facade?
>>
>>
>> Hi,
>>
>>
>> Here is the code segment:
>>
>>
>>
>>          /**
>>           * Select triples in the NamedGraph in the store
>>           */
>>          querystr = "SELECT * WHERE {GRAPH<http://example.com/graphs/test1/>  { ?s ?p ?o . }}";
>>          System.out.println("Running "+querystr);
>>
>>          Query query = QueryFactory.create(querystr);
>>          m_triplestore = ModelFactory.createInfModel(
>>
>   ReasonerRegistry.getOWLMicroReasoner(), m_dataset.getNamedModel("urn:x-arq:UnionGraph"));
>>          query.setPrefixMapping(m_triplestore);
>>
>>          m_triplestore.getLock().enterCriticalSection(Lock.READ) ;
>>          try {
>>              QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
>>              ResultSet results = qexec.execSelect() ;
>>              System.out.println("++++++++");
>>              m_dataset.getNamedModel("urn:x-arq:UnionGraph").write(System.out, "Turtle");
>>              System.out.println("++++++++");
>>              System.out.println("============= Query result m_triplestore
>   ============");
>>              ResultSetFormatter.output(System.out, results, ResultSetFormat.syntaxText);
>>              System.out.println("============= Query result m_triplestore ============");
>>
>>              qexec.close();
>>          } finally {
>>              m_triplestore.getLock().leaveCriticalSection();
>>          }
>>
>>
>> while the output segment is:
>>
>>
>> Running SELECT * WHERE {GRAPH<http://example.com/graphs/test1/>  { ?s ?p ?o . }}
>> ++++++++
>> <http://example.org/book/book99>
>>
>   <http://purl.org/dc/elements/1.1/creator>
>>                "A.N.Other" ;
>>        <http://purl.org/dc/elements/1.1/title>
>>                "A new book" .
>> ++++++++
>> ============= Query result m_triplestore ============
>> -------------
>> | s | p | o |
>> =============
>> -------------
>> ============= Query result m_triplestore ============
>>
>>
>>
>>
>> I think my problem so far was that I expected to be able to query together default graph and union of named graphs. Since default graph contains triplets while named graphs contain quads, it could be expected highly unlikely to work. So, I tried to be more specific by using m_dataset.getNamedModel("urn:x-arq:UnionGraph") but it looks like it didn't work either?
>>
>>
>> Milorad
>>
>>
>>
>>
>>> ________________________________
>>> From: Dave Reynolds<da...@gmail.com>
>>> To: jena-users@incubator.apache.org
>>> Cc: Milorad Tosic<mb...@yahoo.com>
>>> Sent: Tuesday, March 20, 2012 1:47 PM
>>> Subject: Re: is GraphStore deepcopy or a facade?
>>>
>>> On 20/03/12 12:21, Milorad Tosic wrote:
>>>> Andy,
>>>>
>>>> Thank again. I think I understand what your advice is about. If I use dataset for every update query as follows:
>>>>
>>>>                UpdateRequest updateRequest = UpdateFactory.create(querystr);
>>>>                GraphStore graphStore = GraphStoreFactory.create(m_dataset) ;
>>>>                UpdateAction.execute(updateRequest, graphStore);
>>>>
>>>>
>>>> as well as for every select query as follows:
>>>>
>>>>                QueryExecution qexec = QueryExecutionFactory.create(query, m_dataset) ;
>>>>                ResultSet results = qexec.execSelect() ;
>>>>
>>>> then everything works perfectly fine and I get all triples that were previously inserted listed as a result of the
>   select query.
>>>>
>>>> However, if I want to use reasoner for select queries I would need a Model. It is true, isn't it? But if I try to use a Model on top of the existing dataset I got an empty result list:
>>>>
>>>>                m_triplestore = ModelFactory.createInfModel(ReasonerRegistry.getOWLMicroReasoner(), m_dataset.getDefaultModel());
>>>>                QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
>>>>                ResultSet results = qexec.execSelect() ;
>>>
>>> If there really is data in the default graph then that should work.
>>>
>>> Test it with something like:
>>>
>>>       m_dataset.getDefaultModel().write(System.out, "Turtle");
>>>
>>>> Basically, I can not make both Model and GraphStore to work over the same dataset. Does this mean that a reasoner can not work with (multiple)
>   named graphs?
>>>
>>> The reasoners, and reasoner API, predates datsets. You can reason over
>>> single models (or unions of them) and but can't doing anything useful
>>> reasoning over a dataset as a whole.  However, you get a Model out of a
>>> dataset and reason over that just fine.
>>>
>>>
>>> BTW there it is possible to use reasoners at the Graph level, there is
>>> an InfGraph just as there is an InfModel. However, that doesn't help here.
>>>
>>> Dave
>>>
>>>
>>>
>>
>>


Re: is GraphStore deepcopy or a facade?

Posted by Milorad Tosic <mb...@yahoo.com>.
I am re-posting the message to the list for the sake of completeness since I did reply on the personal address by mistake.

Sorry,
Milorad




>________________________________
> From: Milorad Tosic <mb...@yahoo.com>
>To: Dave Reynolds <da...@gmail.com> 
>Sent: Tuesday, March 20, 2012 2:27 PM
>Subject: Re: is GraphStore deepcopy or a facade?
> 
>
>Hi,
>
>
>Here is the code segment:
>
>
>  
>        /**
>         * Select triples in the NamedGraph in the store
>         */
>        querystr = "SELECT * WHERE {GRAPH <http://example.com/graphs/test1/> { ?s ?p ?o . }}";
>        System.out.println("Running "+querystr);
>
>        Query query = QueryFactory.create(querystr);  
>        m_triplestore = ModelFactory.createInfModel(
>               
 ReasonerRegistry.getOWLMicroReasoner(), m_dataset.getNamedModel("urn:x-arq:UnionGraph"));
>        query.setPrefixMapping(m_triplestore);
>        
>        m_triplestore.getLock().enterCriticalSection(Lock.READ) ;
>        try {
>            QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
>            ResultSet results = qexec.execSelect() ;
>            System.out.println("++++++++");
>            m_dataset.getNamedModel("urn:x-arq:UnionGraph").write(System.out, "Turtle");
>            System.out.println("++++++++");
>            System.out.println("============= Query result m_triplestore
 ============");
>            ResultSetFormatter.output(System.out, results, ResultSetFormat.syntaxText);
>            System.out.println("============= Query result m_triplestore ============");
>        
>            qexec.close();
>        } finally { 
>            m_triplestore.getLock().leaveCriticalSection(); 
>        }
>
>
>while the output segment is:
>
>
>Running SELECT * WHERE {GRAPH <http://example.com/graphs/test1/> { ?s ?p ?o . }}
>++++++++
><http://example.org/book/book99>
>     
 <http://purl.org/dc/elements/1.1/creator>
>              "A.N.Other" ;
>      <http://purl.org/dc/elements/1.1/title>
>              "A new book" .
>++++++++
>============= Query result m_triplestore ============
>-------------
>| s | p | o |
>=============
>-------------
>============= Query result m_triplestore ============
>
>
>
>
>I think my problem so far was that I expected to be able to query together default graph and union of named graphs. Since default graph contains triplets while named graphs contain quads, it could be expected highly unlikely to work. So, I tried to be more specific by using m_dataset.getNamedModel("urn:x-arq:UnionGraph") but it looks like it didn't work either? 
>
>
>Milorad
>
>
>
>
>>________________________________
>> From: Dave Reynolds <da...@gmail.com>
>>To: jena-users@incubator.apache.org 
>>Cc: Milorad Tosic <mb...@yahoo.com> 
>>Sent: Tuesday, March 20, 2012 1:47 PM
>>Subject: Re: is GraphStore deepcopy or a facade?
>> 
>>On 20/03/12 12:21, Milorad Tosic wrote:
>>> Andy,
>>>
>>> Thank again. I think I understand what your advice is about. If I use dataset for every update query as follows:
>>>
>>>              UpdateRequest updateRequest = UpdateFactory.create(querystr);
>>>              GraphStore graphStore = GraphStoreFactory.create(m_dataset) ;
>>>              UpdateAction.execute(updateRequest, graphStore);
>>>
>>>
>>> as well as for every select query as follows:
>>>
>>>              QueryExecution qexec = QueryExecutionFactory.create(query, m_dataset) ;
>>>              ResultSet results = qexec.execSelect() ;
>>>
>>> then everything works perfectly fine and I get all triples that were previously inserted listed as a result of the
 select query.
>>>
>>> However, if I want to use reasoner for select queries I would need a Model. It is true, isn't it? But if I try to use a Model on top of the existing dataset I got an empty result list:
>>>
>>>              m_triplestore = ModelFactory.createInfModel(ReasonerRegistry.getOWLMicroReasoner(), m_dataset.getDefaultModel());
>>>              QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
>>>              ResultSet results = qexec.execSelect() ;
>>
>>If there really is data in the default graph then that should work.
>>
>>Test it with something like:
>>
>>     m_dataset.getDefaultModel().write(System.out, "Turtle");
>>
>>> Basically, I can not make both Model and GraphStore to work over the same dataset. Does this mean that a reasoner can not work with (multiple)
 named graphs?
>>
>>The reasoners, and reasoner API, predates datsets. You can reason over 
>>single models (or unions of them) and but can't doing anything useful 
>>reasoning over a dataset as a whole.  However, you get a Model out of a 
>>dataset and reason over that just fine.
>>
>>
>>BTW there it is possible to use reasoners at the Graph level, there is 
>>an InfGraph just as there is an InfModel. However, that doesn't help here.
>>
>>Dave
>>
>>
>>
>
>

Re: is GraphStore deepcopy or a facade?

Posted by Milorad Tosic <mb...@yahoo.com>.
Thanks to you all! I think I sorted out the mess that I had with named graphs in sparql queries, in datasets and models. Now again it all makes sense, and it works so far so good.

Milorad



----- Original Message -----
> From: Dave Reynolds <da...@gmail.com>
> To: Milorad Tosic <mb...@yahoo.com>
> Cc: "jena-users@incubator.apache.org" <je...@incubator.apache.org>
> Sent: Tuesday, March 20, 2012 11:15 PM
> Subject: Re: is GraphStore deepcopy or a facade?
> 
> On 20/03/12 20:51, Milorad Tosic wrote:
>>  Comment is inserted:
>> 
>>>  ________________________________
>>>  From: Dave Reynolds<da...@gmail.com>
>>>  To: Milorad Tosic<mb...@yahoo.com>
>>>  Sent: Tuesday, March 20, 2012 2:51 PM
>>>  Subject: Re: is GraphStore deepcopy or a facade?
>>> 
>>>  Hi,
>>> 
>>>  On 20/03/12 13:27, Milorad Tosic wrote:
>>>>  Hi,
>>> 
>>>  Please keep the conversation on jena-dev so others can join in.
>>> 
>>>  But ...
>>> 
>>>>  Here is the code segment:
>>>> 
>>>> 
>>>>  /**
>>>>  * Select triples in the NamedGraph in the store
>>>>  */
>>>>  querystr = "SELECT * WHERE 
> {GRAPH<http://example.com/graphs/test1/>  {
>>>>  ?s ?p ?o . }}";
>>> 
>>>  So that query is requesting a specific named graph.
>>> 
>> 
>>  Yes.
>> 
>> 
>>>>  System.out.println("Running "+querystr);
>>>> 
>>>>  Query query = QueryFactory.create(querystr);
>>>>  m_triplestore = ModelFactory.createInfModel(
>>>>  ReasonerRegistry.getOWLMicroReasoner(),
>>>>  m_dataset.getNamedModel("urn:x-arq:UnionGraph"));
>>>>  query.setPrefixMapping(m_triplestore);
>>>> 
>>>>  m_triplestore.getLock().enterCriticalSection(Lock.READ) ;
>>>>  try {
>>>>  QueryExecution qexec = QueryExecutionFactory.create(query, 
> m_triplestore) ;
>>>>  ResultSet results = qexec.execSelect() ;
>>> 
>>>  But here you are querying a simple model so there is no named graph
>>>  there so the result will be empty.
>>> 
>> 
>>  The result shouldn't be empty since I am trying to get named model 
> "urn:x-arq:UnionGraph"!
> 
> Yes it should. When you query the Model you are querying one graph and 
> it has no name. Think of it like a dataset with just a default graph.
> The fact that the triples in that model originated by aggregating them 
> from some set of graphs in some dataset makes no difference.
> 
>>  The trick is that exactly the same configuration works without Model in 
> between?
> 
> Yes. Querying one model is fundamentally different from querying a 
> Dataset. A model by definition is just one graph of data, no names.
> 
>>  What I am trying to do is to apply some of the existing Jena models on a 
> set of named graphs within the same TDB dataset.
> 
> Don't follow what you mean by "apply" here.
> 
> If you want to reason over the data in the Dataset then as I've already 
> said you can only reason over a single graph/Model, a single collection 
> of triples. The UnionGraph gives you that. The result is just a bigger 
> single bunch of triples, still no graph names so query it as such.
> If you want to put the inferred triples back into the Dataset as some 
> new named graph then use getNamedModel() and add() the inferred data 
> into it.
> 
>>  I first tried to apply a Model on a integrated set of unnamed (default) 
> graph (set of triples) and one named graph (set of quads) residing within the 
> same dataset. However, it didn't work. Now, I want to try to apply Model on 
> the UnionGraph (that is supposed to contain all named graphs).
> 
> No the UnionGraph is a single graph that contains all the triples from 
> all the named graphs. That is not the same as containing the graphs 
> themselves along with their names.
> 
> Dave
> 

Re: is GraphStore deepcopy or a facade?

Posted by Dave Reynolds <da...@gmail.com>.
On 20/03/12 20:51, Milorad Tosic wrote:
> Comment is inserted:
>
>> ________________________________
>> From: Dave Reynolds<da...@gmail.com>
>> To: Milorad Tosic<mb...@yahoo.com>
>> Sent: Tuesday, March 20, 2012 2:51 PM
>> Subject: Re: is GraphStore deepcopy or a facade?
>>
>> Hi,
>>
>> On 20/03/12 13:27, Milorad Tosic wrote:
>>> Hi,
>>
>> Please keep the conversation on jena-dev so others can join in.
>>
>> But ...
>>
>>> Here is the code segment:
>>>
>>>
>>> /**
>>> * Select triples in the NamedGraph in the store
>>> */
>>> querystr = "SELECT * WHERE {GRAPH<http://example.com/graphs/test1/>  {
>>> ?s ?p ?o . }}";
>>
>> So that query is requesting a specific named graph.
>>
>
> Yes.
>
>
>>> System.out.println("Running "+querystr);
>>>
>>> Query query = QueryFactory.create(querystr);
>>> m_triplestore = ModelFactory.createInfModel(
>>> ReasonerRegistry.getOWLMicroReasoner(),
>>> m_dataset.getNamedModel("urn:x-arq:UnionGraph"));
>>> query.setPrefixMapping(m_triplestore);
>>>
>>> m_triplestore.getLock().enterCriticalSection(Lock.READ) ;
>>> try {
>>> QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
>>> ResultSet results = qexec.execSelect() ;
>>
>> But here you are querying a simple model so there is no named graph
>> there so the result will be empty.
>>
>
> The result shouldn't be empty since I am trying to get named model "urn:x-arq:UnionGraph"!

Yes it should. When you query the Model you are querying one graph and 
it has no name. Think of it like a dataset with just a default graph.
The fact that the triples in that model originated by aggregating them 
from some set of graphs in some dataset makes no difference.

> The trick is that exactly the same configuration works without Model in between?

Yes. Querying one model is fundamentally different from querying a 
Dataset. A model by definition is just one graph of data, no names.

> What I am trying to do is to apply some of the existing Jena models on a set of named graphs within the same TDB dataset.

Don't follow what you mean by "apply" here.

If you want to reason over the data in the Dataset then as I've already 
said you can only reason over a single graph/Model, a single collection 
of triples. The UnionGraph gives you that. The result is just a bigger 
single bunch of triples, still no graph names so query it as such.
If you want to put the inferred triples back into the Dataset as some 
new named graph then use getNamedModel() and add() the inferred data 
into it.

> I first tried to apply a Model on a integrated set of unnamed (default) graph (set of triples) and one named graph (set of quads) residing within the same dataset. However, it didn't work. Now, I want to try to apply Model on the UnionGraph (that is supposed to contain all named graphs).

No the UnionGraph is a single graph that contains all the triples from 
all the named graphs. That is not the same as containing the graphs 
themselves along with their names.

Dave

Re: is GraphStore deepcopy or a facade?

Posted by Milorad Tosic <mb...@yahoo.com>.
Comment is inserted:

>________________________________
> From: Dave Reynolds <da...@gmail.com>
>To: Milorad Tosic <mb...@yahoo.com> 
>Sent: Tuesday, March 20, 2012 2:51 PM
>Subject: Re: is GraphStore deepcopy or a facade?
> 
>Hi,
>
>On 20/03/12 13:27, Milorad Tosic wrote:
>> Hi,
>
>Please keep the conversation on jena-dev so others can join in.
>
>But ...
>
>> Here is the code segment:
>>
>>
>> /**
>> * Select triples in the NamedGraph in the store
>> */
>> querystr = "SELECT * WHERE {GRAPH <http://example.com/graphs/test1/> {
>> ?s ?p ?o . }}";
>
>So that query is requesting a specific named graph.
>

Yes.


>> System.out.println("Running "+querystr);
>>
>> Query query = QueryFactory.create(querystr);
>> m_triplestore = ModelFactory.createInfModel(
>> ReasonerRegistry.getOWLMicroReasoner(),
>> m_dataset.getNamedModel("urn:x-arq:UnionGraph"));
>> query.setPrefixMapping(m_triplestore);
>>
>> m_triplestore.getLock().enterCriticalSection(Lock.READ) ;
>> try {
>> QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
>> ResultSet results = qexec.execSelect() ;
>
>But here you are querying a simple model so there is no named graph 
>there so the result will be empty.
>

The result shouldn't be empty since I am trying to get named model "urn:x-arq:UnionGraph"! The trick is that exactly the same configuration works without Model in between? What I am trying to do is to apply some of the existing Jena models on a set of named graphs within the same TDB dataset. I first tried to apply a Model on a integrated set of unnamed (default) graph (set of triples) and one named graph (set of quads) residing within the same dataset. However, it didn't work. Now, I want to try to apply Model on the UnionGraph (that is supposed to contain all named graphs). However, if I run the same query against the Model I got empty result while against the underlying dataset the same query produces exactly what is expected.



>Whereas presumably a query such as:
>
>     SELECT * WHERE {?s ?p ?o . }
>
>will work.
>
>> System.out.println("++++++++");
>> m_dataset.getNamedModel("urn:x-arq:UnionGraph").write(System.out, "Turtle");
>> System.out.println("++++++++");
>> System.out.println("============= Query result m_triplestore ============");
>> ResultSetFormatter.output(System.out, results, ResultSetFormat.syntaxText);
>> System.out.println("============= Query result m_triplestore ============");
>>
>> qexec.close();
>> } finally {
>> m_triplestore.getLock().leaveCriticalSection();
>> }
>>
>> while the output segment is:
>>
>> Running SELECT * WHERE {GRAPH <http://example.com/graphs/test1/> { ?s ?p
>> ?o . }}
>> ++++++++
>> <http://example.org/book/book99>
>> <http://purl.org/dc/elements/1.1/creator>
>> "A.N.Other" ;
>> <http://purl.org/dc/elements/1.1/title>
>> "A new book" .
>> ++++++++
>> ============= Query result m_triplestore ============
>> -------------
>> | s | p | o |
>> =============
>> -------------
>> ============= Query result m_triplestore ============
>>
>>
>> I think my problem so far was that I expected to be able to query
>> together default graph and union of named graphs. Since default graph
>> contains triplets while named graphs contain quads, it could be expected
>> highly unlikely to work. So, I tried to be more specific by using
>> m_dataset.getNamedModel("urn:x-arq:UnionGraph") but it looks like it
>> didn't work either?
>
>If the above explanation doesn't help then perhaps you could explain (on 
>the list) what exactly it is you are trying do?
>
>Dave
>
>
>

Re: is GraphStore deepcopy or a facade?

Posted by Dave Reynolds <da...@gmail.com>.
On 20/03/12 12:21, Milorad Tosic wrote:
> Andy,
>
> Thank again. I think I understand what your advice is about. If I use dataset for every update query as follows:
>
>              UpdateRequest updateRequest = UpdateFactory.create(querystr);
>              GraphStore graphStore = GraphStoreFactory.create(m_dataset) ;
>              UpdateAction.execute(updateRequest, graphStore);
>
>
> as well as for every select query as follows:
>
>              QueryExecution qexec = QueryExecutionFactory.create(query, m_dataset) ;
>              ResultSet results = qexec.execSelect() ;
>
> then everything works perfectly fine and I get all triples that were previously inserted listed as a result of the select query.
>
> However, if I want to use reasoner for select queries I would need a Model. It is true, isn't it? But if I try to use a Model on top of the existing dataset I got an empty result list:
>
>              m_triplestore = ModelFactory.createInfModel(ReasonerRegistry.getOWLMicroReasoner(), m_dataset.getDefaultModel());
>              QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
>              ResultSet results = qexec.execSelect() ;

If there really is data in the default graph then that should work.

Test it with something like:

     m_dataset.getDefaultModel().write(System.out, "Turtle");

> Basically, I can not make both Model and GraphStore to work over the same dataset. Does this mean that a reasoner can not work with (multiple) named graphs?

The reasoners, and reasoner API, predates datsets. You can reason over 
single models (or unions of them) and but can't doing anything useful 
reasoning over a dataset as a whole.  However, you get a Model out of a 
dataset and reason over that just fine.


BTW there it is possible to use reasoners at the Graph level, there is 
an InfGraph just as there is an InfModel. However, that doesn't help here.

Dave

Re: is GraphStore deepcopy or a facade?

Posted by Milorad Tosic <mb...@yahoo.com>.
Andy,

Thank again. I think I understand what your advice is about. If I use dataset for every update query as follows:

            UpdateRequest updateRequest = UpdateFactory.create(querystr);     
            GraphStore graphStore = GraphStoreFactory.create(m_dataset) ;
            UpdateAction.execute(updateRequest, graphStore);


as well as for every select query as follows:

            QueryExecution qexec = QueryExecutionFactory.create(query, m_dataset) ;
            ResultSet results = qexec.execSelect() ;

then everything works perfectly fine and I get all triples that were previously inserted listed as a result of the select query.

However, if I want to use reasoner for select queries I would need a Model. It is true, isn't it? But if I try to use a Model on top of the existing dataset I got an empty result list:

            m_triplestore = ModelFactory.createInfModel(ReasonerRegistry.getOWLMicroReasoner(), m_dataset.getDefaultModel());
            QueryExecution qexec = QueryExecutionFactory.create(query, m_triplestore) ;
            ResultSet results = qexec.execSelect() ;

Basically, I can not make both Model and GraphStore to work over the same dataset. Does this mean that a reasoner can not work with (multiple) named graphs?

Milorad 


ps. Test code is basically the same as in my first question except small adjustments according to you suggestions, so I didn't paste it here due to readability.




>________________________________
> From: Andy Seaborne <an...@apache.org>
>To: jena-users@incubator.apache.org 
>Sent: Tuesday, March 20, 2012 11:55 AM
>Subject: Re: is GraphStore deepcopy or a facade?
> 
>On 20/03/12 06:39, Milorad Tosic wrote:
>> Andy,
>> 
>> Thanks for sharing the details with us, now it all makes sense. Now I
>> am able to ask the next question ;-)
>> 
>> What is memory vs. processing cost comparison for different dataset
>> wrappers (GraphStore, Model, InfModel, etc)?
>
>GraphStore does not add anything except call-throughs to the wrapped object.  JITting might even remove those.  In Java terms, it's trivial.  There's a lot more wrapping and conversion of abstraction going on all the time.
>
>A Model backed by a TDB dataset is again just calling through to the storage code.
>
>An InfModel is expensive over a database.
>
>> In other words, I assume
>> that one should keep a single dataset instance throughout whole
>> application since the instance is heavy resource vise. But what about
>> the wrappers? For example, If I want to work with a GraphStore across
>> an application, should I create new GraphStore instance over the
>> single datasource each time I need such a wrapper or should I have a
>> single GraphStore instance and reuse it across the whole application?
>> The same question holds for each other type of the datasource wrapper
>> also (Model, InfModel, ...).
>
>Create GraphStore on each update operation.
>
>With transactions, it's better to create it inside the Transaction and use only for the transaction.  This is more style than performance. GraphStore wrappers are very cheap.
>
>> The rationale for the question is as follows: If memory as well as
>> processing cost is negligible then instantiation of a wrapper is
>> voluntary and depends on one's programming style. If memory cost is
>> high while processing cost is low then the overall cost is reduced to
>> memory allocation only, and one should optimize according to the
>> particular system architecture. If memory cost is low while
>> instantiation processing cost is high then one should have a single
>> instance that is initialized once and kept in memory during whole
>> application life time. Since we are dealing with wrappers, the fourth
>> case, memory as well as processing cost are high, is probably not
>> possible.
>
>Each abstraction is there for a reason.  Worrying about performance until there is a observable problem can lead to excess development costs.
>
>    Andy
>
>> 
>> Milorad
>
>
>

Re: is GraphStore deepcopy or a facade?

Posted by Andy Seaborne <an...@apache.org>.
On 20/03/12 06:39, Milorad Tosic wrote:
> Andy,
>
> Thanks for sharing the details with us, now it all makes sense. Now I
> am able to ask the next question ;-)
>
> What is memory vs. processing cost comparison for different dataset
> wrappers (GraphStore, Model, InfModel, etc)?

GraphStore does not add anything except call-throughs to the wrapped 
object.  JITting might even remove those.  In Java terms, it's trivial. 
  There's a lot more wrapping and conversion of abstraction going on all 
the time.

A Model backed by a TDB dataset is again just calling through to the 
storage code.

An InfModel is expensive over a database.

> In other words, I assume
> that one should keep a single dataset instance throughout whole
> application since the instance is heavy resource vise. But what about
> the wrappers? For example, If I want to work with a GraphStore across
> an application, should I create new GraphStore instance over the
> single datasource each time I need such a wrapper or should I have a
> single GraphStore instance and reuse it across the whole application?
> The same question holds for each other type of the datasource wrapper
> also (Model, InfModel, ...).

Create GraphStore on each update operation.

With transactions, it's better to create it inside the Transaction and 
use only for the transaction.  This is more style than performance. 
GraphStore wrappers are very cheap.

> The rationale for the question is as follows: If memory as well as
> processing cost is negligible then instantiation of a wrapper is
> voluntary and depends on one's programming style. If memory cost is
> high while processing cost is low then the overall cost is reduced to
> memory allocation only, and one should optimize according to the
> particular system architecture. If memory cost is low while
> instantiation processing cost is high then one should have a single
> instance that is initialized once and kept in memory during whole
> application life time. Since we are dealing with wrappers, the fourth
> case, memory as well as processing cost are high, is probably not
> possible.

Each abstraction is there for a reason.  Worrying about performance 
until there is a observable problem can lead to excess development costs.

	Andy

>
> Milorad

Re: is GraphStore deepcopy or a facade?

Posted by Milorad Tosic <mb...@yahoo.com>.
Andy,

Thanks for sharing the details with us, now it all makes sense. Now I am able to ask the next question ;-)

What is memory vs. processing cost comparison for different dataset wrappers (GraphStore, Model, InfModel, etc)? In other words, I assume that one should keep a single dataset instance throughout whole application since the instance is heavy resource vise. But what about the wrappers? For example, If I want to work with a GraphStore across an application, should I create new GraphStore instance over the single datasource each time I need such a wrapper or should I have a single GraphStore instance and reuse it across the whole application? The same question holds for each other type of the datasource wrapper also (Model, InfModel, ...). 

The rationale for the question is as follows: If memory as well as processing cost is negligible then instantiation of a wrapper is voluntary and depends on one's programming style. If memory cost is high while processing cost is low then the overall cost is reduced to memory allocation only, and one should optimize according to the particular system architecture. If memory cost is low while instantiation processing cost is high then one should have a single instance that is initialized once and kept in memory during whole application life time. Since we are dealing with wrappers, the fourth case, memory as well as processing cost are high, is probably not possible. 

Milorad




>________________________________
> From: Andy Seaborne <an...@apache.org>
>To: jena-users@incubator.apache.org 
>Sent: Monday, March 19, 2012 5:00 PM
>Subject: Re: is GraphStore deepcopy or a facade?
> 
>On 19/03/12 10:18, Milorad Tosic wrote:
>> Hi,
>> 
>> I am trying to understand how NamedGraphs work in Jena+TDB and how to use SPARQL for the purpose. The API that I found as an eventual candidate that would fit the task is GraphStore. My initial understanding was that GraphStore is a kind of facade on top of the TDB store that should be used in a case of incoming UPDATE query if there is a chance that the query eventually deals with NamedGraphs. However, the test gives somewhat unexpected results (suggesting that the GraphStore may actually be deep copy of the store?).
>> 
>> What am I missing here? I am afraid that my understanding of the conceptual model of NamedGraphs in Jena+TDB is not correct. How should the SPARQL query processing be organized in the case of TDB store working with named graphs?
>> 
>> 
>> Regards,
>> Milorad
>
>
>Hi there,
>
>There is a problem in this line:
>
>>              GraphStore graphStore = GraphStoreFactory.create(m_triplestore) ;
>
>which takes a model, without knowing the dataset it comes from, and asks for a GraphStore.  A temporary dataset is created which in actually another in-memory dataset unconnected to the original m_dataset.
>
>Datasets are the unit of collections of graphs.
>
>Ideally, try to use datasets everywhere and think of the models as views into the dataset.  If you want to work across the model container, work on a dataset.
>
>But, beware, there is second trap waiting for you :-)
>
>TDB does not know about empty graphs - if it has no triples, as far as TDB is concerned, it does not exist.  CREATE GRAPH is a no-op for TDB. There is no separate management of graphs, only the triples and quads tables.  No entries => does not show up in
>
>SELECT ?g WHERE { GRAPH ?g { } }
>
>(you don't need DISTINCT)
>
>GraphStore is actually just a wrapper around the dataset - it's stateless but it works on datasets.
>
>    Andy
>
>
>

Re: is GraphStore deepcopy or a facade?

Posted by Andy Seaborne <an...@apache.org>.
On 19/03/12 10:18, Milorad Tosic wrote:
> Hi,
>
> I am trying to understand how NamedGraphs work in Jena+TDB and how to use SPARQL for the purpose. The API that I found as an eventual candidate that would fit the task is GraphStore. My initial understanding was that GraphStore is a kind of facade on top of the TDB store that should be used in a case of incoming UPDATE query if there is a chance that the query eventually deals with NamedGraphs. However, the test gives somewhat unexpected results (suggesting that the GraphStore may actually be deep copy of the store?).
>
> What am I missing here? I am afraid that my understanding of the conceptual model of NamedGraphs in Jena+TDB is not correct. How should the SPARQL query processing be organized in the case of TDB store working with named graphs?
>
>
> Regards,
> Milorad


Hi there,

There is a problem in this line:

>              GraphStore graphStore = GraphStoreFactory.create(m_triplestore) ;

which takes a model, without knowing the dataset it comes from, and asks 
for a GraphStore.  A temporary dataset is created which in actually 
another in-memory dataset unconnected to the original m_dataset.

Datasets are the unit of collections of graphs.

Ideally, try to use datasets everywhere and think of the models as views 
into the dataset.  If you want to work across the model container, work 
on a dataset.

But, beware, there is second trap waiting for you :-)

TDB does not know about empty graphs - if it has no triples, as far as 
TDB is concerned, it does not exist.  CREATE GRAPH is a no-op for TDB. 
There is no separate management of graphs, only the triples and quads 
tables.  No entries => does not show up in

SELECT ?g WHERE { GRAPH ?g { } }

(you don't need DISTINCT)

GraphStore is actually just a wrapper around the dataset - it's 
stateless but it works on datasets.

	Andy