AGNamedGraphs.java

package com.franz.agbase.examples;

import com.franz.agbase.*;

public class AGNamedGraphs {

    /**
     * Demonstrates some basics of working with named graphs.
     * 
     * @param args unused
     * @throws AllegroGraphException 
     */
    public static void main(String[] args) throws AllegroGraphException {
        // Connect to server, which must already be running.
        AllegroGraphConnection ags = new AllegroGraphConnection();
        try {
            ags.enable();
        } catch (Exception e) {
            throw new AllegroGraphException("Server connection problem", e);
        }
        // Create a fresh triple-store for this example.
        AllegroGraph ts = ags.renew("namedgraphs", AGPaths.TRIPLE_STORES);
        
        // Register any namespaces
        ts.registerNamespace("ex","http://example.org/");
        
        // Add a triple to the default graph in the store
        ts.addStatement("!ex:a", "!ex:p", "!ex:b");
        
        // Add a triple to a named graph in the store
        ts.addStatement("!ex:Dog","!rdf:type","!owl:Class","<http://animals.example.org>"); 
        
        // Show all triples in all graphs in the store
        System.out.println("Show all triples in all graphs in the store");
        TriplesIterator cc = ts.getStatements(null, null, null, null);
        AGUtils.showTriples(cc);
        
        // Show all triples in a named graph in the store
        System.out.println("Show all triples in graph http://animals.example.org");
        cc = ts.getStatements(null, null, null, "<http://animals.example.org>");
        AGUtils.showTriples(cc);
        
        // Show all triples in the default graph
        System.out.println("Show all triples in the default graph");
        cc = ts.getStatements(null, null, null);
        AGUtils.showTriples(cc);
        
        // Another way of showing all triples in the default graph
        System.out.println("Another way of showing all triples in the default graph");
        cc = ts.getStatements(null, null, null, "");
        AGUtils.showTriples(cc);
        
        // Close the store and disconnect from the server.
        ts.closeTripleStore();
        ags.disable();
    }
}

Up | Next

AGJenaNamedGraphs.java NOTE: the information on this page pertains to AllegroGraph version 3.x. There is no corresponding 4.x page at this time.

AGJenaNamedGraphs.java

package com.franz.agjena.examples;



import java.util.Iterator;

import com.franz.agbase.AllegroGraph;
import com.franz.agbase.AllegroGraphConnection;
import com.franz.agbase.AllegroGraphException;
import com.franz.agbase.examples.AGPaths;
import com.franz.agjena.AllegroGraphGraphMaker;
import com.franz.agjena.AllegroGraphModel;
import com.franz.agjena.AllegroGraphReasoner;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.rdf.model.Model;

public class AGJenaNamedGraphs {

    /**
     * Demonstrates using multiple named Graphs in store, and inference
     *
     * @param args unused
     * @throws AllegroGraphException
     */
    public static void main(String[] args) throws AllegroGraphException {
        // Connect to server, which must already be running.
        AllegroGraphConnection ags = new AllegroGraphConnection();
        try {
            ags.enable();
        } catch (Exception e) {
            throw new AllegroGraphException("Server connection problem", e);
        }

        // Create a fresh triple store
        AllegroGraph ts = ags.renew("jenatest4", AGPaths.TRIPLE_STORES);

        // Get a Graph Maker for the store
        AllegroGraphGraphMaker maker = new AllegroGraphGraphMaker(ts);
        maker.setDefaultIsGraphOfAllGraphs(true);

        // Create several Graphs and Models 
        String demoNamespace = "http://ag.franz.com/demo#";
        String graphName1 = demoNamespace + "context1";
        String graphName2 = demoNamespace + "context2";
        Graph graphAll = maker.getGraph(); // default graph represents the graph containing all triples 
        Graph graphOne = maker.createGraph(graphName1);
        Graph graphTwo = maker.createGraph(graphName2);
        Graph graphThree = new AllegroGraphReasoner().bind(graphTwo);
        Model modelAll = new AllegroGraphModel(graphAll);
        Model modelOne = new AllegroGraphModel(graphOne);
        Model modelTwo = new AllegroGraphModel(graphTwo);
        
        // Show all graph names
        for (Iterator<?> it = maker.listGraphs(); it.hasNext();) {
            System.out.println("GraphName: " + it.next());
        }

        // Load some data
        String inputFileName1 = AGPaths.dataSources("vc-db-1.rdf");
        String inputFileName2 = AGPaths.dataSources("football.nt");
        modelOne.read(inputFileName1, "RDF/XML" );
        modelTwo.read(inputFileName2, "N-TRIPLE" );
        
        // A query
        String query = "select ?s ?p ?o where {?s ?p ?o }";
        
        // return all triples (or none)
        AGJenaUtils.doQuery(query, modelAll);
        
        // retrieve all vcard triples
        AGJenaUtils.doQuery(query, modelOne);

        // retrieve all football triples (no inference)         
        AGJenaUtils.doQuery(query, modelTwo);

        // retrieve all football triples with inference
        AGJenaUtils.doQuery(query, graphThree);
    
        // Close the store and disconnect from the server
        ts.closeTripleStore();
        ags.disable();
    }

}

Up | Next

Copyright © 2023 Franz Inc., All Rights Reserved | Privacy Statement Twitter