org.apache.uima.util.CasCreationUtils Java Examples

The following examples show how to use org.apache.uima.util.CasCreationUtils. 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: JCasCoverClassFactoryTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateJCasCoverClass() throws InvalidXMLException, IOException, ResourceInitializationException {
  File file = JUnitExtension.getFile("JCasGen/typeSystemAllKinds.xml");
  TypeSystemDescription tsDesc = UIMAFramework.getXMLParser().parseTypeSystemDescription(
          new XMLInputSource(file));
 
  CAS cas = CasCreationUtils.createCas(tsDesc, null, null);
  
  JCasCoverClassFactory jcf = new JCasCoverClassFactory();
  
  byte[] r = jcf.createJCasCoverClass((TypeImpl) cas.getTypeSystem().getType("pkg.sample.name.All"));

  Path root = Paths.get(".");  // should resolve to the project path
  Path dir = root.resolve("temp/test/JCasGen");
  dir.toFile().mkdirs();
  Files.write(dir.resolve("testOutputAllKinds.class"), r);
  
  System.out.println("debug: generated byte array");
}
 
Example #2
Source File: CurationTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static CAS readXMI(String aPath, TypeSystemDescription aType)
    throws UIMAException, IOException
{
    CollectionReader reader = createReader(XmiReader.class, XmiReader.PARAM_SOURCE_LOCATION,
            "src/test/resources/" + aPath);
    CAS jcas;
    if (aType != null) {
        TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory
                .createTypeSystemDescription();
        List<TypeSystemDescription> allTypes = new ArrayList<>();
        allTypes.add(builtInTypes);
        allTypes.add(aType);
        jcas = JCasFactory.createJCas(CasCreationUtils.mergeTypeSystems(allTypes)).getCas();
    }
    else {
        jcas = JCasFactory.createJCas().getCas();
    }

    reader.getNext(jcas);

    return jcas;
}
 
Example #3
Source File: XCASDeserializerTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testStringArrayWithNullValues() throws Exception {
  CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
  StringArrayFS strArray = cas.createStringArrayFS(3);
  strArray.set(1, "value");
  cas.getIndexRepository().addFS(strArray);
  
  assertEquals(null, strArray.get(0));
  assertEquals("value", strArray.get(1));
  assertEquals(null, strArray.get(2));
  
  //serialize to XCAS and back
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  XCASSerializer.serialize(cas,baos);
  ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  XCASDeserializer.deserialize(bais, cas);

  //check
  Iterator iter = cas.getIndexRepository().getAllIndexedFS(cas.getTypeSystem().getType("uima.cas.StringArray"));
  StringArrayFS strArrayOut = (StringArrayFS)iter.next();
  assertEquals(null, strArrayOut.get(0));
  assertEquals("value", strArrayOut.get(1));
  assertEquals(null, strArrayOut.get(2));
}
 
Example #4
Source File: ConstraintsGeneratorTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
private JCas makeJCasOneSentence() throws UIMAException
{
    TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
    TypeSystemDescription local = TypeSystemDescriptionFactory
            .createTypeSystemDescriptionFromPath(
                    "src/test/resources/desc/types/webannoTestTypes.xml");
   
    TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));
    
    JCas jcas = JCasFactory.createJCas(merged);
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    
    TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class,
            Sentence.class);
    tb.buildTokens(jcas, "This is a test .");
    
    return jcas;
}
 
