From 0eb4d915e632e9bbdea1e90eb1ae3d1ffadd9cd0 Mon Sep 17 00:00:00 2001 From: ftomassetti Date: Sat, 26 Sep 2026 21:13:59 +0200 Subject: [PATCH 1/2] LanguageFactory: declaration order, concept relations, other languages - Build elements, and the features of each classifier, in declaration order instead of grouping them by kind. - Concepts accept extends, implements, abstract and partition (extends on a concept factory used to be ignored); annotations accept extends and implements. - Types, extended and implemented classifiers can be elements of other languages: factories are resolved to the elements built from them instead of being looked up by name in the language being built. - LanguageFactory accepts the dependencies of the language. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 26 ++ src/lionweb/language/definition.py | 266 +++++++++++-------- tests/generation/test_language_generation.py | 1 + tests/language/test_definition.py | 87 ++++++ 4 files changed, 269 insertions(+), 111 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7b5d53..9b5b551 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,32 @@ All notable changes to this project will be documented in this file. +## 0.4.6 + +### Added + +- `LanguageFactory.concept` accepts `extends`, `implements`, `abstract` and `partition`; + `LanguageFactory.annotation` accepts `extends` and `implements`. +- `LanguageFactory` accepts `dependencies`, added to the built language. +- Types, extended and implemented classifiers passed to the factories can be elements of + other languages (for example `LionCoreBuiltins.get_node()` or a concept of a language + the new one depends on). Before, they were looked up by name in the language being + built. A factory of another `LanguageFactory` is rejected with a clear error. + +### Fixed + +- `LanguageFactory.build` creates the elements, and each classifier its features, in the + order in which they are declared. Before, it created all primitive types, then all + enumerations, then all classifiers; and in each classifier all properties, then all + references, then all containments. +- `set_extends` on a concept factory was ignored. + +### Changed + +- `ClassifierFactory` keeps the declared features in a single `features` list instead of + `properties`, `references` and `containments`, and `ClassifierFactory.populate` takes + the map of the built elements. + ## 0.4.5 ### Fixed diff --git a/src/lionweb/language/definition.py b/src/lionweb/language/definition.py index 09f8435..c19d025 100644 --- a/src/lionweb/language/definition.py +++ b/src/lionweb/language/definition.py @@ -13,6 +13,8 @@ from .enumeration_literal import EnumerationLiteral from .interface import Interface from .language import Language +from .language_entity import LanguageEntity +from .link import Link from .primitive_type import PrimitiveType from .property import Property from .reference import Reference @@ -70,6 +72,10 @@ class LiteralData(TypedDict): key: str | None +# The class of each feature declared in a ClassifierFactory +FeatureClass = type[Property] | type[Reference] | type[Containment] + + class ClassifierFactory: """ A factory class for creating and managing different types of classifiers. @@ -79,14 +85,21 @@ class ClassifierFactory: and metadata through a fluent interface. The supported classifier types include Concept, Interface, and Annotation. The factory ensures proper linking between classifiers and their features, leveraging optional and multiple configurations for - flexibility. + flexibility. Features are created in the order in which they are declared. + + Types, extended and implemented classifiers can be factories of the same + LanguageFactory or already built elements, including elements of other languages. Attributes: type: The type of the classifier (one of "Concept", "Interface", "Annotation"). name: The name of the classifier. id: A unique identifier for the classifier. key: A unique key for identifying the classifier. - extends: A list of other ClassifierFactory or Classifier objects that this classifier extends. + extends: The classifiers this classifier extends: the extended interfaces of an + Interface, or at most one extended Concept or Annotation. + implements: The interfaces implemented by a Concept or an Annotation. + abstract: Whether a Concept is abstract. + partition: Whether a Concept is a partition. """ def __init__( @@ -95,19 +108,21 @@ def __init__( name: str, id: str, key: str, - extends: list["ClassifierFactory | Classifier"] = [], + extends: list["ClassifierFactory | Classifier"] | None = None, + implements: list["ClassifierFactory | Interface"] | None = None, + abstract: bool = False, + partition: bool = False, ): self.type = type self.name = name - self.abstract = False - self.partition = False + self.abstract = abstract + self.partition = partition self.id = id self.key = key - self.properties: list[PropertyData] = [] - self.references: list[LinkData] = [] - self.containments: list[LinkData] = [] + self.features: list[tuple[FeatureClass, PropertyData | LinkData]] = [] self.annotates: Classifier | ClassifierFactory | None = None - self.extends = extends + self.extends: list[ClassifierFactory | Classifier] = list(extends or []) + self.implements: list[ClassifierFactory | Interface] = list(implements or []) def property( self, @@ -117,14 +132,17 @@ def property( id: str | None = None, key: str | None = None, ) -> "ClassifierFactory": - self.properties.append( - { - "name": name, - "type": type, - "multiplicity": multiplicity, - "id": id, - "key": key, - } + self.features.append( + ( + Property, + { + "name": name, + "type": type, + "multiplicity": multiplicity, + "id": id, + "key": key, + }, + ) ) return self @@ -136,33 +154,39 @@ def reference( id: str | None = None, key: str | None = None, ) -> "ClassifierFactory": - self.references.append( - { - "name": name, - "type": type, - "multiplicity": multiplicity, - "id": id, - "key": key, - } + self.features.append( + ( + Reference, + { + "name": name, + "type": type, + "multiplicity": multiplicity, + "id": id, + "key": key, + }, + ) ) return self def containment( self, name: str, - type: "ClassifierFactory", + type: "ClassifierFactory | Classifier", multiplicity: Multiplicity = Multiplicity.REQUIRED, id: str | None = None, key: str | None = None, ) -> "ClassifierFactory": - self.containments.append( - { - "name": name, - "type": type, - "multiplicity": multiplicity, - "id": id, - "key": key, - } + self.features.append( + ( + Containment, + { + "name": name, + "type": type, + "multiplicity": multiplicity, + "id": id, + "key": key, + }, + ) ) return self @@ -171,76 +195,51 @@ def populate( classifier: Classifier, id_calculator: Callable[[str | None, str], str], key_calculator: Callable[[str | None, str], str], + built: "dict[EntityFactory, LanguageEntity]", ): - language = classifier.language - assert language is not None - for property_data in self.properties: - property = Property( - lion_web_version=classifier.lion_web_version, - name=property_data.get("name"), - container=classifier, - id=property_data["id"] or id_calculator(classifier.id, property_data["name"]), - key=property_data["key"] or key_calculator(classifier.key, property_data["name"]), - ) - property.set_optional(not property_data["multiplicity"].value["required"]) - property_type = property_data["type"] - if isinstance(property_type, DataType): - type = property_type - else: - if isinstance(property_type, PrimitiveTypeFactory): - type_name = property_type.name - elif isinstance(property_type, EnumerationTypeFactory): - type_name = property_type.name - type = language.require_data_type_by_name(type_name) - if type is None: - raise ValueError(f"Type {type_name} not found") - property.type = type - classifier.add_feature(property) - for data in self.references: - reference = Reference( - lion_web_version=classifier.lion_web_version, - name=data["name"], - container=classifier, - id=data["id"] or id_calculator(classifier.id, data["name"]), - key=data["key"] or key_calculator(classifier.key, data["name"]), - ) - reference.set_optional(not data["multiplicity"].value["required"]) - reference.set_multiple(data["multiplicity"].value["many"]) - type_name = cast(str, data["type"].name) - link_type = language.require_classifier_by_name(type_name) - if link_type is None: - raise ValueError(f"Type {type_name} not found") - reference.set_type(link_type) - classifier.add_feature(reference) - for data in self.containments: - containment = Containment( + """Add the features and the relations to other classifiers to `classifier`. + + Args: + classifier: The classifier built from this factory. + id_calculator: Calculates the id of features without an explicit one. + key_calculator: Calculates the key of features without an explicit one. + built: The elements built so far, by the factory that defined them. + """ + for feature_class, data in self.features: + feature = feature_class( lion_web_version=classifier.lion_web_version, name=data["name"], container=classifier, id=data["id"] or id_calculator(classifier.id, data["name"]), key=data["key"] or key_calculator(classifier.key, data["name"]), ) - containment.set_optional(not data["multiplicity"].value["required"]) - containment.set_multiple(data["multiplicity"].value["many"]) - type_name = cast(str, data["type"].name) - link_type = language.require_classifier_by_name(type_name) - if link_type is None: - raise ValueError(f"Type {type_name} not found") - containment.set_type(link_type) - classifier.add_feature(containment) - if isinstance(classifier, Annotation): - if isinstance(self.annotates, ClassifierFactory): - classifier.annotates = language.require_classifier_by_name(self.annotates.name) - elif isinstance(self.annotates, Classifier): - classifier.annotates = self.annotates + feature.set_optional(not data["multiplicity"].value["required"]) + feature_type = _resolve(data["type"], built) + if isinstance(feature, Link): + feature.set_multiple(data["multiplicity"].value["many"]) + feature.set_type(cast(Classifier, feature_type)) + else: + feature.type = cast(DataType, feature_type) + classifier.add_feature(feature) + if isinstance(classifier, Concept): + if len(self.extends) > 1: + raise ValueError(f"Concept {self.name} can extend at most one concept") + for extended in self.extends: + classifier.set_extended_concept(cast(Concept, _resolve(extended, built))) + for implemented in self.implements: + classifier.add_implemented_interface(cast(Interface, _resolve(implemented, built))) + elif isinstance(classifier, Annotation): + if self.annotates is not None: + classifier.annotates = cast(Classifier, _resolve(self.annotates, built)) + if len(self.extends) > 1: + raise ValueError(f"Annotation {self.name} can extend at most one annotation") + for extended in self.extends: + classifier.extended_annotation = cast(Annotation, _resolve(extended, built)) + for implemented in self.implements: + classifier.add_implemented_interface(cast(Interface, _resolve(implemented, built))) elif isinstance(classifier, Interface): - for extends in self.extends: - if isinstance(extends, ClassifierFactory): - classifier.add_extended_interface( - language.require_interface_by_name(extends.name) - ) - elif isinstance(extends, Classifier): - classifier.add_extended_interface(cast(Interface, extends)) + for extended in self.extends: + classifier.add_extended_interface(cast(Interface, _resolve(extended, built))) def build(self, language: Language) -> "Classifier": match self.type: @@ -343,6 +342,23 @@ def build( return enumeration +EntityFactory = ClassifierFactory | PrimitiveTypeFactory | EnumerationTypeFactory + + +def _resolve( + element: "EntityFactory | LanguageEntity", built: "dict[EntityFactory, LanguageEntity]" +) -> LanguageEntity: + """Return the element built from a factory, or the element itself if already built.""" + if isinstance(element, ClassifierFactory | PrimitiveTypeFactory | EnumerationTypeFactory): + if element not in built: + raise ValueError( + f"{element.name} is defined by another LanguageFactory: " + "use the element built by that factory instead" + ) + return built[element] + return element + + class LanguageFactory: """ Represents a factory for creating and managing languages and their components. @@ -387,6 +403,7 @@ def __init__( key: str | None = None, id_calculator: Callable[[str | None, str], str] | None = None, key_calculator: Callable[[str | None, str], str] | None = None, + dependencies: list[Language] | None = None, ): """ Initializes a new instance of the class with provided parameters and default values where @@ -409,6 +426,7 @@ def __init__( key_calculator (Optional[Callable[[Optional[str], str], str]]): A function to calculate the instance key. Defaults to a lambda function to generate key based on parent key and name if not provided. + dependencies (Optional[list[Language]]): The languages this language depends on. Attributes: lw_version (LionWebVersion): The LionWeb version associated with the instance. @@ -434,6 +452,8 @@ def __init__( ) self.id = id or self.id_calculator(None, name) self.key = key or self.key_calculator(None, name) + self.dependencies: list[Language] = list(dependencies or []) + self.entities: list[EntityFactory] = [] self.classifiers: list[ClassifierFactory] = [] self.primitive_types: list[PrimitiveTypeFactory] = [] self.enumerations: list[EnumerationTypeFactory] = [] @@ -445,8 +465,8 @@ def build(self) -> Language: This function is responsible for creating a `Language` instance based on the attributes of the current object. It initializes the language with relevant details such as its name, id, key, version, and lion web version. Additionally, - it iterates over primitive types, enumerations, and classifiers to build and - populate their corresponding components in the language. + it builds the elements in the order in which they were declared, then populates + the classifiers. Returns: Language: The constructed `Language` object. @@ -459,29 +479,43 @@ def build(self) -> Language: lion_web_version=self.lw_version, ) - for primitive_type in self.primitive_types: - primitive_type.build(language) - for enumeration in self.enumerations: - enumeration.build(language, self.id_calculator, self.key_calculator) + for dependency in self.dependencies: + language.add_dependency(dependency) - classifiers = {} - for classifier in self.classifiers: - classifiers[classifier] = classifier.build(language) + built: dict[EntityFactory, LanguageEntity] = {} + for entity in self.entities: + if isinstance(entity, EnumerationTypeFactory): + built[entity] = entity.build(language, self.id_calculator, self.key_calculator) + else: + built[entity] = entity.build(language) for classifier in self.classifiers: - classifier.populate(classifiers[classifier], self.id_calculator, self.key_calculator) + classifier.populate( + cast(Classifier, built[classifier]), self.id_calculator, self.key_calculator, built + ) return language def concept( - self, name: str, id: str | None = None, key: str | None = None + self, + name: str, + id: str | None = None, + key: str | None = None, + extends: ClassifierFactory | Concept | None = None, + implements: list[ClassifierFactory | Interface] | None = None, + abstract: bool = False, + partition: bool = False, ) -> ClassifierFactory: sub = ClassifierFactory( "Concept", name, id=id or self.id_calculator(self.id, name), key=key or self.key_calculator(self.key, name), + extends=[extends] if extends is not None else None, + implements=implements, + abstract=abstract, + partition=partition, ) - self.classifiers.append(sub) + self._add_classifier(sub) return sub def interface( @@ -489,7 +523,7 @@ def interface( name: str, id: str | None = None, key: str | None = None, - extends: list[ClassifierFactory | Classifier] = [], + extends: list[ClassifierFactory | Classifier] | None = None, ) -> ClassifierFactory: sub = ClassifierFactory( "Interface", @@ -498,7 +532,7 @@ def interface( key=key or self.key_calculator(self.key, name), extends=extends, ) - self.classifiers.append(sub) + self._add_classifier(sub) return sub def annotation( @@ -507,15 +541,19 @@ def annotation( annotates: ClassifierFactory | Classifier, id: str | None = None, key: str | None = None, + extends: ClassifierFactory | Annotation | None = None, + implements: list[ClassifierFactory | Interface] | None = None, ) -> ClassifierFactory: sub = ClassifierFactory( "Annotation", name, id=id or self.id_calculator(self.id, name), key=key or self.key_calculator(self.key, name), + extends=[extends] if extends is not None else None, + implements=implements, ) sub.set_annotates(annotates) - self.classifiers.append(sub) + self._add_classifier(sub) return sub def primitive_type( @@ -527,6 +565,7 @@ def primitive_type( key=key or self.key_calculator(self.key, name), ) self.primitive_types.append(sub) + self.entities.append(sub) return sub def enumeration( @@ -543,4 +582,9 @@ def enumeration( literals=literals, ) self.enumerations.append(sub) + self.entities.append(sub) return sub + + def _add_classifier(self, classifier: ClassifierFactory) -> None: + self.classifiers.append(classifier) + self.entities.append(classifier) diff --git a/tests/generation/test_language_generation.py b/tests/generation/test_language_generation.py index e58a719..19dc8b2 100644 --- a/tests/generation/test_language_generation.py +++ b/tests/generation/test_language_generation.py @@ -55,6 +55,7 @@ def get_language() ->Language: my_other_concept.abstract = False my_other_concept.partition = False language.add_element(my_other_concept) + my_other_concept.set_extended_concept(my_concept) return language diff --git a/tests/language/test_definition.py b/tests/language/test_definition.py index 6547943..b998235 100644 --- a/tests/language/test_definition.py +++ b/tests/language/test_definition.py @@ -299,6 +299,93 @@ def test_ensure_language_definition_starlasu_specs_exactly(self): print(diff.differences) self.assertTrue(diff.are_equivalent()) + def test_elements_and_features_follow_declaration_order(self): + factory = LanguageFactory(name="L") + a = factory.concept("A") + factory.primitive_type("P") + factory.enumeration("E", ["X", "Y"]) + factory.interface("I") + string = LionCoreBuiltins.get_string() + a.containment("c", a).property("p", string).reference("r", a).property("q", string) + + language = factory.build() + + self.assertEqual(["A", "P", "E", "I"], [e.name for e in language.elements]) + self.assertEqual( + ["c", "p", "r", "q"], [f.name for f in language.get_concept_by_name("A").features] + ) + + def test_concept_extends_implements_abstract_partition(self): + factory = LanguageFactory(name="L") + base = factory.concept("Base", abstract=True) + named = factory.interface("Named") + factory.concept("Sub", extends=base, implements=[named], partition=True) + + language = factory.build() + + base_concept = language.get_concept_by_name("Base") + self.assertTrue(base_concept.abstract) + self.assertFalse(base_concept.partition) + sub = language.get_concept_by_name("Sub") + self.assertFalse(sub.abstract) + self.assertTrue(sub.partition) + self.assertIs(base_concept, sub.get_extended_concept()) + self.assertEqual([language.get_interface_by_name("Named")], sub.get_implemented()) + + def test_annotation_extends_implements(self): + factory = LanguageFactory(name="L") + target = factory.concept("Target") + named = factory.interface("Named") + base = factory.annotation("Base", annotates=target) + factory.annotation("Sub", annotates=target, extends=base, implements=[named]) + + language = factory.build() + + sub = language.get_annotation_by_name("Sub") + self.assertIs(language.get_concept_by_name("Target"), sub.annotates) + self.assertIs(language.get_annotation_by_name("Base"), sub.extended_annotation) + self.assertEqual([language.get_interface_by_name("Named")], sub.implemented) + + def test_elements_of_other_languages(self): + other_factory = LanguageFactory(name="Other") + other_factory.concept("Node") + other_factory.interface("Named") + other_factory.primitive_type("Position") + other = other_factory.build() + node = other.get_concept_by_name("Node") + named = other.get_interface_by_name("Named") + position = other.get_primitive_type_by_name("Position") + + factory = LanguageFactory(name="L", dependencies=[other]) + ( + factory.concept("C", extends=node, implements=[named]) + .containment("child", node, Multiplicity.OPTIONAL) + .reference("ref", named, Multiplicity.OPTIONAL) + .property("position", position, Multiplicity.OPTIONAL) + ) + factory.interface("I", extends=[named]) + factory.annotation("A", annotates=node) + + language = factory.build() + + self.assertEqual([other], language.depends_on()) + c = language.get_concept_by_name("C") + self.assertIs(node, c.get_extended_concept()) + self.assertEqual([named], c.get_implemented()) + self.assertIs(node, c.get_containment_by_name("child").type) + self.assertIs(named, c.get_reference_by_name("ref").type) + self.assertIs(position, c.get_property_by_name("position").type) + self.assertEqual([named], language.get_interface_by_name("I").extended_interfaces) + self.assertIs(node, language.get_annotation_by_name("A").annotates) + + def test_factory_of_another_language_factory_is_rejected(self): + other_node = LanguageFactory(name="Other").concept("Node") + factory = LanguageFactory(name="L") + factory.concept("C").reference("ref", other_node) + + with self.assertRaises(ValueError): + factory.build() + if __name__ == "__main__": unittest.main() From a17f8dc99c4b5654e336f777213b50f46fa27e1a Mon Sep 17 00:00:00 2001 From: ftomassetti Date: Sat, 26 Sep 2026 21:22:44 +0200 Subject: [PATCH 2/2] Keep properties, references and containments in separate lists Only the order within each kind of feature matters, not the order across kinds: go back to one list per kind, each kept in declaration order. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 +-- src/lionweb/language/definition.py | 108 ++++++++++++++++------------- tests/language/test_definition.py | 18 +++-- 3 files changed, 74 insertions(+), 62 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5b551..47e2b14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,17 +16,13 @@ All notable changes to this project will be documented in this file. ### Fixed -- `LanguageFactory.build` creates the elements, and each classifier its features, in the - order in which they are declared. Before, it created all primitive types, then all - enumerations, then all classifiers; and in each classifier all properties, then all - references, then all containments. +- `LanguageFactory.build` creates the elements in the order in which they are declared. + Before, it created all primitive types, then all enumerations, then all classifiers. - `set_extends` on a concept factory was ignored. ### Changed -- `ClassifierFactory` keeps the declared features in a single `features` list instead of - `properties`, `references` and `containments`, and `ClassifierFactory.populate` takes - the map of the built elements. +- `ClassifierFactory.populate` takes the map of the built elements. ## 0.4.5 diff --git a/src/lionweb/language/definition.py b/src/lionweb/language/definition.py index c19d025..7c31855 100644 --- a/src/lionweb/language/definition.py +++ b/src/lionweb/language/definition.py @@ -14,7 +14,6 @@ from .interface import Interface from .language import Language from .language_entity import LanguageEntity -from .link import Link from .primitive_type import PrimitiveType from .property import Property from .reference import Reference @@ -72,10 +71,6 @@ class LiteralData(TypedDict): key: str | None -# The class of each feature declared in a ClassifierFactory -FeatureClass = type[Property] | type[Reference] | type[Containment] - - class ClassifierFactory: """ A factory class for creating and managing different types of classifiers. @@ -85,7 +80,8 @@ class ClassifierFactory: and metadata through a fluent interface. The supported classifier types include Concept, Interface, and Annotation. The factory ensures proper linking between classifiers and their features, leveraging optional and multiple configurations for - flexibility. Features are created in the order in which they are declared. + flexibility. Properties, references and containments are each created in the order in + which they are declared. Types, extended and implemented classifiers can be factories of the same LanguageFactory or already built elements, including elements of other languages. @@ -119,7 +115,9 @@ def __init__( self.partition = partition self.id = id self.key = key - self.features: list[tuple[FeatureClass, PropertyData | LinkData]] = [] + self.properties: list[PropertyData] = [] + self.references: list[LinkData] = [] + self.containments: list[LinkData] = [] self.annotates: Classifier | ClassifierFactory | None = None self.extends: list[ClassifierFactory | Classifier] = list(extends or []) self.implements: list[ClassifierFactory | Interface] = list(implements or []) @@ -132,17 +130,14 @@ def property( id: str | None = None, key: str | None = None, ) -> "ClassifierFactory": - self.features.append( - ( - Property, - { - "name": name, - "type": type, - "multiplicity": multiplicity, - "id": id, - "key": key, - }, - ) + self.properties.append( + { + "name": name, + "type": type, + "multiplicity": multiplicity, + "id": id, + "key": key, + } ) return self @@ -154,17 +149,14 @@ def reference( id: str | None = None, key: str | None = None, ) -> "ClassifierFactory": - self.features.append( - ( - Reference, - { - "name": name, - "type": type, - "multiplicity": multiplicity, - "id": id, - "key": key, - }, - ) + self.references.append( + { + "name": name, + "type": type, + "multiplicity": multiplicity, + "id": id, + "key": key, + } ) return self @@ -176,17 +168,14 @@ def containment( id: str | None = None, key: str | None = None, ) -> "ClassifierFactory": - self.features.append( - ( - Containment, - { - "name": name, - "type": type, - "multiplicity": multiplicity, - "id": id, - "key": key, - }, - ) + self.containments.append( + { + "name": name, + "type": type, + "multiplicity": multiplicity, + "id": id, + "key": key, + } ) return self @@ -205,22 +194,41 @@ def populate( key_calculator: Calculates the key of features without an explicit one. built: The elements built so far, by the factory that defined them. """ - for feature_class, data in self.features: - feature = feature_class( + for property_data in self.properties: + property = Property( + lion_web_version=classifier.lion_web_version, + name=property_data["name"], + container=classifier, + id=property_data["id"] or id_calculator(classifier.id, property_data["name"]), + key=property_data["key"] or key_calculator(classifier.key, property_data["name"]), + ) + property.set_optional(not property_data["multiplicity"].value["required"]) + property.type = cast(DataType, _resolve(property_data["type"], built)) + classifier.add_feature(property) + for data in self.references: + reference = Reference( lion_web_version=classifier.lion_web_version, name=data["name"], container=classifier, id=data["id"] or id_calculator(classifier.id, data["name"]), key=data["key"] or key_calculator(classifier.key, data["name"]), ) - feature.set_optional(not data["multiplicity"].value["required"]) - feature_type = _resolve(data["type"], built) - if isinstance(feature, Link): - feature.set_multiple(data["multiplicity"].value["many"]) - feature.set_type(cast(Classifier, feature_type)) - else: - feature.type = cast(DataType, feature_type) - classifier.add_feature(feature) + reference.set_optional(not data["multiplicity"].value["required"]) + reference.set_multiple(data["multiplicity"].value["many"]) + reference.set_type(cast(Classifier, _resolve(data["type"], built))) + classifier.add_feature(reference) + for data in self.containments: + containment = Containment( + lion_web_version=classifier.lion_web_version, + name=data["name"], + container=classifier, + id=data["id"] or id_calculator(classifier.id, data["name"]), + key=data["key"] or key_calculator(classifier.key, data["name"]), + ) + containment.set_optional(not data["multiplicity"].value["required"]) + containment.set_multiple(data["multiplicity"].value["many"]) + containment.set_type(cast(Classifier, _resolve(data["type"], built))) + classifier.add_feature(containment) if isinstance(classifier, Concept): if len(self.extends) > 1: raise ValueError(f"Concept {self.name} can extend at most one concept") diff --git a/tests/language/test_definition.py b/tests/language/test_definition.py index b998235..e9ed6a8 100644 --- a/tests/language/test_definition.py +++ b/tests/language/test_definition.py @@ -299,21 +299,29 @@ def test_ensure_language_definition_starlasu_specs_exactly(self): print(diff.differences) self.assertTrue(diff.are_equivalent()) - def test_elements_and_features_follow_declaration_order(self): + def test_elements_follow_declaration_order(self): factory = LanguageFactory(name="L") a = factory.concept("A") factory.primitive_type("P") factory.enumeration("E", ["X", "Y"]) factory.interface("I") string = LionCoreBuiltins.get_string() - a.containment("c", a).property("p", string).reference("r", a).property("q", string) + ( + a.containment("c2", a) + .property("p2", string) + .reference("r2", a) + .containment("c1", a) + .property("p1", string) + .reference("r1", a) + ) language = factory.build() self.assertEqual(["A", "P", "E", "I"], [e.name for e in language.elements]) - self.assertEqual( - ["c", "p", "r", "q"], [f.name for f in language.get_concept_by_name("A").features] - ) + concept = language.get_concept_by_name("A") + self.assertEqual(["p2", "p1"], [f.name for f in concept.all_properties()]) + self.assertEqual(["r2", "r1"], [f.name for f in concept.all_references()]) + self.assertEqual(["c2", "c1"], [f.name for f in concept.all_containments()]) def test_concept_extends_implements_abstract_partition(self): factory = LanguageFactory(name="L")