Search in sources :

Example 1 with ValueFactory

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

the class EntityhubImpl method importEntity.

/**
     * Imports the Entity
     * @param remoteEntity the Entity to import
     * @return the Entity created and stored within the Entityhub
     * @throws YardException
     */
protected Entity importEntity(Entity remoteEntity) throws YardException {
    if (remoteEntity == null) {
        return null;
    }
    Site site = siteManager.getSite(remoteEntity.getSite());
    if (site == null) {
        log.warn("Unable to import Entity {} because the ReferencedSite {} is currently not active -> return null", remoteEntity.getId(), remoteEntity.getSite());
        return null;
    }
    ValueFactory valueFactory = entityhubYard.getValueFactory();
    //Create the locally managed Entity
    Representation localRep = entityhubYard.create(constructResourceId(DEFAULT_MANAGED_ENTITY_PREFIX));
    Entity localEntity = loadEntity(localRep);
    importEntity(remoteEntity, site, localEntity, valueFactory);
    //Second create and init the Mapping
    Representation entityMappingRepresentation = entityhubYard.create(constructResourceId(DEFAULT_MAPPING_PREFIX));
    Entity entityMappingEntity = loadEntity(entityMappingRepresentation);
    establishMapping(localEntity, remoteEntity, site, entityMappingEntity);
    //Store the entity and the mappedEntity in the entityhubYard
    storeEntity(localEntity);
    storeEntity(entityMappingEntity);
    return localEntity;
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 2 with ValueFactory

use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory 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 3 with ValueFactory

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

the class CacheImpl method applyCacheMappings.

/**
     * Applies the mappings defined by the {@link #baseMapper} and the {@link #additionalMapper}
     * to the parsed Representation.
     *
     * @param yard The yard (local reference to avoid syncronization)
     * @param representation The representation to map
     * @return the mapped representation
     */
private Representation applyCacheMappings(Yard yard, Representation representation) {
    long start = System.currentTimeMillis();
    Representation mapped = null;
    ValueFactory valueFactory = getValueFactory();
    if (baseMapper != null) {
        mapped = yard.getValueFactory().createRepresentation(representation.getId());
        baseMapper.applyMappings(representation, mapped, valueFactory);
    }
    if (additionalMapper != null) {
        if (mapped == null) {
            mapped = yard.getValueFactory().createRepresentation(representation.getId());
        }
        additionalMapper.applyMappings(representation, mapped, valueFactory);
    }
    log.info("  -- applied mappings in " + (System.currentTimeMillis() - start) + "ms");
    return mapped != null ? mapped : representation;
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 4 with ValueFactory

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

the class RepresentationTest method testURIToReferenceConversion.

/**
     * Tests if {@link Reference} instances are correctly generated for {@link URI}. This test also depends on
     * the correct implementation of the {@link Reference#equals(Object)} method
     * 
     * @throws URISyntaxException
     */
@Test
public void testURIToReferenceConversion() throws URISyntaxException {
    String field = "urn:the.field:used.for.this.Test";
    URI uri = new URI("http://www.test.org/uriTest");
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    // test conversion
    rep.add(field, uri);
    Iterator<Reference> refs = rep.getReferences(field);
    assertTrue(refs.hasNext());
    assertEquals(refs.next().getReference(), uri.toString());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, uri);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createReference(uri.toString()));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, uri);
    assertFalse(rep.get(field).hasNext());
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URI(java.net.URI) Test(org.junit.Test)

Example 5 with ValueFactory

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

the class RepresentationTest method testFieldRemoval.

@Test
public void testFieldRemoval() throws URISyntaxException {
    String field = "urn:the.field:used.for.this.Test";
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    // Test removal for References
    String strRef = "urn:testValue";
    rep.addReference(field, strRef);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeReference(field, strRef);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    Reference ref = vf.createReference("urn:testValue2");
    rep.add(field, ref);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, ref);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // test removal for texts (with and without language)
    String strText = "test text";
    String strTextLang = "en";
    rep.addNaturalText(field, strText, strTextLang);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeNaturalText(field, strText, strTextLang);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    String strTextNoLang = "test text without lang";
    rep.addNaturalText(field, strTextNoLang);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeNaturalText(field, strTextNoLang);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // there is also the possibility to explicitly parse null as language
    // could internally case differences however externally this is the same
    rep.addNaturalText(field, strTextNoLang, (String) null);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeNaturalText(field, strTextNoLang, (String) null);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    Text text = vf.createText("Das ist ein Text zum testen des Text Objektes", "de");
    rep.add(field, text);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, text);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // Test a dataTypes values
    Integer intValue = 42;
    rep.add(field, intValue);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, intValue);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // Some Values are converted by the add(String field,Object value) Method
    // to other data types. This MUST also be assured for removal
    // NOTE: testing the conversions is done in other test methods!
    URI testURI = new URI("http://www.test.org/test");
    rep.add(field, testURI);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, testURI);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URI(java.net.URI) Test(org.junit.Test)

Aggregations

ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)17 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)7 Test (org.junit.Test)7 URI (java.net.URI)4 HashSet (java.util.HashSet)4 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)4 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)4 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)3 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)3 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)3 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)3 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)3 URL (java.net.URL)2 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)2 AdaptingIterator (org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)2 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)2 StringReader (java.io.StringReader)1