Example #5
Source File: CurationTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static TypeSystemDescription createMultiLinkWithRoleTestTypeSystem(String... aFeatures)
    throws Exception
{
    List<TypeSystemDescription> typeSystems = new ArrayList<>();

    TypeSystemDescription tsd = new TypeSystemDescription_impl();

    // Link type
    TypeDescription linkTD = tsd.addType(LINK_TYPE, "", CAS.TYPE_NAME_TOP);
    linkTD.addFeature("role", "", CAS.TYPE_NAME_STRING);
    linkTD.addFeature("target", "", CAS.TYPE_NAME_ANNOTATION);

    // Link host
    TypeDescription hostTD = tsd.addType(HOST_TYPE, "", CAS.TYPE_NAME_ANNOTATION);
    hostTD.addFeature("links", "", CAS.TYPE_NAME_FS_ARRAY, linkTD.getName(), false);
    for (String feature : aFeatures) {
        hostTD.addFeature(feature, "", CAS.TYPE_NAME_STRING);
    }

    typeSystems.add(tsd);
    typeSystems.add(TypeSystemDescriptionFactory.createTypeSystemDescription());

    return CasCreationUtils.mergeTypeSystems(typeSystems);
}
 
Example #6
Source File: DiffTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static JCas readWebAnnoTSV(String aPath, TypeSystemDescription aType)
    throws UIMAException, IOException
{
    CollectionReader reader = createReader(WebannoTsv2Reader.class,
            WebannoTsv2Reader.PARAM_SOURCE_LOCATION, "src/test/resources/" + aPath);
    JCas jcas;
    if (aType != null) {
        TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory
                .createTypeSystemDescription();
        List<TypeSystemDescription> allTypes = new ArrayList<>();
        allTypes.add(builtInTypes);
        allTypes.add(aType);
        jcas = JCasFactory.createJCas(CasCreationUtils.mergeTypeSystems(allTypes));
    }
    else {
        jcas = JCasFactory.createJCas();
    }

    reader.getNext(jcas.getCas());

    return jcas;
}
 
Example #7
Source File: JsonCasSerializerTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void setupTypeSystem(String tsdName) throws InvalidXMLException, IOException, ResourceInitializationException, CASException {
    File tsdFile = JUnitExtension.getFile("CasSerialization/desc/" + tsdName);
    tsd = parser.parseTypeSystemDescription(new XMLInputSource(tsdFile));
    cas = (CASImpl) CasCreationUtils.createCas(tsd, null, null);
//    cas.getSofaRef();  // creates the default sofa
    jcas = cas.getJCas();
    tsi = cas.getTypeSystemImpl();
    topType = (TypeImpl) tsi.getTopType();
    annotationType = (TypeImpl) tsi.getType("uima.tcas.Annotation"); 
    allTypesType = (TypeImpl) tsi.getType("org.apache.uima.test.AllTypes");
    tokenType = (TypeImpl) tsi.getType("org.apache.uima.test.Token");
    emptyIntListType = (TypeImpl) tsi.getType("uima.cas.EmptyIntegerList");
//    nonEmptyIntListType = (TypeImpl) tsi.getType("uima.cas.NonEmptyIntegerList");
//    emptyFSListType = (TypeImpl) tsi.getType("uima.cas.EmptyFSList");
//    nonEmptyFSListType = (TypeImpl) tsi.getType("uima.cas.NonEmptyFSList");
    
  }
 
Example #8
Source File: DiffTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static TypeSystemDescription createMultiLinkWithRoleTestTypeSytem()
    throws Exception
{
    List<TypeSystemDescription> typeSystems = new ArrayList<>();

    TypeSystemDescription tsd = new TypeSystemDescription_impl();

    // Link type
    TypeDescription linkTD = tsd.addType(LINK_TYPE, "", CAS.TYPE_NAME_TOP);
    linkTD.addFeature("role", "", CAS.TYPE_NAME_STRING);
    linkTD.addFeature("target", "", Token.class.getName());

    // Link host
    TypeDescription hostTD = tsd.addType(HOST_TYPE, "", CAS.TYPE_NAME_ANNOTATION);
    hostTD.addFeature("links", "", CAS.TYPE_NAME_FS_ARRAY, linkTD.getName(), false);

    typeSystems.add(tsd);
    typeSystems.add(TypeSystemDescriptionFactory.createTypeSystemDescription());

    return CasCreationUtils.mergeTypeSystems(typeSystems);
}
 
