Java Code Examples for org.apache.uima.util.CasCreationUtils#createCas()

The following examples show how to use org.apache.uima.util.CasCreationUtils#createCas() . 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: 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 2
Source File: XCASDeserializerTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testOutOfTypeSystem3() throws Exception {
  // deserialize an XCAS using the implicit value feature into a CAS with no TypeSystem
  CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(),
          new TypePriorities_impl(), new FsIndexDescription[0]);
  String xcas = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><CAS>"
          + "<uima.tcas.Document _content=\"text\">Test Document</uima.tcas.Document>"
          + "<uima.tcas.DocumentAnnotation _indexed=\"1\" _id=\"8\" sofa=\"1\" begin=\"0\" end=\"13\" language=\"en\"/>"
          + "<foo.Bar _indexed=\"1\" _id=\"2\" sofa=\"1\" begin=\"0\" end=\"0\" baz=\"blah\">this is the value feature</foo.Bar></CAS>";
  OutOfTypeSystemData ootsd = new OutOfTypeSystemData();
  XMLReader xmlReader = XMLUtils.createXMLReader();
  XCASDeserializer deser = new XCASDeserializer(cas.getTypeSystem());
  ContentHandler handler = deser.getXCASHandler(cas, ootsd);
  xmlReader.setContentHandler(handler);
  xmlReader.parse(new InputSource(new StringReader(xcas)));

  // now reserialize including OutOfTypeSystem data
  XCASSerializer xcasSer = new XCASSerializer(cas.getTypeSystem());
  StringWriter sw = new StringWriter();
  XMLSerializer xmlSer = new XMLSerializer(sw, false);
  xcasSer.serialize(cas, xmlSer.getContentHandler(), true, ootsd);
  String xml = sw.getBuffer().toString();
  // System.out.println(xml);

  // make sure the value feature was not lost (it will be serialized as an attribute however)
  assertTrue(xml.indexOf("value=\"this is the value feature\"") != -1);
}
 
Example 3
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 4
Source File: JcasSofaTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testIndexTwice() throws Exception {
  try {
    CAS newCas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
    JCas newJCas = newCas.getJCas();
    CAS view = newCas.createView("DetaggedView");
    view.getJCas();

    Annotation annot = new Annotation(newJCas);
    annot.addToIndexes();
    
    Iterator<Annotation> annotIter = newJCas.getAnnotationIndex(Annotation.type).iterator();
    Annotation annot2 = annotIter.next();
    assertEquals(annot, annot2);
    assertEquals(annot2.getSofa(), annot2.getCASImpl().getSofa());
    
    annot2.addToIndexes();
  }
  catch (Exception e) {
    JUnitExtension.handleException(e);      
  }    
}
 
Example 5
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 6
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testOutOfTypeSystemDataComplexCas() throws Exception {
   // deserialize a complex XCAS
   CAS originalCas = CasCreationUtils.createCas(typeSystem, null, indexes);
   InputStream serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/cas.xml"));
   XCASDeserializer.deserialize(serCasStream, originalCas);
   serCasStream.close();
   
   //serialize to XMI
   String xmiStr = serialize(originalCas, null);
   
   //deserialize into a CAS with no type system
   CAS casWithNoTs = CasCreationUtils.createCas(new TypeSystemDescription_impl(),
           new TypePriorities_impl(), new FsIndexDescription[0]);
   XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
   deserialize(xmiStr, casWithNoTs, sharedData, true, -1);
       
   // now reserialize including OutOfTypeSystem data
   String xmiStr2 = serialize(casWithNoTs, sharedData);
   
   //deserialize into a new CAS that has the full type system
   CAS newCas = CasCreationUtils.createCas(typeSystem, null, indexes);
   deserialize(xmiStr2, newCas, null, false, -1);
   
   //compare
   CasComparer.assertEquals(originalCas, newCas);
   
   //Test a partial type system with a missing some missing features and
   //missing "Organization" type
   File partialTypeSystemFile = JUnitExtension.getFile("ExampleCas/partialTestTypeSystem.xml");
   TypeSystemDescription partialTypeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
           new XMLInputSource(partialTypeSystemFile));
   CAS partialTsCas = CasCreationUtils.createCas(partialTypeSystem, null, indexes);
   XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
   deserialize(xmiStr, partialTsCas, sharedData2, true, -1);
       
   String xmiStr3 = serialize(partialTsCas, sharedData2);
   newCas.reset();
   deserialize(xmiStr3, newCas, null, false, -1);
   CasComparer.assertEquals(originalCas, newCas);    
}
 
