Skip to content
Snippets Groups Projects
Commit 93a99ef0 authored by André Fugmann's avatar André Fugmann
Browse files

Add semantic definitions to the translation process

parent 7cf0d1e1
No related branches found
No related tags found
No related merge requests found
...@@ -88,6 +88,7 @@ async def validate(model: str, files: list[UploadFile], semantic: bool = False): ...@@ -88,6 +88,7 @@ async def validate(model: str, files: list[UploadFile], semantic: bool = False):
if files[0].filename.endswith('json'): if files[0].filename.endswith('json'):
json_files = {file.filename[:-5]: json.loads((await file.read()).decode('utf-8')) for file in files} json_files = {file.filename[:-5]: json.loads((await file.read()).decode('utf-8')) for file in files}
translated_files = translate.translate_files_to_text(json_files) translated_files = translate.translate_files_to_text(json_files)
# TODO: Mapping
for filename in translated_files: for filename in translated_files:
with open(os.path.join(in_path, f'{filename}.soil'), 'wb') as outfile: with open(os.path.join(in_path, f'{filename}.soil'), 'wb') as outfile:
outfile.write(translated_files[filename].encode('utf-8')) outfile.write(translated_files[filename].encode('utf-8'))
......
...@@ -80,7 +80,14 @@ def element_to_text(element: Dict[str, any], filename: str, files: dict[str, any ...@@ -80,7 +80,14 @@ def element_to_text(element: Dict[str, any], filename: str, files: dict[str, any
custom_descriptions = {} custom_descriptions = {}
temp_output = element["elementType"] + " " + element["name"].replace(" ", "") + " " temp_output = element["elementType"] + " " + element["name"].replace(" ", "") + " "
if element['elementType'] == 'component' and 'baseComponent' in element: if element['elementType'] == 'component' and 'baseComponent' in element:
temp_output += "extends " + get_element_type_name(element["baseComponent"], filename, files) + " " temp_output += "extends " + element['baseComponent']['name'] + " "
if (element["elementType"] == "component" or
element["elementType"] == "function" or
element["elementType"] == "variable"):
if element['semanticDefinition'] is not None:
temp_output += f"defines <{element['semanticDefinition']}> "
temp_output += "{\n" temp_output += "{\n"
if element["elementType"] == "interface": if element["elementType"] == "interface":
...@@ -94,7 +101,7 @@ def element_to_text(element: Dict[str, any], filename: str, files: dict[str, any ...@@ -94,7 +101,7 @@ def element_to_text(element: Dict[str, any], filename: str, files: dict[str, any
return temp_output + "}\n\n" return temp_output + "}\n\n"
for key in element: for key in element:
if key in ["uuid", "cardOpen", "elementType", "constant"]: if key in ["baseComponent", "uuid", "cardOpen", "elementType", "constant", "semanticDefinition"]:
continue continue
elif key in ["arguments", "returns", "components", "parameters", "functions", "measurements"]: elif key in ["arguments", "returns", "components", "parameters", "functions", "measurements"]:
if len(element[key]): if len(element[key]):
...@@ -109,6 +116,13 @@ def element_to_text(element: Dict[str, any], filename: str, files: dict[str, any ...@@ -109,6 +116,13 @@ def element_to_text(element: Dict[str, any], filename: str, files: dict[str, any
elif key == "streams": elif key == "streams":
if len(element[key]) > 0: if len(element[key]) > 0:
temp_output += " streams:\n" + streams_to_text(element[key]) temp_output += " streams:\n" + streams_to_text(element[key])
elif key == "unit":
# Probably too hacky to just check if the unit string contains ':'
# to determine if it is a semantic unit or not
if ':' in element['unit']:
temp_output += f" unit: <{element['unit']}>\n"
else:
temp_output += f" unit: {element['unit']}\n"
elif key == "range": elif key == "range":
if element["datatype"] != "enum": if element["datatype"] != "enum":
minimum = '' if element["range"][0] is None else str(element["range"][0]) minimum = '' if element["range"][0] is None else str(element["range"][0])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment