Java Code Examples for org.eclipse.rdf4j.model.Resource#stringValue()

The following examples show how to use org.eclipse.rdf4j.model.Resource#stringValue() . 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: SubQuery.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
public SubQuery(Resource subj, IRI pred, Value obj) {
	super();
	if (subj!=null)
		this.subj = subj.stringValue();
	if (pred!=null)
		this.pred = pred.stringValue();
	if (obj!=null)
		this.obj = obj.toString();	
	// we need to take toString() here since stringValue for literals does not contain the datatype
}
 
Example 2
Source File: SpinSailConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void invalidate(Resource subj) {
	if (subj instanceof IRI) {
		parser.reset((IRI) subj);
		String key = subj.stringValue();
		Function func = functionRegistry.get(key).orElse(null);
		if (func instanceof TransientFunction) {
			functionRegistry.remove(func);
		}
		TupleFunction tupleFunc = tupleFunctionRegistry.get(key).orElse(null);
		if (tupleFunc instanceof TransientTupleFunction) {
			tupleFunctionRegistry.remove(tupleFunc);
		}
	}
}
 
Example 3
Source File: SPARQLConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String createInsertDataCommand(Iterable<? extends Statement> statements, Resource... contexts) {
	StringBuilder qb = new StringBuilder();
	qb.append("INSERT DATA \n");
	qb.append("{ \n");
	if (contexts.length > 0) {
		for (Resource context : contexts) {
			if (context != null) {
				String namedGraph = context.stringValue();
				if (context instanceof BNode) {
					// SPARQL does not allow blank nodes as named graph
					// identifiers, so we need to skolemize
					// the blank node id.
					namedGraph = "urn:nodeid:" + context.stringValue();
				}
				qb.append("    GRAPH <" + namedGraph + "> { \n");
			}
			createDataBody(qb, statements, true);
			if (context != null) {
				qb.append(" } \n");
			}
		}
	} else {
		createDataBody(qb, statements, false);
	}
	qb.append("}");

	return qb.toString();
}
 
Example 4
Source File: SPARQLConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String createDeleteDataCommand(Iterable<? extends Statement> statements, Resource... contexts) {
	StringBuilder qb = new StringBuilder();
	qb.append("DELETE DATA \n");
	qb.append("{ \n");
	if (contexts.length > 0) {
		for (Resource context : contexts) {
			if (context != null) {
				String namedGraph = context.stringValue();
				if (context instanceof BNode) {
					// SPARQL does not allow blank nodes as named graph
					// identifiers, so we need to skolemize
					// the blank node id.
					namedGraph = "urn:nodeid:" + context.stringValue();
				}
				qb.append("    GRAPH <" + namedGraph + "> { \n");
			}
			createDataBody(qb, statements, true);
			if (context != null) {
				qb.append(" } \n");
			}
		}
	} else {
		createDataBody(qb, statements, false);
	}
	qb.append("}");

	return qb.toString();
}
 
Example 5
Source File: SPARQLConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String createDeletePatternCommand(Resource subject, IRI predicate, Value object, Resource[] contexts) {
	StringBuilder qb = new StringBuilder();
	qb.append("DELETE WHERE \n");
	qb.append("{ \n");
	if (contexts.length > 0) {
		for (Resource context : contexts) {
			if (context != null) {
				String namedGraph = context.stringValue();
				if (context instanceof BNode) {
					// SPARQL does not allow blank nodes as named graph
					// identifiers, so we need to skolemize
					// the blank node id.
					namedGraph = "urn:nodeid:" + context.stringValue();
				}
				qb.append("    GRAPH <" + namedGraph + "> { \n");
			}
			createBGP(qb, subject, predicate, object);
			if (context != null && context instanceof IRI) {
				qb.append(" } \n");
			}
		}
	} else {
		createBGP(qb, subject, predicate, object);
	}
	qb.append("}");

	return qb.toString();
}
 
Example 6
Source File: RDFJSONWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the correct syntax for a Resource, depending on whether it is a URI or a Blank Node (ie, BNode)
 *
 * @param uriOrBnode The resource to serialise to a string
 * @return The string value of the sesame resource
 */
public static String resourceToString(final Resource uriOrBnode) {
	if (uriOrBnode instanceof IRI) {
		return uriOrBnode.stringValue();
	} else {
		return "_:" + ((BNode) uriOrBnode).getID();
	}
}
 
Example 7
Source File: JSONLDInternalRDFParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String getResourceValue(Resource subject) {
	if (subject == null) {
		return null;
	} else if (subject instanceof IRI) {
		return subject.stringValue();
	} else if (subject instanceof BNode) {
		return JsonLdConsts.BLANK_NODE_PREFIX + subject.stringValue();
	}

	throw new IllegalStateException("Did not recognise resource type: " + subject.getClass().getName());
}
 
