use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class BackendTest method testSingleRepresentationBackend.
@Test
public void testSingleRepresentationBackend() throws Exception {
Representation paris = yard.getRepresentation("http://dbpedia.org/resource/Paris");
assertNotNull(paris);
SingleRepresentationBackend backend = new SingleRepresentationBackend();
backend.setRepresentation(paris);
LDPath<Object> ldPath = new LDPath<Object>(backend);
StringBuilder sb = new StringBuilder();
sb.append("myTest = .[rdf:type is <http://dbpedia.org/ontology/Place>]/rdfs:label;");
Program<Object> program = ldPath.parseProgram(new StringReader(sb.toString()));
Map<String, Collection<?>> result = program.execute(backend, yard.getValueFactory().createReference(paris.getId()));
Assert.assertNotNull(result);
Assert.assertTrue(result.containsKey("myTest"));
Collection<?> values = result.get("myTest");
Assert.assertNotNull(values);
Assert.assertFalse(values.isEmpty());
sb = new StringBuilder();
sb.append("myTest = .[rdf:type is <http://dbpedia.org/ontology/Place2>]/rdfs:label;");
program = ldPath.parseProgram(new StringReader(sb.toString()));
result = program.execute(backend, yard.getValueFactory().createReference(paris.getId()));
Assert.assertNotNull(result);
values = result.get("myTest");
Assert.assertTrue(values == null || values.isEmpty());
}
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());
}
Aggregations