/** * Demonstrates some basics of working with named graphs. * * @param args unused * @throws AllegroGraphException */ publicstaticvoid main(String[] args)throwsAllegroGraphException{ // Connect to server, which must already be running. AllegroGraphConnection ags =newAllegroGraphConnection(); try{ ags.enable(); }catch(Exception e){ thrownewAllegroGraphException("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(); } }
/** * Demonstrates using multiple named Graphs in store, and inference * * @param args unused * @throws AllegroGraphException */ publicstaticvoid main(String[] args)throwsAllegroGraphException{ // Connect to server, which must already be running. AllegroGraphConnection ags =newAllegroGraphConnection(); try{ ags.enable(); }catch(Exception e){ thrownewAllegroGraphException("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 =newAllegroGraphGraphMaker(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 =newAllegroGraphReasoner().bind(graphTwo); Model modelAll =newAllegroGraphModel(graphAll); Model modelOne =newAllegroGraphModel(graphOne); Model modelTwo =newAllegroGraphModel(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(); }