Search in sources :

Example 6 with FieldMapping

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

the class ReferencedSiteComponent method activate.

@SuppressWarnings("unchecked")
@Activate
protected void activate(final ComponentContext ctx) throws ConfigurationException, YardException, InvalidSyntaxException {
    log.debug("in {} activate with properties {}", ReferencedSiteImpl.class.getSimpleName(), ctx.getProperties());
    if (ctx == null || ctx.getProperties() == null) {
        throw new IllegalStateException("No Component Context and/or Dictionary properties object parsed to the acticate methode");
    }
    this.cc = ctx;
    this.bc = ctx.getBundleContext();
    // create the SiteConfiguration based on the parsed properties
    // NOTE that the constructor also validation of the parsed configuration
    siteConfiguration = new ReferencedSiteConfigurationImpl(ctx.getProperties());
    if (PROHIBITED_SITE_IDS.contains(siteConfiguration.getId().toLowerCase())) {
        throw new ConfigurationException(SiteConfiguration.ID, String.format("The ID '%s' of this Referenced Site is one of the following " + "prohibited IDs: {} (case insensitive)", siteConfiguration.getId(), PROHIBITED_SITE_IDS));
    }
    log.info(" > initialise Referenced Site {}", siteConfiguration.getName());
    // if the accessUri is the same as the queryUri and both the
    // dereferencer and the entitySearcher uses the same component, than we
    // need only one component for both dependencies.
    this.dereferencerEqualsEntitySearcherComponent = // (1) accessURI == queryURI
    siteConfiguration.getAccessUri() != null && siteConfiguration.getAccessUri().equals(siteConfiguration.getQueryUri()) && // (2) entity dereferencer == entity searcher
    siteConfiguration.getEntityDereferencerType() != null && siteConfiguration.getEntityDereferencerType().equals(siteConfiguration.getEntitySearcherType());
    // init the fieldMapper based on the configuration
    FieldMapper fieldMappings = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
    if (siteConfiguration.getFieldMappings() != null) {
        log.debug(" > Initialise configured field mappings");
        for (String configuredMapping : siteConfiguration.getFieldMappings()) {
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, nsPrefixService);
            if (mapping != null) {
                log.debug("   - add FieldMapping {}", mapping);
                fieldMappings.addMapping(mapping);
            }
        }
    }
    // now init the referenced Services
    initDereferencerAndEntitySearcher();
    // Reference to the cache!
    if (siteConfiguration.getCacheId() != null) {
        String cacheFilter = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Cache.class.getName(), Cache.CACHE_YARD, siteConfiguration.getCacheId());
        cacheTracker = new ServiceTracker(ctx.getBundleContext(), ctx.getBundleContext().createFilter(cacheFilter), new ServiceTrackerCustomizer() {

            @Override
            public void removedService(ServiceReference reference, Object service) {
                if (service.equals(cache)) {
                    cache = (Cache) cacheTracker.getService();
                    updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                }
                bc.ungetService(reference);
            }

            @Override
            public void modifiedService(ServiceReference reference, Object service) {
                // the service.ranking might have changed ... so check if the
                // top ranked Cache is a different one
                Cache newCache = (Cache) cacheTracker.getService();
                if (newCache == null || !newCache.equals(cache)) {
                    // set the new cahce
                    cache = newCache;
                    // and update the service registration
                    updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                }
            }

            @Override
            public Object addingService(ServiceReference reference) {
                Object service = bc.getService(reference);
                if (service != null) {
                    if (// the first added Service or
                    cacheTracker.getServiceReference() == null || // the new service as higher ranking as the current
                    (reference.compareTo(cacheTracker.getServiceReference()) > 0)) {
                        cache = (Cache) service;
                        updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                    }
                // else the new service has lower ranking as the currently use one
                }
                // else service == null -> ignore
                return service;
            }
        });
        cacheTracker.open();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ServiceReference(org.osgi.framework.ServiceReference) ReferencedSiteConfigurationImpl(org.apache.stanbol.entityhub.core.site.ReferencedSiteConfigurationImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) Cache(org.apache.stanbol.entityhub.servicesapi.yard.Cache) Activate(org.apache.felix.scr.annotations.Activate)

