/* * (c) Copyright 2008 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package dev; import java.net.InetAddress; import java.net.URI; import org.mulgara.server.Session; import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.graph.query.QueryHandler; import com.hp.hpl.jena.mulgara.GraphMulgara; import com.hp.hpl.jena.mulgara.JenaMulgara; import com.hp.hpl.jena.mulgara.PatternMulgara; import com.hp.hpl.jena.query.*; import com.hp.hpl.jena.rdf.model.*; import com.hp.hpl.jena.util.FileManager; import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.RDFS; public class Run { public static void main(String ...args) throws Exception { com.hp.hpl.jena.graph.query.Query q = new com.hp.hpl.jena.graph.query.Query(); Node varX = com.hp.hpl.jena.graph.query.Query.X ; Node varY = com.hp.hpl.jena.graph.query.Query.Y ; q.addMatch(Node.createURI("x:xyz"), RDFS.subClassOf.asNode(), varX ); q.addMatch( com.hp.hpl.jena.graph.query.Query.X, OWL.onProperty.asNode(), com.hp.hpl.jena.graph.query.Query.Y ); QueryHandler queryHandler = new PatternMulgara(null) ; // Note use of Query.X but not in projection. queryHandler.prepareBindings( q, new Node[] {varY} ); System.exit(0) ; System.exit(0) ; test1() ; System.exit(0) ; } public static void code(String ...args) throws Exception { //Host name incantation String hostname = InetAddress.getLocalHost().getCanonicalHostName(); // Server and model. String server = "rmi://localhost/server1" ; String graphURI = server+"#model1" ; Model model = JenaMulgara.connectModel(server, graphURI, true) ; Session session = ((GraphMulgara)(model.getGraph())).getSession() ; URI modelURI = ((GraphMulgara)(model.getGraph())).getModelURI() ; if ( false ) update(model) ; sparqlQuery("SELECT * { ?s ?p 'M_BLANK' }", model) ; RDFNode b = getOne("SELECT ?s { ?s ?p 'M_BLANK' }", model) ; //System.out.println(b) ; QuerySolutionMap init = new QuerySolutionMap() ; init.add("b", b) ; sparqlQuery("SELECT * { ?b ?p ?x }", model, init) ; Property p1 = model.createProperty("http://example/p-m") ; model.add((Resource)b, p1, "1812") ; sparqlQuery("SELECT * { ?b ?p ?x }", model, init) ; System.exit(0) ; // SPARQL query Query q ; QueryExecution qexec ; // //Query q = QueryFactory.create("SELECT * { ?s ?p ?o FILTER regex(?o, 'ARQ') }") ; q = QueryFactory.create("SELECT * { ?s ?p ?o }") ; qexec = QueryExecutionFactory.create(q, model) ; ResultSetFormatter.out(qexec.execSelect()) ; qexec.close(); // // StmtIterator sIter1 = model.listStatements(null, p1, o1) ; // while(sIter1.hasNext()) // { // System.out.println("I1 -- "+sIter1.nextStatement()) ; // } // // // Find a bNode // ResIterator rIter = model.listSubjectsWithProperty(p1, o2) ; // Resource b2 = rIter.nextResource() ; // rIter.close(); // // StmtIterator sIter2 = model.listStatements(b2, p1, (RDFNode)null) ; // while(sIter2.hasNext()) // { // System.out.println("I2 -- "+sIter2.nextStatement()) ; // } // // // Find by SPARQL // q = QueryFactory.create("SELECT * { ?s ?p 'BLANK' }") ; // qexec = QueryExecutionFactory.create(q, model) ; // Resource b3 = qexec.execSelect().nextSolution().getResource("s") ; // qexec.close() ; // // StmtIterator sIter3 = model.listStatements(b3, p1, (RDFNode)null) ; // while(sIter3.hasNext()) // { // System.out.println("I3 -- "+sIter3.nextStatement()) ; // } // NO. // Path by SPARQL q = QueryFactory.create("SELECT * { ?s ?p1 ?x . ?x ?p2 ?o }") ; qexec = QueryExecutionFactory.create(q, model) ; ResultSetFormatter.out(qexec.execSelect()) ; qexec.close(); model.close() ; session.close() ; } private static void test1() { String server = "rmi://localhost/server1" ; String graphURI = server+"#test" ; Model model = JenaMulgara.connectModel(server, graphURI, true) ; System.out.println("Model created") ; model.begin() ; FileManager.get().readModel(model, "test-data.ttl") ; model.commit() ; System.out.println("Model loaded") ; Model model2 = FileManager.get().loadModel("test-data.ttl") ; boolean b = model.isIsomorphicWith(model2) ; System.out.println("Model tested") ; System.out.println(b?"isomorphic":"**** not isomorphic") ; //if ( !b ) model.write(System.out, "TTL") ; Resource x = model.createResource("http://example/m#x") ; Property p = model.createProperty("http://example/m#p") ; StmtIterator sIter = model.listStatements(x, p, (RDFNode)null) ; System.out.println() ; System.out.println("Start iterator") ; while(sIter.hasNext()) System.out.println(sIter.next()) ; System.out.println("End iterator") ; System.out.println() ; JenaMulgara.dropGraph(server, graphURI) ; System.exit(1) ; } private static void test2() { String server = "rmi://localhost/server1" ; String graphURI = server+"#test" ; Model model = JenaMulgara.connectModel(server, graphURI, true) ; System.out.println("Model created") ; model.begin() ; FileManager.get().readModel(model, "test-data.ttl") ; model.commit() ; System.out.println("Model loaded") ; sparqlQuery("SELECT * {?s ?p ?o}", model) ; sparqlQuery("SELECT * { ?p ?o}", model) ; sparqlQuery("SELECT * {[] ?p 'xyz'}", model) ; JenaMulgara.dropGraph(server, graphURI) ; System.exit(1) ; } private static void sparqlQuery(String queryString, Model model) { sparqlQuery(queryString, model, null) ; } private static void sparqlQuery(String queryString, Model model, QuerySolution initialBinding) { Query q = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(q, model, initialBinding) ; ResultSetFormatter.out(qexec.execSelect()) ; qexec.close(); } private static RDFNode getOne(String queryString, Model model) { Query q = QueryFactory.create(queryString) ; if ( q.getResultVars().size() != 0 ) System.err.println("Warning: not a single result var: "+queryString) ; String vn = (String)q.getResultVars().get(0) ; QueryExecution qexec = QueryExecutionFactory.create(q, model) ; ResultSet rs = qexec.execSelect() ; if ( ! rs.hasNext() ) { System.err.println("Warning: not found: "+queryString) ; return null ; } RDFNode x = rs.nextSolution().get(vn) ; if ( rs.hasNext() ) System.err.println("Warning: more than one thing: "+queryString) ; qexec.close(); return x ; } private static void update(Model model) throws Exception { // { // SessionFactory sessionFactory = SessionFactoryFinder.newSessionFactory(new URI(server), true); // Session session = sessionFactory.newSession() ; // System.out.println(session.modelExists(new URI(graphURI))) ; // } //JRDFSession jrdfSession = null ; Session session = ((GraphMulgara)(model.getGraph())).getSession() ; URI modelURI = ((GraphMulgara)(model.getGraph())).getModelURI() ; if ( session.modelExists(modelURI) ) session.removeModel(modelURI) ; if ( ! session.modelExists(modelURI) ) session.createModel(modelURI, null) ; //model.removeAll() ; Resource s = null ; s = model.createResource("http://example/s1") ; Resource b1 = model.createResource() ; Resource b2 = model.createResource() ; Property p1 = model.createProperty("http://example/p1") ; Property p2 = model.createProperty("http://example/p2") ; Literal o1 = model.createLiteral("ARQtick") ; Literal o2 = model.createLiteral("BLANK") ; Literal o3 = model.createLiteral("shh") ; // model.add(s,p1,o1) ; // model.add(b,p1,o2) ; model.add(s,p2,b1) ; model.add(b1,p2,o3) ; model.add(b2,p2,o3) ; } //////URI modelURI = new URI("rmi://localhost/server1#model1") ; ////URI modelURI = new URI("rmi://localhost/server1#tracks") ; // //ItqlInterpreterBean interpreter = new ItqlInterpreterBean(); // //if ( ! session.modelExists(modelURI) ) //{ // System.out.println("Creating model: "+modelURI) ; // session.createModel(modelURI, null) ; // // String insStr1 = "insert 'BAR' into <"+modelURI+"> ;" ; // String insStr2 = "insert 'FOO' into <"+modelURI+"> ;" ; // interpreter.beginTransaction("T") ; // interpreter.executeUpdate(insStr1) ; // interpreter.executeUpdate(insStr2) ; // interpreter.commit("T") ; //// Query query = interpreter.buildQuery(insStr) ; //// session.query(query) ; // // autocommit ssession.commit() ; //} } /* * (c) Copyright 2008 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */