Java Code Examples for org.apache.uima.resource.ResourceManager#setDataPath()

The following examples show how to use org.apache.uima.resource.ResourceManager#setDataPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionProcessingEngine_implTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testExternalResoures() throws Exception {
  try {
    ResourceManager rm = UIMAFramework.newDefaultResourceManager();
    rm.setDataPath(TEST_DATAPATH);
    CpeDescription cpeDesc = UIMAFramework.getXMLParser().parseCpeDescription(
 new XMLInputSource(JUnitExtension
     .getFile("CollectionProcessingEngineImplTest/externalResourceTestCpe.xml")));
    CollectionProcessingEngine cpe = UIMAFramework.produceCollectionProcessingEngine(cpeDesc, rm,
 null);
    CollectionReader colRdr = (CollectionReader) cpe.getCollectionReader();
    assertNotNull(colRdr.getUimaContext().getResourceObject("TestFileResource"));
    CasInitializer casIni = colRdr.getCasInitializer();
    assertNotNull(casIni.getUimaContext().getResourceObject("TestFileLanguageResource",
 new String[] { "en" }));
    AnalysisEngine ae = (AnalysisEngine) cpe.getCasProcessors()[0];
    assertNotNull(ae.getUimaContext().getResourceObject("TestResourceObject"));
    assertNotNull(ae.getUimaContext().getResourceObject("TestLanguageResourceObject",
 new String[] { "en" }));
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 2
Source File: ResourceManager_implTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testOverrides() throws Exception {
  try {
    final String TEST_DATAPATH = JUnitExtension.getFile("AnnotatorContextTest").getPath();

    File descFile = JUnitExtension.getFile("ResourceManagerImplTest/ResourceTestAggregate.xml");
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(descFile));
    ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    resMgr.setDataPath(TEST_DATAPATH);
    UIMAFramework.produceAnalysisEngine(desc, resMgr, null);

    URL url = resMgr.getResourceURL("/Annotator1/TestFileResource");
    assertTrue(url.toString().endsWith("testDataFile2.dat"));
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 3
Source File: CpeImportTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Test a CPE descriptor using import by name and requiring data patht o be set
 */
public void testImportsWithDataPath() throws Exception {
  CpeDescription cpeDesc = UIMAFramework.getXMLParser().parseCpeDescription(
          new XMLInputSource(JUnitExtension.getFile("CollectionProcessingEngineImplTest/CpeImportDataPathTest.xml")));
  ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
  File dataPathDir = JUnitExtension.getFile("CollectionProcessingEngineImplTest/imports");
  resMgr.setDataPath(dataPathDir.getAbsolutePath());
  CollectionProcessingEngine cpe = UIMAFramework.produceCollectionProcessingEngine(cpeDesc, resMgr, null);

  TestStatusCallbackListener listener = new TestStatusCallbackListener();
  cpe.addStatusCallbackListener(listener);

  cpe.process();

  // wait until CPE has finished
  while (!listener.isFinished()) {
    Thread.sleep(5);
  }

  // check that components were called
  final int documentCount = 1000; //this is the # of documents produced by the test CollectionReader
  Assert.assertEquals("StatusCallbackListener", documentCount, listener
          .getEntityProcessCompleteCount());
  Assert.assertEquals("CasConsumer process Count", documentCount, FunctionErrorStore
          .getCasConsumerProcessCount());
  Assert.assertEquals("Annotator process count", documentCount, FunctionErrorStore
          .getAnnotatorProcessCount());
  Assert.assertEquals("Collection reader getNext count", documentCount, FunctionErrorStore
          .getCollectionReaderGetNextCount());
}
 
Example 4
Source File: InstallationTester.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * returns a valid ResourceManager with the information from the PackageBrowser object.
 * 
 * @param pkgBrowser
 *          packageBrowser object of an installed PEAR package
 * 
 * @return a ResourceManager object with the information from the PackageBrowser object.
 * 
 * @throws IOException passthru
 */
private static ResourceManager getResourceManager(PackageBrowser pkgBrowser) throws IOException {
  ResourceManager resourceMgr = UIMAFramework.newDefaultResourceManager();
  // set component data path
  if (pkgBrowser.getComponentDataPath() != null) {
    resourceMgr.setDataPath(pkgBrowser.getComponentDataPath());
  }
  // set component classpath
  if (pkgBrowser.buildComponentClassPath() != null) {
    resourceMgr.setExtensionClassPath(pkgBrowser.buildComponentClassPath(), true);
  }

  return resourceMgr;
}
 
Example 5
Source File: CasCreationUtilsTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testCreateCasCollectionPropertiesResourceManager() throws Exception {
  try {
    // parse an AE descriptor
    File taeDescriptorWithImport = JUnitExtension
            .getFile("CasCreationUtilsTest/TaeWithImports.xml");
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(taeDescriptorWithImport));

    // create Resource Manager & set data path - necessary to resolve imports
    ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    String pathSep = System.getProperty("path.separator");
    resMgr.setDataPath(JUnitExtension.getFile("TypeSystemDescriptionImplTest/dataPathDir")
            .getAbsolutePath()
            + pathSep
            + JUnitExtension.getFile("TypePrioritiesImplTest/dataPathDir").getAbsolutePath()
            + pathSep
            + JUnitExtension.getFile("FsIndexCollectionImplTest/dataPathDir").getAbsolutePath());

    // call method
    ArrayList<AnalysisEngineDescription> descList = new ArrayList<>();
    descList.add(desc);
    CAS cas = CasCreationUtils.createCas(descList, UIMAFramework
            .getDefaultPerformanceTuningProperties(), resMgr);
    // check that imports were resolved correctly
    assertNotNull(cas.getTypeSystem().getType("DocumentStructure"));
    assertNotNull(cas.getTypeSystem().getType("NamedEntity"));
    assertNotNull(cas.getTypeSystem().getType("TestType3"));

    assertNotNull(cas.getIndexRepository().getIndex("TestIndex"));
    assertNotNull(cas.getIndexRepository().getIndex("ReverseAnnotationIndex"));
    assertNotNull(cas.getIndexRepository().getIndex("DocumentStructureIndex"));

    // check of type priority
    AnnotationFS fs1 = cas.createAnnotation(cas.getTypeSystem().getType("Paragraph"), 0, 1);
    AnnotationFS fs2 = cas.createAnnotation(cas.getTypeSystem().getType("Sentence"), 0, 1);
    assertTrue(cas.getAnnotationIndex().compare(fs1, fs2) < 0);
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 6
Source File: CppUimajEngine.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public int initialize(String config, String dataPath, int[] typeInheritance,
        int[] typePriorities, int[] featureDefs, int[] featureOffset, String[] typeNames,
        String[] featureNames, int[] stringSubTypes, String[] stringSubTypeValues,
        int[] stringSubTypeValuePos, String[] indexIDs, int[] indexKinds, int[] compStarts,
        int[] compDefs) {
  int result = 0;
  try {
    // System.out.println("CppUimajEngine::initialize()");
    CASMgrSerializer serializer = new CASMgrSerializer();
    serializer.typeOrder = typePriorities;
    serializer.indexNames = indexIDs;

    // trivalliy construct the name to index map
    serializer.nameToIndexMap = new int[indexIDs.length];
    for (int i = 0; i < serializer.nameToIndexMap.length; ++i) {
      serializer.nameToIndexMap[i] = i;
    }

    serializer.indexingStrategy = indexKinds;
    serializer.comparatorIndex = compStarts;
    serializer.comparators = compDefs;

    serializer.typeNames = typeNames;
    serializer.featureNames = featureNames;
    serializer.typeInheritance = typeInheritance;
    serializer.featDecls = featureDefs;
    serializer.topTypeCode = 1;
    serializer.featureOffsets = featureOffset;
    serializer.stringSubtypes = stringSubTypes;
    serializer.stringSubtypeValues = stringSubTypeValues;
    serializer.stringSubtypeValuePos = stringSubTypeValuePos;

    byte[] bar = config.getBytes(StandardCharsets.UTF_16);
    ByteArrayInputStream bais = new ByteArrayInputStream(bar);
    XMLInputSource in = new XMLInputSource(bais, null);
    ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
    bais.close();

    ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    resMgr.setDataPath(dataPath);

    if (specifier instanceof CasConsumerDescription) {
      cc = UIMAFramework.produceCasConsumer(specifier);
      CasConsumerDescription ccdesc = (CasConsumerDescription) specifier;
      Capability[] capabilities = ccdesc.getCasConsumerMetaData().getCapabilities();
      for (int i = 0; i < capabilities.length; i++) {
        String[] inputsofas = capabilities[i].getInputSofas();
        if (inputsofas.length > 0)
          requiresTCas = false;
      }
    } else {
      ae = UIMAFramework.produceAnalysisEngine(specifier, resMgr, null);
    }

    casImpl = (CASImpl) CASFactory.createCAS();
    casImpl.commitTypeSystem();

    // Create the Base indexes in order to deserialize
    casImpl.initCASIndexes();
    casImpl.getIndexRepositoryMgr().commit();

    // deserialize into this CAS
    CASCompleteSerializer completeSerializer = new CASCompleteSerializer();
    completeSerializer.setCasMgrSerializer(serializer);
    completeSerializer.setCasSerializer(Serialization.serializeCAS(casImpl));

    casImpl.getBinaryCasSerDes().reinit(completeSerializer);

    // System.out.println(cc.getProcessingResourceMetaData().getName());
  } catch (Exception exc) {
    result = 1;
    logException(exc);
  }

  return result;
}
 
Example 7
Source File: PearAnalysisEngineWrapper.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private synchronized ResourceManager createRM(StringPair sp, PackageBrowser pkgBrowser, ResourceManager parentResourceManager)
         throws MalformedURLException {
      // create UIMA resource manager and apply pear settings
//      ResourceManager rsrcMgr = UIMAFramework.newDefaultResourceManager();
     ResourceManager rsrcMgr;
     if (null == parentResourceManager) {
       // could be null for top level Pear not in an aggregate
       rsrcMgr = UIMAFramework.newDefaultResourceManager();
     } else {
       rsrcMgr = ((ResourceManager_impl) parentResourceManager).copy();
//       newPearsParent.set((ResourceManager_impl) parentResourceManager);
//       rsrcMgr = UIMAFramework.newDefaultResourceManagerPearWrapper();
//       newPearsParent.remove();
//       ((ResourceManagerPearWrapper)rsrcMgr).initializeFromParentResourceManager(parentResourceManager);
     }
     rsrcMgr.setExtensionClassPath(sp.classPath, true);
     if (parentResourceManager != null) {
       rsrcMgr.setCasManager(parentResourceManager.getCasManager());  // shares the same merged type system
     }
     UIMAFramework.getLogger(this.getClass()).logrb(
            Level.CONFIG,
            this.getClass().getName(),
            "createRM",
            LOG_RESOURCE_BUNDLE,
            "UIMA_pear_runtime_set_classpath__CONFIG",
            new Object[] { sp.classPath,
                  pkgBrowser.getRootDirectory().getName() });

      // get and set uima.datapath if specified
      if (sp.dataPath != null) {
         rsrcMgr.setDataPath(sp.dataPath);
         UIMAFramework.getLogger(this.getClass()).logrb(
               Level.CONFIG,
               this.getClass().getName(),
               "createRM",
               LOG_RESOURCE_BUNDLE,
               "UIMA_pear_runtime_set_datapath__CONFIG",
               new Object[] { sp.dataPath,
                     pkgBrowser.getRootDirectory().getName() });
      }
      return rsrcMgr;
   }
 
Example 8
Source File: CasCreationUtilsTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testAggregateWithImports() throws Exception {
  try {
    String pathSep = System.getProperty("path.separator");
    ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    resMgr.setDataPath(JUnitExtension.getFile("TypeSystemDescriptionImplTest/dataPathDir")
            .getAbsolutePath()
            + pathSep
            + JUnitExtension.getFile("TypePrioritiesImplTest/dataPathDir").getAbsolutePath()
            + pathSep
            + JUnitExtension.getFile("FsIndexCollectionImplTest/dataPathDir").getAbsolutePath());

    File taeDescriptorWithImport = JUnitExtension
            .getFile("CasCreationUtilsTest/AggregateTaeWithImports.xml");
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(taeDescriptorWithImport));
    ArrayList<AnalysisEngineDescription> mdList = new ArrayList<>();
    mdList.add(desc);
    CAS tcas = CasCreationUtils.createCas(mdList, UIMAFramework
            .getDefaultPerformanceTuningProperties(), resMgr);
    // check that imports were resolved correctly
    assertNotNull(tcas.getTypeSystem().getType("DocumentStructure"));
    assertNotNull(tcas.getTypeSystem().getType("NamedEntity"));
    assertNotNull(tcas.getTypeSystem().getType("TestType3"));
    assertNotNull(tcas.getTypeSystem().getType("Sentence"));

    assertNotNull(tcas.getIndexRepository().getIndex("TestIndex"));
    assertNotNull(tcas.getIndexRepository().getIndex("ReverseAnnotationIndex"));
    assertNotNull(tcas.getIndexRepository().getIndex("DocumentStructureIndex"));

    // Check elementType and multipleReferencesAllowed for array feature
    Feature arrayFeat = tcas.getTypeSystem().getFeatureByFullName("Paragraph:sentences");
    assertNotNull(arrayFeat);
    assertFalse(arrayFeat.isMultipleReferencesAllowed());
    Type sentenceArrayType = arrayFeat.getRange();
    assertNotNull(sentenceArrayType);
    assertTrue(sentenceArrayType.isArray());
    assertEquals(tcas.getTypeSystem().getType("Sentence"), sentenceArrayType.getComponentType());

    Feature arrayFeat2 = tcas.getTypeSystem().getFeatureByFullName(
            "Paragraph:testMultiRefAllowedFeature");
    assertNotNull(arrayFeat2);
    assertTrue(arrayFeat2.isMultipleReferencesAllowed());

    // test imports aren't resolved more than once
    Object spec1 = desc.getDelegateAnalysisEngineSpecifiers().get("Annotator1");
    assertNotNull(spec1);
    Object spec2 = desc.getDelegateAnalysisEngineSpecifiers().get("Annotator1");
    assertTrue(spec1 == spec2);

    // test removal
    desc.getDelegateAnalysisEngineSpecifiersWithImports().remove("Annotator1");
    assertTrue(desc.getDelegateAnalysisEngineSpecifiers().isEmpty());
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 9
Source File: UimaContext_implTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
protected void setUp() throws Exception {
  try {
    // configure ResourceManager to allow test components to locate their resources
    ResourceManager rm = UIMAFramework.newDefaultResourceManager();
    rm.setDataPath(TEST_DATAPATH);
    rm.setExtensionClassPath(TEST_EXTENSION_CLASSPATH, true);

    // create a UimaContext with Config Params and Resources
    UIMAFramework.getXMLParser().enableSchemaValidation(true);
    CasConsumerDescription ccDesc = UIMAFramework.getXMLParser().parseCasConsumerDescription(
            new XMLInputSource(JUnitExtension
                    .getFile("UimaContextTest/CasConsumerForUimaContextTest.xml")));
    CasConsumer cc = UIMAFramework.produceCasConsumer(ccDesc, rm, null);
    mContext = cc.getUimaContext();

    // create a UimaContext with Config Params in Groups but no resources
    XMLInputSource in = new XMLInputSource(JUnitExtension
            .getFile("AnnotatorContextTest/AnnotatorWithConfigurationGroups.xml"));
    AnalysisEngineDescription taeDesc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in);
    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(taeDesc, rm, null);
    mContext2 = ae.getUimaContext();

    // create a UimaContext with Groups and Groupless Parameters
    XMLInputSource in2 = new XMLInputSource(JUnitExtension
            .getFile("AnnotatorContextTest/AnnotatorWithGroupsAndNonGroupParams.xml"));
    AnalysisEngineDescription taeDesc2 = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in2);
    AnalysisEngine ae2 = UIMAFramework.produceAnalysisEngine(taeDesc2, rm, null);
    mContext3 = ae2.getUimaContext();

    // create a UimaContext with duplicate configuration groups
    XMLInputSource in3 = new XMLInputSource(JUnitExtension
            .getFile("TextAnalysisEngineImplTest/AnnotatorWithDuplicateConfigurationGroups.xml"));
    AnalysisEngineDescription taeDesc3 = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in3);
    AnalysisEngine ae3 = UIMAFramework.produceAnalysisEngine(taeDesc3, rm, null);
    mContext4 = ae3.getUimaContext();
    super.setUp();

    // create a UimaContext for a CAS Multiplier
    XMLInputSource in4 = new XMLInputSource(JUnitExtension
            .getFile("TextAnalysisEngineImplTest/NewlineSegmenter.xml"));
    AnalysisEngineDescription taeDesc4 = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in4);
    AnalysisEngine ae4 = UIMAFramework.produceAnalysisEngine(taeDesc4);
    mContext5 = ae4.getUimaContext();
    super.setUp();
    
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 10
Source File: AnnotatorContext_implTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
protected void setUp() throws Exception {
  try {
    super.setUp();
    // create primitive analysis engine with configuration groups
    XMLInputSource in = new XMLInputSource(JUnitExtension
            .getFile("AnnotatorContextTest/AnnotatorWithConfigurationGroups.xml"));
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in);
    PrimitiveAnalysisEngine_impl tae = new PrimitiveAnalysisEngine_impl();
    // set data path just to test that we can get it later
    Map<String, Object> map = new HashMap<>();
    ResourceManager rm = UIMAFramework.newDefaultResourceManager();
    rm.setDataPath(TEST_DATAPATH);
    rm.setExtensionClassPath(TEST_EXTENSION_CLASSPATH, true);
    map.put(AnalysisEngine.PARAM_RESOURCE_MANAGER, rm);
    tae.initialize(desc, map);
    // this should include an annotator context
    mAC1 = new AnnotatorContext_impl(tae.getUimaContextAdmin());

    // create aggregate analysis engine with configuration parameter overrides
    XMLInputSource in2 = new XMLInputSource(JUnitExtension
            .getFile("AnnotatorContextTest/AggregateTaeWithConfigParamOverrides.xml"));
    AnalysisEngineDescription aggDesc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in2);
    AggregateAnalysisEngine_impl aggTae = new AggregateAnalysisEngine_impl();
    aggTae.initialize(aggDesc, map);
    // get the primitive TAE
    PrimitiveAnalysisEngine_impl primTae = (PrimitiveAnalysisEngine_impl) aggTae._getASB()
            .getComponentAnalysisEngines().values().toArray()[0];
    // this should include an annotator context
    mAC2 = new AnnotatorContext_impl(primTae.getUimaContextAdmin());

    // create primitive analysis engine for resource testing
    XMLInputSource in3 = new XMLInputSource(JUnitExtension
            .getFile("AnnotatorContextTest/ResourceTestAnnotator.xml"));
    AnalysisEngineDescription resTestDesc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in3);
    PrimitiveAnalysisEngine_impl resTestTae = new PrimitiveAnalysisEngine_impl();
    resTestTae.initialize(resTestDesc, map);
    // this should include an annotator context
    mAC3 = new AnnotatorContext_impl(resTestTae.getUimaContextAdmin());

    // create primitive TAE with configuration groups and default fallback
    XMLInputSource in4 = new XMLInputSource(JUnitExtension
            .getFile("AnnotatorContextTest/AnnotatorWithDefaultFallbackConfiguration.xml"));
    AnalysisEngineDescription desc4 = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in4);
    PrimitiveAnalysisEngine_impl tae4 = new PrimitiveAnalysisEngine_impl();
    // set data path just to test that we can get it later
    tae4.initialize(desc4, null);
    // this should include an annotator context
    mAC4 = new AnnotatorContext_impl(tae4.getUimaContextAdmin());

    // create primitive TAE with configuration parameters (no groups)
    XMLInputSource in5 = new XMLInputSource(JUnitExtension
            .getFile("AnnotatorContextTest/AnnotatorWithConfigurationParameters.xml"));
    AnalysisEngineDescription desc5 = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in5);
    PrimitiveAnalysisEngine_impl tae5 = new PrimitiveAnalysisEngine_impl();
    // set data path just to test that we can get it later
    tae5.initialize(desc5, null);
    // this should include an annotator context
    mAC5 = new AnnotatorContext_impl(tae5.getUimaContextAdmin());
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}