use of org.openrdf.model.Literal in project blueprints by tinkerpop.
the class PropertyGraphSailConnection method literalToObject.
private Object literalToObject(final Value v) {
if (v instanceof Literal) {
Literal l = (Literal) v;
URI type = l.getDatatype();
if (null == type) {
return l.getLabel();
} else {
if (type.equals(XMLSchema.STRING)) {
return l.stringValue();
} else if (type.equals(XMLSchema.LONG)) {
return l.longValue();
} else if (type.equals(XMLSchema.INT)) {
return l.intValue();
} else if (type.equals(XMLSchema.INTEGER)) {
return l.integerValue();
} else if (type.equals(XMLSchema.BYTE)) {
return l.byteValue();
} else if (type.equals(XMLSchema.BOOLEAN)) {
return l.booleanValue();
} else if (type.equals(XMLSchema.SHORT)) {
return l.shortValue();
} else if (type.equals(XMLSchema.FLOAT)) {
return l.floatValue();
} else if (type.equals(XMLSchema.DOUBLE)) {
return l.doubleValue();
} else {
return null;
}
}
} else {
return null;
}
}
use of org.openrdf.model.Literal in project blueprints by tinkerpop.
the class SailTest method testGetStatementsPO_SG.
@Test
public void testGetStatementsPO_SG() throws Exception {
SailConnection sc = sail.getConnection();
try {
sc.begin();
URI uriA = sail.getValueFactory().createURI("http://example.org/test/PO_SG#a");
URI uriB = sail.getValueFactory().createURI("http://example.org/test/PO_SG#b");
URI foo = sail.getValueFactory().createURI("http://example.org/ns#foo");
URI firstName = sail.getValueFactory().createURI("http://example.org/ns#firstName");
Literal plainLitA = sail.getValueFactory().createLiteral("arbitrary plain literal 8765675");
Literal fooLabel = sail.getValueFactory().createLiteral("foo", XMLSchema.STRING);
int before, after;
// Add statement to the implicit null context.
sc.removeStatements(null, null, null, uriA);
sc.commit();
sc.begin();
before = countStatements(sc, null, uriA, uriB, false);
sc.addStatement(uriA, uriA, uriB);
sc.commit();
sc.begin();
after = countStatements(sc, null, uriA, uriB, false);
assertEquals(0, before);
assertEquals(1, after);
// Add plain literal statement to the default context.
sc.removeStatements(null, null, plainLitA);
sc.commit();
sc.begin();
before = countStatements(sc, null, uriA, plainLitA, false);
sc.addStatement(uriA, uriA, plainLitA);
sc.addStatement(uriA, uriB, plainLitA);
sc.addStatement(uriB, uriB, plainLitA);
sc.commit();
sc.begin();
after = countStatements(sc, null, uriA, plainLitA, false);
assertEquals(0, before);
assertEquals(1, after);
// Add string-typed literal statement to the default context.
sc.removeStatements(null, null, fooLabel);
sc.commit();
sc.begin();
before = countStatements(sc, null, firstName, fooLabel, false);
sc.addStatement(foo, firstName, fooLabel);
sc.commit();
sc.begin();
after = countStatements(sc, null, firstName, fooLabel, false);
assertEquals(0, before);
assertEquals(1, after);
assertEquals(foo, toSet(sc.getStatements(null, firstName, fooLabel, false)).iterator().next().getSubject());
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.model.Literal in project stanbol by apache.
the class RdfRepresentationTest method testTypedLiteralToTextConversion.
/**
* {@link TypedLiteral}s are used to represent literal values for different
* xsd dataTypes within Clerezza. This method tests of {@link TypedLiteral}s
* with the data type xsd:string are correctly treated like {@link String}
* values. This tests especially if they are treated as natural language
* texts without language.
*/
@Test
public void testTypedLiteralToTextConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Literal stringLiteral = valueFactory.getSesameFactory().createLiteral("This is a stirng value", XMLSchema.STRING);
//also add an integer to test that other typed literals are not used as texts
Literal integerLiteral = valueFactory.getSesameFactory().createLiteral(5);
Representation rep = createRepresentation(null);
rep.add(field, Arrays.asList(stringLiteral, integerLiteral));
//test if the literal is returned when asking for natural language text without language
Iterator<Text> noLangTexts = rep.get(field, (String) null);
assertTrue(noLangTexts.hasNext());
assertEquals(stringLiteral.getLabel(), noLangTexts.next().getText());
assertFalse(noLangTexts.hasNext());
//test that string literals are returned when asking for all natural language text values
Iterator<Text> texts = rep.getText(field);
assertTrue(texts.hasNext());
assertEquals(stringLiteral.getLabel(), texts.next().getText());
assertFalse(texts.hasNext());
}
use of org.openrdf.model.Literal in project stanbol by apache.
the class RdfRepresentationTest method testTypedLiteralToValueConversion.
/**
* {@link TypedLiteral}s are used to represent literal values for different
* xsd dataTypes within Clerezza. This method tests if xsd dataTypes are
* converted to the corresponding java types.
* This is dependent on the {@link LiteralFactory} implementation used by
* the {@link RdfRepresentation} implementation.
*/
@SuppressWarnings("unchecked")
@Test
public void testTypedLiteralToValueConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Integer integerValue = 5;
Literal integerLiteral = valueFactory.getSesameFactory().createLiteral(integerValue);
Date dateValue = new Date();
Literal dateLiteeral = valueFactory.getSesameFactory().createLiteral(dateValue);
Double doubleValue = Math.PI;
Literal doubleLiteral = valueFactory.getSesameFactory().createLiteral(doubleValue);
String stringValue = "This is a string literal value";
Literal stringLiteral = valueFactory.getSesameFactory().createLiteral(stringValue, XMLSchema.STRING);
Representation rep = createRepresentation(null);
Collection<Literal> typedLiterals = Arrays.asList(integerLiteral, doubleLiteral, stringLiteral, dateLiteeral);
rep.add(field, typedLiterals);
//now check that such values are available via Sesame Literal
Iterator<Literal> typedLiteralValues = rep.get(field, Literal.class);
int size = 0;
while (typedLiteralValues.hasNext()) {
Literal next = typedLiteralValues.next();
assertTrue(typedLiterals.contains(next));
size++;
}
assertTrue(typedLiterals.size() == size);
//now check that the values are available via the java object types
//1) integer
Iterator<Integer> intValues = rep.get(field, Integer.class);
assertTrue(intValues.hasNext());
assertEquals(integerValue, intValues.next());
assertFalse(intValues.hasNext());
//2) double
Iterator<Double> doubleValues = rep.get(field, Double.class);
assertTrue(doubleValues.hasNext());
assertEquals(doubleValue, doubleValues.next());
assertFalse(doubleValues.hasNext());
//3) string
Iterator<String> stringValues = rep.get(field, String.class);
assertTrue(stringValues.hasNext());
String value = stringValues.next();
assertEquals(stringValue, value);
assertFalse(stringValues.hasNext());
//4) date
Iterator<Date> dateValues = rep.get(field, Date.class);
assertTrue(dateValues.hasNext());
assertEquals(dateValue, dateValues.next());
assertFalse(dateValues.hasNext());
}
use of org.openrdf.model.Literal in project stanbol by apache.
the class RdfRepresentation method removeAllNaturalText.
@Override
public void removeAllNaturalText(String field, String... languages) throws IllegalArgumentException {
if (field == null) {
throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
} else if (field.isEmpty()) {
throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
}
ValueTypeFilter<Literal> vtf = new ValueTypeFilter<Literal>(languages);
Iterator<Statement> statements = model.filter(subject, sesameFactory.createURI(field), null).iterator();
while (statements.hasNext()) {
Statement statement = statements.next();
if (vtf.evaluate(statement.getObject())) {
statements.remove();
}
}
}
Aggregations