Example 7 with FieldMapping

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

the class DefaultFieldMapperImpl method applyMappings.

/* (non-Javadoc)
     * @see org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper#applyMappings(org.apache.stanbol.entityhub.servicesapi.model.Representation, org.apache.stanbol.entityhub.servicesapi.model.Representation)
     */
public Representation applyMappings(Representation source, Representation target, ValueFactory valueFactory) {
    Collection<String> fields = new HashSet<String>();
    for (Iterator<String> fieldIt = source.getFieldNames(); fieldIt.hasNext(); ) {
        fields.add(fieldIt.next());
    }
    for (String field : fields) {
        // log.info("  > process field: "+field);
        // get the active Mappings
        List<FieldMapping> activeMappings = getMappings(field);
        if (!activeMappings.isEmpty()) {
            // get all the values (store them in an Collection, because we need them more than once)
            Collection<Object> values = new ArrayList<Object>();
            for (Iterator<Object> valueIt = source.get(field); valueIt.hasNext(); ) {
                values.add(valueIt.next());
            }
            // only to be sure, that this is not changed by Filters!
            values = Collections.unmodifiableCollection(values);
            /*
                 * (1) Before working with the values first analyse the active
                 * mappings and filters. Two things
                 * a) Init Wildcard Filters:
                 *    Language filters set on namespaces are executed on all field
                 *    mappings that define no language filter
                 * b) calculate the mapped fields. Possible there are no mappings
                 *    left. Than we need not to process all the values
                 */
            Set<String> targetFields = new HashSet<String>();
            TextConstraint globalFilter = null;
            Collection<Object> globalFiltered = null;
            /*
                 * NOTE: the mappings are sorted in the way, that the most
                 *   prominent one will be at index 0. The wildcard "*" will
                 *   be always the last.
                 *   So we need to parse backwards because than more prominent
                 *   things will overwrite and win!
                 */
            for (int i = activeMappings.size() - 1; i >= 0; i--) {
                FieldMapping mapping = activeMappings.get(i);
                if (// if wildcard
                mapping.usesWildcard() && // and not ignore
                !mapping.ignoreField() && // and a filter is present
                mapping.getFilter() != null && mapping.getFilter().getType() == ConstraintType.text) {
                    // and of type text
                    // set the global text filter.
                    // NOTE: the active mappings are sorted in that way, that
                    // the most specific one is set last
                    globalFilter = (TextConstraint) mapping.getFilter();
                }
                for (String targetField : mapping.getMappings()) {
                    if (mapping.ignoreField()) {
                        targetFields.remove(targetField);
                    } else {
                        targetFields.add(targetField);
                    }
                }
            }
            // log.info("    o global text filter: "+globalFilter);
            if (globalFilter != null) {
                globalFiltered = new HashSet<Object>(values);
                // parse false ass third argument, because we need not to filter
                // non-Text values for wildcard filter!
                processFilter(globalFilter, globalFiltered, false);
            }
            // now process the mappings
            for (FieldMapping mapping : activeMappings) {
                if (!mapping.ignoreField() && !Collections.disjoint(targetFields, mapping.getMappings())) {
                    processMapping(mapping, valueFactory, field, values, globalFiltered, targetFields, target);
                // } else if(!mapping.ignoreField()) {
                // log.info(String.format("  << ignore mapping %s ",mapping));
                // } else {
                // log.info(String.format("  << %s ",mapping));
                }
            }
        }
    }
    /*
         * TODO: return a "MappingReport"
         * All mapping activities should be documented and stored with the
         * MappedEntity as MappingActivity!
         */
    return target;
}
Also used : FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ArrayList(java.util.ArrayList) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) HashSet(java.util.HashSet)