Example 8
Source File: StatementSerializer.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a Regular Expression to match serialized statements meeting these constraints. A <code>null</code> or empty parameters imply
 * no constraint. A <code>null</code> return value implies no constraints.
 *
 * @param context
 *            context constraint
 * @param subject
 *            subject constraint
 * @param predicates
 *            list of predicate constraints
 * @return a regular expression that can be used to match serialized statements. A <code>null</code> return value implies no
 *         constraints.
 */
public static String createStatementRegex(final StatementConstraints contraints) {
    final Resource context = contraints.getContext();
    final Resource subject = contraints.getSubject();
    final Set<IRI> predicates = contraints.getPredicates();
    if (context == null && subject == null && (predicates == null || predicates.isEmpty())) {
        return null;
    }

    // match on anything but a separator
    final String anyReg = "[^" + SEP + "]*";

    // if context is empty, match on any context
    final String contextReg = (context == null) ? anyReg : context.stringValue();

    // if subject is empty, match on any subject
    final String subjectReg = (subject == null) ? anyReg : subject.stringValue();

    // if the predicates are empty, match on any predicate. Otherwise, "or" the predicates.
    String predicateReg = "";
    if (predicates == null || predicates.isEmpty()) {
        predicateReg = anyReg;
    } else {
        predicateReg = "(" + StringUtils.join(predicates, "|") + ")";
    }

    return "^" + contextReg + SEP + subjectReg + SEP + predicateReg + SEP + ".*";
}
 
Example 9
Source File: SPARQLConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void createDataBody(StringBuilder qb, Iterable<? extends Statement> statements, boolean ignoreContext) {
	for (Statement st : statements) {
		final Resource context = st.getContext();
		if (!ignoreContext) {
			if (context != null) {
				String namedGraph = context.stringValue();
				if (context instanceof BNode) {
					// SPARQL does not allow blank nodes as named graph
					// identifiers, so we need to skolemize
					// the blank node id.
					namedGraph = "urn:nodeid:" + context.stringValue();
				}
				qb.append("    GRAPH <" + namedGraph + "> { \n");
			}
		}
		if (st.getSubject() instanceof BNode) {
			qb.append("_:" + st.getSubject().stringValue() + " ");
		} else {
			qb.append("<" + st.getSubject().stringValue() + "> ");
		}

		qb.append("<" + st.getPredicate().stringValue() + "> ");

		if (st.getObject() instanceof Literal) {
			Literal lit = (Literal) st.getObject();
			qb.append("\"");
			qb.append(SPARQLUtil.encodeString(lit.getLabel()));
			qb.append("\"");

			if (Literals.isLanguageLiteral(lit)) {
				qb.append("@");
				qb.append(lit.getLanguage().get());
			} else {
				qb.append("^^<" + lit.getDatatype().stringValue() + ">");
			}
			qb.append(" ");
		} else if (st.getObject() instanceof BNode) {
			qb.append("_:" + st.getObject().stringValue() + " ");
		} else {
			qb.append("<" + st.getObject().stringValue() + "> ");
		}
		qb.append(". \n");

		if (!ignoreContext && context != null) {
			qb.append("    }\n");
		}
	}
}
 
Example 10
Source File: TurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
@Test
public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws IOException {
	URL zipfileUrl = TurtleParserTest.class.getResource("sample-with-turtle-data.zip");

	assertNotNull("The sample-with-turtle-data.zip file must be present for this test", zipfileUrl);

	String url = "jar:" + zipfileUrl + "!/index.ttl";

	RDFParser parser = new TurtleParser();

	StatementCollector sc = new StatementCollector();
	parser.setRDFHandler(sc);

	try (InputStream in = new URL(url).openStream()) {
		parser.parse(in, url);
	}

	Collection<Statement> stmts = sc.getStatements();

	assertThat(stmts).hasSize(2);

	Iterator<Statement> iter = stmts.iterator();

	Statement stmt1 = iter.next(), stmt2 = iter.next();

	assertEquals(vf.createIRI("http://www.example.com/#"), stmt1.getSubject());
	assertEquals(vf.createIRI("http://www.example.com/ns/#document-about"), stmt1.getPredicate());

	Resource res = (Resource) stmt1.getObject();

	String resourceUrl = res.stringValue();

	assertThat(resourceUrl).startsWith("jar:" + zipfileUrl + "!");

	URL javaUrl = new URL(resourceUrl);
	assertEquals("jar", javaUrl.getProtocol());

	try (InputStream uc = javaUrl.openStream()) {
		assertEquals("The resource stream should be empty", -1, uc.read());
	}

	assertEquals(res, stmt2.getSubject());
	assertEquals(DC.TITLE, stmt2.getPredicate());
	assertEquals(vf.createLiteral("Empty File"), stmt2.getObject());
}