Example #9
Source File: UimacppAnalysisEngineImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * For an aggregate TAF AE, sets this aggregate AE's Type System, Type Priorities, and FS Index
 * Descriptions equal to the result of merging the information from its delegate AEs.
 * 
 * @throws ResourceInitializationException
 *           if an error occurs
 */
protected void mergeDelegateAnalysisEngineMetaData() throws ResourceInitializationException {
  // do the merge
  TypeSystemDescription aggTypeSystem = CasCreationUtils.mergeDelegateAnalysisEngineTypeSystems(
          (AnalysisEngineDescription) mDescription, getResourceManager());
  TypePriorities aggTypePriorities = CasCreationUtils.mergeDelegateAnalysisEngineTypePriorities(
          (AnalysisEngineDescription) mDescription, getResourceManager());
  FsIndexCollection aggIndexColl = CasCreationUtils
          .mergeDelegateAnalysisEngineFsIndexCollections((AnalysisEngineDescription)mDescription, getResourceManager());

  // assign results of merge to this aggregate AE's metadata
  ProcessingResourceMetaData aggregateMD = (ProcessingResourceMetaData) mDescription.getMetaData();
  aggregateMD.setTypeSystem(aggTypeSystem);
  aggregateMD.setTypePriorities(aggTypePriorities);
  aggregateMD.setFsIndexCollection(aggIndexColl);
}
 
Example #10
Source File: AgreementTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static JCas readWebAnnoTSV(String aPath, TypeSystemDescription aType)
    throws UIMAException, IOException
{
    CollectionReader reader = createReader(WebannoTsv2Reader.class,
            WebannoTsv2Reader.PARAM_SOURCE_LOCATION, "src/test/resources/" + aPath);
    JCas jcas;
    if (aType != null) {
        TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory
                .createTypeSystemDescription();
        List<TypeSystemDescription> allTypes = new ArrayList<>();
        allTypes.add(builtInTypes);
        allTypes.add(aType);
        jcas = JCasFactory.createJCas(CasCreationUtils.mergeTypeSystems(allTypes));
    }
    else {
        jcas = JCasFactory.createJCas();
    }

    reader.getNext(jcas.getCas());

    return jcas;
}
 
Example #11
Source File: AgreementTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static JCas readXMI(String aPath, TypeSystemDescription aType)
    throws UIMAException, IOException
{
    CollectionReader reader = createReader(XmiReader.class, XmiReader.PARAM_SOURCE_LOCATION,
            "src/test/resources/" + aPath);
    JCas jcas;
    if (aType != null) {
        TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory
                .createTypeSystemDescription();
        List<TypeSystemDescription> allTypes = new ArrayList<>();
        allTypes.add(builtInTypes);
        allTypes.add(aType);
        jcas = JCasFactory.createJCas(CasCreationUtils.mergeTypeSystems(allTypes));
    }
    else {
        jcas = JCasFactory.createJCas();
    }

    reader.getNext(jcas.getCas());

    return jcas;
}
 
Example #12
Source File: AgreementTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static TypeSystemDescription createMultiLinkWithRoleTestTypeSytem()
    throws Exception
{
    List<TypeSystemDescription> typeSystems = new ArrayList<>();

    TypeSystemDescription tsd = new TypeSystemDescription_impl();

    // Link type
    TypeDescription linkTD = tsd.addType(LINK_TYPE, "", CAS.TYPE_NAME_TOP);
    linkTD.addFeature("role", "", CAS.TYPE_NAME_STRING);
    linkTD.addFeature("target", "", Token.class.getName());

    // Link host
    TypeDescription hostTD = tsd.addType(HOST_TYPE, "", CAS.TYPE_NAME_ANNOTATION);
    hostTD.addFeature("links", "", CAS.TYPE_NAME_FS_ARRAY, linkTD.getName(), false);

    typeSystems.add(tsd);
    typeSystems.add(TypeSystemDescriptionFactory.createTypeSystemDescription());

    return CasCreationUtils.mergeTypeSystems(typeSystems);
}
 
