Search in sources :

Example 21 with Representation

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());
}
Also used : StringReader(java.io.StringReader) LDPath(org.apache.marmotta.ldpath.LDPath) Collection(java.util.Collection) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Test(org.junit.Test)

Example 22 with Representation

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());
    }
}
Also used : Entry(java.util.Map.Entry) Collection(java.util.Collection) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Test(org.junit.Test)

Example 23 with Representation

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());
}
Also used : Literal(org.openrdf.model.Literal) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 24 with Representation

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());
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) TreeMap(java.util.TreeMap) FieldQueryImpl(org.apache.stanbol.entityhub.core.query.FieldQueryImpl) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) Test(org.junit.Test)

Example 25 with Representation

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());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Date(java.util.Date) Literal(org.openrdf.model.Literal) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Aggregations

Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)198 Test (org.junit.Test)117 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)32 HashSet (java.util.HashSet)31 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)25 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)16 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)15 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)12 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)12 ArrayList (java.util.ArrayList)11 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)10 IOException (java.io.IOException)9 IRI (org.apache.clerezza.commons.rdf.IRI)9 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)8 Graph (org.apache.clerezza.commons.rdf.Graph)8 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)8 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)8 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)8 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)8