AGSailSparqlQueries.java

package com.franz.agsail.examples;

import com.knowledgereefsystems.agsail.*;

import info.aduna.iteration.CloseableIteration;

import java.io.File;
import java.util.Iterator;

import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.query.BindingSet;
import org.openrdf.query.Dataset;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.impl.EmptyBindingSet;
import org.openrdf.query.parser.ParsedQuery;
import org.openrdf.query.parser.sparql.SPARQLParser;
import org.openrdf.sail.*;

public class AGSailSparqlQueries {

    /**
     * Demonstrates basics of executing SPARQL Queries with Sail
     * 
     * @param args unused
     * @throws AllegroGraphException
     */
    public static void main(String[] args) throws Exception {
        
        // Specify the SAIL, including AllegroGraph server and store parameters
        AllegroSail sail = new AllegroSail("localhost", 4567, false, "store", new File(AGPaths.TRIPLE_STORES), 0, 0, false, false);

        // Connect to the server and open the specified store (create it if necessary)  
        sail.initialize();
        SailConnection sc = sail.getConnection();
        sc.clear();
        
        // Create some Values to work with
        ValueFactory f = sail.getValueFactory();
        String ex = "http://example.org/";
        String rdf = sc.getNamespace("rdf");
        String rdfs = sc.getNamespace("rdfs");
        URI a = f.createURI(ex, "a");
        URI classA = f.createURI(ex, "A");
        URI classB = f.createURI(ex, "B");
        URI rdf_type = f.createURI(rdf, "type");
        URI rdfs_subClassOf = f.createURI(rdfs, "subClassOf");
        
        // Add a statement to the store
        sc.addStatement(a, a, a);
        sc.commit();

        // Check that we see it with SPARQL 
        String queryStr = "SELECT ?y ?z WHERE { <http://example.org/a> ?y ?z }";
        doSparqlQuery(sc,queryStr);
       
        // Add more statements to the store
        sc.addStatement(a, rdf_type, classA);
        sc.addStatement(classA,rdfs_subClassOf,classB);

        // Check that we see them with SPARQL 
        queryStr = "SELECT * WHERE { ?s ?p ?o }";
        doSparqlQuery(sc,queryStr);
        
        // Close the connection and shut down the Sail
        sc.close();
        sail.shutDown();
    }
    
    public static void doSparqlQuery(SailConnection sc, String queryStr) throws Exception {
        SPARQLParser parser = new SPARQLParser();
        BindingSet bindings = new EmptyBindingSet();
        String baseURI = "http://example.org/bogus/";
        ParsedQuery query;
        CloseableIteration<? extends BindingSet, QueryEvaluationException> results;

        System.out.println("Query: " + queryStr);
        query = parser.parseQuery(queryStr, baseURI);
        TupleExpr tupleExpr = query.getTupleExpr();
        Dataset dataset = query.getDataset();
        results = sc.evaluate(tupleExpr, dataset, bindings, false);
        for (int i=0; results.hasNext(); i++) {
            BindingSet set = results.next();
            Iterator<org.openrdf.query.Binding> it = set.iterator();
            System.out.println("Solution " + i);
            while (it.hasNext()) {
                System.out.println("  " + it.next());
            }
        }
        results.close();
    }
}

Up | Next