def from_component(
        cls,
        component: etree._Element,
    ) -> "ClassNameAndDimensions":
        className = TypeName(component.attrib["className"])

        def dimension_generator(
        ) -> typing.Iterator[typing.Optional[int]]:
            for dimension in component.xpath('./dimensions/*'):
                try:
                    yield int(dimension.attrib["size"])
                except ValueError:
                    yield None
        dimensions: Dimensions = tuple(dimension_generator())

        # Convert intrinsic types
        if className.last_identifier == VariableName("TypeName"):
            className = TypeName("TypeName")
        elif className.last_identifier == VariableName("VariableName"):
            className = TypeName("VariableName")
        elif className.last_identifier == VariableName("VariableNames"):
            if dimensions:
                raise ValueError(
                    "VariableNames must be scalar"
                )
            className = TypeName("VariableName")
            dimensions = (None,)

        return cls(className, dimensions)
def generate_session_class(
    omc_interface_xml: etree._ElementTree,
) -> Code:
    elements_code = Code()
    code = Code(
        "class OMCSession(",
        CodeWithIndent(
            "OMCSessionBase,",
        ),
        "):",
        CodeWithIndent(
            elements_code
        )
    )

    elements_code.append("OpenModelica = OpenModelica")
    OpenModelica_Scripting, = omc_interface_xml.xpath(
        '//*[@id="OpenModelica.Scripting"]'
    )
    for modelica_class in OpenModelica_Scripting.xpath('./classes/*'):
        if modelica_class.tag == "package":
            continue

        className = TypeName(modelica_class.attrib["id"])
        if is_supported_element(modelica_class):
            elements_code.append(
                f"{className.last_identifier} = {className}"
            )
        else:
            elements_code.append(
                f"# {className.last_identifier} = {className}"
            )

    return code
Exemplo n.º 3
0
    def visit_omc_record_literal(self, node,
                                 children) -> typing.Dict[str, typing.Any]:
        className, _ = children.type_specifier
        record = super().visit_omc_record_literal(node, children)

        if (className == TypeName("OpenModelica.Scripting.SourceInfo")
                and "filename" in record and "fileName" not in record):
            record["fileName"] = record.pop("filename")

        return record
        def valid_component(
            component: etree._Element,
        ) -> bool:
            className, _ = ClassNameAndDimensions.from_component(
                component
            )
            if className in {
                TypeName("Real"),
                TypeName("Integer"),
                TypeName("Boolean"),
                TypeName("String"),
                TypeName("VariableName"),
                TypeName("TypeName"),
            }:
                return True

            class_ = element.xpath(f'//*[@id="{className!s}"]')
            return bool(class_) and is_supported_element(class_[0])
def generate_omc_interface_xml(
    session: OMCSessionBootstrap
) -> etree._ElementTree:

    root = generate_root_element(session)

    OpenModelica = TypeName("OpenModelica")

    classNames = [
        OpenModelica,
        *session.getClassNames(
            OpenModelica/"Scripting",
            recursive=True
        ),
    ]

    for className in tqdm.tqdm(classNames):
        assert(
            className == OpenModelica
            or root.xpath(f'//*[@id="{className.parent!s}"]')
        )
        generate_class_element(session, root, className)

    return etree.ElementTree(root)
Exemplo n.º 6
0
 def visit_type_specifier(self, node, children) -> TypeName:
     name = children.name[0]
     if node[0].value == ".":
         return TypeName(".", *name)
     else:
         return TypeName(*name)
    def to_code(
        self
    ) -> AbstractCode:
        external_code = Code()
        contents_code = Code(external_code, sep=empty_line)
        code = Code(
            self.generate_class_header("ModelicaFunction"),
            CodeWithIndent(
                self.generate___doc__(),
                contents_code,
            )
        )

        inputArguments = [
            InputArgument(
                component_literal=get_component_literal(argument),
                modelica_name=argument.attrib["name"],
                required=(
                    "optional"
                    if argument.attrib["hasDefault"] == "true"
                    else "required"
                ),
            )
            for argument in self.element.xpath(
                './components/arguments/*[@inputOutput="input"]'
            )
        ]

        arguments_code = Code()
        execution_code = Code()
        external_code.extend(
            [
                "@external",
                "def _(",
                CodeWithIndent(arguments_code),
                "):",
                CodeWithIndent(execution_code)
            ]
        )

        arguments_code.append("_cls_,")
        arguments_code.append("_session_: AbstractOMCSession,")
        for argument in sorted(
            inputArguments,
            key=lambda argument: 0 if argument.required == "required" else 1
        ):
            if argument.required == "required":
                s_default = ""
            else:
                s_default = "=None"
            arguments_code.append(
                f"{argument.py_name}{s_default},"
            )

        if self.className.parent == TypeName("OpenModelica.Scripting"):
            funcName = str(self.className.last_identifier)
        else:
            funcName = str(self.className)

        execution_code.extend([
            "return _session_.__omc__.call_function(",
            CodeWithIndent(
                f"funcName={funcName!r},",
                "inputArguments=[",
                CodeWithIndent(
                    *(
                        "("
                        f"{argument.component_literal}, "
                        f"{argument.modelica_name!r}, "
                        f"{argument.py_name}, "
                        f"{argument.required!r}"
                        "),"
                        for argument in inputArguments
                    )
                ),
                "],",
                "outputArguments=[",
                CodeWithIndent(
                    *(
                        "("
                        f"{get_component_literal(argument)}, "
                        f'{argument.attrib["name"]!r}'
                        "),"
                        for argument in self.element.xpath(
                            './components/arguments/*[@inputOutput="output"]'
                        )
                    )
                ),
                "],",
                "parser=parse_OMCValue,",
            ),
            ")",
        ])

        contents_code.extend(self.generate_class_codes())

        if is_supported_element(self.element):
            return code
        else:
            return CommentOut(code)
 def className(
     self
 ) -> TypeName:
     return TypeName(self.element.attrib["id"])