use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class UIMAToTriples method computeEnhancements.
public void computeEnhancements(ContentItem ci) throws EngineException {
FeatureStructureListHolder holder;
LiteralFactory literalFactory = LiteralFactory.getInstance();
try {
IRI uimaIRI = new IRI(uimaUri);
logger.info(new StringBuilder("Trying to load holder for ref:").append(uimaUri).toString());
holder = ci.getPart(uimaIRI, FeatureStructureListHolder.class);
for (String source : sourceNames) {
logger.info(new StringBuilder("Processing UIMA source:").append(source).toString());
List<FeatureStructure> sourceList = holder.getFeatureStructureList(source);
if (sourceList != null) {
logger.info(new StringBuilder("UIMA source:").append(source).append(" contains ").append(sourceList.size()).append(" annotations.").toString());
} else {
logger.info(new StringBuilder("Source list is null:").append(source).toString());
continue;
}
for (FeatureStructure fs : sourceList) {
String typeName = fs.getTypeName();
logger.debug(new StringBuilder("Checking ").append(typeName).toString());
if (tnfs.checkFeatureStructureAllowed(typeName, fs.getFeatures())) {
logger.debug(new StringBuilder("Adding ").append(typeName).toString());
IRI textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
Graph metadata = ci.getMetadata();
String uriRefStr = uimaUri + ":" + typeName;
if (mappings.containsKey(typeName)) {
uriRefStr = mappings.get(typeName);
}
metadata.add(new TripleImpl(textAnnotation, DC_TYPE, new IRI(uriRefStr)));
if (fs.getFeature("begin") != null) {
metadata.add(new TripleImpl(textAnnotation, ENHANCER_START, literalFactory.createTypedLiteral(fs.getFeature("begin").getValueAsInteger())));
}
if (fs.getFeature("end") != null) {
metadata.add(new TripleImpl(textAnnotation, ENHANCER_END, literalFactory.createTypedLiteral(fs.getFeature("end").getValueAsInteger())));
}
if (fs.getCoveredText() != null && !fs.getCoveredText().isEmpty()) {
metadata.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT, new PlainLiteralImpl(fs.getCoveredText())));
}
for (Feature f : fs.getFeatures()) {
if (!f.getName().equals("begin") && !f.getName().equals("end") && tnfs.checkFeatureToConvert(typeName, f)) {
String predRefStr = uimaUri + ":" + f.getName();
if (mappings.containsKey(f.getName())) {
predRefStr = mappings.get(f.getName());
}
IRI predicate = new IRI(predRefStr);
metadata.add(new TripleImpl(textAnnotation, predicate, new PlainLiteralImpl(f.getValueAsString())));
}
}
}
}
}
} catch (NoSuchPartException e) {
logger.error(new StringBuilder("No UIMA results found with ref:").append(uimaUri).toString(), e);
}
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class ContentItemTest method removeRemoveByUri.
@Test
public void removeRemoveByUri() throws IOException {
ContentItem ci = createContentItem(contentSource);
IRI uri = new IRI("urn:content.part:remove.test");
ci.addPart(uri, new Date());
try {
ci.getPart(uri, Date.class);
} catch (NoSuchPartException e) {
assertFalse("The part with the uri " + uri + " was not added correctly", true);
}
ci.removePart(uri);
try {
ci.getPart(uri, Date.class);
assertFalse("The part with the uri " + uri + " was not removed correctly", true);
} catch (NoSuchPartException e) {
// expected
}
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class AbstractEnhancerResource method enhance.
/**
* Enhances the parsed ContentItem
* @param ci the content item to enhance
* @param reqProp the request properties or <code>null</code> if none
* @throws EnhancementException
*/
protected void enhance(ContentItem ci, Map<String, Object> reqProp) throws EnhancementException {
if (jobManager != null) {
jobManager.enhanceContent(ci, getChain());
}
Graph graph = ci.getMetadata();
Boolean includeExecutionMetadata = RequestPropertiesHelper.isIncludeExecutionMetadata(reqProp);
if (includeExecutionMetadata != null && includeExecutionMetadata.booleanValue()) {
try {
graph.addAll(ci.getPart(ExecutionMetadata.CHAIN_EXECUTION, Graph.class));
} catch (NoSuchPartException e) {
// no executionMetadata available
}
}
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class ContentItemHelper method getContentParts.
/**
* Returns a Map with the current content parts of the parsed type. future
* changes to the contentParts of the content item will NOT be reflected
* within the returned map. The ordering of the {@link Iterator}s over the
* returned map is consistent with the ordering of the contentPart within the
* {@link ContentItem}. <p> When parsing {@link Object} as class the number
* of the element will be equals to the index of that content part.<p>
* In contrast to the contentPart related methods of the {@link ContentItem}
* this method does NOT throw {@link NoSuchPartException}.
* @param ci the content item
* @param clazz the class of the content part
* @return the Map with the {@link IRI id}s and the content as entries.
*/
public static <T> Map<IRI, T> getContentParts(ContentItem ci, Class<T> clazz) {
if (ci == null) {
throw new IllegalArgumentException("The parsed ContentItem MUST NOT be NULL!");
}
LinkedHashMap<IRI, T> blobs = new LinkedHashMap<IRI, T>();
IRI cpUri = null;
int index = 0;
ci.getLock().readLock().lock();
try {
do {
try {
cpUri = ci.getPartUri(index);
index++;
try {
blobs.put(cpUri, ci.getPart(cpUri, clazz));
} catch (ClassCastException e) {
// not of type T -> skip
}
} catch (NoSuchPartException e) {
// no more parts
cpUri = null;
}
} while (cpUri != null);
} finally {
ci.getLock().readLock().unlock();
}
return blobs;
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class EnhancementEngineHelper method getChainExecutionProperties.
/**
* Getter for the {@link Chain} scoped (chain and chain-engine scoped) properties
* for the parsed enhancement engine and content item.
* @param engine the enhancement engine
* @param ci the content item
* @return the chain scoped enhancement properties. This will not include any
* request scoped properties.
* @since 0.12.1 (<a href="https://issues.apache.org/jira/browse/STANBOL-1361">STANBOL-1361</a>)
*/
public static Map<String, Object> getChainExecutionProperties(EnhancementEngine engine, ContentItem ci) {
if (engine == null) {
throw new IllegalArgumentException("The parsed EnhancementEngine MUST NOT be NULL");
}
if (ci == null) {
throw new IllegalArgumentException("The parsed ContentItem MUST NOT be NULL");
}
Map<String, Object> chainExProps = new HashMap<String, Object>();
Map<String, Object> engineExProps = new HashMap<String, Object>();
ci.getLock().readLock().lock();
try {
Graph em = ExecutionMetadataHelper.getExecutionMetadata(ci);
// (1.a) retrieve EnhancementProperties from the ep:ExecutionPlan
log.debug("> extract EnhancementProperties form the ExecutionPlan");
BlankNodeOrIRI executionPlanNode = ExecutionMetadataHelper.getExecutionPlanNode(em, ExecutionMetadataHelper.getChainExecution(em, ci.getUri()));
extractEnhancementProperties(chainExProps, em, executionPlanNode, "Chain Execution");
// (1.b) retrieve Enhancement Properties from the ep:ExectutionNode
// for the parsed EnhancementEngine
log.debug("> extract EnhancementProperties form the ExecutionNode of Engine {}", engine.getName());
Iterator<Triple> engineExecutions = em.filter(null, ExecutionPlan.ENGINE, new PlainLiteralImpl(engine.getName()));
// there are multiple we will merge the properties of those
while (engineExecutions.hasNext()) {
BlankNodeOrIRI engineExecution = engineExecutions.next().getSubject();
if (em.contains(new TripleImpl(executionPlanNode, ExecutionPlan.HAS_EXECUTION_NODE, engineExecution))) {
extractEnhancementProperties(engineExProps, em, engineExecution, "Engine Execution");
}
// else engine execution of a different execution plan
}
} catch (NoSuchPartException e) {
// no execution metadata are present
log.debug(" - no ExecutionMetadata are present ...");
} finally {
ci.getLock().readLock().unlock();
}
// finally merge the chain-engine scoped properties into the chain scoped properties
chainExProps.putAll(engineExProps);
return chainExProps;
}
Aggregations