|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# inline_expression_expansion_transformer.py |
| 4 | +# |
| 5 | +# This file is part of NEST. |
| 6 | +# |
| 7 | +# Copyright (C) 2004 The NEST Initiative |
| 8 | +# |
| 9 | +# NEST is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 2 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# NEST is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with NEST. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +from typing import List, Optional, Mapping, Any, Union, Sequence |
| 25 | + |
| 26 | +import re |
| 27 | + |
| 28 | +from pynestml.frontend.frontend_configuration import FrontendConfiguration |
| 29 | +from pynestml.meta_model.ast_inline_expression import ASTInlineExpression |
| 30 | +from pynestml.meta_model.ast_node import ASTNode |
| 31 | +from pynestml.meta_model.ast_ode_equation import ASTOdeEquation |
| 32 | +from pynestml.transformers.transformer import Transformer |
| 33 | +from pynestml.utils.ast_utils import ASTUtils |
| 34 | +from pynestml.utils.logger import Logger, LoggingLevel |
| 35 | +from pynestml.utils.string_utils import removesuffix |
| 36 | +from pynestml.visitors.ast_higher_order_visitor import ASTHigherOrderVisitor |
| 37 | +from pynestml.visitors.ast_parent_visitor import ASTParentVisitor |
| 38 | +from pynestml.visitors.ast_symbol_table_visitor import ASTSymbolTableVisitor |
| 39 | + |
| 40 | + |
| 41 | +class InlineExpressionExpansionTransformer(Transformer): |
| 42 | + r""" |
| 43 | + Make inline expressions self contained, i.e. without any references to other inline expressions. |
| 44 | +
|
| 45 | + Additionally, replace variable symbols referencing inline expressions in defining expressions of ODEs with the corresponding defining expressions from the inline expressions. |
| 46 | + """ |
| 47 | + |
| 48 | + _variable_matching_template = r'(\b)({})(\b)' |
| 49 | + |
| 50 | + def __init__(self, options: Optional[Mapping[str, Any]] = None): |
| 51 | + super(Transformer, self).__init__(options) |
| 52 | + |
| 53 | + def transform(self, models: Union[ASTNode, Sequence[ASTNode]]) -> Union[ASTNode, Sequence[ASTNode]]: |
| 54 | + single = False |
| 55 | + if isinstance(models, ASTNode): |
| 56 | + single = True |
| 57 | + models = [models] |
| 58 | + |
| 59 | + for model in models: |
| 60 | + if not model.get_equations_blocks(): |
| 61 | + continue |
| 62 | + |
| 63 | + for equations_block in model.get_equations_blocks(): |
| 64 | + self.make_inline_expressions_self_contained(equations_block.get_inline_expressions()) |
| 65 | + |
| 66 | + for equations_block in model.get_equations_blocks(): |
| 67 | + self.replace_inline_expressions_through_defining_expressions(equations_block.get_ode_equations(), equations_block.get_inline_expressions()) |
| 68 | + |
| 69 | + if single: |
| 70 | + return models[0] |
| 71 | + |
| 72 | + return models |
| 73 | + |
| 74 | + def make_inline_expressions_self_contained(self, inline_expressions: List[ASTInlineExpression]) -> List[ASTInlineExpression]: |
| 75 | + r""" |
| 76 | + Make inline expressions self contained, i.e. without any references to other inline expressions. |
| 77 | +
|
| 78 | + :param inline_expressions: A sorted list with entries ASTInlineExpression. |
| 79 | + :return: A list with ASTInlineExpressions. Defining expressions don't depend on each other. |
| 80 | + """ |
| 81 | + from pynestml.utils.model_parser import ModelParser |
| 82 | + from pynestml.visitors.ast_symbol_table_visitor import ASTSymbolTableVisitor |
| 83 | + |
| 84 | + for source in inline_expressions: |
| 85 | + source_position = source.get_source_position() |
| 86 | + for target in inline_expressions: |
| 87 | + matcher = re.compile(self._variable_matching_template.format(source.get_variable_name())) |
| 88 | + target_definition = str(target.get_expression()) |
| 89 | + target_definition = re.sub(matcher, "(" + str(source.get_expression()) + ")", target_definition) |
| 90 | + old_parent = target.expression.parent_ |
| 91 | + target.expression = ModelParser.parse_expression(target_definition) |
| 92 | + target.expression.update_scope(source.get_scope()) |
| 93 | + target.expression.parent_ = old_parent |
| 94 | + target.expression.accept(ASTParentVisitor()) |
| 95 | + target.expression.accept(ASTSymbolTableVisitor()) |
| 96 | + |
| 97 | + def log_set_source_position(node): |
| 98 | + if node.get_source_position().is_added_source_position(): |
| 99 | + node.set_source_position(source_position) |
| 100 | + |
| 101 | + target.expression.accept(ASTHigherOrderVisitor(visit_funcs=log_set_source_position)) |
| 102 | + |
| 103 | + return inline_expressions |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def replace_inline_expressions_through_defining_expressions(self, definitions: Sequence[ASTOdeEquation], |
| 107 | + inline_expressions: Sequence[ASTInlineExpression]) -> Sequence[ASTOdeEquation]: |
| 108 | + r""" |
| 109 | + Replace variable symbols referencing inline expressions in defining expressions of ODEs with the corresponding defining expressions from the inline expressions. |
| 110 | +
|
| 111 | + :param definitions: A list of ODE definitions (**updated in-place**). |
| 112 | + :param inline_expressions: A list of inline expression definitions. |
| 113 | + :return: A list of updated ODE definitions (same as the ``definitions`` parameter). |
| 114 | + """ |
| 115 | + from pynestml.utils.model_parser import ModelParser |
| 116 | + from pynestml.visitors.ast_symbol_table_visitor import ASTSymbolTableVisitor |
| 117 | + |
| 118 | + for m in inline_expressions: |
| 119 | + if "mechanism" not in [e.namespace for e in m.get_decorators()]: |
| 120 | + """ |
| 121 | + exclude compartmental mechanism definitions in order to have the |
| 122 | + inline as a barrier inbetween odes that are meant to be solved independently |
| 123 | + """ |
| 124 | + source_position = m.get_source_position() |
| 125 | + for target in definitions: |
| 126 | + matcher = re.compile(self._variable_matching_template.format(m.get_variable_name())) |
| 127 | + target_definition = str(target.get_rhs()) |
| 128 | + target_definition = re.sub(matcher, "(" + str(m.get_expression()) + ")", target_definition) |
| 129 | + old_parent = target.rhs.parent_ |
| 130 | + target.rhs = ModelParser.parse_expression(target_definition) |
| 131 | + target.update_scope(m.get_scope()) |
| 132 | + target.rhs.parent_ = old_parent |
| 133 | + target.rhs.accept(ASTParentVisitor()) |
| 134 | + target.accept(ASTSymbolTableVisitor()) |
| 135 | + |
| 136 | + def log_set_source_position(node): |
| 137 | + if node.get_source_position().is_added_source_position(): |
| 138 | + node.set_source_position(source_position) |
| 139 | + |
| 140 | + target.accept(ASTHigherOrderVisitor(visit_funcs=log_set_source_position)) |
| 141 | + |
| 142 | + return definitions |
0 commit comments