Example 7
Source File: FixedFlowControllerTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveAnalysisEngines() throws Exception {
  CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
  Flow flow = fixedFlowController.computeFlow(cas);
  //one step in flow
  Step step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key1", ((SimpleStep)step).getAnalysisEngineKey());
  
  //remove "key2"
  analysisEngineMetaDataMap.remove("key2");
  List<String> removedKeys = new ArrayList<>();
  removedKeys.add("key2");
  fixedFlowController.removeAnalysisEngines(removedKeys);
  
  //finish flow
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key3", ((SimpleStep)step).getAnalysisEngineKey());    
  step = flow.next();
  assertTrue(step instanceof FinalStep);
  
  //test new flow
  flow = fixedFlowController.computeFlow(cas);
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key1", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key3", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof FinalStep);
}
 
Example 8
Source File: TypeSystemReinitTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testReinitCASCompleteSerializerWithArrays() throws Exception {
  try {
    AnalysisEngineDescription aed = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(JUnitExtension
                    .getFile("ExampleTae/arrayTypeSerialization.xml")));
   
    CAS cas1 = CasCreationUtils.createCas(aed);
    cas1.setDocumentText("foo");
    CASCompleteSerializer ser = Serialization.serializeCASComplete((CASMgr) cas1);

    CAS tcas2 = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
    CASImpl cas2 = ((CASImpl) tcas2).getBaseCAS();
    tcas2.setDocumentText("bar");

    // reinit
    //  This uses cas2 which only has a base type system to start, 
    //    and loads it from a complete serialization which has other new types
    cas2.getBinaryCasSerDes().reinit(ser);
    CAS tcas3 = cas2.getCurrentView();

    assertTrue(tcas2 == tcas3);
    assertNotNull(cas1.getTypeSystem().getType("Test.ArrayType"));
    assertNotNull(tcas3.getTypeSystem().getType("Test.ArrayType"));
    
    TypeSystemImpl ts = (TypeSystemImpl)cas2.getTypeSystem();
    Type arrayType = ts.getType("Test.ArrayType");
    Feature arrayFeat = arrayType.getFeatureByBaseName("arrayFeature");
    TypeImpl featRange = (TypeImpl)(arrayFeat.getRange());
   
    assertTrue(ts.ll_isArrayType(featRange.getCode()));
    assertFalse(arrayFeat.isMultipleReferencesAllowed());
    
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 9
Source File: AutomationCasStorageServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public CAS readCas(TrainingDocument aDocument)
    throws IOException
{
    log.debug("Reading CAs for Automation document [{}] ({}) in project [{}] ({})",
            aDocument.getName(), aDocument.getId(), aDocument.getProject().getName(),
            aDocument.getProject().getId());

    // DebugUtils.smallStack();

    synchronized (lock) {
        File annotationFolder = getAutomationFolder(aDocument);

        String file = aDocument.getName() + ".ser";

        try {
            File serializedCasFile = new File(annotationFolder, file);
            if (!serializedCasFile.exists()) {
                throw new FileNotFoundException("Annotation document of  Training document "
                        + "[" + aDocument.getName() + "] (" + aDocument.getId()
                        + ") not found in project[" + aDocument.getProject().getName() + "] ("
                        + aDocument.getProject().getId() + ")");
            }

            CAS cas = CasCreationUtils.createCas((TypeSystemDescription) null, null, null);
            CasPersistenceUtils.readSerializedCas(cas, serializedCasFile);

            analyzeAndRepair(aDocument, cas);

            return cas;
        }
        catch (UIMAException e) {
            throw new DataRetrievalFailureException("Unable to parse annotation", e);
        }
    }
}
 
Example 10
Source File: FeaturePathTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testInitializeWithAddAPI() throws Exception {

      XMLInputSource in = new XMLInputSource(JUnitExtension
            .getFile("featurePathTests/FeaturePathTestTypeSystem.xml"));
      TypeSystemDescription typeSystemDescription = UIMAFramework
            .getXMLParser().parseTypeSystemDescription(in);
      CAS cas = CasCreationUtils.createCas(typeSystemDescription, null, null);
      cas.setDocumentText("Sample Text");

      Feature stringFeat = cas.getDocumentAnnotation().getType()
            .getFeatureByBaseName("stringFeature");
      Feature refFeat = cas.getDocumentAnnotation().getType()
            .getFeatureByBaseName("refFeature2");

      cas.getDocumentAnnotation().setStringValue(stringFeat, "MyExample");
      cas.getDocumentAnnotation().setFeatureValue(refFeat,
            cas.getDocumentAnnotation());

      FeaturePath featurePath = new FeaturePathImpl();
      featurePath.initialize("/refFeature2");
      featurePath.addFeature(stringFeat);

      assertEquals("MyExample", featurePath.getValueAsString(cas
            .getDocumentAnnotation()));
      assertEquals("/refFeature2/stringFeature", featurePath.getFeaturePath());
      assertTrue(featurePath.size() == 2);
      // test case change: new impl sets features as paths are traversed; 
      assertTrue(featurePath.getFeature(1) == stringFeat);
      featurePath.typeInit(cas.getDocumentAnnotation().getType());
      assertEquals("MyExample", featurePath.getValueAsString(cas
            .getDocumentAnnotation()));
      assertEquals("MyExample", featurePath.getStringValue(cas
            .getDocumentAnnotation()));
      assertTrue(featurePath.size() == 2);
      assertTrue(featurePath.getFeature(1) == stringFeat);
   }
 
Example 11
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testOutOfTypeSystemData() throws Exception {
   // deserialize a simple XMI into a CAS with no TypeSystem    
   CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(),
           new TypePriorities_impl(), new FsIndexDescription[0]);
   File xmiFile = JUnitExtension.getFile("ExampleCas/simpleCas.xmi");
   String xmiStr = FileUtils.file2String(xmiFile, "UTF-8");
   
   XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
   deserialize(xmiStr, cas, sharedData, true, -1);
   
   //do some checks on the out-of-type system data
   List ootsElems = sharedData.getOutOfTypeSystemElements();
   assertEquals(9, ootsElems.size());
   List ootsViewMembers = sharedData.getOutOfTypeSystemViewMembers("1");
   assertEquals(7, ootsViewMembers.size());
   
   // now reserialize including OutOfTypeSystem data
   String xmiStr2 = serialize(cas, sharedData);
   
   //deserialize both original and new XMI into CASes that do have the full typesystem
   CAS newCas1 = CasCreationUtils.createCas(typeSystem, null, indexes);
   TypeSystem ts = newCas1.getTypeSystem();
   deserialize(xmiStr, newCas1, null, false, -1);
   CAS newCas2 = CasCreationUtils.createCas(ts, null, indexes, null);
   deserialize(xmiStr2, newCas2, null, false, -1);
   CasComparer.assertEquals(newCas1, newCas2);  
   
   //Test a partial type system with a missing some missing features and
   //missing "Organization" type
   File partialTypeSystemFile = JUnitExtension.getFile("ExampleCas/partialTestTypeSystem.xml");
   TypeSystemDescription partialTypeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
           new XMLInputSource(partialTypeSystemFile));
   CAS partialTsCas = CasCreationUtils.createCas(partialTypeSystem, null, indexes);
   XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
   deserialize(xmiStr, partialTsCas, sharedData2, true, -1);
   
   assertEquals(1,sharedData2.getOutOfTypeSystemElements().size());
   OotsElementData ootsFeats3 = sharedData2.getOutOfTypeSystemFeatures(sharedData2.getFsForXmiId(3));
   assertEquals(1, ootsFeats3.attributes.size());
   XmlAttribute ootsAttr = ootsFeats3.attributes.get(0);
   assertEquals("mentionType", ootsAttr.name);
   assertEquals("NAME", ootsAttr.value);
   OotsElementData ootsFeats5 = sharedData2.getOutOfTypeSystemFeatures(sharedData2.getFsForXmiId(5));
   assertEquals(0, ootsFeats5.attributes.size());
   assertEquals(1, ootsFeats5.childElements.size());
   XmlElementNameAndContents ootsChildElem = ootsFeats5.childElements.get(0);
   assertEquals("mentionType", ootsChildElem.name.qName);
   assertEquals("NAME", ootsChildElem.contents);
   
   OotsElementData ootsFeats8 = sharedData2.getOutOfTypeSystemFeatures(sharedData2.getFsForXmiId(8));
   assertEquals(1, ootsFeats8.attributes.size());
   OotsElementData ootsFeats10 = sharedData2.getOutOfTypeSystemFeatures(sharedData2.getFsForXmiId(10));
   assertEquals(1, ootsFeats10.attributes.size());
   OotsElementData ootsFeats11 = sharedData2.getOutOfTypeSystemFeatures(sharedData2.getFsForXmiId(11));
   assertEquals(4, ootsFeats11.childElements.size());
   
   String xmiStr3 = serialize(partialTsCas, sharedData2);
   newCas2.reset();
   deserialize(xmiStr3, newCas2, null, false, -1);
   CasComparer.assertEquals(newCas1, newCas2);    
}
 
