Search in sources :

Example 56 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class EnhancementJob method updateRunningExec.

/**
     * updates the {@link #runningExec} based on {@link #running}
     */
private void updateRunningExec() {
    Set<BlankNodeOrIRI> runningExec = new HashSet<BlankNodeOrIRI>(running.size());
    for (BlankNodeOrIRI node : running) {
        runningExec.add(getExecution(node));
    }
    this.runningExec = Collections.unmodifiableSet(runningExec);
}
Also used : BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashSet(java.util.HashSet)

Example 57 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class EnhancementJob method setNodeCompleted.

/**
     * Internally used to update the state kept in {@link #completed} and
     * {@link #running} and {@link #executable} after an execution was set to
     * {@link #setCompleted(BlankNodeOrIRI) completed} or 
     * {@link #setFailed(BlankNodeOrIRI, EnhancementEngine, Exception) failed}.<p>
     * This method expects to be called within an active {@link #writeLock}.
     * @param executionNode the ep:ExecutionNode linked to the em:Execution that
     * finished. 
     */
private void setNodeCompleted(BlankNodeOrIRI executionNode) {
    String engine = getEngine(executionPlan, executionNode);
    boolean optional = isOptional(executionPlan, executionNode);
    Set<BlankNodeOrIRI> dependsOn = getDependend(executionPlan, executionNode);
    if (completed.contains(executionNode)) {
        log.warn("Execution of Engine '{}' for ContentItem {} already " + "marked as completed(chain: {}, node: {}, optional {})." + " -> call ignored", new Object[] { engine, contentItem.getUri().getUnicodeString(), chain, executionNode, optional });
        return;
    }
    if (!completed.containsAll(dependsOn)) {
        // TODO maybe define an own Exception for such cases
        throw new IllegalStateException("Unable to set state of ExectionNode '" + executionNode + "' (chain '" + chain + "' | contentItem '" + contentItem.getUri() + "') to completed, because some of its depended " + "nodes are not marked completed yet. This indicates an Bug in the " + "implementation of the JobManager used to execute the ExecutionPlan. " + "(this.dependsOn=" + dependsOn + "| chain.completed " + completed + " | chain.running " + running + ")!");
    }
    if (running.remove(executionNode)) {
        log.trace("Execution of '{}' for ContentItem {} completed " + "(chain: {}, node: {}, optional {})", new Object[] { engine, contentItem.getUri().getUnicodeString(), chain, executionNode, optional });
    }
    completed.add(executionNode);
    //update the set with the completed and running executables
    updateCompletedExec();
    updateRunningExec();
    // update the executables ... this will also recognise if finished 
    checkExecutable();
}
Also used : BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) EnhancementEngineHelper.getString(org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString)

Example 58 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class EnhancementJob method setFailed.

public void setFailed(BlankNodeOrIRI execution, EnhancementEngine engine, Exception exception) {
    if (execution == null) {
        throw new IllegalArgumentException("The parsed em:Execution instance MUST NOT be NULL!");
    }
    BlankNodeOrIRI executionNode = getExecutionNode(execution);
    final boolean optional = isOptional(executionPlan, executionNode);
    final String engineName = getEngine(executionPlan, executionNode);
    log.trace("++ w: {}: {}", "setFailed", ExecutionPlanHelper.getEngine(executionPlan, executionNode));
    writeLock.lock();
    try {
        log.trace(">> w: {}: {}", "setFailed", ExecutionPlanHelper.getEngine(executionPlan, executionNode));
        StringBuilder message = new StringBuilder();
        message.append(String.format("Unable to process ContentItem '%s' with " + "Enhancement Engine '%s' because the engine ", contentItem.getUri(), engineName));
        if (engine == null) {
            message.append("is currently not active");
        } else {
            message.append(String.format("was unable to process the content " + "(Engine class: %s)", engine.getClass().getName()));
        }
        if (exception != null) {
            message.append("(Reason: ").append(exception.getMessage()).append(')');
        }
        message.append('!');
        //update the internal state
        setNodeCompleted(executionNode);
        //set this execution to failed
        setExecutionFaild(executionMetadata, execution, message.toString());
        //if not optional and the chain is not yet failed
        if (!optional && !isExecutionFailed(executionMetadata, chainExecutionNode)) {
            //set also the whole chain to faild!
            String chainMessage = String.format("Enhancement Chain failed because of required Engine '%s' failed " + "with Message: %s", engineName, message);
            setExecutionFaild(executionMetadata, chainExecutionNode, chainMessage);
            //this member stores the exception to allow
            error = exception;
        //re-throwing by the EnhancementJobManager.
        }
    } finally {
        log.trace("<< w: {}: {}", "setFailed", ExecutionPlanHelper.getEngine(executionPlan, executionNode));
        writeLock.unlock();
    }
}
Also used : BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) EnhancementEngineHelper.getString(org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString)

