Java Code Examples for org.openrdf.repository.sail.SailRepository#getConnection()

The following examples show how to use org.openrdf.repository.sail.SailRepository#getConnection() . 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 vote down vote up
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: TestTicket632.java    From database with GNU General Public License v2.0 6 votes vote down vote up
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: RangeQueriesTest.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
/**
 * Setup fixture for this test case.
 * 
 * @throws Exception never, otherwise the test fails.
 */
@BeforeClass
public static void setUp() throws Exception {
	_repository = new SailRepository(new MemoryStore());
	_repository.initialize();

	_tripleStore = newTripleStore();
	_tripleStore.enableRangeIndexesSupport();
	_tripleStore.open();

	assertTrue("Ranges have not been enabled for this triple store!", _tripleStore.isRangeIndexesSupportEnabled());

	_tripleStore.bulkLoad(DATA, RDFFormat.NTRIPLES);

	_repositoryConnection = _repository.getConnection();
	_repositoryConnection.add(new File(DATA), "http://nb-base-uri-not-actually-used", RDFFormat.NTRIPLES);
}
 
Example 4
Source File: SPARQLInferenceTest.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
@Before
public void before()
		throws RepositoryException, IOException, RDFParseException,
		       MalformedQueryException, QueryResultParseException,
			   QueryResultHandlerException {
	repo = new SailRepository(new MemoryStore());
	repo.initialize();
	conn = repo.getConnection();
	vf = conn.getValueFactory();
	conn.add(getResource(data), "file://", RDFFormat.TURTLE);
	SPARQLResultsXMLParserFactory factory =
			new SPARQLResultsXMLParserFactory();
	parser = factory.getParser();
	parser.setValueFactory(vf);
	List<Rule> rules;
	rules = Rules.fromOntology(getResource(data));
	QueryRewriter rewriter = new QueryRewriter(conn, rules);
	query = (TupleQuery) rewriter.rewrite(QueryLanguage.SPARQL, queryString);
	nonInfQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
	System.out.println("== QUERY (" + this.name + ") ==");
	System.out.println(nonInfQuery);
	System.out.println("== REWRITTEN QUERY (" + this.name + ") ==");
	System.out.println(query);
}
 
Example 5
Source File: TestTicket967.java    From database with GNU General Public License v2.0 5 votes vote down vote up
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 6
Source File: TestTicket353.java    From database with GNU General Public License v2.0 5 votes vote down vote up
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: TestTicket276.java    From database with GNU General Public License v2.0 5 votes vote down vote up
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 8
Source File: TestTicket355.java    From database with GNU General Public License v2.0 5 votes vote down vote up
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 ?subj WHERE { "
					+ "?subj <os:prop> ?val . "
					+ "FILTER(STR(?val) != ?arg)}";
			TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
			tq.setBinding("arg", vf.createLiteral("notValue"));
			TupleQueryResult tqr = tq.evaluate();
			assertTrue(tqr.hasNext());
			tqr.close();
		} finally {
			conn.close();
		}
	} finally {
		repo.shutDown();
	}
}
 
Example 9
Source File: TestTicket1681.java    From database with GNU General Public License v2.0 5 votes vote down vote up
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 10
Source File: TestTicket275.java    From database with GNU General Public License v2.0 5 votes vote down vote up
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 11
Source File: TestTicket348.java    From database with GNU General Public License v2.0 5 votes vote down vote up
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 12
Source File: IntegrationTestSupertypeLayer.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Setup fixture for this test.
 */
@BeforeClass
public static void init() throws Exception {
	inMemoryRepository = new SailRepository(new MemoryStore());
	cumulusRepository = new SailRepository(new CumulusRDFSail(new TripleStore()));
	
	inMemoryRepository.initialize();
	cumulusRepository.initialize();
	
	localConnection = inMemoryRepository.getConnection();
	cumulusConnection = cumulusRepository.getConnection();
}
 
Example 13
Source File: RangeQueryOptimizationTest.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
	// Set up standard Sesame Repository for comparison
	EMBEDDED_REPOSITORY = new SailRepository(new MemoryStore());
	EMBEDDED_REPOSITORY.initialize();
	EMBEDDED_REPOSITORY_CONNECTION = EMBEDDED_REPOSITORY.getConnection();
	EMBEDDED_REPOSITORY_CONNECTION.add(DATAFILE, null, RDFFormat.N3);

	_tripleStore = new TripleStore();
	_sail = new CumulusRDFSail(_tripleStore);
	_repository = new SailRepository(_sail);
	_repository.initialize();

	CUMULUS_CONNECTION = _repository.getConnection();
}
 
Example 14
Source File: TestTicket1682.java    From database with GNU General Public License v2.0 4 votes vote down vote up
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 15
Source File: AbstractRDFTestCase.java    From ldp4j with Apache License 2.0 4 votes vote down vote up
@Before
public void setUpConnection() throws Exception {
	repository = new SailRepository(new MemoryStore());
	repository.initialize();
	connection = repository.getConnection();
}