Example 12
Source File: FixedFlowControllerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddAnalysisEngines() throws Exception {
  CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
  Flow flow = fixedFlowController.computeFlow(cas);
  //two steps in flow
  Step step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key1", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key2", ((SimpleStep)step).getAnalysisEngineKey());
  
  //now add two new AEs
  //first update AE metadata map
  AnalysisEngineMetaData delegateMd = new AnalysisEngineMetaData_impl();
  delegateMd.setOperationalProperties(new OperationalProperties_impl());
  analysisEngineMetaDataMap.put("key4", delegateMd);    
  analysisEngineMetaDataMap.put("key5", delegateMd);    
  //then notify FC
  List<String> newAeKeys = new ArrayList<>();
  newAeKeys.add("key4");
  newAeKeys.add("key5");
  fixedFlowController.addAnalysisEngines(newAeKeys);
  
  //finish flow
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key3", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key4", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key5", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof FinalStep);
  
  //test new flow
  flow = fixedFlowController.computeFlow(cas);
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key1", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key2", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key3", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key4", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof SimpleStep);
  assertEquals("key5", ((SimpleStep)step).getAnalysisEngineKey());
  step = flow.next();
  assertTrue(step instanceof FinalStep);
}
 
Example 13
Source File: Biocreative2GeneCollectionReaderTest.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
 * <pre>
 * GENE.eval
 * P00001606T0076|14 33|alkaline phosphatases
 * P00001606T0076|37 50|5-nucleotidase
 * 
 * train.in:
 * P00001606T0076 Comparison with alkaline phosphatases and 5-nucleotidase
 * </pre>
 */
