Java Code Examples for org.eclipse.rdf4j.model.ValueFactory#createURI()

The following examples show how to use org.eclipse.rdf4j.model.ValueFactory#createURI() . 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: BuildURI.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length < 1) {
		throw new ValueExprEvaluationException("Incorrect number of arguments");
	}
	if (!(args[0] instanceof Literal)) {
		throw new ValueExprEvaluationException("First argument must be a string");
	}
	Literal s = (Literal) args[0];
	String tmpl = s.getLabel();
	Map<String, String> mappings = new HashMap<>(args.length);
	for (int i = 1; i < args.length; i++) {
		mappings.put(Integer.toString(i), args[i].stringValue());
	}
	String newValue = StringSubstitutor.replace(tmpl, mappings, "{?", "}");
	if (tmpl.charAt(0) == '<' && tmpl.charAt(tmpl.length() - 1) == '>') {
		return valueFactory.createURI(newValue.substring(1, newValue.length() - 1));
	}
	throw new ValueExprEvaluationException("Invalid URI template: " + tmpl);
}
 
Example 2
Source File: TestNativeStoreMemoryOverflow.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test() throws Exception {
	int size = 10000; // this should really be bigger
	// load a lot of triples in two different contexts
	testCon.begin();
	final ValueFactory vf = testCon.getValueFactory();
	URI context1 = vf.createURI("http://my.context.1");
	URI context2 = vf.createURI("http://my.context.2");
	final URI predicate = vf.createURI("http://my.predicate");
	final URI object = vf.createURI("http://my.object");

	testCon.add(new DynamicIteration(size, predicate, object, vf), context1);
	testCon.add(new DynamicIteration(size, predicate, object, vf), context2);

	assertEquals(size, Iterations.asList(testCon.getStatements(null, null, null, false, context1)).size());
	assertEquals(size, Iterations.asList(testCon.getStatements(null, null, null, false, context2)).size());
	testCon.commit();

	assertEquals(size, Iterations.asList(testCon.getStatements(null, null, null, false, context1)).size());
	assertEquals(size, Iterations.asList(testCon.getStatements(null, null, null, false, context2)).size());

	testCon.close();
}
 
Example 3
Source File: SpinxFunction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	Bindings bindings = scriptEngine.createBindings();
	for (int i = 0; i < args.length; i++) {
		Argument argument = arguments.get(i);
		Value arg = args[i];
		Object jsArg;
		if (arg instanceof Literal) {
			Literal argLiteral = (Literal) arg;
			if (XMLSchema.INTEGER.equals(argLiteral.getDatatype())) {
				jsArg = argLiteral.intValue();
			} else if (XMLSchema.DECIMAL.equals(argLiteral.getDatatype())) {
				jsArg = argLiteral.doubleValue();
			} else {
				jsArg = argLiteral.getLabel();
			}
		} else {
			jsArg = arg.stringValue();
		}
		bindings.put(argument.getPredicate().getLocalName(), jsArg);
	}

	Object result;
	try {
		if (compiledScript == null && scriptEngine instanceof Compilable) {
			compiledScript = ((Compilable) scriptEngine).compile(script);
		}
		if (compiledScript != null) {
			result = compiledScript.eval(bindings);
		} else {
			result = scriptEngine.eval(script, bindings);
		}
	} catch (ScriptException e) {
		throw new ValueExprEvaluationException(e);
	}

	ValueFactory vf = ValueFactoryImpl.getInstance();
	return (returnType != null) ? vf.createLiteral(result.toString(), returnType) : vf.createURI(result.toString());
}