Java Code Examples for org.openrdf.repository.RepositoryConnection#setAutoCommit()
The following examples show how to use
org.openrdf.repository.RepositoryConnection#setAutoCommit() .
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: 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 2
Source File: TestTicket632.java From database with GNU General Public License v2.0 | 6 votes |
private void executeQuery(final URI serviceUri, final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, RDFHandlerException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); final ValueFactory vf = conn.getValueFactory(); conn.setAutoCommit(false); try { final String query = "SELECT ?x { SERVICE <" + serviceUri.stringValue() + "> { ?x <u:1> ?bool1 } }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("bool1", vf.createLiteral(true)); final TupleQueryResult tqr = q.evaluate(); try { tqr.hasNext(); } finally { tqr.close(); } } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 3
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 4
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 5
Source File: TestTicket275.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("TestTicket275.ttl"), "", RDFFormat.TURTLE); conn.commit(); final String query = "SELECT ?lookup WHERE { ?lookup <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <os:class/Lookup> . ?lookup <os:prop/lookup/majorType> ?majorType . OPTIONAL{?lookup <os:prop/lookup/minorType> ?minorType}. FILTER(STR(?majorType) = ?argMajorType). FILTER(!bound(?minorType))}"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("argMajorType", conn.getValueFactory() .createLiteral("majoor")); 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: 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 7
Source File: TestRollbacks.java From database with GNU General Public License v2.0 | 5 votes |
public Void call() throws Exception { // if (writer) { // // Initial sleep on the writer. // Thread.sleep(500); // } RepositoryConnection conn = null; try { int counter2 = 0; conn = repo.getConnection(); conn.setAutoCommit(false); while (firstCause.get() == null && counter < maxCounter) { if (writer) writer(conn); else reader(conn); /* * Note: If connection obtained/closed within the loop then * the query is more likely to have some data to visit * within its tx view. */ if (++counter2 % 4 == 0) { conn.close(); conn = repo.getConnection(); conn.setAutoCommit(false); } // conn = repo.getConnection(); // conn.setAutoCommit(false); // conn.close(); } return (Void) null; } catch (Throwable t) { firstCause.compareAndSet(null/* expect */, t); throw new RuntimeException(t); } finally { if (conn != null) conn.close(); } }
Example 8
Source File: TestTicket348.java From database with GNU General Public License v2.0 | 5 votes |
private void executeTest(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, RDFHandlerException, IOException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); try { conn.setAutoCommit(false); final ValueFactory vf = conn.getValueFactory(); final URI uri = vf.createURI("os:/elem/example"); // run a query which looks for a statement and then adds it if it is not found. addDuringQueryExec(conn, uri, RDF.TYPE, vf.createURI("os:class/Clazz")); // now try to export the statements. final RepositoryResult<Statement> stats = conn.getStatements(null, null, null, false); try { // materialize the newly added statement. stats.next(); } catch (RuntimeException e) { fail(e.getLocalizedMessage(), e); // With Bigdata this fails } finally { stats.close(); } conn.rollback(); // discard the result (or commit, but do something to avoid a logged warning from Sesame). } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 9
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 10
Source File: TestTicket967.java From database with GNU General Public License v2.0 | 5 votes |
private void executeTest(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, RDFHandlerException, IOException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); try { conn.setAutoCommit(false); final ValueFactory vf = conn.getValueFactory(); final URI uri = vf.createURI("os:/elem/example"); // run a query which looks for a statement and then adds it if it is not found. addDuringQueryExec(conn, uri, RDF.TYPE, vf.createURI("os:class/Clazz")); // now try to export the statements. final RepositoryResult<Statement> stats = conn.getStatements(null, null, null, false); try { // materialize the newly added statement. stats.next(); } catch (RuntimeException e) { fail(e.getLocalizedMessage(), e); // With Bigdata this fails } finally { stats.close(); } conn.rollback(); // discard the result (or commit, but do something to avoid a logged warning from Sesame). } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 11
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 12
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 13
Source File: StressTest_ClosedByInterrupt_RW.java From database with GNU General Public License v2.0 | 4 votes |
@Override public void run() { RepositoryConnection conn = null; TupleQueryResult result = null; int loop = 0; while (stopRequested == false) { try { System.out.println("[Read ] snooze"); snooze(MILLIS_BETWEEN_QUERY_BURSTS); System.out.println("[Read ] enter loop " + loop); for (int invocation = 0; invocation < NUM_SELECTS; ++invocation) { conn = repo.getReadOnlyConnection(); conn.setAutoCommit(false); final String sparql = "SELECT ?s WHERE { ?s ?p ?o } LIMIT " + NUM_STATEMENTS_PER_SELECT; final TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, sparql); result = query.evaluate(); final List<String> duds = new ArrayList<String>(); while (result.hasNext()) { final BindingSet bindingSet = result.next(); for (final Iterator<Binding> i = bindingSet.iterator(); i.hasNext();) { final Binding b = i.next(); if (b.getValue() != null) { duds.add(b.getValue().stringValue()); } } } result.close(); result = null; conn.close(); conn = null; } } catch (Throwable t) { printError("Read Only threw in loop " + loop, t); } finally { closeNoException(result); closeNoException(conn); } System.out.println("[Read ] leave loop " + loop); ++loop; } }
Example 14
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 15
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 16
Source File: TestSingleTailRule.java From database with GNU General Public License v2.0 | 4 votes |
public void testOptionalFilter() throws Exception { final BigdataSail sail = getSail(); final BigdataSailRepository repo = new BigdataSailRepository(sail); // final Sail sail = new MemoryStore(); // final Repository repo = new SailRepository(sail); repo.initialize(); final RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { final ValueFactory vf = sail.getValueFactory(); URI s = vf.createURI("urn:test:s"); URI p1 = vf.createURI("urn:test:p1"); URI p2 = vf.createURI("urn:test:p2"); Literal v1 = vf.createLiteral(1); Literal v2 = vf.createLiteral(2); Literal v3 = vf.createLiteral(3); cxn.add(s, p1, v1); cxn.add(s, p2, v2); cxn.add(s, p1, v3); cxn.commit(); String qry = "PREFIX :<urn:test:> " + "SELECT ?s ?v1 ?v2 " + "WHERE { " + " ?s :p1 ?v1 . " + " OPTIONAL {?s :p2 ?v2 FILTER(?v1 < 3) } " + "}"; TupleQuery query = cxn.prepareTupleQuery(QueryLanguage.SPARQL, qry); TupleQueryResult result = query.evaluate(); // while (result.hasNext()) { // System.err.println(result.next()); // } Collection<BindingSet> solution = new LinkedList<BindingSet>(); solution.add(createBindingSet(new Binding[] { new BindingImpl("s", s), new BindingImpl("v1", v1), new BindingImpl("v2", v2), })); solution.add(createBindingSet(new Binding[] { new BindingImpl("s", s), new BindingImpl("v1", v3), })); compare(result, solution); } finally { cxn.close(); if (sail instanceof BigdataSail) ((BigdataSail)sail).__tearDownUnitTest(); } }
Example 17
Source File: TestReadWriteTransactions.java From database with GNU General Public License v2.0 | 4 votes |
/** * Test of abort semantics. */ public void test_abort() throws Exception { class AbortException extends RuntimeException { private static final long serialVersionUID = 1L; } // final LocalTripleStore store = (LocalTripleStore) getStore(); final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection store = repo.getReadWriteConnection(); store.setAutoCommit(false); // Should be a nop. store.rollback(); // final long s = 1, p = 2, o = 3; final URI s = uri("a"), p = uri("b"), o = uri("c"); try { // add the statement. // store.addStatements(new SPO[] { // // new SPO(s, p, o, StatementEnum.Explicit) // // },// // 1); store.add(stmt(s, p, o)); // visible in the repo. assertTrue(store.hasStatement(s, p, o, true)); // discard the write set. store.rollback(); // no longer visible in the repo. assertFalse(store.hasStatement(s, p, o, true)); } finally { store.close(); } } finally { sail.__tearDownUnitTest(); } }
Example 18
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 19
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 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(); } }