@Test
public void testTrainCorpus() throws Exception {

    CollectionReader cr = CollectionReaderFactory.createReader(
            Biocreative2GeneCollectionReader.class,
            BlueUima.PARAM_MODE, "train");

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

    Collection<BioEntityMention> genes = JCasUtil.select(cas.getJCas(),
            BioEntityMention.class);
    assertEquals(2, genes.size());
    Iterator<BioEntityMention> iterator = genes.iterator();
    BioEntityMention gene = iterator.next();
    assertEquals("alkaline phosphatases", gene.getCoveredText());
    gene = iterator.next();
    assertEquals("5-nucleotidase", gene.getCoveredText());

    // move to 'P00027739T0000 Serum gamma glutamyltransferase in the
    // diagnosis of liver disease in cattle.' to test ALTGENE annotations
    for (int i = 0; i < 11; i++) {
        cas = CasCreationUtils
                .createCas(cr.getProcessingResourceMetaData());
        cr.hasNext();
        cr.getNext(cas);
        Header header = JCasUtil.selectSingle(cas.getJCas(), Header.class);
        LOG.debug("docid:{}, text:{}", header.getDocId(),
                cas.getDocumentText());
    }

    genes = JCasUtil.select(cas.getJCas(), BioEntityMention.class);
    iterator = genes.iterator();

    // check the 2 alternate forms
    assertEquals(2, genes.size());
    gene = iterator.next();
    LOG.debug(gene.getCoveredText());
    assertEquals("Serum gamma glutamyltransferase", gene.getCoveredText());
    gene = iterator.next();
    LOG.debug(gene.getCoveredText());
    assertEquals("gamma glutamyltransferase", gene.getCoveredText());
}
 
