From a Knowledge Graph to B-UML =============================== BESSER also enables the definition of a :doc:`../model_types/structural` from a Knowledge Graph (KG). The KG to be imported can be either a tripple store in .ttl, an RDF file in .rdf, or a property graph exported in .json. For this we use `OpenAI's GPT4 `_ and you must have an OpenAI token to implement this BESSER functionality. The following example KG represents the relationship between a company, its employees and their acquanitances. .. literalinclude:: ../../../../tests/BUML/notations/kg_to_buml/kg_example.ttl :language: turtle :linenos: .. warning:: Please make sure your KG is below 20 MB in size and is of one the following formats: ['ttl', 'json', 'rdf'] To transform this KG into a :doc:`../model_types/structural` you can use the following code. .. code-block:: python from besser.BUML.metamodel.structural import DomainModel from besser.utilities import kg_to_buml structural_model: DomainModel = kg_to_buml(kg_path="kg_test.ttl", openai_token="****") The function ``kg_to_buml()`` has as parameters the KG path and the OpenAI token, and returns a DomainModel object. .. note:: By default, the ``gpt-4o`` model is used. If you wish to use another version of the GPT model, you can specify it using the ``openai_model`` parameter in the ``kg_to_buml()`` function. More details of the function in the :doc:`API documentation <../../api/BUML/notations/kg2buml/kg>` Additionally, one could use :py:func:`besser.utilities.buml_code_builder.domain_model_to_code` to automatically generate the source code from the obtained structural model. You could reuse that code to modify, enhance, and recreate your structural model. The structural model source code generated by the transformation from the ``kg.test.ttl`` model is as follows. .. literalinclude:: ../../../../tests/BUML/notations/kg_to_buml/buml_model_from_kg.py :language: python :linenos: From a KG to PlantUML ------------------------- Similarly, it is also possible to obtain the PlantUML code from a model in a KG, as follows. .. code-block:: python from besser.utilities import kg_to_plantuml plantUML_model: str = kg_to_plantuml(kg_path="kg_test.ttl", openai_token="****") print(plantUML_model) The result of this transformation is a string with the code specified in the PlantUML notation: .. code-block:: console @startuml class Person { name : str age : int knows(person : Person) worksAt(company : Organization) } class Organization { name : str industry : str location : Place } class Place { name : str countryCode : str } Person "0..*" -- "0..*" Person : knows Person "0..*" -- "0..1" Organization : worksAt Organization "0..1" -- "0..1" Place : location @enduml