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();
    }
    
}

Reply via email to