Virtuoso Open-Source Wiki
Virtuoso Open-Source, OpenLink Data Spaces, and OpenLink Ajax Toolkit
Advanced Search
Help?
Location: / Dashboard / Main / VOSIndex / VOSRDFDataProviders / VirtJenaProvider / VirtJenaSPARQLExample12

Virtuoso Jena Provider - SPARQL Example 12

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.vocabulary.DC;
import java.util.Iterator;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample12 {

    /**
     * Executes a SPARQL query against a virtuoso url and prints results.
     */
    public static void main(String[] args) 
    {
	String url;
	if(args.length == 0)
	    url = "jdbc:virtuoso://localhost:1111";
	else
	    url = args[0];

        Model m = VirtModel.openDatabaseModel("my:test", url, "dba", "dba");
        m.removeAll();

        Resource r1 = m.createResource("http://example.org/book#1") ;
        Resource r2 = m.createResource("http://example.org/book#2") ;
        
        r1.addProperty(DC.title, "SPARQL - the book")
          .addProperty(DC.description, "A book about SPARQL") ;
        
        r2.addProperty(DC.title, "Advanced techniques for SPARQL") ;

        String prolog = "PREFIX dc: <"+DC.getURI()+"> \n" ;
        
        // Query string.
        String queryString = prolog + "SELECT ?title WHERE {?x dc:title ?title}" ; 
        System.out.println("Execute query=\n"+queryString) ;
        System.out.println() ;

	Query query = QueryFactory.create(queryString) ;

System.out.println("\n==CASE 1 ==Parse ARQ  Execute ARQ  GraphStore Virtuoso");
//NOTE: query is parsed & executed by ARQ, so it works slow
// and you can't use Virtuoso SPARQL features
        QueryExecution qexec = QueryExecutionFactory.create(query, m) ;
        // Or QueryExecutionFactory.create(queryString, model) ;
        System.out.println("Titles: ") ;
        try {
            ResultSet rs = qexec.execSelect() ;
            for ( ; rs.hasNext() ; ) {
                QuerySolution rb = rs.nextSolution() ;
                RDFNode x = rb.get("title") ;
                if ( x.isLiteral() ) {
                    Literal titleStr = (Literal)x  ;
                    System.out.println("    "+titleStr) ;
                } else
                    System.out.println("Strange - not a literal: "+x) ;
            }
        } finally {
            qexec.close() ;
        }


System.out.println("\n==CASE 2a ==Parse ARQ  Execute Virtuoso  GraphStore Virtuoso");
//NOTE: query is parsed by ARQ, so you can't use Virtuoso SPARQL features
//      execution speed fast

        VirtuosoQueryEngine.register();
	
        qexec = QueryExecutionFactory.create(query, m) ;
        // Or QueryExecutionFactory.create(queryString, model) ;
        System.out.println("Titles: ") ;
        try {
            ResultSet rs = qexec.execSelect() ;
            for ( ; rs.hasNext() ; ) {
                QuerySolution rb = rs.nextSolution() ;
                RDFNode x = rb.get("title") ;
                if ( x.isLiteral() ) {
                    Literal titleStr = (Literal)x  ;
                    System.out.println("    "+titleStr) ;
                } else
                    System.out.println("Strange - not a literal: "+x) ;
            }
        } finally {
            qexec.close() ;
        }

        VirtuosoQueryEngine.unregister();
	

System.out.println("\n==CASE 2b ==Parse ARQ  Execute Virtuoso  GraphStore Virtuoso");
//NOTE: query is parsed by ARQ, so you can't use Virtuoso SPARQL features
//      execution speed fast
	
        qexec = VirtuosoQueryExecutionFactory.create(query, m) ;
        System.out.println("Titles: ") ;
        try {
            ResultSet rs = qexec.execSelect() ;
            for ( ; rs.hasNext() ; ) {
                QuerySolution rb = rs.nextSolution() ;
                RDFNode x = rb.get("title") ;
                if ( x.isLiteral() ) {
                    Literal titleStr = (Literal)x  ;
                    System.out.println("    "+titleStr) ;
                } else
                    System.out.println("Strange - not a literal: "+x) ;
            }
        } finally {
            qexec.close() ;
        }

	
System.out.println("\n==CASE 3 ==Parse & Execute Virtuoso  GraphStore Virtuoso");
//NOTE: query is parsed & executed by Virtuoso, so you can use all Virtuoso SPARQL features
//      execution speed fast

        qexec = VirtuosoQueryExecutionFactory.create(queryString, m) ;
        System.out.println("Titles: ") ;
        try {
            ResultSet rs = qexec.execSelect() ;
            for ( ; rs.hasNext() ; ) {
                QuerySolution rb = rs.nextSolution() ;
                RDFNode x = rb.get("title") ;
                if ( x.isLiteral() ) {
                    Literal titleStr = (Literal)x  ;
                    System.out.println("    "+titleStr) ;
                } else
                    System.out.println("Strange - not a literal: "+x) ;
            }
        } finally {
            qexec.close() ;
        }



    }
}
Powered By Virtuoso