Example #13
Source File: CasManager_impl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public CAS createNewCas(Properties aPerformanceTuningSettings) throws ResourceInitializationException {

    if (mCurrentTypeSystem != null) {
      return CasCreationUtils.createCas(getCasDefinition(), aPerformanceTuningSettings, mCurrentTypeSystem);      
    } else {
      synchronized(this) {
        if (mCurrentTypeSystem != null) { // double check idiom
          return CasCreationUtils.createCas(getCasDefinition(), aPerformanceTuningSettings, mCurrentTypeSystem);      
        } else {
          CAS cas = CasCreationUtils.createCas(getCasDefinition(), aPerformanceTuningSettings);
          mCurrentTypeSystem = cas.getTypeSystem();
          return cas;
        }
      }
    }    
  }
 
Example #14
Source File: PubmedCentralCollectionReaderTest.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Test
   @Ignore
   // FIXME
   public void testCount() throws Exception {

CollectionReader cr = PubmedCentralCollectionReader.getCR("pmc_test_archive");

int i = 0;
while (cr.hasNext()) {
    CAS cas = CasCreationUtils.createCas(cr
	    .getProcessingResourceMetaData());
    cr.getNext(cas);
    i++;
}
cr.close();
assertEquals(6, i);
   }
 
Example #15
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testTwoSentencesWithNoSpaceInBetween() throws Exception
{
    TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
    TypeSystemDescription local = TypeSystemDescriptionFactory
            .createTypeSystemDescriptionFromPath(
                    "src/test/resources/desc/type/webannoTestTypes.xml");
   
    TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));
    
    JCas jcas = JCasFactory.createJCas(merged);
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("onetwo");
    new Token(jcas, 0, 3).addToIndexes();
    new Sentence(jcas, 0, 3).addToIndexes();
    new Token(jcas, 3, 6).addToIndexes();
    new Sentence(jcas, 3, 6).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #16
Source File: JcasSofaTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testGetSofa() throws Exception {
  try {
    File typeSystemFile = JUnitExtension.getFile("ExampleCas/testTypeSystem.xml");
    TypeSystemDescription typeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(typeSystemFile));
    CAS newCas = CasCreationUtils.createCas(typeSystem, null, null);
    File xcasFile = JUnitExtension.getFile("ExampleCas/multiSofaCas.xml"); 
    XCASDeserializer.deserialize(new FileInputStream(xcasFile), newCas);
    JCas newJCas = newCas.getJCas();
    
    SofaID sofaId = new SofaID_impl("EnglishDocument");
    JCas view = newJCas.getView(newJCas.getSofa(sofaId));
  }
  catch (Exception e) {
    JUnitExtension.handleException(e);      
  }      
}
 
Example #17
Source File: AnnotatorTester.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * create a CAS object from the given XCAS and typesystem files.
 *
 * @param tsFile -
 *           a typesystem file
 * @param xcasFile -
 *           a xcas file
 * @return CAS - CAS object created from the given input data
 * @throws Exception passthru
 */
public static CAS getCASfromXCAS(File tsFile, File xcasFile)
      throws Exception {
   try {
      Object tsDescriptor = UIMAFramework.getXMLParser().parse(
            new XMLInputSource(tsFile));
      TypeSystemDescription tsDesc = (TypeSystemDescription) tsDescriptor;
      CAS cas = CasCreationUtils.createCas(tsDesc, null,
            new FsIndexDescription[0]);

      SAXParser parser = XMLUtils.createSAXParserFactory().newSAXParser();
      XCASDeserializer xcasDeserializer = new XCASDeserializer(cas
            .getTypeSystem());
      parser.parse(xcasFile, xcasDeserializer.getXCASHandler(cas));

      return cas;
   } catch (Exception ex) {
      JUnitExtension.handleException(ex);
   }

   return null;
}
 
