How to use the pymoca.tree.TreeListener function in pymoca

To help you get started, we’ve selected a few pymoca examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pymoca / pymoca / src / pymoca / backends / casadi / generator.py View on Github external
"""TreeWalker that skips processing of annotations"""

    def skip_child(self, tree: ast.Node, child_name: str) -> bool:
        skip = super().skip_child(tree, child_name)
        if isinstance(tree, ast.Class) and child_name == "annotation":
            return True
        return skip

    def order_keys(self, keys: Iterable[str]):
        # Symbols must come before classes, as we need to access symbol values when creating
        # CasADi interpolant functions.
        return sorted(keys, key=lambda attr: 0 if attr == 'symbols' else 1)


# noinspection PyPep8Naming,PyUnresolvedReferences
class Generator(TreeListener):
    def __init__(self, root: ast.Tree, class_name: str, options: Dict[str, bool]):
        super(Generator, self).__init__()
        self.src = {}
        self.model = Model()
        self.root = root
        c = self.root.classes[class_name]
        self.nodes = {c: {'time': self.model.time}}
        self.derivative = {}
        self.for_loops = deque()
        self.functions = {}
        self.entered_classes = deque()
        self.map_mode = 'inline' if options['unroll_loops'] else 'serial'
        self.function_mode = (True, False) if options['inline_functions'] else (False, True)
        self.delay_counter = 0

        # NOTE: Part of MTensor workaround.
github pymoca / pymoca / src / pymoca / backends / xml / generator.py View on Github external
import copy
import os

from lxml import etree, objectify

from pymoca import ast
from pymoca.tree import TreeListener, TreeWalker, flatten

FILE_DIR = os.path.dirname(os.path.realpath(__file__))
# noinspection PyUnresolvedReferences
BUILTINS = dir(__builtins__) + ['psi']

E = objectify.E


class XmlGenerator(TreeListener):

    def __init__(self):
        super().__init__()
        self.xml = {}

    def exitEquation(self, tree: ast.Equation):
        self.xml[tree] = E(
            'equal',
            self.xml[tree.left],
            self.xml[tree.right],
        )

    def exitExpression(self, tree: ast.Expression):
        if isinstance(tree.operator, ast.ComponentRef):
            op_name = tree.operator.name
        else:
github pymoca / pymoca / src / pymoca / backends / sympy / generator.py View on Github external
import copy
import os
from typing import List

import jinja2

from pymoca import ast
from pymoca.tree import TreeListener, TreeWalker, flatten

FILE_DIR = os.path.dirname(os.path.realpath(__file__))
# noinspection PyUnresolvedReferences
BUILTINS = dir(__builtins__) + ['psi']


class SympyGenerator(TreeListener):
    def __init__(self):
        super(SympyGenerator, self).__init__()
        self.src = {}

    def exitTree(self, tree: ast.Tree):
        d = {'classes': []}
        for key in sorted(tree.classes.keys()):
            d['classes'] += [self.src[tree.classes[key]]]

        template = jinja2.Template('''
# do not edit, generated by pymoca

from __future__ import print_function, division
import sympy
import sympy.physics.mechanics as mech
from pymoca.backends.sympy.runtime import OdeModel