Skip to content
Snippets Groups Projects
Select Git revision
  • 6deca9931ef61f00a79a484441aa434f7ebbc40d
  • 5.3 default protected
  • 5.6
  • 5.5 protected
  • 5.4 protected
  • 4.26 protected
  • 4.27 protected
  • 5.1 protected
  • 5.2 protected
  • before-invasion
  • 4.22 protected
  • 4.21 protected
  • 4.22.1 protected
  • 4.22.0 protected
14 results

SM_logo.uasset

Blame
  • translate.py 7.22 KiB
    from typing import List, Dict
    
    
    def get_element_type_name(uuid: str, filename: str, files: dict[str, any]) -> str:
        own_file = files[filename]
        element_type_name = "Undefined"
        for element in own_file['graphModel']:
            if element["uuid"] == uuid:
                return element["name"].replace(" ", "")
    
        for import_ in own_file['imports']:
            imported_file = files[import_]
            for element in imported_file['graphModel']:
                if element["uuid"] == uuid:
                    return f'{import_}.{element["name"].replace(" ", "")}'
        return element_type_name
    
    
    def streams_to_text(streams: List[Dict]) -> str:
        output = ""
        for stream in streams:
            output += "       " + stream["measurement"] + ": " + stream["streamType"]
            if stream["streamType"] == "fixed":
                output += "(" + str(stream["intervalValue"]) + ")"
            elif stream["streamType"] == "dynamic":
                output += "(" + stream["intervalParameter"] + ")"
            output += "\n"
    
        return output
    
    
    def events_to_text(events: List[Dict]) -> str:
        output = ""
        for event in events:
            output += "   if " + event["element"] + " " + event["trigger"] + " " + str(event["target"]) + ": " + event[
                "severity"] + "(\"" + event["message"] + "\")\n"
        return output
    
    
    def child_components_to_text(child_components: List[Dict[str, str]], filename: str, files: dict[str, any]) -> (str, dict[str, str], dict[str, str]):
        temp_text = ""
        custom_descriptions = {}
        custom_names = {}
        for child_component in child_components:
            if "constant" in child_component and child_component["constant"]:
                temp_text += "        constant "
            elif "internal" in child_component and child_component["internal"]:
                temp_text += "        internal "
            elif "dynamic" in child_component and child_component["dynamic"]:
                temp_text += "        dynamic "
            else:
                temp_text += "        "
            element_name = get_element_type_name(child_component["value"], filename, files)
            temp_text += f'{element_name} {child_component["name"]}'
    
            if "initialValue" in child_component:
                if child_component["initialValue"] != "":
                    temp_text += f' = {child_component["initialValue"]}'
    
            if "description" in child_component:
                custom_descriptions[child_component["name"]] = child_component["description"]
    
            if "customName" in child_component:
                custom_names[child_component["name"]] = child_component["customName"]
    
            temp_text += '\n'
            # if "isImport" in get_element_in_visual_model_by_uuid(child_component["value"], visual_model):
            #     temp_text += get_element_in_visual_model_by_uuid(child_component["value"], visual_model)[
            #                      "isImport"] + "."
            # temp_text += get_element_in_visual_model_by_uuid(child_component["value"], visual_model)["name"].replace(
            #     " ", "") + " " + child_component["name"] + "\n"
            # else:
            #     temp_text += "Undefined " + child_component["name"] + "\n"
        return temp_text, custom_names, custom_descriptions
    
    
    # Converts an element to text
    def element_to_text(element: Dict[str, any], filename: str, files: dict[str, any]) -> str:
        custom_names = {}
        custom_descriptions = {}
        temp_output = element["elementType"] + " " + element["name"].replace(" ", "") + " "
        if element['elementType'] == 'component' and 'baseComponent' in element:
            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"
    
        if element["elementType"] == "interface":
            root_name = get_element_type_name(element["rootComponent"], filename, files)
            return element["elementType"] + " " + root_name + " " + element["name"].replace(" ", "") + " " + "{}\n\n"
    
        if element['elementType'] == 'enum':
            for value in element['values']:
                temp_output += f'   {value.upper()}\n'
    
            return temp_output + "}\n\n"
    
        for key in element:
            if key in ["baseComponent", "uuid", "cardOpen", "elementType", "constant", "semanticDefinition"]:
                continue
            elif key in ["arguments", "returns", "components", "parameters", "functions", "measurements"]:
                if len(element[key]):
                    child_components_text, custom_names_p, custom_descriptions_p = child_components_to_text(element[key], filename, files)
                    temp_output += "   " + key + ":\n" + child_components_text
                    custom_names.update(custom_names_p)
                    custom_descriptions.update(custom_descriptions_p)
            elif key in ["name", "description"]:
                temp_output += "   " + key + ": \"" + str(element[key]) + "\"\n"
            elif key == "events":
                temp_output += "\n" + events_to_text(element[key])
            elif key == "streams":
                if len(element[key]) > 0:
                    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":
                if element["datatype"] != "enum":
                    minimum = '' if element["range"][0] is None else str(element["range"][0])
                    maximum = '' if element["range"][1] is None else str(element["range"][1])
                    temp_output += "   " + key + ": (" + minimum + ',' + maximum + ")\n"
                else:
                    temp_output += "   " + key + ": " + get_element_type_name(element[key][0], filename, files) + "\n"
            elif key == "default" and element['datatype'] == "string":
                temp_output += "   " + key + ": \"" + str(element[key]) + "\"\n"
            elif key == "default" and element['datatype'] == "boolean":
                temp_output += "   " + key + ": " + str(element[key]).lower() + "\n"
            else:
                temp_output += "   " + key + ": " + str(element[key]) + "\n"
        for variable, custom_name in custom_names.items():
            temp_output += "   " + variable + '.name = "' + custom_name + '"\n'
    
        for variable, custom_description in custom_descriptions.items():
            temp_output += "   " + variable + '.description = "' + custom_description + '"\n'
    
        return temp_output + "}\n\n"
    
    
    def translate_files_to_text(files: dict[str, any]) -> dict[str, str]:
        result: dict[str, str] = {}
        for filename in files:
            file = files[filename]
            imports = ""
            for import_ in file['imports']:
                imports += f"import {import_};\n"
    
            elements = ""
            for element in file['graphModel']:
                elements += element_to_text(element, filename, files)
    
            while elements.endswith('\n'):
                elements = elements[:-2]
    
            result[filename] = imports + ('\n' if len(imports) > 0 else '') + elements
        return result