use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class EntityhubLDPathTest method testReprentationMappings.
@Test
public void testReprentationMappings() throws Exception {
EntityhubLDPath ldPath = new EntityhubLDPath(backend);
Program<Object> program = ldPath.parseProgram(getReader(DATA_TYPE_TEST_PROGRAM));
assertNotNull("The Program MUST NOT be NULL", program);
Representation result = ldPath.execute(vf.createReference(CONTEXT_LONDON), program);
assertEquals("The id of the Representation '" + result.getId() + "' is not the same as the parsed Context '" + CONTEXT_LONDON + "'!", CONTEXT_LONDON, result.getId());
Iterator<Entry<String, Collection<?>>> entryIt = cloneExpected(EXPECTED_RESULTS_LONDON).entrySet().iterator();
while (entryIt.hasNext()) {
Entry<String, Collection<?>> entry = entryIt.next();
Iterator<Object> valueIt = result.get(entry.getKey());
assertNotNull("The result is missing the expected field '" + entry.getKey() + "'!", valueIt);
Collection<Object> values = ModelUtils.asCollection(valueIt);
entry.getValue().removeAll(values);
assertTrue("The following expected values " + entry.getValue() + "' are missing (present: " + values + ")!", entry.getValue().isEmpty());
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation 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.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class RdfResultListTest method testRdfResultSorting.
/**
* Providing a sorted Iteration over query results stored in an RDF
* graph is not something trivial. Therefore this test
*/
@Test
public void testRdfResultSorting() {
SortedMap<Double, RdfRepresentation> sorted = new TreeMap<Double, RdfRepresentation>();
Graph resultGraph = new IndexedGraph();
RdfValueFactory vf = new RdfValueFactory(resultGraph);
IRI resultListNode = new IRI(RdfResourceEnum.QueryResultSet.getUri());
IRI resultProperty = new IRI(RdfResourceEnum.queryResult.getUri());
for (int i = 0; i < 100; i++) {
Double rank;
do {
//avoid duplicate keys
rank = Math.random();
} while (sorted.containsKey(rank));
RdfRepresentation r = vf.createRepresentation("urn:sortTest:rep." + i);
//link the representation with the query result set
resultGraph.add(new TripleImpl(resultListNode, resultProperty, r.getNode()));
r.set(RdfResourceEnum.resultScore.getUri(), rank);
sorted.put(rank, r);
}
RdfQueryResultList resultList = new RdfQueryResultList(new FieldQueryImpl(), resultGraph);
if (log.isDebugEnabled()) {
log.debug("---DEBUG Sorting ---");
for (Iterator<Representation> it = resultList.iterator(); it.hasNext(); ) {
Representation r = it.next();
log.debug("{}: {}", r.getFirst(RdfResourceEnum.resultScore.getUri()), r.getId());
}
}
log.debug("---ASSERT Sorting ---");
for (Iterator<Representation> it = resultList.iterator(); it.hasNext(); ) {
Representation r = it.next();
Double lastkey = sorted.lastKey();
Representation last = sorted.get(lastkey);
Assert.assertEquals("score: " + r.getFirst(RdfResourceEnum.resultScore.getUri()) + " of Representation " + r.getId() + " is not as expected " + last.getFirst(RdfResourceEnum.resultScore.getUri()) + " of Representation " + last.getId() + "!", r, last);
sorted.remove(lastkey);
}
Assert.assertTrue(sorted.isEmpty());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation 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.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class RdfRepresentationTest method testBNodeFiltering.
/**
* Test for STANBOL-1301
*/
@Test
public void testBNodeFiltering() {
URI concept = new URIImpl("http://example.org/mySkos#Concept123");
Representation r = createRepresentation(concept.stringValue());
assertTrue(r instanceof RdfRepresentation);
RdfRepresentation rep = (RdfRepresentation) r;
//add the example as listed in STANBOL-1301 to directly to the
//Sesame Model backing the created Representation
Model m = rep.getModel();
m.add(concept, RDF.TYPE, SKOS.CONCEPT);
m.add(concept, DCTERMS.IDENTIFIER, new LiteralImpl("123"));
m.add(concept, SKOS.PREF_LABEL, new LiteralImpl("Concept123", "en"));
BNode note1 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19576");
m.add(concept, SKOS.SCOPE_NOTE, note1);
m.add(note1, DCTERMS.CREATOR, new LiteralImpl("User1"));
m.add(note1, DCTERMS.CREATED, new LiteralImpl("2013-03-03T02:02:02Z", XMLSchema.DATETIME));
m.add(note1, RDFS.COMMENT, new LiteralImpl("The scope of this example global", "en"));
BNode note2 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19634");
m.add(concept, SKOS.SCOPE_NOTE, note2);
m.add(note2, DCTERMS.CREATOR, new LiteralImpl("User2"));
m.add(note2, DCTERMS.CREATED, new LiteralImpl("2013-03-03T04:04:04Z", XMLSchema.DATETIME));
m.add(note2, RDFS.COMMENT, new LiteralImpl("Der Geltungsbereich ist Global", "de"));
//now assert that BNodes are not reported via the Representation API
Iterator<Object> scopeNotes = rep.get(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNotes.hasNext());
Iterator<Reference> scopeNoteRefs = rep.getReferences(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNoteRefs.hasNext());
}
Aggregations