Example #18
Source File: ExternalResourceFactoryTest.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiBinding() throws Exception {
  ExternalResourceDescription extDesc = createResourceDescription(ResourceWithAssert.class);

  // Binding external resource to each Annotator individually
  AnalysisEngineDescription aed1 = createEngineDescription(MultiBindAE.class,
          MultiBindAE.RES_KEY, extDesc);
  AnalysisEngineDescription aed2 = createEngineDescription(MultiBindAE.class,
          MultiBindAE.RES_KEY, extDesc);

  // Check the external resource was injected
  MultiBindAE.reset();
  AnalysisEngineDescription aed = createEngineDescription(aed1, aed2);
  AnalysisEngine ae = createEngine(aed);
  ae.process(ae.newJCas());


  // Check the external resource was injected
  MultiBindAE.reset();
  SimplePipeline.runPipeline(CasCreationUtils.createCas(aed), aed);
}
 
Example #19
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testDuplicateNsPrefixes() throws Exception {
  TypeSystemDescription ts = new TypeSystemDescription_impl();
  ts.addType("org.bar.foo.Foo", "", "uima.tcas.Annotation");
  ts.addType("org.baz.foo.Foo", "", "uima.tcas.Annotation");
  CAS cas = CasCreationUtils.createCas(ts, null, null);
  cas.setDocumentText("Foo");
  Type t1 = cas.getTypeSystem().getType("org.bar.foo.Foo");
  Type t2 = cas.getTypeSystem().getType("org.baz.foo.Foo");
  AnnotationFS a1 = cas.createAnnotation(t1,0,3);
  cas.addFsToIndexes(a1);
  AnnotationFS a2 = cas.createAnnotation(t2,0,3);
  cas.addFsToIndexes(a2);

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  XmiCasSerializer.serialize(cas, baos);
  baos.close();
  byte[] bytes = baos.toByteArray();
  
  CAS cas2 = CasCreationUtils.createCas(ts, null, null);
  ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  XmiCasDeserializer.deserialize(bais, cas2);
  bais.close();
  
  CasComparer.assertEquals(cas, cas2);
}
 
Example #20
Source File: DocumentAnnotationTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testDocMeta() throws Exception {
  File typeSystemFile = JUnitExtension.getFile("ExampleCas/testTypeSystem_docmetadata.xml");
  TypeSystemDescription typeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
          new XMLInputSource(typeSystemFile));
  
  source = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), null);
  target = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), null);
  
  jcas = source.getJCas();
  
  tstSerdesB4Sofa(SerialFormat.XMI);
  tstSerdesB4Sofa(SerialFormat.XCAS);
  tstSerdesB4Sofa(SerialFormat.BINARY);
  tstSerdesB4Sofa(SerialFormat.COMPRESSED);
  tstSerdesB4Sofa(SerialFormat.COMPRESSED_FILTERED);    
}
 
Example #21
Source File: SerDesTest4.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void setUp() {
//    long seed = 1_449_257_605_347_913_923L;
    // long seed = 949_754_466_380_345_024L;
//     long seed = 6_761_039_426_734_540_557L;
//    long seed = 1_217_808_400_762_898_611L;
//    random.setSeed(randomseed.nextLong());
//    System.out.format("SerDesTest4 setup RandomSeed: %,d%n", seed);

    try {
      CASTestSetup cts = new CASTestSetup();
      this.cas = (CASImpl) CASInitializer.initCas(cts, t -> cts.reinitTypeSystem(t));
      this.ts = (TypeSystemImpl) this.cas.getTypeSystem();
      this.cas2 = (CASImpl) CasCreationUtils.createCas(ts, null, null, null);
      deserCas = (CASImpl) CasCreationUtils.createCas(ts, null, null, null);
      deltaCas = (CASImpl) CasCreationUtils.createCas(ts, null, null, null);
      lfs = new ArrayList<>();
      lfs2 = new ArrayList<>();
    } catch (Exception e) {
      assertTrue(false);
    }
  }
 