Example 14
Source File: ComplexTypeTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void testCountryType()
    throws Exception
{

    TypeSystemDescription tsd = TypeSystemDescriptionFactory
            .createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");

    CAS cas = CasCreationUtils.createCas(tsd, null, null);
    cas.setDocumentText("Asia is the largest continent on Earth. Asia is subdivided into 48 "
            + "countries, two of them (Russia and Turkey) having part of their land in "
            + "Europe. The most active place on Earth for tropical cyclone activity lies "
            + "northeast of the Philippines and south of Japan. The Gobi Desert is in "
            + "Mongolia and the Arabian Desert stretches across much of the Middle East. "
            + "The Yangtze River in China is the longest river in the continent. The "
            + "Himalayas between Nepal and China is the tallest mountain range in the "
            + "world. Tropical rainforests stretch across much of southern Asia and "
            + "coniferous and deciduous forests lie farther north.");
    TypeSystem ts = cas.getTypeSystem();
    Type continentType = ts.getType("de.Continent");
    Feature continentName = continentType.getFeatureByBaseName("name");
    AnnotationFS asiaContinent = cas.createAnnotation(continentType, 0, 4);
    asiaContinent.setStringValue(continentName, "Asia");
    cas.addFsToIndexes(asiaContinent);

    Type countryType = ts.getType("de.Country");
    Feature countryName = countryType.getFeatureByBaseName("name");
    AnnotationFS russia = cas.createAnnotation(countryType, 56, 62);
    russia.setStringValue(countryName, "Russian Federation");
    Feature continentFeature = countryType.getFeatureByBaseName("continent");
    russia.setFeatureValue(continentFeature, asiaContinent);
    cas.addFsToIndexes(russia);

    ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream(
            "src/test/resources/rules/region.rules"));
    ParsedConstraints constraints = parser.Parse().accept(new ParserVisitor());

    Evaluator constraintsEvaluator = new ValuesGenerator();

    List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(russia,
            "regionType", constraints);

    List<PossibleValue> exValues = new LinkedList<>();
    exValues.add(new PossibleValue("cold", true));

    assertEquals(possibleValues, exValues);
}
 
Example 15
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaCasIndexExistingFsInNewView() throws Exception {
    CAS cas1 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    cas1.setDocumentText("This is a test document in the initial view");
    Type referentType = cas1.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    FeatureStructure fs1 = cas1.createFS(referentType);
    cas1.getIndexRepository().addFS(fs1);

    //serialize complete
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    String xml = serialize(cas1, sharedData);
//    System.out.println(xml);
    int maxOutgoingXmiId = sharedData.getMaxXmiId();

    //deserialize into cas2
    XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
    this.deserialize(xml, cas2, sharedData2, true, -1);
    CasComparer.assertEquals(cas1, cas2);

    //create Marker, add/modify fs and serialize in delta xmi format.
    Marker marker = cas2.createMarker();

    //create View
    CAS view = cas2.createView("NewView");
    //add FS to index
    Type referentType2 = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    Iterator<FeatureStructure> fsIter = cas2.getIndexRepository().getAllIndexedFS(referentType2);
    while (fsIter.hasNext()) {
      FeatureStructure fs = fsIter.next();
      view.getIndexRepository().addFS(fs);
    }
    AnnotationFS cas2newAnnot = view.createAnnotation(cas2.getAnnotationType(), 6, 8);
    view.getIndexRepository().addFS(cas2newAnnot);

    // serialize cas2 in delta format
    String deltaxml1 = serialize(cas2, sharedData2, marker);
//    System.out.println(deltaxml1);

    //deserialize delta xmi into cas1
    this.deserialize(deltaxml1, cas1, sharedData, true, maxOutgoingXmiId, AllowPreexistingFS.allow);

    //check that new View contains the FS
    CAS deserView = cas1.getView("NewView");
    Iterator<FeatureStructure> deserFsIter = deserView.getIndexRepository().getAllIndexedFS(referentType);
    assertTrue(deserFsIter.hasNext());
  }
 
Example 16
Source File: AnnotationViewerDialog.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Launch that viewer.
 *
 * @param inputDirPath the input dir path
 * @param fileName the file name
 * @param typeSystem the type system
 * @param aTypesToDisplay the a types to display
 * @param javaViewerRBisSelected the java viewer R bis selected
 * @param javaViewerUCRBisSelected the java viewer UCR bis selected
 * @param xmlRBisSelected the xml R bis selected
 * @param styleMapFile the style map file
 * @param viewerDirectory the viewer directory
 */
