Java Code Examples for org.eclipse.rdf4j.model.Value#toString()

The following examples show how to use org.eclipse.rdf4j.model.Value#toString() . 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: StatementSerializer.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Write a {@link Statement} to a {@link String}
 *
 * @param statement
 *            the {@link Statement} to write
 * @return a {@link String} representation of the statement
 */
public static String writeStatement(final Statement statement) {
    final Resource subject = statement.getSubject();
    final Resource context = statement.getContext();
    final IRI predicate = statement.getPredicate();
    final Value object = statement.getObject();

    Validate.notNull(subject);
    Validate.notNull(predicate);
    Validate.notNull(object);

    String s = "";
    if (context == null) {
        s = SEP + subject.toString() + SEP + predicate.toString() + SEP + object.toString();
    } else {
        s = context.toString() + SEP + subject.toString() + SEP + predicate.toString() + SEP + object.toString();
    }
    return s;
}
 
Example 2
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 3
Source File: Formatter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String prefix(Value in) {

		if (in == null) {
			return "null";
		}

		if (in instanceof IRI) {

			String namespace = ((IRI) in).getNamespace();

			if (namespace.equals(RDF.NAMESPACE)) {
				return in.toString().replace(RDF.NAMESPACE, RDF.PREFIX + ":");
			}
			if (namespace.equals(RDFS.NAMESPACE)) {
				return in.toString().replace(RDFS.NAMESPACE, RDFS.PREFIX + ":");
			}
			if (namespace.equals(SHACL.NAMESPACE)) {
				return in.toString().replace(SHACL.NAMESPACE, SHACL.PREFIX + ":");
			}
			if (namespace.equals("http://example.com/ns#")) {
				return in.toString().replace("http://example.com/ns#", "ex:");
			}
			if (namespace.equals("http://www.w3.org/2001/XMLSchema#")) {
				return in.toString().replace("http://www.w3.org/2001/XMLSchema#", "xsd:");
			}

		}

		return in.toString();

	}
 
Example 4
Source File: AbstractNQuadsParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Tests the correct decoding of UTF-8 encoded chars in URIs.
 */
@Test
public void testURIDecodingManagement() throws RDFHandlerException, IOException, RDFParseException {
	TestParseLocationListener parseLocationListener = new TestParseLocationListener();
	TestRDFHandler rdfHandler = new TestRDFHandler();
	parser.setParseLocationListener(parseLocationListener);
	parser.setRDFHandler(rdfHandler);

	final ByteArrayInputStream bais = new ByteArrayInputStream(
			"<http://s/\\u306F\\u3080> <http://p/\\u306F\\u3080> <http://o/\\u306F\\u3080> <http://g/\\u306F\\u3080> ."
					.getBytes());
	parser.parse(bais, "http://base-uri");

	rdfHandler.assertHandler(1);
	final Statement statement = rdfHandler.getStatements().iterator().next();

	final Resource subject = statement.getSubject();
	Assert.assertTrue(subject instanceof IRI);
	final String subjectURI = subject.toString();
	Assert.assertEquals("http://s/はむ", subjectURI);

	final Resource predicate = statement.getPredicate();
	Assert.assertTrue(predicate instanceof IRI);
	final String predicateURI = predicate.toString();
	Assert.assertEquals("http://p/はむ", predicateURI);

	final Value object = statement.getObject();
	Assert.assertTrue(object instanceof IRI);
	final String objectURI = object.toString();
	Assert.assertEquals("http://o/はむ", objectURI);

	final Resource graph = statement.getContext();
	Assert.assertTrue(graph instanceof IRI);
	final String graphURI = graph.toString();
	Assert.assertEquals("http://g/はむ", graphURI);
}
 
Example 5
Source File: PCJKeyToJoinBindingSetIterator.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param key
 *            - Accumulo key obtained from scan
 * @return - Entry<String,BindingSet> satisfying the constant constraints
 * @throws BindingSetConversionException
 */
private Map.Entry<String, BindingSet> getBindingSetEntryAndMatchConstants(
		Key key) throws BindingSetConversionException {
	byte[] row = key.getRow().getBytes();
	String[] varOrder = key.getColumnFamily().toString()
			.split(ExternalTupleSet.VAR_ORDER_DELIM);

	BindingSet bindingSet = converter.convert(row, new VariableOrder(
			varOrder));

	QueryBindingSet bs = new QueryBindingSet();
	for (String var : bindingSet.getBindingNames()) {
		String mappedVar = pcjVarMap.get(var);
		if (VarNameUtils.isConstant(mappedVar)
				&& constantConstraints.containsKey(mappedVar)
				&& !constantConstraints.get(mappedVar).equals(
						bindingSet.getValue(var))) {
			return EMPTY_ENTRY;
		} else {
			bs.addBinding(mappedVar, bindingSet.getValue(var));
		}
	}

	String orderedValueString = bindingSet.getValue(varOrder[0]).toString();
	for (int i = 1; i < maxPrefixLen; i++) {
		Value value = bindingSet.getValue(varOrder[i]);
		if (value != null) {
			orderedValueString = orderedValueString
					+ ExternalTupleSet.VALUE_DELIM + value.toString();
		}
	}

	return new RdfCloudTripleStoreUtils.CustomEntry<String, BindingSet>(
			orderedValueString, bs);
}
 
