Search in sources :

Example 1 with ResourceMetaData

use of org.apache.uima.resource.metadata.ResourceMetaData 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);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LifeCycleException(org.dkpro.lab.engine.LifeCycleException) Logger(org.apache.uima.util.Logger) Feature(org.apache.uima.cas.Feature) PrimitiveAnalysisEngine_impl(org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl) UimaTask(org.dkpro.lab.uima.task.UimaTask) ExecutionException(org.dkpro.lab.engine.ExecutionException) UimaLoggingAdapter(org.dkpro.lab.uima.task.impl.UimaLoggingAdapter) Progress(org.apache.uima.util.Progress) TaskContext(org.dkpro.lab.engine.TaskContext) UIMAFramework.produceCollectionReader(org.apache.uima.UIMAFramework.produceCollectionReader) CollectionReader(org.apache.uima.collection.CollectionReader) ResourceManager(org.apache.uima.resource.ResourceManager) UIMAFramework.newDefaultResourceManager(org.apache.uima.UIMAFramework.newDefaultResourceManager) AggregateAnalysisEngine_impl(org.apache.uima.analysis_engine.impl.AggregateAnalysisEngine_impl) CollectionReaderDescription(org.apache.uima.collection.CollectionReaderDescription) CAS(org.apache.uima.cas.CAS) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) UimaContextAdmin(org.apache.uima.UimaContextAdmin) ResourceMetaData(org.apache.uima.resource.metadata.ResourceMetaData) AnalysisEngine(org.apache.uima.analysis_engine.AnalysisEngine)

Example 2 with ResourceMetaData

use of org.apache.uima.resource.metadata.ResourceMetaData in project dkpro-lab by dkpro.

the class UimaAsExecutionEngine method initializeService.

protected void initializeService() throws Exception {
    // Create Asynchronous Engine API
    uimaAsEngine = new BaseUIMAAsynchronousEngine_impl();
    // Save the AED to a file because UIMA-AS cannot have an AED direclty embedded in its
    // descriptor
    AnalysisEngineDescription topDescriptor = configuration.getAnalysisEngineDescription(ctx);
    ResourceMetaData topMetaData = topDescriptor.getMetaData();
    File topDescriptorFile = File.createTempFile(getClass().getSimpleName(), ".xml");
    topDescriptorFile.deleteOnExit();
    try (OutputStream os = new FileOutputStream(topDescriptorFile)) {
        topDescriptor.toXML(os);
    }
    // Create service descriptor
    ServiceContext context = new ServiceContextImpl(topMetaData.getName(), topMetaData.getDescription(), topDescriptorFile.getAbsolutePath(), endpoint, brokerUrl);
    UimaASPrimitiveDeploymentDescriptor dd = DeploymentDescriptorFactory.createPrimitiveDeploymentDescriptor(context);
    // Store service descriptor also to a temporary file
    File deploymentDescriptionFile = File.createTempFile(getClass().getSimpleName(), ".xml");
    deploymentDescriptionFile.deleteOnExit();
    dd.save(deploymentDescriptionFile);
    Map<String, Object> serviceCtx = new HashMap<String, Object>();
    serviceCtx.put(UimaAsynchronousEngine.DD2SpringXsltFilePath, getUrlAsFile(getClass().getResource("/uima-as/dd2spring.xsl"), true).getAbsolutePath());
    serviceCtx.put(UimaAsynchronousEngine.SaxonClasspath, getClass().getResource("/uima-as/saxon8.jar").toString());
    serviceId = uimaAsEngine.deploy(deploymentDescriptionFile.getAbsolutePath(), serviceCtx);
    ctx.message("Deployed experiment as UIMA-AS service: [" + serviceId + "]");
}
Also used : HashMap(java.util.HashMap) ServiceContext(org.apache.uima.resourceSpecifier.factory.ServiceContext) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BaseUIMAAsynchronousEngine_impl(org.apache.uima.adapter.jms.client.BaseUIMAAsynchronousEngine_impl) FileOutputStream(java.io.FileOutputStream) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) UimaASPrimitiveDeploymentDescriptor(org.apache.uima.resourceSpecifier.factory.UimaASPrimitiveDeploymentDescriptor) Util.getUrlAsFile(org.dkpro.lab.Util.getUrlAsFile) File(java.io.File) ResourceMetaData(org.apache.uima.resource.metadata.ResourceMetaData) ServiceContextImpl(org.apache.uima.resourceSpecifier.factory.impl.ServiceContextImpl)

Example 3 with ResourceMetaData

use of org.apache.uima.resource.metadata.ResourceMetaData in project dkpro-lab by dkpro.

the class SimpleService method start.

/**
 * Initialize the UIMA-AS client.
 */
