use of org.apache.uima.resource.ResourceManager in project dkpro-lab by dkpro.
the class CpeExecutionEngine method run.
@Override
public String run(Task aConfiguration) throws ExecutionException, LifeCycleException {
if (!(aConfiguration instanceof UimaTask)) {
throw new ExecutionException("This engine can only execute [" + UimaTask.class.getName() + "]");
}
UimaTask configuration = (UimaTask) aConfiguration;
// Create persistence service for injection into analysis components
TaskContext ctx = contextFactory.createContext(aConfiguration);
try {
ResourceManager resMgr = newDefaultResourceManager();
// Make sure the descriptor is fully resolved. It will be modified and
// thus should not be modified again afterwards by UIMA.
AnalysisEngineDescription analysisDesc = configuration.getAnalysisEngineDescription(ctx);
analysisDesc.resolveImports(resMgr);
// Scan components that accept the service and bind it to them
bindResource(analysisDesc, TaskContext.class, TaskContextProvider.class, TaskContextProvider.PARAM_FACTORY_NAME, contextFactory.getId(), TaskContextProvider.PARAM_CONTEXT_ID, ctx.getId());
CpeBuilder mgr = new CpeBuilder();
ctx.message("CPE will be using " + Runtime.getRuntime().availableProcessors() + " parallel threads to optimally utilize your cpu cores");
mgr.setMaxProcessingUnitThreadCount(Runtime.getRuntime().availableProcessors());
mgr.setReader(configuration.getCollectionReaderDescription(ctx));
mgr.setAnalysisEngine(analysisDesc);
StatusCallbackListenerImpl status = new StatusCallbackListenerImpl(ctx);
CollectionProcessingEngine engine = mgr.createCpe(status);
// Now the setup is complete
ctx.getLifeCycleManager().initialize(ctx, aConfiguration);
// Start recording
ctx.getLifeCycleManager().begin(ctx, aConfiguration);
// Run the experiment
engine.process();
try {
synchronized (status) {
while (status.isProcessing) {
status.wait();
}
}
} catch (InterruptedException e) {
ctx.message("CPE interrupted.");
}
if (status.exceptions.size() > 0) {
throw status.exceptions.get(0);
}
// End recording
ctx.getLifeCycleManager().complete(ctx, aConfiguration);
return ctx.getId();
} catch (LifeCycleException e) {
ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
throw e;
} catch (Throwable e) {
ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
throw new ExecutionException(e);
} finally {
if (ctx != null) {
ctx.getLifeCycleManager().destroy(ctx, aConfiguration);
}
}
}
use of org.apache.uima.resource.ResourceManager in project dkpro-tc by dkpro.
the class FeatureUtil method createResource.
// TODO this will eventually also be included in uimaFit and can be removed then
@SuppressWarnings("unchecked")
public static <T extends Resource> T createResource(Class<T> aClass, Object... aParam) throws ResourceInitializationException, ResourceAccessException {
// Configure external resource
ExternalResourceDescription desc = ExternalResourceFactory.createExternalResourceDescription(aClass, aParam);
// Configure resource manager
ResourceManagerConfiguration cfg = UIMAFramework.getResourceSpecifierFactory().createResourceManagerConfiguration();
ExternalResourceFactory.bindExternalResource(cfg, "rootResource", desc);
// Instantiate resource manager (internally instantiates resources)
final ResourceManager manager = new ResourceManager_impl();
manager.initializeExternalResources(cfg, "/", null);
T res = (T) manager.getResource("/rootResource");
// Initialize nested resources
UimaContextAdmin ctx = new RootUimaContext_impl() {
@Override
public ResourceManager getResourceManager() {
return manager;
}
};
ExternalResourceInitializer.initialize(res, ctx);
// Get resource instance
return res;
}
use of org.apache.uima.resource.ResourceManager in project dkpro-tc by dkpro.
the class TcAnnotator method getModelFeatureAwareResourceManager.
/*
* Produces a resource manager that is used when creating the engine which is aware of the class
* files located in the model folder
*/
private static ResourceManager getModelFeatureAwareResourceManager(File tcModelLocation) throws ResourceInitializationException, MalformedURLException {
// The features of a model are located in a subfolder where Java does
// not look for them by default. This avoids that during model execution
// several features with the same name are on the classpath which might
// cause undefined behavior as it is not know which feature is first
// found if several with same name exist. We create a new resource
// manager here and point the manager explicitly to this subfolder where
// the features to be used are located.
ResourceManager resourceManager = ResourceManagerFactory.newResourceManager();
String classpathOfModelFeatures = tcModelLocation.getAbsolutePath() + "/" + Constants.MODEL_FEATURE_CLASS_FOLDER;
resourceManager.setExtensionClassPath(classpathOfModelFeatures, true);
return resourceManager;
}
use of org.apache.uima.resource.ResourceManager in project dkpro-lab by dkpro.
the class SimpleExecutionEngine method run.
@Override
public String run(Task aConfiguration) throws ExecutionException, LifeCycleException {
if (!(aConfiguration instanceof UimaTask)) {
throw new ExecutionException("This engine can only execute [" + UimaTask.class.getName() + "]");
}
UimaTask configuration = (UimaTask) aConfiguration;
// Create persistence service for injection into analysis components
TaskContext ctx = contextFactory.createContext(aConfiguration);
try {
ResourceManager resMgr = newDefaultResourceManager();
// Make sure the descriptor is fully resolved. It will be modified and
// thus should not be modified again afterwards by UIMA.
AnalysisEngineDescription analysisDesc = configuration.getAnalysisEngineDescription(ctx);
analysisDesc.resolveImports(resMgr);
if (analysisDesc.getMetaData().getName() == null) {
analysisDesc.getMetaData().setName("Analysis for " + aConfiguration.getType());
}
// Scan components that accept the service and bind it to them
bindResource(analysisDesc, TaskContext.class, TaskContextProvider.class, TaskContextProvider.PARAM_FACTORY_NAME, contextFactory.getId(), TaskContextProvider.PARAM_CONTEXT_ID, ctx.getId());
// Set up UIMA context & logging
Logger logger = new UimaLoggingAdapter(ctx);
UimaContextAdmin uimaCtx = newUimaContext(logger, resMgr, newConfigurationManager());
// Set up reader
CollectionReaderDescription readerDesc = configuration.getCollectionReaderDescription(ctx);
if (readerDesc.getMetaData().getName() == null) {
readerDesc.getMetaData().setName("Reader for " + aConfiguration.getType());
}
Map<String, Object> addReaderParam = new HashMap<String, Object>();
addReaderParam.put(Resource.PARAM_UIMA_CONTEXT, uimaCtx);
addReaderParam.put(Resource.PARAM_RESOURCE_MANAGER, resMgr);
CollectionReader reader = produceCollectionReader(readerDesc, resMgr, addReaderParam);
// Set up analysis engine
AnalysisEngine engine;
if (analysisDesc.isPrimitive()) {
engine = new PrimitiveAnalysisEngine_impl();
} else {
engine = new AggregateAnalysisEngine_impl();
}
Map<String, Object> addEngineParam = new HashMap<String, Object>();
addReaderParam.put(Resource.PARAM_UIMA_CONTEXT, uimaCtx);
addReaderParam.put(Resource.PARAM_RESOURCE_MANAGER, resMgr);
engine.initialize(analysisDesc, addEngineParam);
// Now the setup is complete
ctx.getLifeCycleManager().initialize(ctx, aConfiguration);
// Start recording
ctx.getLifeCycleManager().begin(ctx, aConfiguration);
// Run the experiment
// Apply the engine to all documents provided by the reader
List<ResourceMetaData> metaData = new ArrayList<ResourceMetaData>();
metaData.add(reader.getMetaData());
metaData.add(engine.getMetaData());
CAS cas = CasCreationUtils.createCas(metaData);
while (reader.hasNext()) {
reader.getNext(cas);
engine.process(cas);
String documentTitle = "";
Feature documentTitleFeature = cas.getDocumentAnnotation().getType().getFeatureByBaseName("documentTitle");
if (documentTitleFeature != null) {
documentTitle = cas.getDocumentAnnotation().getFeatureValueAsString(documentTitleFeature);
}
cas.reset();
Progress[] progresses = reader.getProgress();
if (progresses != null) {
for (Progress p : progresses) {
ctx.message("Progress " + readerDesc.getImplementationName() + " " + p.getCompleted() + "/" + p.getTotal() + " " + p.getUnit() + " " + "(" + documentTitle + ")");
}
}
}
// Shut down engine and reader
engine.collectionProcessComplete();
reader.close();
engine.destroy();
reader.destroy();
// End recording
ctx.getLifeCycleManager().complete(ctx, aConfiguration);
return ctx.getId();
} catch (LifeCycleException e) {
ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
throw e;
} catch (Throwable e) {
ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
throw new ExecutionException(e);
} finally {
if (ctx != null) {
ctx.getLifeCycleManager().destroy(ctx, aConfiguration);
}
}
}
use of org.apache.uima.resource.ResourceManager in project dkpro-lab by dkpro.
the class UimaAsExecutionEngine method run.
@Override
public String run(Task aConfiguration) throws ExecutionException, LifeCycleException {
if (!(aConfiguration instanceof UimaTask)) {
throw new ExecutionException("This engine can only execute [" + UimaTask.class.getName() + "]");
}
configuration = (UimaTask) aConfiguration;
ctx = contextFactory.createContext(aConfiguration);
try {
ResourceManager resMgr = newDefaultResourceManager();
// Make sure the descriptor is fully resolved. It will be modified and
// thus should not be modified again afterwards by UIMA.
AnalysisEngineDescription analysisDesc = configuration.getAnalysisEngineDescription(ctx);
analysisDesc.resolveImports(resMgr);
// Scan components that accept the service and bind it to them
bindResource(analysisDesc, TaskContext.class, TaskContextProvider.class, TaskContextProvider.PARAM_FACTORY_NAME, contextFactory.getId(), TaskContextProvider.PARAM_CONTEXT_ID, ctx.getId());
ctx.message("Bound external resources");
// Now the setup is complete
ctx.getLifeCycleManager().initialize(ctx, aConfiguration);
// Deploy experiment as UIMA-AS service
initializeService();
// Initialize the client
initializeClient();
// Start recording
ctx.getLifeCycleManager().begin(ctx, aConfiguration);
// Run the experiment
process();
// End recording
ctx.getLifeCycleManager().complete(ctx, aConfiguration);
return ctx.getId();
} catch (LifeCycleException e) {
ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
throw e;
} catch (Exception e) {
ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
throw new ExecutionException(e);
} finally {
destroy();
}
}
Aggregations