Search in sources :

Example 1 with EnhancementPropertyException

use of org.apache.stanbol.enhancer.servicesapi.EnhancementPropertyException in project stanbol by apache.

the class EntityDereferenceEngine method computeEnhancements.

@Override
public final void computeEnhancements(ContentItem ci) throws EngineException {
    if (offline && !dereferencer.supportsOfflineMode()) {
        //entity dereferencer does no longer support offline mode
        return;
    }
    log.debug("> dereference Entities for ContentItem {}", ci.getUri());
    long start = System.nanoTime();
    Map<String, Object> enhancemntProps = EnhancementEngineHelper.getEnhancementProperties(this, ci);
    final DereferenceContext derefContext;
    final Graph metadata = ci.getMetadata();
    Set<IRI> referencedEntities = new HashSet<IRI>();
    ci.getLock().readLock().lock();
    try {
        //(1) Create the DereferenceContext
        if (filterContentLanguages) {
            //parse the languages detected for the content
            Set<String> contentLanguages = new HashSet<String>();
            for (BlankNodeOrIRI langAnno : EnhancementEngineHelper.getLanguageAnnotations(metadata)) {
                contentLanguages.add(EnhancementEngineHelper.getString(metadata, langAnno, DC_LANGUAGE));
            }
            enhancemntProps.put(DereferenceContext.INTERNAL_CONTENT_LANGUAGES, contentLanguages);
        }
        //create the dereference context and handle possible configuration exceptions
        try {
            derefContext = contextFactory.createContext(this, enhancemntProps);
            derefContext.setOfflineMode(offline);
        } catch (DereferenceConfigurationException e) {
            StringBuilder message = new StringBuilder("Unsupported Derefernece Configuarion ");
            if (e.getProperty() != null) {
                message.append("for property '").append(e.getProperty()).append("' ");
            }
            message.append(" parsed via the EnhancementProperties of this request!");
            throw new EnhancementPropertyException(this, ci, e.getProperty(), message.toString(), e);
        }
        //parse the referenced entities from the graph
        //(2) read all Entities we need to dereference from the parsed contentItem
        Set<IRI> checked = new HashSet<IRI>();
        //since STANBOL-1334 the list of properties that refer to entities can be configured
        for (IRI referenceProperty : derefContext.getEntityReferences()) {
            Iterator<Triple> entityReferences = metadata.filter(null, referenceProperty, null);
            while (entityReferences.hasNext()) {
                Triple triple = entityReferences.next();
                RDFTerm entityReference = triple.getObject();
                if (//only URIs
                (entityReference instanceof IRI) && //do not check a URI twice
                checked.add((IRI) entityReference) && //fallback mode
                chekcFallbackMode((IRI) entityReference, metadata) && checkURI((IRI) entityReference)) {
                    //URI prefixes and patterns
                    boolean added = referencedEntities.add((IRI) entityReference);
                    if (added && log.isTraceEnabled()) {
                        log.trace("  ... schedule Entity {} (referenced-by: {})", entityReference, referenceProperty);
                    }
                } else if (log.isTraceEnabled()) {
                    log.trace(" ... ignore Entity {} (referenced-by: {})", entityReference, referenceProperty);
                }
            }
        }
    } finally {
        ci.getLock().readLock().unlock();
    }
    long schedule = System.nanoTime();
    final Lock writeLock = ci.getLock().writeLock();
    log.trace(" - scheduled {} Entities for dereferencing", referencedEntities.size());
    //(2) dereference the Entities
    ExecutorService executor = dereferencer.getExecutor();
    Set<IRI> failedEntities = new HashSet<IRI>();
    int dereferencedCount = 0;
    List<DereferenceJob> dereferenceJobs = new ArrayList<DereferenceJob>(referencedEntities.size());
    if (executor != null && !executor.isShutdown()) {
        //schedule all entities to dereference
        for (final IRI entity : referencedEntities) {
            DereferenceJob dereferenceJob = new DereferenceJob(entity, metadata, writeLock, derefContext);
            dereferenceJob.setFuture(executor.submit(dereferenceJob));
            dereferenceJobs.add(dereferenceJob);
        }
        //wait for all entities to be dereferenced
        for (DereferenceJob dereferenceJob : dereferenceJobs) {
            try {
                if (dereferenceJob.await()) {
                    dereferencedCount++;
                }
            } catch (InterruptedException e) {
                // Restore the interrupted status
                Thread.currentThread().interrupt();
                throw new EngineException(this, ci, "Interupted while waiting for dereferencing Entities", e);
            } catch (ExecutionException e) {
                if (e.getCause() instanceof DereferenceException) {
                    failedEntities.add(dereferenceJob.entity);
                    log.debug(" ... error while dereferencing " + dereferenceJob.entity + "!", e);
                } else {
                    //unknown error
                    throw new EngineException(this, ci, "Unchecked Error while " + "dereferencing Entity " + dereferenceJob.entity + "!", e);
                }
            }
        }
    } else {
        //dereference using the current thread
        for (IRI entity : referencedEntities) {
            try {
                log.trace("  ... dereference {}", entity);
                if (dereferencer.dereference(entity, metadata, writeLock, derefContext)) {
                    dereferencedCount++;
                    log.trace("    + success");
                } else {
                    log.trace("    - not found");
                }
            } catch (DereferenceException e) {
                log.debug(" ... error while dereferencing " + entity + "!", e);
                failedEntities.add(entity);
            }
        }
    }
    long end = System.nanoTime();
    float sheduleDuration = ((schedule - start) / 10000) / 100f;
    float dereferenceDuration = ((end - schedule) / 10000) / 100f;
    float duration = ((end - start) / 10000) / 100f;
    if (!failedEntities.isEmpty()) {
        log.warn(" - unable to dereference {} of {} for ContentItem {}", new Object[] { failedEntities.size(), referencedEntities.size(), ci.getUri() });
    }
    if (log.isDebugEnabled() && dereferencedCount > 0) {
        log.debug(" - dereferenced {} of {} Entities in {}ms | schedule:{}ms | " + " dereference: {}ms ({}ms/entity)", new Object[] { dereferencedCount, referencedEntities.size(), duration, sheduleDuration, dereferenceDuration, dereferenceDuration / dereferencedCount });
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ArrayList(java.util.ArrayList) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) EnhancementPropertyException(org.apache.stanbol.enhancer.servicesapi.EnhancementPropertyException) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Lock(java.util.concurrent.locks.Lock) Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Lock (java.util.concurrent.locks.Lock)1 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)1 Triple (org.apache.clerezza.commons.rdf.Triple)1 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)1 EnhancementPropertyException (org.apache.stanbol.enhancer.servicesapi.EnhancementPropertyException)1