public void start() throws ResourceInitializationException {
    uimaAsEngine = new BaseUIMAAsynchronousEngine_impl();
    Map<String, Object> serviceCtx = new HashMap<String, Object>();
    File deploymentDescriptionFile;
    try {
        // Save the AED to a file because UIMA-AS cannot have an AED direclty embedded in its
        // descriptor
        ResourceMetaData topMetaData = aeDesc.getMetaData();
        File topDescriptorFile = File.createTempFile(getClass().getSimpleName(), ".xml");
        topDescriptorFile.deleteOnExit();
        try (OutputStream os = new FileOutputStream(topDescriptorFile)) {
            aeDesc.toXML(os);
        } catch (SAXException e) {
            throw new ResourceInitializationException(e);
        }
        // Create service descriptor
        ServiceContext context = new ServiceContextImpl(topMetaData.getName(), topMetaData.getDescription(), topDescriptorFile.getAbsolutePath(), endpoint, getBrokerUrl());
        UimaASPrimitiveDeploymentDescriptor dd = DeploymentDescriptorFactory.createPrimitiveDeploymentDescriptor(context);
        deploymentDescriptionFile = File.createTempFile(getClass().getSimpleName(), ".xml");
        deploymentDescriptionFile.deleteOnExit();
        try {
            dd.save(deploymentDescriptionFile);
        } catch (Exception e) {
            throw new ResourceInitializationException(e);
        }
        serviceCtx.put(UimaAsynchronousEngine.DD2SpringXsltFilePath, getUrlAsFile(getClass().getResource("/uima-as/dd2spring.xsl"), true).getAbsolutePath());
        serviceCtx.put(UimaAsynchronousEngine.SaxonClasspath, getClass().getResource("/uima-as/saxon8.jar").toString());
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
    try {
        serviceId = uimaAsEngine.deploy(deploymentDescriptionFile.getAbsolutePath(), serviceCtx);
        log.debug("UIMA AS service deployed: [" + serviceId + "]");
    } catch (Exception e) {
        throw new ResourceInitializationException(e);
    }
}
Also used : HashMap(java.util.HashMap) ServiceContext(org.apache.uima.resourceSpecifier.factory.ServiceContext) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) SAXException(org.xml.sax.SAXException) BaseUIMAAsynchronousEngine_impl(org.apache.uima.adapter.jms.client.BaseUIMAAsynchronousEngine_impl) SAXException(org.xml.sax.SAXException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) FileOutputStream(java.io.FileOutputStream) UimaASPrimitiveDeploymentDescriptor(org.apache.uima.resourceSpecifier.factory.UimaASPrimitiveDeploymentDescriptor) Util.getUrlAsFile(org.dkpro.lab.Util.getUrlAsFile) File(java.io.File) ResourceMetaData(org.apache.uima.resource.metadata.ResourceMetaData) ServiceContextImpl(org.apache.uima.resourceSpecifier.factory.impl.ServiceContextImpl)

Example 4 with ResourceMetaData

use of org.apache.uima.resource.metadata.ResourceMetaData in project dkpro-tc by dkpro.

the class DiscriminableNameConverter method getCollectionReaderDescription.

public static String getCollectionReaderDescription(CollectionReaderDescription crd) {
    ResourceMetaData metaData = crd.getMetaData();
    ConfigurationParameterSettings settings = metaData.getConfigurationParameterSettings();
    NameValuePair[] params = settings.getParameterSettings();
    String implementationName = crd.getImplementationName();
    List<String> entries = new ArrayList<>();
    entries.add(implementationName);
    entries = addParameters(entries, params);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < entries.size(); i++) {
        sb.append(entries.get(i));
        if (i + 1 < entries.size()) {
            sb.append(", ");
        }
    }
    String description = "[" + sb.toString() + "]";
    return description;
}
Also used : ConfigurationParameterSettings(org.apache.uima.resource.metadata.ConfigurationParameterSettings) NameValuePair(org.apache.uima.resource.metadata.NameValuePair) ArrayList(java.util.ArrayList) ResourceMetaData(org.apache.uima.resource.metadata.ResourceMetaData)

Aggregations

ResourceMetaData (org.apache.uima.resource.metadata.ResourceMetaData)4 HashMap (java.util.HashMap)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 BaseUIMAAsynchronousEngine_impl (org.apache.uima.adapter.jms.client.BaseUIMAAsynchronousEngine_impl)2 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)2 ServiceContext (org.apache.uima.resourceSpecifier.factory.ServiceContext)2 UimaASPrimitiveDeploymentDescriptor (org.apache.uima.resourceSpecifier.factory.UimaASPrimitiveDeploymentDescriptor)2 ServiceContextImpl (org.apache.uima.resourceSpecifier.factory.impl.ServiceContextImpl)2 Util.getUrlAsFile (org.dkpro.lab.Util.getUrlAsFile)2 IOException (java.io.IOException)1 UIMAFramework.newDefaultResourceManager (org.apache.uima.UIMAFramework.newDefaultResourceManager)1 UIMAFramework.produceCollectionReader (org.apache.uima.UIMAFramework.produceCollectionReader)1 UimaContextAdmin (org.apache.uima.UimaContextAdmin)1 AnalysisEngine (org.apache.uima.analysis_engine.AnalysisEngine)1 AggregateAnalysisEngine_impl (org.apache.uima.analysis_engine.impl.AggregateAnalysisEngine_impl)1 PrimitiveAnalysisEngine_impl (org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl)1 CAS (org.apache.uima.cas.CAS)1