Example #22
Source File: XmlTestcaseCollectionReaderTest.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {

    CollectionReader cr = createReader(XmlTestcaseCollectionReader.class,
            PARAM_INPUT_FILE, "testcases/example.xml");

    CAS cas = CasCreationUtils
            .createCas(cr.getProcessingResourceMetaData());
    cr.getNext(cas);
    cr.close();

    Collection<CellTypeProteinConcentration> prots = JCasUtil.select(
            cas.getJCas(), CellTypeProteinConcentration.class);
    assertTrue(prots.size() > 1);
    Prin.t(prots);
    // TODO assert on object
}
 
Example #23
Source File: BioNLPGeniaEventsCollectionReaderTest.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Test
   public void test() throws Exception {

CollectionReader cr = CollectionReaderFactory.createReader(
	BioNLPGeniaEventsCollectionReader.class);

int i = 0;
while (cr.hasNext()) {
    CAS cas = CasCreationUtils.createCas(cr
	    .getProcessingResourceMetaData());
    cr.getNext(cas);

    // if (createHtml)
    // viewer.createHtml(cas.getJCas(), cas.getTypeSystem(),
    // styleMapFile, new File("target/" + i));

    i++;
}
cr.close();
assertEquals(259, i);

   }
 
Example #24
Source File: BioNLPGeniaEventsReaderTest.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Test
public void testCount() throws Exception {

    CollectionReader cr = CollectionReaderFactory.createReader(
            BioNLPGeniaEventsCollectionReader.class,
            BlueUima.PARAM_INPUT_DIRECTORY, TEST_DIR);

    int i = 0;
    while (cr.hasNext()) {
        CAS cas = CasCreationUtils.createCas(cr
                .getProcessingResourceMetaData());
        cr.getNext(cas);
        LOG.debug(To.string("cas nr " + i, cas.getJCas()));
        i++;
    }
    cr.close();
    assertEquals(3, i);
}
 
Example #25
Source File: AgreementTestUtils.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static TypeSystemDescription createMultiLinkWithRoleTestTypeSystem(String... aFeatures)
    throws Exception
{
    List<TypeSystemDescription> typeSystems = new ArrayList<>();

    TypeSystemDescription tsd = new TypeSystemDescription_impl();

    // Link type
    TypeDescription linkTD = tsd.addType(LINK_TYPE, "", CAS.TYPE_NAME_TOP);
    linkTD.addFeature("role", "", CAS.TYPE_NAME_STRING);
    linkTD.addFeature("target", "", CAS.TYPE_NAME_ANNOTATION);

    // Link host
    TypeDescription hostTD = tsd.addType(HOST_TYPE, "", CAS.TYPE_NAME_ANNOTATION);
    hostTD.addFeature("links", "", CAS.TYPE_NAME_FS_ARRAY, linkTD.getName(), false);
    for (String feature : aFeatures) {
        hostTD.addFeature(feature, "", CAS.TYPE_NAME_STRING);
    }

    typeSystems.add(tsd);
    typeSystems.add(TypeSystemDescriptionFactory.createTypeSystemDescription());

    return CasCreationUtils.mergeTypeSystems(typeSystems);
}
 
Example #26
Source File: JCasUtilv3Test.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectSingle() throws UIMAException {
  JCas jcas = CasCreationUtils.createCas(createTypeSystemDescription(), null, null).getJCas();

  // uimaFIT:
  // assertThatExceptionOfType(IllegalArgumentException.class)
  //    .isThrownBy(() -> selectSingle(jcas, Token.class)); 
  
  assertThatExceptionOfType(CASRuntimeException.class)
      .isThrownBy(() -> jcas.select(Token.class).single()); 

  new Token(jcas, 0, 1).addToIndexes();

  // uimaFIT: selectSingle(jcas, Token.class);
  jcas.select(Token.class).single();

  new Token(jcas, 1, 2).addToIndexes();

  // uimaFIT:
  // assertThatExceptionOfType(IllegalArgumentException.class)
  //    .isThrownBy(() -> selectSingle(jcas, Token.class))
  //    .as("selectSingle must fail if there is more than one annotation of the type"); 

  assertThatExceptionOfType(CASRuntimeException.class)
    .isThrownBy(() -> jcas.select(Token.class).single())
    .as("selectSingle must fail if there is more than one annotation of the type"); 
}
 
