Java Code Examples for org.openrdf.repository.RepositoryConnection#getValueFactory()

The following examples show how to use org.openrdf.repository.RepositoryConnection#getValueFactory() . 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: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
/**
 * Literal factory
 * 
 * @param s
 *            the literal value
 * @param typeuri
 *            uri representing the type (generally xsd)
 * @return
 */
public org.openrdf.model.Literal Literal(String s, URI typeuri) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			ValueFactory vf = con.getValueFactory();
			if (typeuri == null) {
				return vf.createLiteral(s);
			} else {
				return vf.createLiteral(s, typeuri);
			}
		} finally {
			con.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 2
Source File: TestTicket4249.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void executeQuery(final RepositoryConnection conn, final String string, final double start, final double length, final String expected)
		throws RepositoryException, MalformedQueryException,
		QueryEvaluationException, RDFParseException, IOException, VisitorException {

	final ValueFactory vf = conn.getValueFactory();
	final String query = "select ?substring WHERE { BIND ( SUBSTR(?string, ?start, ?length) as ?substring ) . }";
	final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
	q.setBinding("string", vf.createLiteral(string));
	q.setBinding("start", vf.createLiteral(start));
	q.setBinding("length", vf.createLiteral(length));
	final TupleQueryResult tqr = q.evaluate();
	try {
		while (tqr.hasNext()) {
			final BindingSet bindings = tqr.next();
			// assert expected value
			assertEquals(expected, bindings.getBinding("substring").getValue().stringValue());
		}
	} finally {
		tqr.close();
	}
}
 
Example 3
Source File: TestTicket4249.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void executeQuery(final RepositoryConnection conn, final Literal string, final int start, final Literal expected)
		throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	final ValueFactory vf = conn.getValueFactory();
	final String query = "select ?substring WHERE { BIND ( SUBSTR(?string, ?start) as ?substring ) . }";
	final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
	q.setBinding("string", string);
	q.setBinding("start", vf.createLiteral(start));
	final TupleQueryResult tqr = q.evaluate();
	try {
		while (tqr.hasNext()) {
			final BindingSet bindings = tqr.next();
			// assert expected value
			assertEquals(expected, bindings.getBinding("substring").getValue());
		}
	} finally {
		tqr.close();
	}
}
 
Example 4
Source File: TestTicket4249.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void executeQuery(final RepositoryConnection conn, final Literal string, final int start, final int length, final Literal expected)
		throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	final ValueFactory vf = conn.getValueFactory();
	final String query = "select ?substring WHERE { BIND ( SUBSTR(?string, ?start, ?length) as ?substring ) . }";
	final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
	q.setBinding("string", string);
	q.setBinding("start", vf.createLiteral(start));
	q.setBinding("length", vf.createLiteral(length));
	final TupleQueryResult tqr = q.evaluate();
	try {
		while (tqr.hasNext()) {
			final BindingSet bindings = tqr.next();
			// assert expected value
			assertEquals(expected, bindings.getBinding("substring").getValue());
		}
	} finally {
		tqr.close();
	}
}
 
Example 5
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 6
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 7
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public void remove(Resource s, URI p, Value o, Resource... context) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			ValueFactory myFactory = con.getValueFactory();
			Statement st = myFactory.createStatement((Resource) s, p,
					(Value) o);
			con.remove(st, context);
		} finally {
			con.close();
		}
	} catch (Exception e) {
		// handle exception
	}
}
 
Example 8
Source File: SesameTransformationInjectRouteSensor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<SesameRouteSensorInjectMatch> matches) throws RepositoryException {
	final RepositoryConnection connection = driver.getConnection();
	final ValueFactory vf = connection.getValueFactory();

	final List<Statement> statementsToRemove = new ArrayList<>(matches.size());
	final URI requires = vf.createURI(BASE_PREFIX + ModelConstants.REQUIRES);
	for (final SesameRouteSensorInjectMatch match : matches) {
		final Statement statement = vf.createStatement(match.getRoute(), requires, match.getSensor());
		statementsToRemove.add(statement);
	}
	connection.remove(statementsToRemove);
}
 
Example 9
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
/**
 * BNode factory
 * 
 * @return
 */
public BNode bnode() {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			ValueFactory vf = con.getValueFactory();
			return vf.createBNode();
		} finally {
			con.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 10
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
/**
 * URIref factory
 * 
 * @param uri
 * @return
 */
public URI URIref(String uri) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			ValueFactory vf = con.getValueFactory();
			return vf.createURI(uri);
		} finally {
			con.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
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: 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 13
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 14
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 15
Source File: StressTest_ClosedByInterrupt_RW.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void doDelete(final RepositoryConnection conn, final int loop, final int index)
        throws RepositoryException {
    final ValueFactory vf = conn.getValueFactory();
    final URI context = vf.createURI("context:loop:" + loop + ":item:" + index);
    final Collection<Statement> statements = getStatementsForContext(conn, context);
    for (Statement statement : statements) {
        conn.remove(statement, context);
    }
}
 
Example 16
Source File: StressTest_ClosedByInterrupt_RW.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void doInsert(final RepositoryConnection conn, final int loop, final int index)
        throws RepositoryException {
    final ValueFactory vf = conn.getValueFactory();
    final URI c = vf.createURI("context:loop:" + loop + ":item:" + index);
    final URI s = vf.createURI("subject:loop:" + loop + ":item:" + index);
    for (int x = 0; x < NUM_STATEMENTS_PER_INSERT; ++x) {
        final URI p = vf.createURI("predicate:" + x);
        final Literal o = vf.createLiteral("SomeValue");
        conn.add(s, p, o, c);
    }
}
 
Example 17
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 18
Source File: TestTicket4249.java    From database with GNU General Public License v2.0 4 votes vote down vote up
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 19
Source File: RDFList.java    From anno4j with Apache License 2.0 4 votes vote down vote up
ValueFactory getValueFactory() {
	RepositoryConnection conn = getObjectConnection();
	return conn.getValueFactory();
}