Example 59 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class EnhancementJobHandler method executeNextNodes.

/**
     * triggers the execution of the next nodes or if 
     * {@link EnhancementJob#isFinished()} notifies the one who registered 
     * the {@link EnhancementJob} with this component.
     * @param job the enhancement job to process
     * @return if an Execution event was sent
     */
protected boolean executeNextNodes(EnhancementJob job) {
    //getExecutable returns an snapshot so we do not need to lock
    boolean startedExecution = false;
    for (BlankNodeOrIRI executable : job.getExecutable()) {
        if (log.isTraceEnabled()) {
            log.trace("PREPARE execution of Engine {}", getEngine(job.getExecutionPlan(), job.getExecutionNode(executable)));
        }
        Dictionary<String, Object> properties = new Hashtable<String, Object>();
        properties.put(PROPERTY_JOB_MANAGER, job);
        properties.put(PROPERTY_EXECUTION, executable);
        job.setRunning(executable);
        if (log.isTraceEnabled()) {
            log.trace("SHEDULE execution of Engine {}", getEngine(job.getExecutionPlan(), job.getExecutionNode(executable)));
        }
        eventAdmin.postEvent(new Event(TOPIC_JOB_MANAGER, properties));
        startedExecution = true;
    }
    return startedExecution;
}
Also used : Hashtable(java.util.Hashtable) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Event(org.osgi.service.event.Event)

Example 60 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class ContentItemResource method initOccurrences.

