Search in sources :

Example 1 with FieldMapping

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping in project stanbol by apache.

the class FieldMappingUtils method serialiseFieldMapper.

public static String[] serialiseFieldMapper(FieldMapper mapper) {
    if (mapper == null) {
        return null;
    } else {
        Collection<FieldMapping> mappings = mapper.getMappings();
        String[] mappingStrings = new String[mappings.size()];
        int index = 0;
        for (Iterator<FieldMapping> it = mappings.iterator(); it.hasNext(); index++) {
            mappingStrings[index] = serialiseFieldMapping(it.next());
        }
        return mappingStrings;
    }
}
Also used : FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)

Example 2 with FieldMapping

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping in project stanbol by apache.

the class FieldMappingUtils method parseFieldMapping.

/**
 * Parses fieldMappings from a String formated like follows
 * <code><pre>
 *    fieldPattern &gt; mapping_1 mapping_2 ... mapping_n
 * </pre></code>
 * Parsing is done like follows:
 * <ul>
 * <li> The elements of the parsed string are split by spaces. Leading and
 *      tailing spaces are ignored.
 * <li> the <code>fieldPattern</code> supports {@link PatternType#wildcard}.
 *      '*' and '?' within this part are interpreted accordingly
 * <li> Each mapping can have an optional Filter. The filter section starts with
 *      <code>" | "</code> and ends with the next space.<br>
 *      Currently two types of Filters are supported.<br>
 *      <b>Language Filter:</b> Syntax:<code>@=&lt;lang-1&gt;,&lt;lang-2&gt;,
 *      ... ,&lt;lang-n&gt;</code>. <br>The default language can be activated by
 *      using an empty String (e.g. <code> "@=en,,de"</code>) or null
 *      (e.g.<code>"@=en,null,de</code>).<br>
 *      <b>Data Type Filter:</b> Syntax:<code>d=&lt;type-1&gt;,&lt;type-2&gt;,
 *      ... ,&lt;type-n&gt;</code>. Types can be specified by the full URI
 *      however the preferred way is to use the prefix and the local name
 *      (e.g.to allow all kind of floating point values one could use a
 *      filter like <code>"d=xsd:decimal,xsd:float,xsd:double"</code>).
 * <li> If the field should be mapped to one or more other fields, than the
 *      second element of the field MUST BE equals to <code>'&gt'</code>
 * <li> If the second element equals to '&gt', than all further Elements are
 *      interpreted as mapping target by field names that match the
 *      FieldPattern define in the first element.
 * </ul>
 * Examples:
 * <ul>
 * <li> To copy all fields define the Mapping<br>
 *      <code><pre>*</pre></code>
 * <li> This pattern copy all fields of the foaf namespace<br>
 *      <code><pre>http://xmlns.com/foaf/0.1/*</pre></code>
 * <li> The following Pattern uses the values of the foaf:name field as
 *      entityhub symbol label<br>
 *      <code><pre>http://xmlns.com/foaf/0.1/name &gt; http://www.iks-project.eu/ontology/entityhub/model/label</pre></code>
 * </ul>
 * Notes:
 * <ul>
 * <li> The combination of patterns for the source field and the definition of
 *      mappings is possible, but typically results in situations where all
 *      field names matched by the defined pattern are copied as values of the
 *      mapped field.
 * </ul>
 * TODO: Add Support for {@link Constraint}s on the field values.
 * @param mapping The mapping
 * @param nps Optionally a namespace prefix service used to convert
 * '{prefix}:{localname}' configurations to full URIs
 * @return the parsed {@link FieldMapping} or <code>null</code> if the parsed
 *    String can not be parsed.
 */