Example 6
Source File: TimRdfHandler.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
private String handleNode(Value resource) {
  if (resource instanceof BNode) {
    String nodeName = resource.toString();
    String nodeId = nodeName.substring(nodeName.indexOf(":") + 1);
    return baseUri + ".well-known/genid/" + DigestUtils.md5Hex(fileName) + "_" + nodeId;
  } else {
    return resource.stringValue();
  }
}
 
Example 7
Source File: PowsyblWriter.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
private void writeNewSubject(Resource subj, Value obj, String ctxt) throws IOException {
    flushPendingStatements();

    String objString = obj.toString();
    int objSplitIdx = XMLUtil.findURISplitIndex(objString);
    if (objSplitIdx == -1) {
        throw new RDFHandlerException("Unable to create XML namespace-qualified name for predicate: " + objString);
    }
    String objNamespace = objString.substring(0, objSplitIdx);
    String objLocalName = objString.substring(objSplitIdx);

    // Write new subject:
    writeNewLine();
    writeStartOfStartTag(objNamespace, objLocalName);

    // FIXME This is hard-coded logic for processing CGMES data
    IRI uri = (IRI) subj;
    String attName = "ID";
    String value = uri.toString();
    String prefix = namespaceTable.get(uri.getNamespace());
    if (uri.getNamespace().equals("urn:uuid:")) {
        if (objLocalName.equals("FullModel")) {
            attName = "about";
        } else {
            value = "_" + uri.getLocalName();
        }
    }
    if (prefix != null && prefix.equals("data")) {
        if (ctxt.contains("_SSH_") || (ctxt.contains("_DY_") && objLocalName.equals("EnergyConsumer"))
                || (ctxt.contains("_TP_") && !objLocalName.equals("TopologicalNode"))) {
            attName = "about";
            value = "#" + uri.getLocalName();
        } else {
            value = uri.getLocalName();
        }
    }

    writeAttribute(RDF.NAMESPACE, attName, value);
    writeEndOfStartTag();
    writeNewLine();
    lastWrittenSubject = subj;
    lastObjNamespace = objNamespace;
    lastObjLocalName = objLocalName;
}
 
Example 8
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the two operands match according to the <code>like</code> operator. The operator is defined as
 * a string comparison with the possible use of an asterisk (*) at the end and/or the start of the second operand to
 * indicate substring matching.
 *
 * @return <tt>true</tt> if the operands match according to the <tt>like</tt> operator, <tt>false</tt> otherwise.
 */
public Value evaluate(Like node, BindingSet bindings)
		throws QueryEvaluationException {
	Value val = evaluate(node.getArg(), bindings);
	String strVal = null;

	if (val instanceof IRI) {
		strVal = val.toString();
	} else if (val instanceof Literal) {
		strVal = ((Literal) val).getLabel();
	}

	if (strVal == null) {
		throw new ValueExprEvaluationException();
	}

	if (!node.isCaseSensitive()) {
		// Convert strVal to lower case, just like the pattern has been done
		strVal = strVal.toLowerCase();
	}

	int valIndex = 0;
	int prevPatternIndex = -1;
	int patternIndex = node.getOpPattern().indexOf('*');

	if (patternIndex == -1) {
		// No wildcards
		return BooleanLiteral.valueOf(node.getOpPattern().equals(strVal));
	}

	String snippet;

	if (patternIndex > 0) {
		// Pattern does not start with a wildcard, first part must match
		snippet = node.getOpPattern().substring(0, patternIndex);
		if (!strVal.startsWith(snippet)) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();
		prevPatternIndex = patternIndex;
		patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
	}

	while (patternIndex != -1) {
		// Get snippet between previous wildcard and this wildcard
		snippet = node.getOpPattern().substring(prevPatternIndex + 1, patternIndex);

		// Search for the snippet in the value
		valIndex = strVal.indexOf(snippet, valIndex);
		if (valIndex == -1) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();
		prevPatternIndex = patternIndex;
		patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
	}

	// Part after last wildcard
	snippet = node.getOpPattern().substring(prevPatternIndex + 1);

	if (snippet.length() > 0) {
		// Pattern does not end with a wildcard.

		// Search last occurence of the snippet.
		valIndex = strVal.indexOf(snippet, valIndex);
		int i;
		while ((i = strVal.indexOf(snippet, valIndex + 1)) != -1) {
			// A later occurence was found.
			valIndex = i;
		}

		if (valIndex == -1) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();

		if (valIndex < strVal.length()) {
			// Some characters were not matched
			return BooleanLiteral.FALSE;
		}
	}

	return BooleanLiteral.TRUE;
}
 
Example 9
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 4 votes vote down vote up
private static String escape(Value r) {
      if (r instanceof IRI) {
	return "<" + r.toString() +">";
}
      return r.toString();
  }