public void launchThatViewer(String inputDirPath, String fileName, TypeSystem typeSystem,
        final String[] aTypesToDisplay, boolean javaViewerRBisSelected,
        boolean javaViewerUCRBisSelected, boolean xmlRBisSelected, File styleMapFile,
        File viewerDirectory) {
  try {

    File xcasFile = new File(inputDirPath, fileName);
    // create a new CAS
    CAS cas = CasCreationUtils.createCas(Collections.EMPTY_LIST, typeSystem, UIMAFramework
            .getDefaultPerformanceTuningProperties());
    // deserialize XCAS into CAS
    try (InputStream xcasInStream = new FileInputStream(xcasFile)) {
      XmlCasDeserializer.deserialize(xcasInStream, cas, true);
    }
    
    //get the specified view
    cas = cas.getView(this.defaultCasViewName);

    // launch appropriate viewer
    if (javaViewerRBisSelected || javaViewerUCRBisSelected) { // JMP
      // record preference for next time
      med1.setViewType(javaViewerRBisSelected ? "Java Viewer" : "JV User Colors");

      // create tree viewer component
      CasAnnotationViewer viewer = new CasAnnotationViewer();
      viewer.setDisplayedTypes(aTypesToDisplay);
      if (javaViewerUCRBisSelected)
        getColorsForTypesFromFile(viewer, styleMapFile);
      else
        viewer.setHiddenTypes(new String[] { "uima.cpm.FileLocation" });
      // launch viewer in a new dialog
      viewer.setCAS(cas);
      JDialog dialog = new JDialog(AnnotationViewerDialog.this, "Annotation Results for "
              + fileName + " in " + inputDirPath); // JMP
      dialog.getContentPane().add(viewer);
      dialog.setSize(850, 630);
      dialog.pack();
      dialog.show();
    } else {
      CAS defaultView = cas.getView(CAS.NAME_DEFAULT_SOFA);
      if (defaultView.getDocumentText() == null) {
        displayError("The HTML and XML Viewers can only view the default text document, which was not found in this CAS.");
        return;
      }
      // generate inline XML
      File inlineXmlFile = new File(viewerDirectory, "inline.xml");
      CasToInlineXml casToInlineXml = new CasToInlineXml();
      casToInlineXml.setFormattedOutput(false);
      String xmlAnnotations = casToInlineXml.generateXML(defaultView);
      FileOutputStream outStream = new FileOutputStream(inlineXmlFile);
      outStream.write(xmlAnnotations.getBytes(StandardCharsets.UTF_8));
      outStream.close();

      if (xmlRBisSelected) // JMP passed in
      {
        // record preference for next time
        med1.setViewType("XML");

        BrowserUtil.openUrlInDefaultBrowser(inlineXmlFile.getAbsolutePath());
      } else
      // HTML view
      {
        med1.setViewType("HTML");
        // generate HTML view
        // first process style map if not done already
        if (!processedStyleMap) {
          if (!styleMapFile.exists()) {
            annotationViewGenerator.autoGenerateStyleMapFile(
                    promptForAE().getAnalysisEngineMetaData(), styleMapFile);
          }
          annotationViewGenerator.processStyleMap(styleMapFile);
          processedStyleMap = true;
        }
        annotationViewGenerator.processDocument(inlineXmlFile);
        File genFile = new File(viewerDirectory, "index.html");
        // open in browser
        BrowserUtil.openUrlInDefaultBrowser(genFile.getAbsolutePath());
      }
    }

    // end LTV here

  } catch (Exception ex) {
    displayError(ex);
  }
}
 
Example 17
Source File: SimplePipeline.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
/**
 * <p>
 * Run the CollectionReader and AnalysisEngines as a pipeline. After processing all CASes provided
 * by the reader, the method calls the life-cycle methods
 * ({@link AnalysisEngine#collectionProcessComplete() collectionProcessComplete()} on the engines
 * and {@link Resource#destroy() destroy()}) on all engines. Note that the life-cycle methods are
 * <b>NOT</b> called on the reader. As the reader was instantiated by the caller, it must also be
 * managed (i.e. destroyed) the caller.
 * </p>
 * <p>
 * Note that with this method, external resources cannot be shared between the reader and the
 * analysis engines. They can be shared amongst the analysis engines.
 * </p>
 * <p>
 * The CAS is created using the resource manager used by the collection reader.
 * </p>
 * 
 * @param reader
 *          The CollectionReader that loads the documents into the CAS.
 * @param descs
 *          Primitive AnalysisEngineDescriptions that process the CAS, in order. If you have a mix
 *          of primitive and aggregate engines, then please create the AnalysisEngines yourself
 *          and call the other runPipeline method.
 * @throws IOException
 *           if there is an I/O problem in the reader
 * @throws ResourceInitializationException 
 *           if there is a problem initializing or running the pipeline.
 * @throws CollectionException 
 *           if there is a problem initializing or running the pipeline.
 * @throws AnalysisEngineProcessException 
 *           if there is a problem initializing or running the pipeline.
 */
