Skip to content

Commit

Permalink
Merge branch 'bugfix/174-FSes-that-are-only-transitively-referenced-c…
Browse files Browse the repository at this point in the history
…annot-be-serialized' into feature/168-Experimental-JSON-CAS-support

* bugfix/174-FSes-that-are-only-transitively-referenced-cannot-be-serialized:
  #174 - FSes that are only transitively referenced cannot be serialized (#179)
  • Loading branch information
reckart committed Aug 18, 2021
2 parents 13e9816 + 4380c50 commit 61de56e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cassis/cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ def typecheck(self) -> List[TypeCheckError]:

return all_errors

def _find_all_fs(self) -> Iterable[FeatureStructure]:
def _find_all_fs(self, generate_missing_ids: bool = False) -> Iterable[FeatureStructure]:
"""This function traverses the whole CAS in order to find all directly and indirectly referenced
feature structures. Traversing is needed as it can be that a feature structure is not added to the sofa but
referenced by another feature structure as a feature."""
Expand All @@ -645,6 +645,8 @@ def _find_all_fs(self) -> Iterable[FeatureStructure]:
ts = self.typesystem
while openlist:
fs = openlist.pop(0)
if generate_missing_ids:
fs.xmiID = self._get_next_xmi_id()
all_fs[fs.xmiID] = fs

t = ts.get_type(fs.type)
Expand Down
7 changes: 1 addition & 6 deletions cassis/xmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,8 @@ def serialize(self, sink: Union[IO, str], cas: Cas, pretty_print=True):

self._serialize_cas_null(root)

# Generate XMI ids for unset ones
for fs in cas._find_all_fs():
if fs.xmiID is None:
fs.xmiID = cas._get_next_xmi_id()

# Find all fs, even the ones that are not directly added to a sofa
for fs in sorted(cas._find_all_fs(), key=lambda a: a.xmiID):
for fs in sorted(cas._find_all_fs(generate_missing_ids=True), key=lambda a: a.xmiID):
self._serialize_feature_structure(cas, root, fs)

for sofa in cas.sofas:
Expand Down
10 changes: 7 additions & 3 deletions tests/test_xmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ def test_serializing_with_unset_xmi_ids_works():
BarType = typesystem.create_type("bar.test.Bar")

# Check that two annotations of the same type get the same namespace
foo = FooType()
cas.add(foo)
foo.bar = BarType()
foo1 = FooType()
cas.add(foo1)
foo1.bar = BarType()

foo2 = FooType()
cas.add(foo2)
foo2.bar = BarType()

# assert no error
cas.to_xmi(pretty_print=True)
Expand Down

0 comments on commit 61de56e

Please sign in to comment.