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;
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class ContentItemTest method addingAndRetrieving.
@Test
public void addingAndRetrieving() throws IOException {
ContentItem ci = createContentItem(contentSource);
assertNotNull(ci);
assertNotNull(ci.getUri());
IRI partUri = new IRI("http://foo/");
Date someObject = new Date();
ci.addPart(partUri, someObject);
ci.getMetadata().add(new TripleImpl(ci.getUri(), new IRI("http://example.org/ontology#hasPart"), partUri));
ci.getMetadata().add(new TripleImpl(partUri, new IRI("http://example.org/ontology#isPartOf"), ci.getUri()));
assertEquals(someObject, ci.getPart(partUri, Date.class));
assertEquals(someObject, ci.getPart(1, Date.class));
assertEquals(partUri, ci.getPartUri(1));
assertEquals(new IRI(ci.getUri().getUnicodeString() + "_main"), ci.getPartUri(0));
try {
ci.getPart(2, Object.class);
assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false);
} catch (NoSuchPartException e) {
/* expected*/
}
try {
ci.getPart(new IRI("http://foo/nonexisting"), Object.class);
assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false);
} catch (NoSuchPartException e) {
/* expected*/
}
try {
ci.getPartUri(2);
assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false);
} catch (NoSuchPartException e) {
/* expected*/
}
//finally log the toString
log.info("toString: {}", ci);
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class ContentItemHelper method getBlob.
/**
* Searches an {@link ContentItem#getPart(IRI, Class) content part}
* of the type {@link Blob} with one of the the parsed mimeTypes. <p>
* NOTE:<ul>
* <li> MimeTypes are converted to lower case before compared with
* the entries of the parsed set. Therefore it is important that the parsed
* set only contains lower case values!
* <li> A read lock on the parsed {@link ContentItem} is applied while
* searching for a fitting {@link Blob}
* </ul><p>
* In contrast to the contentPart related methods of the {@link ContentItem}
* this method does NOT throw {@link NoSuchPartException}.
* @param ci the contentITem
* @param mimeTypes List of possible mimeTypes
* @return the {@link IRI URI} and the {@link Blob content} of the content
* part or <code>null</code> if not found
* @throws IllegalArgumentException If the parsed {@link ContentItem} is
* <code>null</code> or the parsed Set with the mimeTypes is <code>null</code>
* or {@link Set#isEmpty() empty}.
*/
public static Entry<IRI, Blob> getBlob(ContentItem ci, Set<String> mimeTypes) {
if (ci == null) {
throw new IllegalArgumentException("The parsed ContentItem MUST NOT be NULL!");
}
if (mimeTypes == null || mimeTypes.isEmpty()) {
throw new IllegalArgumentException("The parsed Set with mime type MUST NOT be NULL nor empty!");
}
IRI cpUri = null;
int index = 0;
ci.getLock().readLock().lock();
try {
do {
try {
cpUri = ci.getPartUri(index);
index++;
try {
Blob blob = ci.getPart(cpUri, Blob.class);
if (mimeTypes.contains(blob.getMimeType().toLowerCase())) {
return Collections.singletonMap(cpUri, blob).entrySet().iterator().next();
}
// else no match
} catch (ClassCastException e) {
// not a Blob -> ignore!
}
} catch (NoSuchPartException e) {
// no more parts
cpUri = null;
}
} while (cpUri != null);
} finally {
ci.getLock().readLock().unlock();
}
// not found
return null;
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class ExecutionMetadataHelper method initExecutionMetadataContentPart.
/**
* Getter/Initialiser for the execution metadata content part of the parsed
* content item. This part is expected to be registered with the URI
* {@link ExecutionMetadata#CHAIN_EXECUTION}. If it does not already exist
* this method creates an empty graph and register it with the parsed
* content item otherwise it returns the existing part registered under that
* URI.<p>
* Typically users will also want to use
* {@link #initExecutionMetadata(Graph, Graph, IRI, String, boolean)}
* to initialise the state based on the grpah returned by this method.
* NOTES:<ul>
* <li> If a content part is registered under the URI
* {@link ExecutionMetadata#CHAIN_EXECUTION} that is not of type
* {@link Graph} this method will replace it with an empty {@link Graph}.
* <li> This method acquires a write lock on the content item while checking
* for the content part.
* </ul>
* @param contentItem the contentItem
* @return the {@link Graph} with the execution metadata as registered as
* content part with the URI {@link ExecutionMetadata#CHAIN_EXECUTION} to
* the {@link ContentItem}
* @throws IllegalArgumentException if the parsed content itme is <code>null</code>.
*/
public static Graph initExecutionMetadataContentPart(ContentItem contentItem) {
if (contentItem == null) {
throw new IllegalArgumentException("The parsed ContentItme MUST NOT be NULL!");
}
Graph executionMetadata;
contentItem.getLock().writeLock().lock();
try {
try {
executionMetadata = contentItem.getPart(CHAIN_EXECUTION, Graph.class);
} catch (NoSuchPartException e) {
executionMetadata = new IndexedGraph();
contentItem.addPart(CHAIN_EXECUTION, executionMetadata);
}
} finally {
contentItem.getLock().writeLock().unlock();
}
return executionMetadata;
}
use of org.apache.stanbol.enhancer.servicesapi.NoSuchPartException in project stanbol by apache.
the class UIMARemoteClient method computeEnhancements.
@Override
public void computeEnhancements(ContentItem ci) throws EngineException {
Entry<IRI, Blob> contentPart = ContentItemHelper.getBlob(ci, SUPPORTED_MIMETYPES);
if (contentPart == null) {
throw new IllegalStateException("No ContentPart with an supported Mimetype '" + SUPPORTED_MIMETYPES + "' found for ContentItem " + ci.getUri() + ": This is also checked in the canEnhance method! -> This " + "indicated an Bug in the implementation of the " + "EnhancementJobManager!");
}
String text;
try {
text = ContentItemHelper.getText(contentPart.getValue());
} catch (IOException e) {
throw new InvalidContentException(this, ci, e);
}
for (UIMASimpleServletClient ussc : usscList) {
logger.info("Accessing uima source:" + ussc.getSourceName() + " endpoint:" + ussc.getUri());
List<FeatureStructure> featureSetList = ussc.process(text);
IRI uimaIRI = new IRI(uimaUri);
FeatureStructureListHolder holder;
ci.getLock().writeLock().lock();
try {
holder = ci.getPart(uimaIRI, FeatureStructureListHolder.class);
} catch (NoSuchPartException e) {
holder = new FeatureStructureListHolder();
logger.info("Adding FeatureSet List Holder content part with uri:" + uimaUri);
ci.addPart(uimaIRI, holder);
logger.info(uimaUri + " content part added.");
} finally {
ci.getLock().writeLock().unlock();
}
ci.getLock().writeLock().lock();
try {
holder.addFeatureStructureList(ussc.getSourceName(), featureSetList);
} finally {
ci.getLock().writeLock().unlock();
}
}
}
Aggregations