Skip to content

Commit

Permalink
#170 - Handling of the "uima.noNamespace" prefix (#178)
Browse files Browse the repository at this point in the history
- Move no namespace handling to xmi
- Add better tests for this
  • Loading branch information
jcklie authored Aug 14, 2021
1 parent 9140380 commit f5d396c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 46 deletions.
17 changes: 2 additions & 15 deletions cassis/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,12 +782,6 @@ def deserialize(self, source: Union[IO, str]) -> TypeSystem:
description = self._get_elem_as_str(elem.find("{*}description"))
supertypeName = self._get_elem_as_str(elem.find("{*}supertypeName"))

if "." not in type_name:
type_name = "uima.noNamespace." + type_name

if "." not in supertypeName:
supertypeName = "uima.noNamespace." + supertypeName

types[type_name] = Type(name=type_name, supertypeName=supertypeName, description=description)
type_dependencies[type_name].add(supertypeName)

Expand Down Expand Up @@ -923,20 +917,13 @@ def _serialize_type(self, xf: IO, type_: Type):
typeDescription = etree.Element("typeDescription")

name = etree.SubElement(typeDescription, "name")
type_name = type_.name
if type_name.startswith("uima.noNamespace."):
type_name = type_name.replace("uima.noNamespace.", "")

name.text = type_name
name.text = type_.name

description = etree.SubElement(typeDescription, "description")
description.text = type_.description

supertype_name_node = etree.SubElement(typeDescription, "supertypeName")
supertype_name = type_.supertypeName
if supertype_name.startswith("uima.noNamespace."):
supertype_name = supertype_name.replace("uima.noNamespace.", "")
supertype_name_node.text = supertype_name
supertype_name_node.text = type_.supertypeName

# Only create the `feature` element if there is at least one feature
feature_list = list(type_.features)
Expand Down
13 changes: 10 additions & 3 deletions cassis/xmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,12 @@ def _parse_view(self, elem) -> ProtoView:
def _parse_feature_structure(self, typesystem: TypeSystem, elem, children: Dict[str, List[str]]):
# Strip the http prefix, replace / with ., remove the ecore part
# TODO: Error checking
typename = elem.tag[9:].replace("/", ".").replace("ecore}", "").strip()
type_name: str = elem.tag[9:].replace("/", ".").replace("ecore}", "").strip()

AnnotationType = typesystem.get_type(typename)
if type_name.startswith("uima.noNamespace."):
type_name = type_name[17:]

AnnotationType = typesystem.get_type(type_name)
attributes = dict(elem.attrib)
attributes.update(children)

Expand Down Expand Up @@ -363,8 +366,12 @@ def _serialize_cas_null(self, root: etree.Element):
def _serialize_feature_structure(self, cas: Cas, root: etree.Element, fs: FeatureStructure):
ts = cas.typesystem

type_name = fs.type
if "." not in type_name:
type_name = f"uima.noNamespace.{type_name}"

# The type name is a Java package, e.g. `org.myproj.Foo`.
parts = fs.type.split(".")
parts = type_name.split(".")

# The CAS type namespace is converted to an XML namespace URI by the following rule:
# replace all dots with slashes, prepend http:///, and append .ecore.
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ def cas_with_leniency_xmi(cas_with_leniency_path):
return f.read()


# CAS and with feature structures whose types have no namespace


@pytest.fixture
def cas_has_fs_with_no_namespace_path():
return os.path.join(FIXTURE_DIR, "xmi", "cas_has_fs_with_no_namespace.xmi")


@pytest.fixture
def cas_has_fs_with_no_namespace_xmi(cas_has_fs_with_no_namespace_path):
with open(cas_has_fs_with_no_namespace_path, "r") as f:
return f.read()


# Small type system


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,18 @@
<typeSystemDescription xmlns="http://uima.apache.org/resourceSpecifier">
<types>
<typeDescription>
<name>ArtifactID</name>
<name>TypeWithNoNameSpace</name>
<description>A unique artifact identifier.</description>
<supertypeName>uima.cas.TOP</supertypeName>
<supertypeName>uima.tcas.Annotation</supertypeName>
<features>
<featureDescription>
<name>artifactID</name>
<description>A unique identification string for the artifact. This should be the file name for files,
or the unique identifier used in a database if the document source is a database
collection reader.</description>
<name>testFeature</name>
<description>I am the testFeatureDescription</description>
<rangeTypeName>uima.cas.String</rangeTypeName>
</featureDescription>
</features>
</typeDescription>
<typeDescription>
<name>ArtifactMetadata</name>
<description>A piece of metadata about the artifact in the form of a key value pair.</description>
<supertypeName>uima.cas.TOP</supertypeName>
<features>
<featureDescription>
<name>key</name>
<description/>
<rangeTypeName>uima.cas.String</rangeTypeName>
</featureDescription>
<featureDescription>
<name>value</name>
<description/>
<rangeTypeName>uima.cas.String</rangeTypeName>
</featureDescription>
</features>
</typeDescription>
<typeDescription>
<name>ChildOfArtifactID</name>
<supertypeName>ArtifactID</supertypeName>
<description/>

</typeDescription>

</types>
</typeSystemDescription>
7 changes: 7 additions & 0 deletions tests/test_files/xmi/cas_has_fs_with_no_namespace.xmi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><xmi:XMI xmlns:noNamespace="http:///uima/noNamespace.ecore" xmlns:tcas="http:///uima/tcas.ecore" xmlns:xmi="http://www.omg.org/XMI" xmlns:cas="http:///uima/cas.ecore" xmi:version="2.0">
<cas:NULL xmi:id="0"/>
<noNamespace:TypeWithNoNameSpace xmi:id="2" sofa="1" begin="0" end="9"/>
<tcas:DocumentAnnotation xmi:id="3" sofa="1" begin="0" end="9" language="x-unspecified"/>
<cas:Sofa xmi:id="1" sofaNum="1" sofaID="_InitialView" mimeType="text" sofaString="I am test"/>
<cas:View sofa="1" members="2 3"/>
</xmi:XMI>
4 changes: 4 additions & 0 deletions tests/test_xmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
(pytest.lazy_fixture("cas_with_reserved_names_xmi"), pytest.lazy_fixture("typesystem_with_reserved_names_xml")),
(pytest.lazy_fixture("cas_with_two_sofas_xmi"), pytest.lazy_fixture("small_typesystem_xml")),
(pytest.lazy_fixture("cas_with_smileys_xmi"), pytest.lazy_fixture("dkpro_typesystem_xml")),
(
pytest.lazy_fixture("cas_has_fs_with_no_namespace_xmi"),
pytest.lazy_fixture("typesystem_has_types_with_no_namespace_xml"),
),
]


Expand Down

0 comments on commit f5d396c

Please sign in to comment.