-
Notifications
You must be signed in to change notification settings - Fork 9
/
plantuml_to_buml.py
35 lines (32 loc) · 1.37 KB
/
plantuml_to_buml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from antlr4 import CommonTokenStream, FileStream, ParseTreeWalker
from .PlantUMLLexer import PlantUMLLexer
from .PlantUMLParser import PlantUMLParser
from .plantUML_buml_listener import BUMLGenerationListener
from besser.BUML.metamodel.structural import DomainModel
import os
def plantuml_to_buml(plantUML_model_path:str, buml_model_file_name:str = "buml_model"):
"""Transforms a PlantUML model into a B-UML model.
Args:
plantUML_model_path (str): The path to the file containing the PlantUML code.
buml_model_file_name (str, optional): the name of the file produced with the base code to build the B-UML model.
Returns:
BUML_model (DomainModel): the B-UML model object.
"""
lexer = PlantUMLLexer(FileStream(plantUML_model_path))
parser = PlantUMLParser(CommonTokenStream(lexer))
parse_tree = parser.domainModel()
# file creation
if not os.path.exists("buml"):
os.makedirs("buml")
output = open("buml/" + buml_model_file_name + ".py","w+")
listen = BUMLGenerationListener(output)
walker = ParseTreeWalker()
walker.walk(listen, parse_tree)
output.close()
# model creation
namespace = {}
with open("buml/" + buml_model_file_name + ".py", 'r') as model_code:
code = model_code.read()
exec(code, namespace)
BUML_model: DomainModel = namespace.get('domain')
return(BUML_model)