Java Code Examples for org.openrdf.repository.RepositoryConnection#commit()
The following examples show how to use
org.openrdf.repository.RepositoryConnection#commit() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestNoExceptions.java From database with GNU General Public License v2.0 | 6 votes |
private void executeQuery(final SailRepository repo, final String query) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, RDFHandlerException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { final ValueFactory vf = conn.getValueFactory(); conn.commit(); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); TupleQueryResult tqr = tq.evaluate(); tqr.close(); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 2
Source File: TripleStoreBlazegraph.java From powsybl-core with Mozilla Public License 2.0 | 6 votes |
@Override public void read(InputStream is, String base, String name) { RepositoryConnection cnx = null; try { cnx = repo.getConnection(); cnx.begin(); Resource context = context(cnx, name); read(is, base, name, cnx, context); cnx.commit(); addNamespaceForBase(cnx, base); } catch (RepositoryException x) { throw new TripleStoreException(String.format("Reading. Repo problem %s %s", name, base), x); } finally { closeConnection(cnx, "Reading"); } }
Example 3
Source File: SampleCode.java From database with GNU General Public License v2.0 | 6 votes |
/** * Add a statement to a repository. * * @param repo * @throws Exception */ public void loadSomeData(Repository repo) throws Exception { RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { Resource s = new URIImpl("http://www.bigdata.com/rdf#Mike"); URI p = new URIImpl("http://www.bigdata.com/rdf#loves"); Value o = new URIImpl("http://www.bigdata.com/rdf#RDF"); Statement stmt = new StatementImpl(s, p, o); cxn.add(stmt); cxn.commit(); } catch (Exception ex) { cxn.rollback(); throw ex; } finally { // close the repository connection cxn.close(); } }
Example 4
Source File: SampleCode.java From database with GNU General Public License v2.0 | 5 votes |
/** * Demonstrate execution of a free-text query. * * @param repo * @throws Exception */ public boolean executeFreeTextQuery(Repository repo) throws Exception { if (((BigdataSailRepository) repo).getDatabase().getLexiconRelation() .getSearchEngine() == null) { /* * Only if the free text index exists. */ return false; } RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { cxn.add(new URIImpl("http://www.bigdata.com/A"), RDFS.LABEL, new LiteralImpl("Yellow Rose")); cxn.add(new URIImpl("http://www.bigdata.com/B"), RDFS.LABEL, new LiteralImpl("Red Rose")); cxn.add(new URIImpl("http://www.bigdata.com/C"), RDFS.LABEL, new LiteralImpl("Old Yellow House")); cxn.add(new URIImpl("http://www.bigdata.com/D"), RDFS.LABEL, new LiteralImpl("Loud Yell")); cxn.commit(); } catch (Exception ex) { cxn.rollback(); throw ex; } finally { // close the repository connection cxn.close(); } String query = "select ?x where { ?x <"+BDS.SEARCH+"> \"Yell\" . }"; executeSelectQuery(repo, query, QueryLanguage.SPARQL); // will match A, C, and D return true; }
Example 5
Source File: TestTicket276.java From database with GNU General Public License v2.0 | 5 votes |
private void executeQuery(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, RDFHandlerException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { final ValueFactory vf = conn.getValueFactory(); addData(conn); conn.commit(); final String query = "SELECT ?x { ?x ?a ?t . ?x ?lookup ?l }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding( "a", vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")); q.setBinding("t", vf.createURI("os:class/Location")); q.setBinding("lookup", vf.createURI("os:prop/lookupName")); q.setBinding("l", vf.createLiteral("amsterdam")); final TupleQueryResult tqr = q.evaluate(); while (tqr.hasNext()) { final Set<String> bindingNames = tqr.next() .getBindingNames(); if (log.isInfoEnabled()) log.info("bindingNames=" + bindingNames); } tqr.close(); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 6
Source File: TestTicket353.java From database with GNU General Public License v2.0 | 5 votes |
private void executeQuery(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, RDFHandlerException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { final ValueFactory vf = conn.getValueFactory(); conn.add(vf.createURI("os:subject"), vf.createURI("os:prop"), vf.createLiteral("value")); conn.commit(); String query = "SELECT ?b { {} union { ?a ?b ?c } }" ; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); TupleQueryResult tqr = tq.evaluate(); assertTrue(tqr.hasNext()); while (tqr.hasNext()) { System.err.println(tqr.next()); } tqr.close(); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 7
Source File: TestTicket2043b.java From database with GNU General Public License v2.0 | 5 votes |
public void testBug() throws Exception { final BigdataSail sail = getSail(); try { BigdataSailRepository repo = new BigdataSailRepository(sail); try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { conn.add(getClass().getResourceAsStream("TestTicket2043.n3"), "", RDFFormat.TURTLE); conn.commit(); // Check existing int literal executeQuery(conn, "1", 2); // Check not existing int literal executeQuery(conn, "2", 0); // Check not existing plain literal executeQuery(conn, "\"3\"", 0); // Check not existing boolean literal executeQuery(conn, "true", 0); // Check not existing datetime literal executeQuery(conn, "\"2000-01-01T00:00:00Z\"^^xsd:dateTime", 0); } finally { conn.close(); } } finally { repo.shutDown(); } } finally { sail.__tearDownUnitTest(); } }
Example 8
Source File: TestTicket2043.java From database with GNU General Public License v2.0 | 5 votes |
public void testBug() throws Exception { final BigdataSail sail = getSail(); try { BigdataSailRepository repo = new BigdataSailRepository(sail); try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { conn.add(getClass().getResourceAsStream("TestTicket2043.n3"), "", RDFFormat.TURTLE); conn.commit(); // Check existing int literal executeQuery(conn, "1", 2); // Check not existing int literal executeQuery(conn, "2", 0); // Check not existing plain literal executeQuery(conn, "\"3\"", 0); // Check not existing boolean literal executeQuery(conn, "true", 0); // Check not existing datetime literal executeQuery(conn, "\"2000-01-01T00:00:00Z\"^^xsd:dateTime", 0); } finally { conn.close(); } } finally { repo.shutDown(); } } finally { sail.__tearDownUnitTest(); } }
Example 9
Source File: SPARQLQueryTest.java From database with GNU General Public License v2.0 | 5 votes |
protected void upload(URI graphURI, Resource context) throws Exception { RepositoryConnection con = dataRep.getConnection(); try { con.begin(); RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString(), RDFFormat.TURTLE); RDFParser rdfParser = Rio.createParser(rdfFormat, dataRep.getValueFactory()); rdfParser.setVerifyData(false); rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE); // rdfParser.setPreserveBNodeIDs(true); RDFInserter rdfInserter = new RDFInserter(con); rdfInserter.enforceContext(context); rdfParser.setRDFHandler(rdfInserter); URL graphURL = new URL(graphURI.toString()); InputStream in = graphURL.openStream(); try { rdfParser.parse(in, graphURI.toString()); } finally { in.close(); } con.commit(); } catch (Exception e) { if (con.isActive()) { con.rollback(); } throw e; } finally { con.close(); } }
Example 10
Source File: RdfIndexerService.java From attic-polygene-java with Apache License 2.0 | 5 votes |
@Override public void notifyChanges( Iterable<EntityState> entityStates ) { try { if( repository == null || !repository.isActive() ) // has been shut down, or not yet started... { return; } final RepositoryConnection connection = repository.get().getConnection(); // The Repository is being initialized and not ready yet. // This happens when the Repository is being initialized and it is accessing its own configuration. if( connection == null ) { return; } connection.setAutoCommit( false ); try { removeEntityStates( entityStates, connection ); connection.commit(); final Set<EntityDescriptor> entityTypes = indexUpdates( entityStates, connection ); indexNewTypes( connection, entityTypes ); } finally { connection.commit(); connection.close(); } } catch( Throwable e ) { e.printStackTrace(); //TODO What shall we do with the exception? Probably figure out if we can recover, and possibly queue the state changes and retry later. } }
Example 11
Source File: Utils.java From blazegraph-samples with GNU General Public License v2.0 | 5 votes |
public static void loadDataFromResources(Repository repo, String resource, String baseURL) throws OpenRDFException, IOException { RepositoryConnection cxn = repo.getConnection(); try { cxn.begin(); try { InputStream is = SampleBlazegraphCustomFunctionEmbedded.class.getClassLoader().getResourceAsStream(resource); if (is == null) { throw new IOException("Could not locate resource: " + resource); } Reader reader = new InputStreamReader(new BufferedInputStream(is)); try { cxn.add(reader, baseURL, RDFFormat.N3); } finally { reader.close(); } cxn.commit(); } catch (OpenRDFException ex) { cxn.rollback(); throw ex; } } finally { // close the repository connection cxn.close(); } }
Example 12
Source File: TestTicket1681.java From database with GNU General Public License v2.0 | 5 votes |
private void executeQuery(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { conn.add(getClass().getResourceAsStream("TestTicket1681.nt"), "", RDFFormat.TURTLE); conn.commit(); final String query = "SELECT * WHERE { ?s <http://p> ?o . FILTER (?o IN (<http://o2>, <http://o3>) ) }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); final TupleQueryResult tqr = q.evaluate(); int cnt = 0; while (tqr.hasNext()) { final Set<String> bindingNames = tqr.next().getBindingNames(); cnt++; if(log.isInfoEnabled()) log.info("bindingNames="+bindingNames); } tqr.close(); assertEquals(1, cnt); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 13
Source File: TripleStoreBlazegraph.java From powsybl-core with Mozilla Public License 2.0 | 5 votes |
private String addStatement(RepositoryConnection cnx, String contextName, String objNs, String objType, PropertyBag properties) throws RepositoryException { cnx.begin(); String id = createStatements(cnx, objNs, objType, properties, context(cnx, contextName)); cnx.commit(); return id; }
Example 14
Source File: DemoSesameServer.java From database with GNU General Public License v2.0 | 4 votes |
public static void _main(String[] args) throws Exception { String sesameURL, repoID; if (args != null && args.length == 2) { sesameURL = args[0]; repoID = args[1]; } else { sesameURL = DemoSesameServer.sesameURL; repoID = DemoSesameServer.repoID; } Repository repo = new HTTPRepository(sesameURL, repoID); repo.initialize(); RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { // load some statements built up programmatically URI mike = new URIImpl(BD.NAMESPACE + "Mike"); URI bryan = new URIImpl(BD.NAMESPACE + "Bryan"); URI loves = new URIImpl(BD.NAMESPACE + "loves"); URI rdf = new URIImpl(BD.NAMESPACE + "RDF"); Graph graph = new GraphImpl(); graph.add(mike, loves, rdf); graph.add(bryan, loves, rdf); cxn.add(graph); cxn.commit(); } finally { cxn.close(); } { // show the entire contents of the repository SparqlBuilder sparql = new SparqlBuilder(); sparql.addTriplePattern("?s", "?p", "?o"); GraphQuery query = cxn.prepareGraphQuery( QueryLanguage.SPARQL, sparql.toString()); GraphQueryResult result = query.evaluate(); while (result.hasNext()) { Statement stmt = result.next(); System.err.println(stmt); } } }
Example 15
Source File: TestMaterialization.java From database with GNU General Public License v2.0 | 4 votes |
public void testXsdStr() throws Exception { final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection cxn = repo.getConnection(); try { cxn.setAutoCommit(false); final ValueFactory vf = sail.getValueFactory(); /* * Create some terms. */ final URI X = vf.createURI(BD.NAMESPACE + "X"); final Literal label = vf.createLiteral("John"); /* * Create some statements. */ cxn.add(X, RDF.TYPE, RDFS.RESOURCE); cxn.add(X, RDFS.LABEL, label); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.commit(); if (log.isInfoEnabled()) { log.info(((BigdataSailRepositoryConnection) cxn).getTripleStore().dumpStore()); } { String query = "select * where { ?s ?p ?o . FILTER (xsd:string(?o) = \""+RDF.PROPERTY+"\"^^xsd:string) }"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(true /* includeInferred */); if (log.isInfoEnabled()) { log.info(query); final TupleQueryResult result = tupleQuery.evaluate(); if (result.hasNext()) { while (result.hasNext()) log.info(result.next()); } else { fail("expecting result from the vocab"); } } // final Collection<BindingSet> answer = new LinkedList<BindingSet>(); // answer.add(createBindingSet( // new BindingImpl("a", paul), // new BindingImpl("b", mary) // )); // answer.add(createBindingSet( // new BindingImpl("a", brad), // new BindingImpl("b", john) // )); // // final TupleQueryResult result = tupleQuery.evaluate(); // compare(result, answer); } } finally { cxn.close(); } } finally { if (sail instanceof BigdataSail) ((BigdataSail)sail).__tearDownUnitTest();//shutDown(); } }
Example 16
Source File: TestMaterialization.java From database with GNU General Public License v2.0 | 4 votes |
public void testStr() throws Exception { final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection cxn = repo.getConnection(); try { cxn.setAutoCommit(false); final ValueFactory vf = sail.getValueFactory(); /* * Create some terms. */ final URI X = vf.createURI(BD.NAMESPACE + "X"); final Literal label = vf.createLiteral("John"); /* * Create some statements. */ cxn.add(X, RDF.TYPE, RDFS.RESOURCE); cxn.add(X, RDFS.LABEL, label); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.commit(); if (log.isInfoEnabled()) { log.info(((BigdataSailRepositoryConnection) cxn).getTripleStore().dumpStore()); } { String query = "select * where { ?s ?p ?o . FILTER (str(?o) = \""+RDF.PROPERTY+"\") }"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(true /* includeInferred */); if (log.isInfoEnabled()) { log.info(query); final TupleQueryResult result = tupleQuery.evaluate(); if (result.hasNext()) { while (result.hasNext()) log.info(result.next()); } else { fail("expecting result from the vocab"); } } // final Collection<BindingSet> answer = new LinkedList<BindingSet>(); // answer.add(createBindingSet( // new BindingImpl("a", paul), // new BindingImpl("b", mary) // )); // answer.add(createBindingSet( // new BindingImpl("a", brad), // new BindingImpl("b", john) // )); // // final TupleQueryResult result = tupleQuery.evaluate(); // compare(result, answer); } } finally { cxn.close(); } } finally { if (sail instanceof BigdataSail) ((BigdataSail)sail).__tearDownUnitTest();//shutDown(); } }
Example 17
Source File: TestSesameFilters.java From database with GNU General Public License v2.0 | 4 votes |
public void testRegex() throws Exception { // final Sail sail = new MemoryStore(); // sail.initialize(); // final Repository repo = new SailRepository(sail); final BigdataSail sail = getSail(); sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { final ValueFactory vf = sail.getValueFactory(); /* * Create some terms. */ final URI mike = vf.createURI(BD.NAMESPACE + "mike"); final URI bryan = vf.createURI(BD.NAMESPACE + "bryan"); final URI person = vf.createURI(BD.NAMESPACE + "Person"); final Literal l1 = vf.createLiteral("mike personick"); final Literal l2 = vf.createLiteral("bryan thompson"); /* * Create some statements. */ cxn.add(mike, RDF.TYPE, person); cxn.add(mike, RDFS.LABEL, l1); cxn.add(bryan, RDF.TYPE, person); cxn.add(bryan, RDFS.LABEL, l2); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.commit(); { String query = "prefix bd: <"+BD.NAMESPACE+"> " + "prefix rdf: <"+RDF.NAMESPACE+"> " + "prefix rdfs: <"+RDFS.NAMESPACE+"> " + "select * " + "where { " + " ?s rdf:type bd:Person . " + " ?s rdfs:label ?label . " + " FILTER regex(?label, \"mike\") . " + "}"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(false /* includeInferred */); final Collection<BindingSet> answer = new LinkedList<BindingSet>(); answer.add(createBindingSet( new BindingImpl("s", mike), new BindingImpl("label", l1) )); final TupleQueryResult result = tupleQuery.evaluate(); compare(result, answer); } } finally { cxn.close(); sail.shutDown(); } }
Example 18
Source File: TestTicket1682.java From database with GNU General Public License v2.0 | 4 votes |
private void executeQuery(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { conn.add(getClass().getResourceAsStream("TestTicket1682.nt"), "", RDFFormat.TURTLE); conn.commit(); final String query = "select ?s with { " + " select ?s where {" + " ?s <http://p> ?o" + " } VALUES (?o) {" + " (\"a\") (\"b\")" + " }" + "} AS %sub1 " + "where {" + " INCLUDE %sub1" + "}"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); final TupleQueryResult tqr = q.evaluate(); int cnt = 0; while (tqr.hasNext()) { final Set<String> bindingNames = tqr.next().getBindingNames(); cnt++; if(log.isInfoEnabled()) log.info("bindingNames="+bindingNames); } tqr.close(); assertEquals(2, cnt); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 19
Source File: TestTicket4249.java From database with GNU General Public License v2.0 | 4 votes |
public void testBug() throws Exception { final BigdataSail sail = getSail(); try { BigdataSailRepository repo = new BigdataSailRepository(sail); try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); ValueFactory vf = conn.getValueFactory(); try { conn.add(getClass().getResourceAsStream("TestTicket2043.n3"), "", RDFFormat.TURTLE); conn.commit(); executeQuery(conn, "123", -1, 2, ""); executeQuery(conn, "123", 0, 2, "1"); executeQuery(conn, "123", 1, 2, "12"); executeQuery(conn, "123", 1, 2, "12"); executeQuery(conn, "123", 100, 1, ""); executeQuery(conn, "12345", 1.5, 2.6, "234"); executeQuery(conn, "12345", 0, 3, "12"); executeQuery(conn, "12345", 0d/0d, 3, ""); executeQuery(conn, "12345", 1, 0d/0d, ""); executeQuery(conn, "12345", -42, 1d/0d, "12345"); executeQuery(conn, "12345", -1d/0d, 2d/0d, ""); executeQuery(conn, vf.createLiteral("foobar"), 4, vf.createLiteral("bar")); executeQuery(conn, vf.createLiteral("foobar","en"), 4, vf.createLiteral("bar", "en")); executeQuery(conn, vf.createLiteral("foobar", XMLSchema.STRING), 4, vf.createLiteral("bar", XMLSchema.STRING)); executeQuery(conn, vf.createLiteral("foobar"), 4, 1, vf.createLiteral("b")); executeQuery(conn, vf.createLiteral("foobar", "en"), 4, 1, vf.createLiteral("b", "en")); executeQuery(conn, vf.createLiteral("foobar", XMLSchema.STRING), 4, 1, vf.createLiteral("b", XMLSchema.STRING)); } finally { conn.close(); } } finally { repo.shutDown(); } } finally { sail.__tearDownUnitTest(); } }
Example 20
Source File: TestMaterialization.java From database with GNU General Public License v2.0 | 4 votes |
public void testRegex() throws Exception { final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection cxn = repo.getConnection(); try { cxn.setAutoCommit(false); final ValueFactory vf = sail.getValueFactory(); /* * Create some terms. */ final URI X = vf.createURI(BD.NAMESPACE + "X"); final Literal label = vf.createLiteral("John"); /* * Create some statements. */ cxn.add(X, RDF.TYPE, RDFS.RESOURCE); cxn.add(X, RDFS.LABEL, label); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.commit(); if (log.isInfoEnabled()) { log.info(((BigdataSailRepositoryConnection) cxn).getTripleStore().dumpStore()); } { final String query = "select * where { ?s ?p ?o . FILTER (regex(?o,\"John\",\"i\")) }"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(true /* includeInferred */); if (log.isInfoEnabled()) { log.info(query); final TupleQueryResult result = tupleQuery.evaluate(); while (result.hasNext()) { log.info(result.next()); } } // final Collection<BindingSet> answer = new LinkedList<BindingSet>(); // answer.add(createBindingSet( // new BindingImpl("a", paul), // new BindingImpl("b", mary) // )); // answer.add(createBindingSet( // new BindingImpl("a", brad), // new BindingImpl("b", john) // )); // // final TupleQueryResult result = tupleQuery.evaluate(); // compare(result, answer); } } finally { cxn.close(); } } finally { if (sail instanceof BigdataSail) ((BigdataSail)sail).__tearDownUnitTest();//shutDown(); } }