Java Code Examples for org.apache.uima.resource.metadata.TypeSystemDescription#addType()
The following examples show how to use
org.apache.uima.resource.metadata.TypeSystemDescription#addType() .
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: XmiCasDeserializerTest.java From uima-uimaj with Apache License 2.0 | 6 votes |
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 2
Source File: AgreementTestUtils.java From webanno with Apache License 2.0 | 6 votes |
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 3
Source File: AgreementTestUtils.java From webanno with Apache License 2.0 | 6 votes |
public static TypeSystemDescription createMultiLinkWithRoleTestTypeSytem(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", "", 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); for (String feature : aFeatures) { hostTD.addFeature(feature, "", CAS.TYPE_NAME_STRING); } typeSystems.add(tsd); typeSystems.add(TypeSystemDescriptionFactory.createTypeSystemDescription()); return CasCreationUtils.mergeTypeSystems(typeSystems); }
Example 4
Source File: ChainLayerSupport.java From webanno with Apache License 2.0 | 6 votes |
@Override public void generateTypes(TypeSystemDescription aTsd, AnnotationLayer aLayer, List<AnnotationFeature> aAllFeaturesInProject) { TypeDescription tdChains = aTsd.addType(aLayer.getName() + "Chain", aLayer.getDescription(), CAS.TYPE_NAME_ANNOTATION_BASE); tdChains.addFeature("first", "", aLayer.getName() + "Link"); // Custom features on chain layers are currently not supported // generateFeatures(aTsd, tdChains, type); TypeDescription tdLink = aTsd.addType(aLayer.getName() + "Link", "", CAS.TYPE_NAME_ANNOTATION); tdLink.addFeature("next", "", aLayer.getName() + "Link"); tdLink.addFeature("referenceType", "", CAS.TYPE_NAME_STRING); tdLink.addFeature("referenceRelation", "", CAS.TYPE_NAME_STRING); }
Example 5
Source File: TypeSystemUtilTest.java From uima-uimaj with Apache License 2.0 | 6 votes |
public void testTypeSystem2TypeSystemDescription() throws Exception { //create a CAS with example type system File typeSystemFile = JUnitExtension.getFile("ExampleCas/testTypeSystem.xml"); TypeSystemDescription tsDesc = UIMAFramework.getXMLParser().parseTypeSystemDescription( new XMLInputSource(typeSystemFile)); //add an example type to test FSArrays with and without elementTypes TypeDescription type = tsDesc.addType("example.TestType", "", "uima.tcas.Annotation"); type.addFeature("testFeat", "", "uima.cas.FSArray","uima.tcas.Annotation", null); TypeDescription type2 = tsDesc.addType("example.TestType2", "", "uima.tcas.Annotation"); type2.addFeature("testFeat", "", "uima.cas.FSArray"); CAS cas = CasCreationUtils.createCas(tsDesc, null, null); //convert that CAS's type system back to a TypeSystemDescription TypeSystemDescription tsDesc2 = TypeSystemUtil.typeSystem2TypeSystemDescription(cas.getTypeSystem()); //test that this is valid by creating a new CAS CasCreationUtils.createCas(tsDesc2, null, null); // Check that can be written (without cluttering up the console) StringWriter out = new StringWriter(); tsDesc2.toXML(out); }
Example 6
Source File: CurationTestUtils.java From webanno with Apache License 2.0 | 6 votes |
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 7
Source File: CurationTestUtils.java From webanno with Apache License 2.0 | 6 votes |
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", "", 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); typeSystems.add(tsd); typeSystems.add(TypeSystemDescriptionFactory.createTypeSystemDescription()); return CasCreationUtils.mergeTypeSystems(typeSystems); }
Example 8
Source File: DocumentMetadataLayerSupport.java From inception with Apache License 2.0 | 5 votes |
@Override public void generateTypes(TypeSystemDescription aTsd, AnnotationLayer aLayer, List<AnnotationFeature> aAllFeaturesInProject) { TypeDescription td = aTsd.addType(aLayer.getName(), "", CAS.TYPE_NAME_ANNOTATION_BASE); List<AnnotationFeature> featureForLayer = aAllFeaturesInProject.stream() .filter(feature -> aLayer.equals(feature.getLayer())) .collect(toList()); generateFeatures(aTsd, td, featureForLayer); }
Example 9
Source File: TypeSection.java From uima-uimaj with Apache License 2.0 | 5 votes |
/** * Handle add type. */ public void handleAddType() { AddTypeDialog dialog = new AddTypeDialog(this); if (dialog.open() == Window.CANCEL) return; TypeSystemDescription tsd = getMergedTypeSystemDescription(); TypeSystemDescription localTsd = getTypeSystemDescription(); TypeDescription td = localTsd.addType(dialog.typeName, multiLineFix(dialog.description), dialog.supertypeName); if (!isImportedType(dialog.typeName)) { td = tsd.addType(dialog.typeName, multiLineFix(dialog.description), dialog.supertypeName); addTypeToGUI(td); } else { rebuildMergedTypeSystem(); } if (isImportedType(dialog.typeName) || isBuiltInType(dialog.typeName)) { refresh(); selectTypeInGui(td); } editor.addDirtyTypeName(dialog.typeName); finishActionPack(); }
Example 10
Source File: CasCreationUtilsTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
public void testSetupTypeSystem() throws Exception { try { // test that duplicate feature names on supertype and subtype works // regardless of the order in which the types appear in the TypeSystemDescription TypeSystemDescription tsd1 = new TypeSystemDescription_impl(); TypeDescription supertype = tsd1.addType("test.Super", "", "uima.cas.TOP"); supertype.addFeature("testfeat", "", "uima.cas.Integer"); TypeDescription subtype = tsd1.addType("test.Sub", "", "test.Super"); subtype.addFeature("testfeat", "", "uima.cas.Integer"); CASMgr casMgr = CASFactory.createCAS(); CasCreationUtils.setupTypeSystem(casMgr, tsd1); assertNotNull(casMgr.getTypeSystemMgr().getType("test.Super") .getFeatureByBaseName("testfeat")); TypeSystemDescription tsd2 = new TypeSystemDescription_impl(); tsd2.setTypes(new TypeDescription[] { subtype, supertype }); casMgr = CASFactory.createCAS(); CasCreationUtils.setupTypeSystem(casMgr, tsd2); assertNotNull(casMgr.getTypeSystemMgr().getType("test.Super") .getFeatureByBaseName("testfeat")); } catch (ResourceInitializationException e) { JUnitExtension.handleException(e); } }
Example 11
Source File: AnnotationSchemaServiceImpl.java From webanno with Apache License 2.0 | 5 votes |
private void exportBuiltInTypeDescription(TypeSystemDescription aSource, TypeSystemDescription aTarget, String aType) { TypeDescription builtInType = aSource.getType(aType); if (builtInType == null) { throw new IllegalArgumentException( "No type description found for type [" + aType + "]"); } TypeDescription clonedType = aTarget.addType(builtInType.getName(), builtInType.getDescription(), builtInType.getSupertypeName()); if (builtInType.getFeatures() != null) { for (FeatureDescription feature : builtInType.getFeatures()) { clonedType.addFeature(feature.getName(), feature.getDescription(), feature.getRangeTypeName(), feature.getElementType(), feature.getMultipleReferencesAllowed()); // Export types referenced by built-in types also as built-in types. Note that // it is conceptually impossible for built-in types to refer to custom types, so // this is cannot lead to a custom type being exported as a built-in type. if ( feature.getElementType() != null && !isNativeUimaType(feature.getElementType()) && aTarget.getType(feature.getElementType()) == null ) { exportBuiltInTypeDescription(aSource, aTarget, feature.getElementType()); } else if ( feature.getRangeTypeName() != null && !isNativeUimaType(feature.getRangeTypeName()) && aTarget.getType(feature.getRangeTypeName()) == null ) { exportBuiltInTypeDescription(aSource, aTarget, feature.getRangeTypeName()); } } } }
Example 12
Source File: AllAnnotationsIndexedCheckTest.java From webanno with Apache License 2.0 | 5 votes |
@Test public void testOK() throws Exception { TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory() .createTypeSystemDescription(); String refTypeName = "RefType"; TypeDescription refTypeDesc = tsd.addType(refTypeName, null, CAS.TYPE_NAME_ANNOTATION); refTypeDesc.addFeature("ref", null, CAS.TYPE_NAME_ANNOTATION); CAS cas = CasCreationUtils.createCas(tsd, null, null); Type refType = cas.getTypeSystem().getType(refTypeName); // A regular index annotation AnnotationFS anno1 = cas.createAnnotation(cas.getAnnotationType(), 0, 1); cas.addFsToIndexes(anno1); // An indexed annotation but reachable through an indexe one (below) AnnotationFS anno2 = cas.createAnnotation(cas.getAnnotationType(), 0, 1); cas.addFsToIndexes(anno2); // An indexed annotation that references the non-indexed annotation above AnnotationFS anno3 = cas.createAnnotation(refType, 0, 1); anno3.setFeatureValue(refType.getFeatureByBaseName("ref"), anno2); cas.addFsToIndexes(anno3); List<LogMessage> messages = new ArrayList<>(); CasDoctor cd = new CasDoctor(AllFeatureStructuresIndexedCheck.class); // A project is not required for this check boolean result = cd.analyze(null, cas, messages); messages.forEach(System.out::println); assertTrue(result); }
Example 13
Source File: AllAnnotationsIndexedCheckTest.java From webanno with Apache License 2.0 | 5 votes |
@Test public void testFail() throws Exception { TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory() .createTypeSystemDescription(); String refTypeName = "RefType"; TypeDescription refTypeDesc = tsd.addType(refTypeName, null, CAS.TYPE_NAME_ANNOTATION); refTypeDesc.addFeature("ref", null, CAS.TYPE_NAME_ANNOTATION); CAS cas = CasCreationUtils.createCas(tsd, null, null); Type refType = cas.getTypeSystem().getType(refTypeName); // A regular index annotation AnnotationFS anno1 = cas.createAnnotation(cas.getAnnotationType(), 0, 1); cas.addFsToIndexes(anno1); // A non-index annotation but reachable through an indexe one (below) AnnotationFS anno2 = cas.createAnnotation(cas.getAnnotationType(), 0, 1); // An indexed annotation that references the non-indexed annotation above AnnotationFS anno3 = cas.createAnnotation(refType, 0, 1); anno3.setFeatureValue(refType.getFeatureByBaseName("ref"), anno2); cas.addFsToIndexes(anno3); List<LogMessage> messages = new ArrayList<>(); CasDoctor cd = new CasDoctor(AllFeatureStructuresIndexedCheck.class); // A project is not required for this check boolean result = cd.analyze(null, cas, messages); messages.forEach(System.out::println); assertFalse(result); }
Example 14
Source File: MultiprocessingAnalysisEngine_implTest.java From uima-uimaj with Apache License 2.0 | 5 votes |
/** * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { try { super.setUp(); mSimpleDesc = new AnalysisEngineDescription_impl(); mSimpleDesc.setFrameworkImplementation(Constants.JAVA_FRAMEWORK_NAME); mSimpleDesc.setPrimitive(true); mSimpleDesc .setAnnotatorImplementationName("org.apache.uima.analysis_engine.impl.TestAnnotator"); mSimpleDesc.getMetaData().setName("Simple Test"); TypeSystemDescription typeSys = new TypeSystemDescription_impl(); typeSys.addType("foo.Bar", "test", "uima.tcas.Annotation"); typeSys.addType("NamedEntity", "test", "uima.tcas.Annotation"); typeSys.addType("DocumentStructure", "test", "uima.tcas.Annotation"); mSimpleDesc.getAnalysisEngineMetaData().setTypeSystem(typeSys); Capability cap = new Capability_impl(); cap.addOutputType("NamedEntity", true); cap.addOutputType("DocumentStructure", true); Capability[] caps = new Capability[] {cap}; mSimpleDesc.getAnalysisEngineMetaData().setCapabilities(caps); mAggDesc = new AnalysisEngineDescription_impl(); mAggDesc.setPrimitive(false); mAggDesc.getMetaData().setName("Simple Test Aggregate"); mAggDesc.getDelegateAnalysisEngineSpecifiersWithImports().put("Test", mSimpleDesc); FixedFlow_impl flow = new FixedFlow_impl(); flow.setFixedFlow(new String[] { "Test" }); mAggDesc.getAnalysisEngineMetaData().setFlowConstraints(flow); mAggDesc.getAnalysisEngineMetaData().setCapabilities(caps); } catch (Exception e) { JUnitExtension.handleException(e); } }
Example 15
Source File: SpanLayerSupport.java From webanno with Apache License 2.0 | 5 votes |
@Override public void generateTypes(TypeSystemDescription aTsd, AnnotationLayer aLayer, List<AnnotationFeature> aAllFeaturesInProject) { TypeDescription td = aTsd.addType(aLayer.getName(), aLayer.getDescription(), CAS.TYPE_NAME_ANNOTATION); List<AnnotationFeature> featureForLayer = aAllFeaturesInProject.stream() .filter(feature -> aLayer.equals(feature.getLayer())) .collect(toList()); generateFeatures(aTsd, td, featureForLayer); }
Example 16
Source File: SlotFeatureSupport.java From webanno with Apache License 2.0 | 5 votes |
@Override public void generateFeature(TypeSystemDescription aTSD, TypeDescription aTD, AnnotationFeature aFeature) { // Link type TypeDescription linkTD = aTSD.addType(aFeature.getLinkTypeName(), "", CAS.TYPE_NAME_TOP); linkTD.addFeature(aFeature.getLinkTypeRoleFeatureName(), "", CAS.TYPE_NAME_STRING); linkTD.addFeature(aFeature.getLinkTypeTargetFeatureName(), "", aFeature.getType()); // Link feature aTD.addFeature(aFeature.getName(), aFeature.getDescription(), CAS.TYPE_NAME_FS_ARRAY, linkTD.getName(), false); }
Example 17
Source File: SubjectObjectFeatureSupport.java From inception with Apache License 2.0 | 5 votes |
@Override public void generateFeature(TypeSystemDescription aTSD, TypeDescription aTD, AnnotationFeature aFeature) { // Link type TypeDescription linkTD = aTSD.addType(aFeature.getLinkTypeName(), "", CAS.TYPE_NAME_TOP); linkTD.addFeature(aFeature.getLinkTypeRoleFeatureName(), "", CAS.TYPE_NAME_STRING); linkTD.addFeature(aFeature.getLinkTypeTargetFeatureName(), "", aFeature.getType()); // Link feature aTD.addFeature(aFeature.getName(), "", CAS.TYPE_NAME_FS_ARRAY, linkTD.getName(), false); }
Example 18
Source File: AnalysisEngine_implTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
public void testProcess() throws Exception { try { // test simple primitive TextAnalysisEngine (using TestAnnotator class) // This test should work with or without a type system description AnalysisEngineDescription primitiveDesc = new AnalysisEngineDescription_impl(); primitiveDesc.setPrimitive(true); primitiveDesc .setAnnotatorImplementationName("org.apache.uima.analysis_engine.impl.TestAnnotator"); primitiveDesc.getMetaData().setName("Test Primitive TAE"); // TypeSystemDescription tsd = new TypeSystemDescription_impl(); // tsd.addType("NamedEntity", "", "uima.tcas.Annotation"); // tsd.addType("DocumentStructure", "", "uima.cas.TOP"); // primitiveDesc.getAnalysisEngineMetaData().setTypeSystem(tsd); Capability cap = new Capability_impl(); cap.addOutputType("NamedEntity", true); cap.addOutputType("DocumentStructure", true); Capability[] caps = new Capability[] {cap}; primitiveDesc.getAnalysisEngineMetaData().setCapabilities(caps); _testProcess(primitiveDesc); primitiveDesc = new AnalysisEngineDescription_impl(); primitiveDesc.setPrimitive(true); primitiveDesc .setAnnotatorImplementationName("org.apache.uima.analysis_engine.impl.TestAnnotator"); primitiveDesc.getMetaData().setName("Test Primitive TAE"); TypeSystemDescription tsd = new TypeSystemDescription_impl(); tsd.addType("NamedEntity", "", "uima.tcas.Annotation"); tsd.addType("DocumentStructure", "", "uima.cas.TOP"); primitiveDesc.getAnalysisEngineMetaData().setTypeSystem(tsd); cap = new Capability_impl(); cap.addOutputType("NamedEntity", true); cap.addOutputType("DocumentStructure", true); caps = new Capability[] {cap}; primitiveDesc.getAnalysisEngineMetaData().setCapabilities(caps); _testProcess(primitiveDesc); // test simple aggregate TextAnalysisEngine (again using TestAnnotator class) AnalysisEngineDescription aggDesc = new AnalysisEngineDescription_impl(); aggDesc.setPrimitive(false); aggDesc.getMetaData().setName("Test Aggregate TAE"); aggDesc.getDelegateAnalysisEngineSpecifiersWithImports().put("Test", primitiveDesc); FixedFlow_impl flow = new FixedFlow_impl(); flow.setFixedFlow(new String[] { "Test" }); aggDesc.getAnalysisEngineMetaData().setFlowConstraints(flow); aggDesc.getAnalysisEngineMetaData().setCapabilities(caps); _testProcess(aggDesc); // test aggregate TAE containing a CAS Consumer File outFile = JUnitExtension.getFile("CpmOutput.txt"); if(outFile != null && outFile.exists()) { //outFile.delete() //can't be relied upon. Instead set file to zero length. FileOutputStream fos = new FileOutputStream(outFile, false); fos.close(); assertEquals(0,outFile.length()); } AnalysisEngineDescription aggWithCcDesc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription( new XMLInputSource(JUnitExtension .getFile("TextAnalysisEngineImplTest/AggregateTaeWithCasConsumer.xml"))); _testProcess(aggWithCcDesc, new String[] {"en"}); // test that CAS Consumer ran if (null == outFile) { outFile = JUnitExtension.getFile("CpmOutput.txt"); } assertTrue(outFile != null && outFile.exists()); assertTrue(outFile.length() > 0); outFile.delete(); //test aggregate that uses ParallelStep AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription( new XMLInputSource(JUnitExtension.getFile("TextAnalysisEngineImplTest/AggregateForParallelStepTest.xml"))); AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc); CAS cas = ae.newCAS(); cas.setDocumentText("new test"); ae.process(cas); assertEquals("new test", TestAnnotator.lastDocument); assertEquals("new test", TestAnnotator2.lastDocument); cas.reset(); } catch (Exception e) { JUnitExtension.handleException(e); } }
Example 19
Source File: XmiCasDeserializerTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
public void testOutOfTypeSystemArrayElement() throws Exception { //add to type system an annotation type that has an FSArray feature TypeDescription testAnnotTypeDesc = typeSystem.addType("org.apache.uima.testTypeSystem.TestAnnotation", "", "uima.tcas.Annotation"); testAnnotTypeDesc.addFeature("arrayFeat", "", "uima.cas.FSArray"); //populate a CAS with such an array CAS cas = CasCreationUtils.createCas(typeSystem, null, null); Type testAnnotType = cas.getTypeSystem().getType("org.apache.uima.testTypeSystem.TestAnnotation"); Type orgType = cas.getTypeSystem().getType( "org.apache.uima.testTypeSystem.Organization"); AnnotationFS orgAnnot1 = cas.createAnnotation(orgType, 0, 10); cas.addFsToIndexes(orgAnnot1); AnnotationFS orgAnnot2 = cas.createAnnotation(orgType, 10, 20); cas.addFsToIndexes(orgAnnot2); AnnotationFS testAnnot = cas.createAnnotation(testAnnotType, 0, 20); cas.addFsToIndexes(testAnnot); ArrayFS arrayFs = cas.createArrayFS(2); arrayFs.set(0, orgAnnot1); arrayFs.set(1, orgAnnot2); Feature arrayFeat = testAnnotType.getFeatureByBaseName("arrayFeat"); testAnnot.setFeatureValue(arrayFeat, arrayFs); //serialize to XMI String xmiStr = serialize(cas, null); //deserialize into a CAS that's missing the Organization type File partialTypeSystemFile = JUnitExtension.getFile("ExampleCas/partialTestTypeSystem.xml"); TypeSystemDescription partialTypeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription( new XMLInputSource(partialTypeSystemFile)); testAnnotTypeDesc = partialTypeSystem.addType("org.apache.uima.testTypeSystem.TestAnnotation", "", "uima.tcas.Annotation"); testAnnotTypeDesc.addFeature("arrayFeat", "", "uima.cas.FSArray"); CAS partialTsCas = CasCreationUtils.createCas(partialTypeSystem, null, null); XmiSerializationSharedData sharedData = new XmiSerializationSharedData(); deserialize(xmiStr, partialTsCas, sharedData, true, -1); //check out of type system data Type testAnnotType2 = partialTsCas.getTypeSystem().getType("org.apache.uima.testTypeSystem.TestAnnotation"); FeatureStructure testAnnot2 = partialTsCas.getAnnotationIndex(testAnnotType2).iterator().get(); Feature arrayFeat2 = testAnnotType2.getFeatureByBaseName("arrayFeat"); FeatureStructure arrayFs2 = testAnnot2.getFeatureValue(arrayFeat2); List ootsElems = sharedData.getOutOfTypeSystemElements(); assertEquals(2, ootsElems.size()); List ootsArrayElems = sharedData.getOutOfTypeSystemArrayElements((FSArray) arrayFs2); assertEquals(2, ootsArrayElems.size()); for (int i = 0; i < 2; i++) { OotsElementData oed = (OotsElementData)ootsElems.get(i); XmiArrayElement arel = (XmiArrayElement)ootsArrayElems.get(i); assertEquals(oed.xmiId, arel.xmiId); } //reserialize along with out of type system data String xmiStr2 = serialize(partialTsCas, sharedData); //deserialize into a new CAS and compare CAS cas2 = CasCreationUtils.createCas(typeSystem, null, null); deserialize(xmiStr2, cas2, null, false, -1); CasComparer.assertEquals(cas, cas2); }
Example 20
Source File: CasIOUtilsAlwaysHoldOnTest.java From uima-uimaj with Apache License 2.0 | 4 votes |
@Test public void thatDocumentAnnotationIsNotResurrected() throws Exception { // Must set this to true, otherwise the test will not fail. Setting it to true will cause // FSes which are not in any index to still be serialized out. When reading this data back, // UIMA will find the non-indexed DocumentAnnotation and add it back without checking whether // is was actually indexed or not. System.setProperty(CASImpl.ALWAYS_HOLD_ONTO_FSS, "true"); String customDocAnnoTypeName = "org.apache.uima.testing.CustomDocumentAnnotation"; TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory().createTypeSystemDescription(); tsd.addType(customDocAnnoTypeName, "", CAS.TYPE_NAME_DOCUMENT_ANNOTATION); CAS cas = CasCreationUtils.createCas(tsd, null, null); // Initialize the default document annotation // ... then immediately remove it from the indexes. FeatureStructure da = cas.getDocumentAnnotation(); assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList()) .extracting(fs -> fs.getType().getName()) .containsExactly(CAS.TYPE_NAME_DOCUMENT_ANNOTATION); cas.removeFsFromIndexes(da); // Now add a new document annotation of our custom type FeatureStructure cda = cas.createFS(cas.getTypeSystem().getType(customDocAnnoTypeName)); cas.addFsToIndexes(cda); assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList()) .extracting(fs -> fs.getType().getName()) .containsExactly(customDocAnnoTypeName); // Serialize to a buffer ByteArrayOutputStream bos = new ByteArrayOutputStream(); CasIOUtils.save(cas, bos, SerialFormat.SERIALIZED_TSI); // Deserialize from the buffer ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); CasIOUtils.load(bis, cas); assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList()) .extracting(fs -> fs.getType().getName()) .containsExactly(customDocAnnoTypeName); }