public static FieldMapping parseFieldMapping(String mapping, NamespacePrefixService nps) {
    if (mapping == null) {
        return null;
    }
    if (mapping.isEmpty()) {
        return null;
    }
    if (mapping.charAt(0) == '#') {
        // commend
        return null;
    }
    final boolean ignore = mapping.charAt(0) == '!';
    if (ignore) {
        mapping = mapping.substring(1);
    }
    // needed by the split(" ") used to get the parts.
    if (mapping.charAt(0) == '|') {
        // thats because the Apache Felix Webconsole likes to call trim and
        // users do like to ignore (the silly) required of leading spaces ...
        mapping = ' ' + mapping;
    }
    // TODO: maybe we should not use the spaces here
    String[] parts = mapping.split(" ");
    List<String> mappedTo = Collections.emptyList();
    String fieldPattern;
    if (!parts[0].isEmpty() && !parts[0].equals("*")) {
        try {
            fieldPattern = NamespaceMappingUtils.getConfiguredUri(nps, parts[0]);
        } catch (IllegalArgumentException e) {
            log.warn("Unable to parse fieldMapping because of unknown namespace prefix", e);
            return null;
        }
    } else {
        fieldPattern = parts[0];
    }
    Constraint filter = null;
    for (int i = 1; i < parts.length; i++) {
        if ("|".equals(parts[i]) && parts.length > i + 1) {
            filter = parseConstraint(parts[i + 1]);
        }
        if (">".equals(parts[i]) && parts.length > i + 1) {
            mappedTo = parseMappings(parts, i + 1, nps);
        }
    }
    if (ignore && filter != null) {
        log.warn("Filters are not supported for '!<fieldPatter>' type field mappings! Filter {} ignored", filter);
        filter = null;
    }
    try {
        return new FieldMapping(fieldPattern, filter, mappedTo.toArray(new String[mappedTo.size()]));
    } catch (RuntimeException e) {
        log.warn(String.format("Unable to parse FieldMapping from Line '%s'", mapping), e);
        return null;
    }
}
Also used : Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)

Example 3 with FieldMapping

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping in project stanbol by apache.

the class TrackingDereferencerBase method setDereferencedFields.

/**
 * Setter for the dereferenced fields
 * @param dereferencedFields the set containing the fields that need to be
 * dereferenced. If <code>null</code> or an empty set all fields will be
 * dereferenced.
 */
public void setDereferencedFields(List<String> dereferencedFields) {
    if (dereferencedFields != null && !dereferencedFields.isEmpty()) {
        List<FieldMapping> mappings = new ArrayList<FieldMapping>(dereferencedFields.size());
        log.debug(" > parse configured field mappings");
        for (String configuredMapping : dereferencedFields) {
            log.trace(" - parse configure mapping '{}'", configuredMapping);
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, nsPrefixService);
            if (mapping != null) {
                log.debug("   - add FieldMapping {}", mapping);
                mappings.add(mapping);
            } else if (configuredMapping != null && !configuredMapping.isEmpty()) {
                log.warn("   - unable to parse FieldMapping '{}'", configuredMapping);
            }
        }
        if (!mappings.isEmpty()) {
            log.debug(" > apply {} valid mappings", mappings.size());
            fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
            for (FieldMapping mapping : mappings) {
                fieldMapper.addMapping(mapping);
            }
        } else {
            // no valid mapping parsed
            log.debug(" > no valid mapping parsed ... will dereference all fields");
            fieldMapper = null;
        }
    } else {
        fieldMapper = null;
    }
}
Also used : DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ArrayList(java.util.ArrayList)

Example 4 with FieldMapping

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping in project stanbol by apache.

the class EntityhubDereferenceContext method initFieldMappings.