Example #27
Source File: TypeSystemFileOpenEventHandler.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Action performed.
 *
 * @param event the event
 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
 */
@Override
public void actionPerformed(ActionEvent event) {
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.setDialogTitle("Open Type System File");
  if (this.main.getXcasFileOpenDir() != null) {
    fileChooser.setCurrentDirectory(this.main.getXcasFileOpenDir());
  }
  int rc = fileChooser.showOpenDialog(this.main);
  if (rc == JFileChooser.APPROVE_OPTION) {
    File tsFile = fileChooser.getSelectedFile();
    if (tsFile.exists() && tsFile.isFile()) {
      try {
        this.main.setXcasFileOpenDir(tsFile.getParentFile());
        Timer time = new Timer();
        time.start();
        Object descriptor = UIMAFramework.getXMLParser().parse(new XMLInputSource(tsFile));
        // instantiate CAS to get type system. Also build style
        // map file if there is none.
        TypeSystemDescription tsDesc = (TypeSystemDescription) descriptor;
        tsDesc.resolveImports();
        this.main.destroyAe();
        this.main.setCas(CasCreationUtils
            .createCas(tsDesc, null, new FsIndexDescription[0]));
        this.main.setRunOnCasEnabled();
        this.main.setRerunEnabled(false);
        this.main.getTextArea().setText("");
        this.main.resetTrees();
        this.main.setTypeSystemViewerEnabled(true);
        this.main.setEnableCasFileReadingAndWriting();
        time.stop();
        this.main.setStatusbarMessage("Done loading type system file in " + time.getTimeSpan() + ".");
      } catch (Exception e) {
        e.printStackTrace();
        this.main.handleException(e);
      }
    }
  }
}
 
Example #28
Source File: CasUtilBenchmark.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  if (cas == null) {
    cas = CasCreationUtils.createCas(createTypeSystemDescription(), null, null);
  }
  else {
    cas.reset();
  }
}
 
Example #29
Source File: FixedFlowControllerTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Test
public void testComputeFlow() throws Exception {
  CAS cas1 = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
  CAS cas2 = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
  Flow flow1 = fixedFlowController.computeFlow(cas1);
  Flow flow2 = fixedFlowController.computeFlow(cas2);
  //two steps in flow 1
  Step step = flow1.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key1", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow1.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key2", ((SimpleStep)step).getAnalysisEngineKey());
  
  //one step in flow 2
  step = flow2.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key1", ((SimpleStep)step).getAnalysisEngineKey());

  //third step in flow 1
  step = flow1.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key3", ((SimpleStep)step).getAnalysisEngineKey());

  //one step in flow 2
  step = flow2.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key2", ((SimpleStep)step).getAnalysisEngineKey());

  //finish flow 1
  step = flow1.next();
  assertTrue(step instanceof FinalStep);
  
  //finish flow 2
  step = flow2.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key3", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow2.next();
  assertTrue(step instanceof FinalStep);
}
 
Example #30
Source File: JCasUtilv3Test.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@Test
public void testExists() throws UIMAException {
  JCas jcas = CasCreationUtils.createCas(createTypeSystemDescription(), null, null).getJCas();

  // uimaFIT: exists(jcas, Token.class)
  assertFalse(jcas.select(Token.class).findAny().isPresent());

  new Token(jcas, 0, 1).addToIndexes();

  // uimaFIT: exists(jcas, Token.class)
  assertTrue(jcas.select(Token.class).findAny().isPresent());
}