AGSailGetStatements.java

package com.franz.agsail.examples;

import com.knowledgereefsystems.agsail.*;

import info.aduna.iteration.CloseableIteration;

import java.io.File;

import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.sail.*;

public class AGSailGetStatements {

    /**
     * Demonstrates basics of getting statements from a Sail
     * 
     * @param args unused
     * @throws AllegroGraphException
     */
    public static void main(String[] args) throws SailException {
        
        // Specify the SAIL, including AllegroGraph server and store parameters
        Sail 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();
       
        // 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 some statements to the store
        sc.addStatement(a, rdf_type, classA);
        sc.addStatement(classA,rdfs_subClassOf,classB);
        
        // Get some Statements without inference and show them
        System.out.println("Statements <ex:a, rdf:type, null> - no inference");
        CloseableIteration<? extends Statement, SailException> iter1 = sc.getStatements(a, rdf_type, null, false);
        try {
            while (iter1.hasNext()) {
                System.out.println(iter1.next());
            }
        } catch (SailException e) {
            iter1.close();
        }
        
        // Get some Statements with inference and show them
        System.out.println("Statements <ex:a, rdf:type, null> - with inference");
        CloseableIteration<? extends Statement, SailException> iter2 = sc.getStatements(a, rdf_type, null, true);
        try {
            while (iter2.hasNext()) {
                System.out.println(iter2.next());
            }
        } catch (SailException e) {
            iter2.close();
        }
        
        // Close the connection and shut down the Sail
        sc.close();
        sail.shutDown();
    }
}

Up | Next