diff --git a/CHANGELOG.md b/CHANGELOG.md index d7b5d53..47e2b14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,28 @@ 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 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.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..7c31855 100644 --- a/src/lionweb/language/definition.py +++ b/src/lionweb/language/definition.py @@ -13,6 +13,7 @@ from .enumeration_literal import EnumerationLiteral from .interface import Interface from .language import Language +from .language_entity import LanguageEntity from .primitive_type import PrimitiveType from .property import Property from .reference import Reference @@ -79,14 +80,22 @@ 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. 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. 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 +104,23 @@ 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.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, @@ -150,7 +163,7 @@ def reference( def containment( self, name: str, - type: "ClassifierFactory", + type: "ClassifierFactory | Classifier", multiplicity: Multiplicity = Multiplicity.REQUIRED, id: str | None = None, key: str | None = None, @@ -171,30 +184,26 @@ 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 + """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 property_data in self.properties: property = Property( lion_web_version=classifier.lion_web_version, - name=property_data.get("name"), + 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 = 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 + property.type = cast(DataType, _resolve(property_data["type"], built)) classifier.add_feature(property) for data in self.references: reference = Reference( @@ -206,11 +215,7 @@ def populate( ) 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) + reference.set_type(cast(Classifier, _resolve(data["type"], built))) classifier.add_feature(reference) for data in self.containments: containment = Containment( @@ -222,25 +227,27 @@ def populate( ) 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) + containment.set_type(cast(Classifier, _resolve(data["type"], built))) 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 + 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 +350,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 +411,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 +434,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 +460,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 +473,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 +487,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 +531,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 +540,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 +549,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 +573,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 +590,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..e9ed6a8 100644 --- a/tests/language/test_definition.py +++ b/tests/language/test_definition.py @@ -299,6 +299,101 @@ def test_ensure_language_definition_starlasu_specs_exactly(self): print(diff.differences) self.assertTrue(diff.are_equivalent()) + 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("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]) + 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") + 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()