Skip to content

Commit

Permalink
#242 - Array features are wrapped in extra array
Browse files Browse the repository at this point in the history
- Fixed bug that wraps array in another array instance for FS-array features where multiple references are not allowed
  • Loading branch information
reckart committed Dec 15, 2021
1 parent dd16c7b commit 4564886
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 10 additions & 2 deletions cassis/cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
from attr import validators
from sortedcontainers import SortedKeyList

from cassis.typesystem import TYPE_NAME_SOFA, FeatureStructure, TypeCheckError, TypeSystem, TYPE_NAME_FS_LIST, \
TYPE_NAME_FS_ARRAY, FEATURE_BASE_NAME_HEAD, TypeSystemMode
from cassis.typesystem import (
FEATURE_BASE_NAME_HEAD,
TYPE_NAME_FS_ARRAY,
TYPE_NAME_FS_LIST,
TYPE_NAME_SOFA,
FeatureStructure,
TypeCheckError,
TypeSystem,
TypeSystemMode,
)

_validator_optional_string = validators.optional(validators.instance_of(str))

Expand Down
1 change: 1 addition & 0 deletions cassis/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@

_LIST_TYPES = _PRIMITIVE_LIST_TYPES | {TYPE_NAME_FS_LIST}


class TypeSystemMode(Enum):
"""How much type system information to include."""

Expand Down
18 changes: 13 additions & 5 deletions cassis/xmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

from cassis.cas import Cas, IdGenerator, Sofa, View
from cassis.typesystem import (
_LIST_TYPES,
_PRIMITIVE_ARRAY_TYPES,
_PRIMITIVE_LIST_TYPES,
FEATURE_BASE_NAME_BEGIN,
FEATURE_BASE_NAME_END,
FEATURE_BASE_NAME_HEAD,
Expand All @@ -26,6 +28,7 @@
TYPE_NAME_EMPTY_FLOAT_LIST,
TYPE_NAME_EMPTY_FS_LIST,
TYPE_NAME_EMPTY_INTEGER_LIST,
TYPE_NAME_EMPTY_STRING_LIST,
TYPE_NAME_FLOAT,
TYPE_NAME_FLOAT_ARRAY,
TYPE_NAME_FLOAT_LIST,
Expand All @@ -39,16 +42,17 @@
TYPE_NAME_NON_EMPTY_FLOAT_LIST,
TYPE_NAME_NON_EMPTY_FS_LIST,
TYPE_NAME_NON_EMPTY_INTEGER_LIST,
TYPE_NAME_NON_EMPTY_STRING_LIST,
TYPE_NAME_SHORT,
TYPE_NAME_SHORT_ARRAY,
TYPE_NAME_SOFA,
TYPE_NAME_STRING,
TYPE_NAME_STRING_ARRAY,
TYPE_NAME_STRING_LIST,
FeatureStructure,
Type,
TypeNotFoundError,
TypeSystem, TYPE_NAME_STRING_LIST, TYPE_NAME_EMPTY_STRING_LIST, TYPE_NAME_NON_EMPTY_STRING_LIST,
_PRIMITIVE_LIST_TYPES, _LIST_TYPES,
TypeSystem,
)

NAN_VALUE = "NaN"
Expand Down Expand Up @@ -244,7 +248,9 @@ def deserialize(self, source: Union[IO, str], typesystem: TypeSystem, lenient: b
continue

# Resolve references
if fs.type.name == TYPE_NAME_FS_ARRAY or feature.rangeType.name == TYPE_NAME_FS_ARRAY:
if fs.type.name == TYPE_NAME_FS_ARRAY or (
feature.rangeType.name == TYPE_NAME_FS_ARRAY and not feature.multipleReferencesAllowed
):
# An array of references is a list of integers separated
# by single spaces, e.g. <foo:bar elements="1 2 3 42" />
targets = []
Expand Down Expand Up @@ -627,7 +633,9 @@ def _serialize_feature_structure(self, cas: Cas, root: etree.Element, fs: Featur
elem.attrib[feature_name] = " ".join(str(e.xmiID) for e in value.elements)
elif feature.rangeType.name == TYPE_NAME_FS_LIST and not feature.multipleReferencesAllowed:
if value is not None: # Compare to none to not skip if elements is empty!
elem.attrib[feature_name] = " ".join(str(e.xmiID) for e in self._collect_list_elements(feature.rangeType.name, value))
elem.attrib[feature_name] = " ".join(
str(e.xmiID) for e in self._collect_list_elements(feature.rangeType.name, value)
)
elif feature_name == FEATURE_BASE_NAME_SOFA:
elem.attrib[feature_name] = str(value.xmiID)
elif feature.rangeType.name == TYPE_NAME_BOOLEAN:
Expand Down Expand Up @@ -659,7 +667,7 @@ def _serialize_view(self, root: etree.Element, view: View):
elem.attrib["sofa"] = str(view.sofa.xmiID)
elem.attrib["members"] = " ".join(sorted((str(x.xmiID) for x in view.get_all_annotations()), key=int))

def _collect_list_elements(self, type_name: str, value) -> List[str]:
def _collect_list_elements(self, type_name: str, value) -> List[str]:
if type_name not in _LIST_TYPES:
raise ValueError(f"Not a primitive list: {type_name}")

Expand Down

0 comments on commit 4564886

Please sign in to comment.