public static void runPipeline(final CollectionReader reader,
        final AnalysisEngineDescription... descs) throws IOException,
        ResourceInitializationException, AnalysisEngineProcessException, CollectionException {
  AnalysisEngine aae = null;
  try {
    // Create AAE
    final AnalysisEngineDescription aaeDesc = createEngineDescription(descs);

    // Instantiate AAE
    aae = createEngine(aaeDesc);

    // Create CAS from merged metadata
    final CAS cas = CasCreationUtils.createCas(asList(reader.getMetaData(), aae.getMetaData()), 
            null, reader.getResourceManager());
    reader.typeSystemInit(cas.getTypeSystem());

    // Process
    while (reader.hasNext()) {
      reader.getNext(cas);
      aae.process(cas);
      cas.reset();
    }

    // Signal end of processing
    aae.collectionProcessComplete();
  } finally {
    // Destroy
    LifeCycleUtil.destroy(aae);
  }
}
 
Example 18
Source File: CasUtilTest.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
@Test
public void testExists() throws UIMAException {
  CAS cas = CasCreationUtils.createCas(createTypeSystemDescription(), null, null);

  Type tokenType = CasUtil.getAnnotationType(cas, Token.class);
  
  assertFalse(exists(cas, tokenType));

  cas.addFsToIndexes(cas.createAnnotation(tokenType, 0, 1));

  assertTrue(exists(cas, tokenType));
}
 
Example 19
Source File: JCasReinitTest.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
   * Make a type system having type T with a features f1
   * Make a type system having type T with no features
   * 
   * Have a JCas class for that type, with f1 defined
   * 
   * Create a Cas with a type system with the T(no features)
   * 
   * confirm that the JCas class getter for f1 offset returns -1
   * 
   * Create a Cas with a type system with the T(with f1)
   * 
   * confirm that the JCas class getter for f1 offset returns 0 
   * @throws Throwable 
   */

  
  public void testReinit() throws Throwable {
    File typeSystemFile1;
    
//    // x.y.z.Token with no features
//    typeSystemFile1 = JUnitExtension.getFile("ExampleCas/testTypeSystem_token_no_features.xml");
//    typeSystemDescription  = UIMAFramework.getXMLParser().parseTypeSystemDescription(
//        new XMLInputSource(typeSystemFile1));
//   
//    cas = (CASImpl) CasCreationUtils.createCas(typeSystemDescription, new TypePriorities_impl(), null);
//    T.dumpOffset();
//    

    typeSystemFile1 = JUnitExtension.getFile("ExampleCas/testTypeSystem_t_nofeatures.xml");
    typeSystemDescription  = UIMAFramework.getXMLParser().parseTypeSystemDescription(
        new XMLInputSource(typeSystemFile1));
    
    cas_no_features = (CASImpl) CasCreationUtils.createCas(typeSystemDescription, new TypePriorities_impl(), null);
    
    T.dumpOffset();

    typeSystemFile1 = JUnitExtension.getFile("ExampleCas/testTypeSystem_t_1_feature.xml");
    typeSystemDescription  = UIMAFramework.getXMLParser().parseTypeSystemDescription(
        new XMLInputSource(typeSystemFile1));
   
    cas = (CASImpl) CasCreationUtils.createCas(typeSystemDescription, new TypePriorities_impl(), null);
    
    T.dumpOffset();



   
  }
 
Example 20
Source File: CasFactory.java    From uima-uimafit with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link CAS} from type system descriptor files found by name. No auto-detection 
 * for type priorities, or indexes is performed.
 * 
 * @param typeSystemDescriptorNames
 *          names of the type system descriptors on the classpath used to initialize the CAS (in
 *          Java notation, e.g. "my.package.TypeSystem" without the ".xml" extension)
 * @return a new CAS
 * @throws ResourceInitializationException
 *           if the CAS could not be initialized
 */
public static CAS createCas(String... typeSystemDescriptorNames)
        throws ResourceInitializationException {
  return CasCreationUtils.createCas(createTypeSystemDescription(typeSystemDescriptorNames), null,
          null);
}