private void initOccurrences() {
    Graph graph = contentItem.getMetadata();
    LiteralFactory lf = LiteralFactory.getInstance();
    Map<IRI, Collection<BlankNodeOrIRI>> suggestionMap = new HashMap<IRI, Collection<BlankNodeOrIRI>>();
    // 1) get Entity Annotations
    Map<BlankNodeOrIRI, Map<EAProps, Object>> entitySuggestionMap = new HashMap<BlankNodeOrIRI, Map<EAProps, Object>>();
    Iterator<Triple> entityAnnotations = graph.filter(null, RDF.type, ENHANCER_ENTITYANNOTATION);
    while (entityAnnotations.hasNext()) {
        BlankNodeOrIRI entityAnnotation = entityAnnotations.next().getSubject();
        //to avoid multiple lookups (e.g. if one entityAnnotation links to+
        //several TextAnnotations) we cache the data in an intermediate Map
        Map<EAProps, Object> eaData = new EnumMap<EAProps, Object>(EAProps.class);
        eaData.put(EAProps.entity, getReference(graph, entityAnnotation, ENHANCER_ENTITY_REFERENCE));
        eaData.put(EAProps.label, getString(graph, entityAnnotation, ENHANCER_ENTITY_LABEL));
        eaData.put(EAProps.confidence, EnhancementEngineHelper.get(graph, entityAnnotation, ENHANCER_CONFIDENCE, Double.class, lf));
        entitySuggestionMap.put(entityAnnotation, eaData);
        Iterator<IRI> textAnnotations = getReferences(graph, entityAnnotation, DC_RELATION);
        while (textAnnotations.hasNext()) {
            IRI textAnnotation = textAnnotations.next();
            Collection<BlankNodeOrIRI> suggestions = suggestionMap.get(textAnnotation);
            if (suggestions == null) {
                suggestions = new ArrayList<BlankNodeOrIRI>();
                suggestionMap.put(textAnnotation, suggestions);
            }
            suggestions.add(entityAnnotation);
        }
    }
    // 2) get the TextAnnotations
    Iterator<Triple> textAnnotations = graph.filter(null, RDF.type, ENHANCER_TEXTANNOTATION);
    while (textAnnotations.hasNext()) {
        BlankNodeOrIRI textAnnotation = textAnnotations.next().getSubject();
        //we need to process those to show multiple mentions
        //            if (graph.filter(textAnnotation, DC_RELATION, null).hasNext()) {
        //                // this is not the most specific occurrence of this name: skip
        //                continue;
        //            }
        String text = getString(graph, textAnnotation, Properties.ENHANCER_SELECTED_TEXT);
        //TextAnnotations without fise:selected-text are no longer ignored
        //            if(text == null){
        //                //ignore text annotations without text
        //                continue;
        //            }
        Integer start = EnhancementEngineHelper.get(graph, textAnnotation, ENHANCER_START, Integer.class, lf);
        Integer end = EnhancementEngineHelper.get(graph, textAnnotation, ENHANCER_END, Integer.class, lf);
        Double confidence = EnhancementEngineHelper.get(graph, textAnnotation, ENHANCER_CONFIDENCE, Double.class, lf);
        Iterator<IRI> types = getReferences(graph, textAnnotation, DC_TYPE);
        if (!types.hasNext()) {
            //create an iterator over null in case no types are present
            types = Collections.singleton((IRI) null).iterator();
        }
        while (types.hasNext()) {
            IRI type = types.next();
            Map<EntityExtractionSummary, EntityExtractionSummary> occurrenceMap = extractionsByTypeMap.get(type);
            if (occurrenceMap == null) {
                occurrenceMap = new TreeMap<EntityExtractionSummary, EntityExtractionSummary>();
                extractionsByTypeMap.put(type, occurrenceMap);
            }
            //in case of a language annotation use the detected language as label
            if (DC_LINGUISTIC_SYSTEM.equals(type)) {
                text = EnhancementEngineHelper.getString(graph, textAnnotation, DC_LANGUAGE);
            }
            EntityExtractionSummary entity = new EntityExtractionSummary(text, type, start, end, confidence, defaultThumbnails);
            Collection<BlankNodeOrIRI> suggestions = suggestionMap.get(textAnnotation);
            if (suggestions != null) {
                for (BlankNodeOrIRI entityAnnotation : suggestions) {
                    Map<EAProps, Object> eaData = entitySuggestionMap.get(entityAnnotation);
                    entity.addSuggestion((IRI) eaData.get(EAProps.entity), (String) eaData.get(EAProps.label), (Double) eaData.get(EAProps.confidence), graph);
                }
            }
            EntityExtractionSummary existingSummary = occurrenceMap.get(entity);
            if (existingSummary == null) {
                //new extraction summary
                occurrenceMap.put(entity, entity);
            } else {
                //extraction summary with this text and suggestions already
                //present ... only add a mention to the existing
                existingSummary.addMention(new Mention(text, start, end, confidence));
            }
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) EnhancementEngineHelper.getString(org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString) EnumMap(java.util.EnumMap) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Collection(java.util.Collection) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Aggregations

BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)89 Triple (org.apache.clerezza.commons.rdf.Triple)52 IRI (org.apache.clerezza.commons.rdf.IRI)41 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)30 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)27 HashSet (java.util.HashSet)24 Graph (org.apache.clerezza.commons.rdf.Graph)22 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)14 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)14 Literal (org.apache.clerezza.commons.rdf.Literal)13 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)12 Lock (java.util.concurrent.locks.Lock)10 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)10 EnhancementEngineHelper.getString (org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString)8 Test (org.junit.Test)8 Collection (java.util.Collection)7 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)7 Language (org.apache.clerezza.commons.rdf.Language)6 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)6