Search in sources :

Example 51 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class YardSite method getEntity.

@Override
public Entity getEntity(String id) throws ManagedSiteException {
    Representation rep;
    try {
        rep = getYard().getRepresentation(id);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
    if (rep != null) {
        Entity entity = new EntityImpl(config.getId(), rep, null);
        SiteUtils.initEntityMetadata(entity, siteMetadata, null);
        return entity;
    } else {
        return null;
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 52 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class YardSite method store.

/**
     * Stores the parsed representations to the Yard and also applies the
     * configured {@link #getFieldMapper() FieldMappings}.
     * @param The representations to store
     */
@Override
public void store(final Iterable<Representation> representations) throws ManagedSiteException {
    try {
        Yard yard = getYard();
        final ValueFactory vf = yard.getValueFactory();
        yard.store(new Iterable<Representation>() {

            @Override
            public Iterator<Representation> iterator() {
                return new Iterator<Representation>() {

                    Iterator<Representation> it = representations.iterator();

                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public Representation next() {
                        Representation next = it.next();
                        fieldMapper.applyMappings(next, next, vf);
                        return next;
                    }

                    @Override
                    public void remove() {
                        it.remove();
                    }
                };
            }
        });
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator) Iterator(java.util.Iterator) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) InMemoryValueFactory(org.apache.stanbol.entityhub.core.model.InMemoryValueFactory) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 53 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class ClerezzaModelWriter method toRDF.

private Graph toRDF(QueryResultList<?> resultList) {
    final Graph resultGraph;
    Class<?> type = resultList.getType();
    if (String.class.isAssignableFrom(type)) {
        //create a new ImmutableGraph
        resultGraph = new IndexedGraph();
        for (Object result : resultList) {
            //add a triple to each reference in the result set
            resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, new IRI(result.toString())));
        }
    } else {
        //first determine the type of the resultList
        final boolean isSignType;
        if (Representation.class.isAssignableFrom(type)) {
            isSignType = false;
        } else if (Representation.class.isAssignableFrom(type)) {
            isSignType = true;
        } else {
            //incompatible type -> throw an Exception
            throw new IllegalArgumentException("Parsed type " + type + " is not supported");
        }
        //special treatment for RdfQueryResultList for increased performance
        if (resultList instanceof RdfQueryResultList) {
            resultGraph = ((RdfQueryResultList) resultList).getResultGraph();
            if (isSignType) {
                //if we build a ResultList for Signs, that we need to do more things
                //first remove all triples representing results
                Iterator<Triple> resultTripleIt = resultGraph.filter(QUERY_RESULT_LIST, QUERY_RESULT, null);
                while (resultTripleIt.hasNext()) {
                    resultTripleIt.next();
                    resultTripleIt.remove();
                }
                //to the Sign IDs
                for (Object result : resultList) {
                    IRI signId = new IRI(((Entity) result).getId());
                    addEntityTriplesToGraph(resultGraph, (Entity) result);
                    resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, signId));
                }
            }
        } else {
            //any other implementation of the QueryResultList interface
            //create a new graph
            resultGraph = new IndexedGraph();
            if (Representation.class.isAssignableFrom(type)) {
                for (Object result : resultList) {
                    IRI resultId;
                    if (!isSignType) {
                        addRDFTo(resultGraph, (Representation) result);
                        resultId = new IRI(((Representation) result).getId());
                    } else {
                        addRDFTo(resultGraph, (Entity) result);
                        resultId = new IRI(((Entity) result).getId());
                    }
                    //Note: In case of Representation this Triple points to
                    //      the representation. In case of Signs it points to
                    //      the sign.
                    resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, resultId));
                }
            }
        }
    }
    return resultGraph;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) JSONObject(org.codehaus.jettison.json.JSONObject) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 54 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class LDPathTestBase method testSetup.

/**
     * Tests that the yard is setup correctly by checking for the
     * {@link Representation}s of the ids returned by {@link #checkContexts()}.
     * <p>
     * This methods should make is more easy to detect if a failure of a test
     * is because of a wrong setup of the Yard. 
     * @throws Exception 
     */
@Test
public void testSetup() throws Exception {
    log.info("check Setup");
    for (String context : checkContexts()) {
        Representation rep = yard.getRepresentation(context);
        log.info("  > check Entity {}", rep.getId());
        assertNotNull(rep);
        assertEquals(rep.getId(), context);
        if (log.isInfoEnabled()) {
            log.info("Data for Entity {}: \n {}", rep.getId(), ModelUtils.getRepresentationInfo(rep));
        }
    }
    log.info("   ... check completed");
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Test(org.junit.Test)

Example 55 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 = literalFactory.createTypedLiteral("This is a stirng value");
    //also add an integer to test that other typed literals are not used as texts
    Literal integerLiteral = literalFactory.createTypedLiteral(new Integer(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.getLexicalForm(), 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.getLexicalForm(), texts.next().getText());
    assertFalse(texts.hasNext());
}
Also used : Literal(org.apache.clerezza.commons.rdf.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)

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