Example 8 with FieldMapping

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

the class DefaultFieldMapperImpl method addMapping.

/* (non-Javadoc)
     * @see org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper#addMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)
     */
public void addMapping(FieldMapping mapping) {
    if (mapping == null) {
        return;
    }
    if (mappings.add(mapping)) {
        if (mapping.usesWildcard()) {
            Pattern fieldPattern = mapping.getRegexPattern();
            synchronized (wildcardMap) {
                Set<FieldMapping> fieldPatternMappings = wildcardMap.get(fieldPattern);
                if (fieldPatternMappings == null) {
                    // new TreeSet<FieldMapping>(FieldMappingUtils.FIELD_MAPPING_COMPARATOR);
                    fieldPatternMappings = new HashSet<FieldMapping>();
                    wildcardMap.put(fieldPattern, fieldPatternMappings);
                }
                fieldPatternMappings.add(mapping);
            }
        } else {
            String fieldName = mapping.getFieldPattern();
            synchronized (fieldMap) {
                Set<FieldMapping> fieldPatternMappings = fieldMap.get(fieldName);
                if (fieldPatternMappings == null) {
                    // new TreeSet<FieldMapping>(FieldMappingUtils.FIELD_MAPPING_COMPARATOR);
                    fieldPatternMappings = new HashSet<FieldMapping>();
                    fieldMap.put(fieldName, fieldPatternMappings);
                }
                fieldPatternMappings.add(mapping);
            }
        }
    }
// else already present -> nothing todo
}
Also used : Pattern(java.util.regex.Pattern) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)

Example 9 with FieldMapping

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

the class FieldMappingUtils method parseFieldMappings.

/**
 * Parses FieldMappings from the parsed strings
 * @param mappings the mappings to parse
 * @return the parsed mappings
 */
public static List<FieldMapping> parseFieldMappings(Iterator<String> mappings, NamespacePrefixService nps) {
    List<FieldMapping> fieldMappings = new ArrayList<FieldMapping>();
    log.debug("Parse FieldMappings");
    while (mappings.hasNext()) {
        String mappingString = mappings.next();
        log.debug(mappingString);
        if (mappingString != null && // not an empty line
        !mappingString.isEmpty() && !(mappingString.charAt(0) == FieldMapping.COMMENT_CHAR)) {
            // not an comment
            FieldMapping fieldMapping = parseFieldMapping(mappingString, nps);
            if (fieldMapping != null) {
                fieldMappings.add(fieldMapping);
            } else {
                log.warn("Unable to parse FieldMapping for '{}'", mappingString);
            }
        }
    }
    return fieldMappings;
}
Also used : FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ArrayList(java.util.ArrayList)

Example 10 with FieldMapping

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

the class CacheUtils method readFieldConfig.

/**
 * Reads the field mapping config from an document
 * @param yard the yard of the parsed Representation
 * @param config the configuration MUST NOT be <code>null</code>
 * @param nsPrefixService if present '{prefix}:{localname}' configurations are
 * supported for the fieldmappings used by the cache.
 * @return A field mapper configured based on the configuration in the parsed {@link Representation}
 * @throws if the parsed {@link Representation} does not contain a value for {@value CacheConstants.FIELD_MAPPING_CONFIG_FIELD}.
 */
private static FieldMapper readFieldConfig(Yard yard, Representation config, NamespacePrefixService nsPrefixService) {
    Object mappingValue = config.getFirst(Cache.FIELD_MAPPING_CONFIG_FIELD);
    if (mappingValue != null) {
        DefaultFieldMapperImpl fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
        for (String mappingStirng : mappingValue.toString().split("\n")) {
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(mappingStirng, nsPrefixService);
            if (mapping != null) {
                log.info("  > add Mapping: " + mappingStirng);
                fieldMapper.addMapping(mapping);
            }
        }
        return fieldMapper;
    } else {
        return null;
    }
}
Also used : DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)

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