Package com.hp.hpl.jena.tdb.store

Source Code of com.hp.hpl.jena.tdb.store.Test_SPARQL_TDB

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hp.hpl.jena.tdb.store;

import org.junit.Test ;
import org.openjena.atlas.junit.BaseTest ;

import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.graph.Triple ;
import com.hp.hpl.jena.query.Dataset ;
import com.hp.hpl.jena.query.Query ;
import com.hp.hpl.jena.query.QueryExecution ;
import com.hp.hpl.jena.query.QueryExecutionFactory ;
import com.hp.hpl.jena.query.QueryFactory ;
import com.hp.hpl.jena.query.ReadWrite ;
import com.hp.hpl.jena.query.ResultSet ;
import com.hp.hpl.jena.query.ResultSetFormatter ;
import com.hp.hpl.jena.rdf.model.Model ;
import com.hp.hpl.jena.rdf.model.ModelFactory ;
import com.hp.hpl.jena.sparql.sse.SSE ;
import com.hp.hpl.jena.tdb.TDBFactory ;
import com.hp.hpl.jena.tdb.base.file.Location ;
import com.hp.hpl.jena.update.GraphStore ;
import com.hp.hpl.jena.update.GraphStoreFactory ;
import com.hp.hpl.jena.update.UpdateExecutionFactory ;
import com.hp.hpl.jena.update.UpdateFactory ;
import com.hp.hpl.jena.update.UpdateProcessor ;
import com.hp.hpl.jena.update.UpdateRequest ;

/**
* Test SPARQL
*/
public class Test_SPARQL_TDB extends BaseTest
{
    private static Dataset create()
    {
        return TDBFactory.createDataset() ;
    }
   
    private static Dataset create(Location location)
    {
        return TDBFactory.createDataset(location) ;
    }

   
    @Test public void sparql1()
    {
        // Test OpExecutor.execute(OpFilter)for a named graph used as a standalone model
        String graphName = "http://example/" ;
        Triple triple = SSE.parseTriple("(<x> <y> 123)") ;
        Dataset ds = create() ;
        Graph g2 = ds.asDatasetGraph().getGraph(Node.createURI(graphName)) ;
        // Graphs only exists if they have a triple in them
        g2.add(triple) ;
       
        Model m = ModelFactory.createModelForGraph(g2) ;
        String qs = "SELECT * { ?s ?p ?o . FILTER ( ?o < 456 ) }" ;
        Query query = QueryFactory.create(qs) ;
        QueryExecution qexec = QueryExecutionFactory.create(query, m) ;
        ResultSet rs = qexec.execSelect() ;
        ResultSetFormatter.consume(rs) ;
    }
   
    @Test public void sparql2()
    {
        // Test OpExecutor.execute(OpBGP) for a named graph used as a standalone model
        String graphName = "http://example/" ;
        Triple triple = SSE.parseTriple("(<x> <y> 123)") ;
        Dataset ds = create() ;
        Graph g2 = ds.asDatasetGraph().getGraph(Node.createURI(graphName)) ;
        // Graphs only exists if they have a triple in them
        g2.add(triple) ;
       
        Model m = ModelFactory.createModelForGraph(g2) ;
        String qs = "SELECT * { ?s ?p ?o . }" ;
        Query query = QueryFactory.create(qs) ;
        QueryExecution qexec = QueryExecutionFactory.create(query, m) ;
        ResultSet rs = qexec.execSelect() ;
        ResultSetFormatter.consume(rs) ;
    }
   
    @Test public void sparql3()
    {
        // Requires OpDatasetNames
        Dataset dataset = create() ;
        Query query = QueryFactory.create("SELECT ?g { GRAPH ?g {} }") ;
        QueryExecution qExec = QueryExecutionFactory.create(query, dataset) ;
        ResultSet rs = qExec.execSelect() ;
        int n = ResultSetFormatter.consume(rs) ;
        assertEquals(0, n) ;
    }
   
    @Test public void sparql4()
    {
        // Requires OpDatasetNames
        Dataset dataset = create() ;
       
        String graphName = "http://example/" ;
        Triple triple = SSE.parseTriple("(<x> <y> 123)") ;
        Graph g2 = dataset.asDatasetGraph().getGraph(Node.createURI(graphName)) ;
        // Graphs only exists if they have a triple in them
        g2.add(triple) ;
       
        Query query = QueryFactory.create("SELECT ?g { GRAPH ?g {} }") ;
        QueryExecution qExec = QueryExecutionFactory.create(query, dataset) ;
        ResultSet rs = qExec.execSelect() ;
        int n = ResultSetFormatter.consume(rs) ;
        assertEquals(1, n) ;
    }
   
    // Test transactions effective.
   
    @Test public void sparql_txn_1()
    {
        Dataset dataset = create() ;
        update(dataset, "INSERT DATA { <x:s> <x:p> <x:o> }") ;

        dataset.begin(ReadWrite.READ) ;
        try {
            int n = count(dataset) ;
            assertEquals(1, n) ;
            n = count(dataset, "SELECT * { <x:s> <x:p> <x:o>}") ;
            assertEquals(1, n) ;
        } finally { dataset.end() ; }
    }

    @Test public void sparql_txn_2()
    {
        Dataset dataset1 = create(Location.mem("foo")) ;
        Dataset dataset2 = create(Location.mem("foo")) ;
       
        // Test the test setup.
        update(dataset1, "INSERT DATA { <x:s> <x:p> <x:o> }") ;
        //TDB.sync(dataset1) ;
        assertEquals(1, count(dataset2)) ;
       
        dataset1.begin(ReadWrite.READ) ;
       
        dataset2.begin(ReadWrite.WRITE) ;
        update(dataset2, "INSERT DATA { <x:s> <x:p> <x:o2> }") ;
       
        assertEquals(1, count(dataset1)) ;
        assertEquals(2, count(dataset2)) ;
        dataset2.commit();
        dataset2.end() ;
       
        // This is 2 if dataset1 is not in a transaction
        // but that replies on dataset2 commit doing the write back.
        assertEquals(1, count(dataset1))
       
        dataset1.end() ;
       
        dataset1.begin(ReadWrite.READ) ;
        assertEquals(2, count(dataset1)) ;
        dataset1.end() ;
    }

    private int count(Dataset dataset)
    { return count(dataset, "SELECT * { ?s ?p ?o }") ; }
   
    private int count(Dataset dataset, String queryString)
   
    {
        Query query = QueryFactory.create(queryString) ;
        QueryExecution qExec = QueryExecutionFactory.create(query, dataset) ;
        ResultSet rs = qExec.execSelect() ;
        return ResultSetFormatter.consume(rs) ;
    }


    private void update(Dataset dataset, String string)
    {
        UpdateRequest req = UpdateFactory.create(string) ;
        GraphStore gs = GraphStoreFactory.create(dataset) ;
        UpdateProcessor proc = UpdateExecutionFactory.create(req, gs) ;
        proc.execute() ;
    }
}
TOP

Related Classes of com.hp.hpl.jena.tdb.store.Test_SPARQL_TDB

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.