Search in sources :

Example 56 with Representation

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

the class TrackingDereferencerBase method dereference.

@Override
public final boolean dereference(IRI uri, Graph graph, Lock writeLock, DereferenceContext dc) throws DereferenceException {
    T service = getService();
    if (service == null) {
        throw new DereferenceException(uri, serviceClass.getClass().getSimpleName() + "service is currently not available");
    }
    EntityhubDereferenceContext derefContext = (EntityhubDereferenceContext) dc;
    Representation rep;
    try {
        rep = getRepresentation(service, uri.getUnicodeString(), derefContext.isOfflineMode());
    } catch (EntityhubException e) {
        throw new DereferenceException(uri, e);
    }
    //we need the languages as strings
    final Set<String> langs = derefContext.getLanguages();
    final FieldMapper fieldMapper = derefContext.getFieldMapper();
    final Program<Object> ldpathProgram = derefContext.getProgram();
    if (rep != null) {
        if (fieldMapper == null && ldpathProgram == null && (langs == null || langs.isEmpty())) {
            copyAll(uri, rep, graph, writeLock);
        } else {
            //we need to apply some filters while dereferencing
            if (fieldMapper != null || (langs != null && !langs.isEmpty())) {
                //this considers speficied fields and included languages
                copyMapped(uri, rep, fieldMapper, langs, graph, writeLock);
            }
            if (ldpathProgram != null) {
                //this executes LDPath statements
                copyLdPath(uri, getRdfBackend(service), ldpathProgram, langs, graph, writeLock);
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : DereferenceException(org.apache.stanbol.enhancer.engines.dereference.DereferenceException) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)

Example 57 with Representation

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

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

the class CacheUtils method storeBaseMappingsConfiguration.

/**
     * Stores the baseMappings to the {@link Yard}. This may cause unexpected
     * behaviour for subsequest calls of the stored configuration does not
     * correspond with the actual data stored within the cache.<p>
     * Typically this is only used at the start or end of the creation of a
     * full Cache ({@link CacheStrategy#all}) of an referenced site (entity source).<p>
     * Note also that if the {@link #baseMapper} is <code>null</code> this
     * method removes any existing configuration from the yard.
     * @throws YardException an any error while storing the config to the yard.
     * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}.
     */
public static void storeBaseMappingsConfiguration(Yard yard, FieldMapper baseMapper) throws YardException, IllegalArgumentException {
    if (yard == null) {
        throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!");
    }
    if (baseMapper == null) {
        yard.remove(Cache.BASE_CONFIGURATION_URI);
    } else {
        Representation config = yard.getValueFactory().createRepresentation(Cache.BASE_CONFIGURATION_URI);
        writeFieldConfig(config, baseMapper);
        yard.store(config);
    }
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 59 with Representation

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

the class CacheUtils method loadBaseMappings.

/**
     * Loads the base mappings form the parsed Yard
     * @param yard The yard
     * @param nsPrefixService if present '{prefix}:{localname}' configurations are
     * @return The baseMappings
     * @throws YardException on any Error while getting the Representation holding
     * the Configuration from the Yard.
     * @throws CacheInitialisationException if the configuration is found but not
     * valid.
     * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}
     */
public static FieldMapper loadBaseMappings(Yard yard, NamespacePrefixService nsPrefixService) throws YardException, CacheInitialisationException {
    if (yard == null) {
        throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!");
    }
    Representation baseConfig = yard.getRepresentation(Cache.BASE_CONFIGURATION_URI);
    if (baseConfig != null) {
        FieldMapper mapper = readFieldConfig(yard, baseConfig, nsPrefixService);
        if (mapper == null) {
            String msg = "Invalid Base Configuration: Unable to parse FieldMappings from Field " + Cache.FIELD_MAPPING_CONFIG_FIELD;
            log.error(msg);
            if (log.isErrorEnabled()) {
                log.error(ModelUtils.getRepresentationInfo(baseConfig));
            }
            throw new CacheInitialisationException(msg);
        } else {
            return mapper;
        }
    } else {
        return null;
    //throw new CacheInitialisationException("Base Configuration not present");
    }
}
Also used : CacheInitialisationException(org.apache.stanbol.entityhub.servicesapi.yard.CacheInitialisationException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)

Example 60 with Representation

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

the class RepresentationTest method testEmptyFieldGetFirstDataType.

@Test(expected = IllegalArgumentException.class)
public void testEmptyFieldGetFirstDataType() {
    Representation rep = createRepresentation(null);
    rep.getFirst("", Integer.class);
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) 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