KG to B-UML

besser.utilities.kg_to_buml.clean_json_response(response: str) str[source]

Clean JSON response from LLM (remove markdown formatting)

besser.utilities.kg_to_buml.clean_plantuml_response(response: str) str[source]

Clean PlantUML response from LLM (remove markdown formatting)

besser.utilities.kg_to_buml.convert_spec_json_to_buml(system_spec, title='KG Imported Diagram')[source]

Convert class specifications from GPT-generated JSON to Apollon/BUML format.

besser.utilities.kg_to_buml.kg_to_buml(kg_path: str, openai_token: str, openai_model: str = 'gpt-4o')[source]

Convert a Knowledge Graph (KG) into a B-UML class diagram.

Supported inputs

  • Turtle files (.ttl)

  • RDF KGs (.rdf files)

  • Neo4j graphs (JSON) exported using the Cypher command below:

    CALL apoc.export.json.all(null, {stream:true, pretty:true})
    YIELD data
    RETURN data AS json
    

The function reads the KG (TTL/RDF or Neo4j JSON export), extracts classes, attributes, methods, and associations, and uses an LLM to generate a DomainModel object defined in besser.BUML.metamodel.structural.

Parameters:
  • kg_path (str) – Path to the input Knowledge Graph file.

  • openai_token (str) – Your OpenAI API token.

  • openai_model (str, optional) – Model name to use, by default “gpt-4o”.

Returns:

domain (DomainModel)

Return type:

the B-UML model object.

besser.utilities.kg_to_buml.kg_to_plantuml(kg_path: str, openai_token: str, openai_model: str = 'gpt-4o')[source]

Transform a Knowledge Graph (KG) into a PlantUML class diagram.

Supported inputs:

  • Turtle files (.ttl)

  • RDF KGs (.rdf files)

  • Neo4j graphs (JSON) exported using the Cypher command below:

    CALL apoc.export.json.all(null, {stream:true, pretty:true})
    YIELD data
    RETURN data AS json
    

The function extracts classes, attributes, methods, and associations from the KG and asks an LLM to produce a complete PlantUML class diagram using only the data types defined in besser.BUML.metamodel.structural.

Parameters:
  • kg_path (str) – Path to the input Knowledge Graph file (raw JSON, TTL, RDF).

  • openai_token (str) – Your OpenAI API token.

  • openai_model (str, optional) – Model name to use, by default “gpt-5”.

Returns:

A cleaned PlantUML diagram ready to render.

Return type:

str

besser.utilities.kg_to_buml.parse_json_safely(json_text: str)[source]

Parse JSON with error handling