use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class RdfRepresentation method removeAllNaturalText.
@Override
public void removeAllNaturalText(String field, String... languages) {
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");
}
// if(languages == null || languages.length == 0){
// languages = new String []{null};
// }
IRI fieldIRI = new IRI(field);
//get all the affected Literals
Collection<Literal> toRemove = new ArrayList<Literal>();
Iterator<Literal> it = new FilteringIterator<Literal>(graphNode.getLiterals(fieldIRI), new NaturalTextFilter(languages), Literal.class);
while (it.hasNext()) {
toRemove.add(it.next());
}
for (Literal l : toRemove) {
graphNode.deleteProperty(fieldIRI, l);
}
}
use of org.apache.clerezza.commons.rdf.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 = literalFactory.createTypedLiteral(integerValue);
Date dateValue = new Date();
Literal dateLiteeral = literalFactory.createTypedLiteral(dateValue);
Double doubleValue = Math.PI;
Literal doubleLiteral = literalFactory.createTypedLiteral(doubleValue);
String stringValue = "This is a string literal value";
Literal stringLiteral = literalFactory.createTypedLiteral(stringValue);
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 TypedLiteral
Iterator<Literal> typedLiteralValues = rep.get(field, Literal.class);
int size = 0;
while (typedLiteralValues.hasNext()) {
assertTrue(typedLiterals.contains(typedLiteralValues.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());
assertEquals(stringValue, stringValues.next());
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.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class RdfResourceUtils method getLiteralValues.
/**
* Extracts the literal values for {@link Literal} instances.
*
* @param literals the Iterator holding the literals
* @return The collection with the literal values
*/
public static Collection<String> getLiteralValues(Iterator<Literal> literals) {
Collection<String> results = new ArrayList<String>();
while (literals.hasNext()) {
Literal act = literals.next();
results.add(act.getLexicalForm());
}
return results;
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class RdfRepresentationTest method testPlainLiteralToTextConversion.
/*--------------------------------------------------------------------------
* Additional Tests for special Features of the Clerezza based implementation
*
* This includes mainly support for additional types like PlainLiteral,
* TypedLiteral, IRIs. The conversion to such types as well as getter for
* such types.
*--------------------------------------------------------------------------
*/
/**
* {@link PlainLiteral} is used for natural language text in the Clerezza
* RDF API. This tests if adding {@link PlainLiteral}s to the
* {@link Representation#add(String, Object)} method makes them available
* as {@link Text} instances via the {@link Representation} API (e.g.
* {@link Representation#get(String, String...)}).
*/
@Test
public void testPlainLiteralToTextConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Literal noLangLiteral = new PlainLiteralImpl("A plain literal without Language");
Literal enLiteral = new PlainLiteralImpl("An english literal", new Language("en"));
Literal deLiteral = new PlainLiteralImpl("Ein Deutsches Literal", new Language("de"));
Literal deATLiteral = new PlainLiteralImpl("Ein Topfen Verband hilft bei Zerrungen", new Language("de-AT"));
Collection<Literal> plainLiterals = Arrays.asList(noLangLiteral, enLiteral, deLiteral, deATLiteral);
Representation rep = createRepresentation(null);
rep.add(field, plainLiterals);
//now test, that the Plain Literals are available as natural language
//tests via the Representation Interface!
//1) one without a language
Iterator<Text> noLangaugeTexts = rep.get(field, (String) null);
assertTrue(noLangaugeTexts.hasNext());
Text noLanguageText = noLangaugeTexts.next();
assertEquals(noLangLiteral.getLexicalForm(), noLanguageText.getText());
assertNull(noLanguageText.getLanguage());
//only a single result
assertFalse(noLangaugeTexts.hasNext());
//2) one with a language
Iterator<Text> enLangaugeTexts = rep.get(field, "en");
assertTrue(enLangaugeTexts.hasNext());
Text enLangageText = enLangaugeTexts.next();
assertEquals(enLiteral.getLexicalForm(), enLangageText.getText());
assertEquals(enLiteral.getLanguage().toString(), enLangageText.getLanguage());
//only a single result
assertFalse(enLangaugeTexts.hasNext());
//3) test to get all natural language values
Set<String> stringValues = new HashSet<String>();
for (Literal plainLiteral : plainLiterals) {
stringValues.add(plainLiteral.getLexicalForm());
}
Iterator<Text> texts = rep.getText(field);
while (texts.hasNext()) {
assertTrue(stringValues.remove(texts.next().getText()));
}
assertTrue(stringValues.isEmpty());
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class ClerezzaRDFParser method handleStatement.
private void handleStatement(RDFDataset result, Triple t, Map<BlankNode, String> bNodeMap) {
final String subject = getResourceValue(t.getSubject(), bNodeMap);
final String predicate = getResourceValue(t.getPredicate(), bNodeMap);
final RDFTerm object = t.getObject();
if (object instanceof Literal) {
final String value = ((Literal) object).getLexicalForm();
final String language;
final String datatype;
datatype = getResourceValue(((Literal) object).getDataType(), bNodeMap);
Language l = ((Literal) object).getLanguage();
if (l == null) {
language = null;
} else {
language = l.toString();
}
result.addTriple(subject, predicate, value, datatype, language);
count++;
} else {
result.addTriple(subject, predicate, getResourceValue((BlankNodeOrIRI) object, bNodeMap));
count++;
}
}
Aggregations