protected void initFieldMappings(List<String> fields) throws DereferenceConfigurationException {
    TrackingDereferencerBase<?> dereferencer = getEntityhubDereferencer();
    FieldMapper fieldMapper;
    if (fields != null && !fields.isEmpty()) {
        log.debug("parse FieldMappings from EnhancementProperties");
        List<FieldMapping> mappings = new ArrayList<FieldMapping>(fields.size());
        for (String configuredMapping : fields) {
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, dereferencer.getNsPrefixService());
            if (mapping != null) {
                log.debug("   - add FieldMapping {}", mapping);
                mappings.add(mapping);
            } else if (configuredMapping != null && !configuredMapping.isEmpty()) {
                log.warn("   - unable to parse FieldMapping '{}'", configuredMapping);
            }
        }
        if (!mappings.isEmpty()) {
            log.debug(" > apply {} valid mappings", mappings.size());
            fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
            for (FieldMapping mapping : mappings) {
                fieldMapper.addMapping(mapping);
            }
        } else {
            // no valid mapping parsed
            log.debug(" > no valid mapping parsed ... will dereference all fields");
            fieldMapper = null;
        }
    } else if (dereferencer.getFieldMapper() != null) {
        fieldMapper = dereferencer.getFieldMapper().clone();
    } else {
        fieldMapper = null;
    }
    // TODO: uncomment this to merge context with engine mappings. Not sure
    // if this is desirable
    // if(fieldMapper != null){
    // if(dereferencer.getFieldMapper() != null){
    // //add mappings of the engine configuration to the context mappings
    // for(FieldMapping mapping : dereferencer.getFieldMapper().getMappings()){
    // fieldMapper.addMapping(mapping);
    // }
    // }
    // }
    // if a fieldMapper is present and languages are set we will add a language
    // filter to the fieldMapper. If the fieldmapper is null languages are
    // filtered separately.
    Collection<String> langs = getLanguages();
    if (langs != null && !langs.isEmpty()) {
        if (fieldMapper == null) {
            // create a fieldMapper for filtering languages
            fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
        }
        fieldMapper.addMapping(new FieldMapping(new TextConstraint((String) null, langs.toArray(new String[langs.size()]))));
    }
    // set the field
    this.fieldMapper = fieldMapper;
}
Also used : DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ArrayList(java.util.ArrayList) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 5 with FieldMapping

use of org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping in project stanbol by apache.

the class EntityhubImpl method importEntity.

/**
 * Imports a {@link Entity} from a {@link Site}. This Method imports
 * the {@link Representation} by applying all configured mappings. It also
 * sets the {@link ManagedEntityState} to the configured default value by the
 * referenced site of the imported entity or the default for the Entityhub
 * if the site does not define this configuration.<p>
 * @param remoteEntity The entity to import
 * @param site the referenced site of the entity to import
 * @param localEntity the target entity for the import
 * @param valueFactory the valusFactory used to create instance while importing
 */
private void importEntity(Entity remoteEntity, Site site, Entity localEntity, ValueFactory valueFactory) {
    SiteConfiguration siteConfig = site.getConfiguration();
    ManagedEntityState state;
    state = siteConfig.getDefaultManagedEntityState();
    if (state == null) {
        state = config.getDefaultManagedEntityState();
    }
    // this wrapper allows to use an API to write metadata
    ManagedEntity managedEntity = ManagedEntity.init(localEntity, state);
    FieldMapper siteMapper = site.getFieldMapper();
    FieldMapper mapper = this.fieldMapper.clone();
    for (FieldMapping siteMapping : siteMapper.getMappings()) {
        mapper.addMapping(siteMapping);
    }
    // TODO: As soon as MappingActivities are implemented we need to add such
    // information to the EntityMapping instance!
    mapper.applyMappings(remoteEntity.getRepresentation(), localEntity.getRepresentation(), valueFactory);
    // set general metadata
    managedEntity.setCreated(new Date());
    // set the metadata required by the referenced site
    managedEntity.addAttributionLink(site.getConfiguration().getAttributionUrl());
    managedEntity.addAttributionText(site.getConfiguration().getAttribution(), null);
    // TODO: maybe replace with the URL of the site
    managedEntity.addContributorName(site.getConfiguration().getName());
}
Also used : FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) SiteConfiguration(org.apache.stanbol.entityhub.servicesapi.site.SiteConfiguration) ManagedEntityState(org.apache.stanbol.entityhub.servicesapi.model.ManagedEntityState) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) Date(java.util.Date)

Aggregations

FieldMapping (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)11 ArrayList (java.util.ArrayList)4 DefaultFieldMapperImpl (org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl)4 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)4 FieldMapper (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)3 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)3 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1 Activate (org.apache.felix.scr.annotations.Activate)1 ReferencedSiteConfigurationImpl (org.apache.stanbol.entityhub.core.site.ReferencedSiteConfigurationImpl)1 ManagedEntityState (org.apache.stanbol.entityhub.servicesapi.model.ManagedEntityState)1 SiteConfiguration (org.apache.stanbol.entityhub.servicesapi.site.SiteConfiguration)1 Cache (org.apache.stanbol.entityhub.servicesapi.yard.Cache)1 ServiceReference (org.osgi.framework.ServiceReference)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1