|
| 1 | +# FILE: mcg_cc_component_converter.py |
| 2 | +# |
| 3 | +# DESCRIPTION: |
| 4 | +# This module contains definition of ComponentConverter class, which is child |
| 5 | +# class of Converter class and is responsible for conversion of component content |
| 6 | +# into configuration file. |
| 7 | +# |
| 8 | +# COPYRIGHT: Copyright (C) 2021-2022 Kamil Deć github.com/deckamil |
| 9 | +# DATE: 20 FEB 2022 |
| 10 | +# |
| 11 | +# LICENSE: |
| 12 | +# This file is part of Mod Code Generator (MCG). |
| 13 | +# |
| 14 | +# MCG is free software: you can redistribute it and/or modify |
| 15 | +# it under the terms of the GNU General Public License as published by |
| 16 | +# the Free Software Foundation, either version 3 of the License, or |
| 17 | +# (at your option) any later version. |
| 18 | +# |
| 19 | +# MCG is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# Under Section 7 of GPL version 3, you are granted additional |
| 25 | +# permissions described in the MCG Output Exception, version 1, which |
| 26 | +# copy you should have received along with this program. |
| 27 | +# |
| 28 | +# You should have received a copy of the GNU General Public License |
| 29 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 30 | + |
| 31 | + |
| 32 | +from mcg_cc_converter import Converter |
| 33 | +from mcg_cc_logger import Logger |
| 34 | + |
| 35 | + |
| 36 | +# Class: |
| 37 | +# ComponentConverter() |
| 38 | +# |
| 39 | +# Description: |
| 40 | +# This is child class responsible for converting of component content into configuration file. |
| 41 | +class ComponentConverter(Converter): |
| 42 | + |
| 43 | + # Method: |
| 44 | + # convert_action_interaction() |
| 45 | + # |
| 46 | + # Description: |
| 47 | + # This method is responsible for conversion of action interaction into configuration file. |
| 48 | + # |
| 49 | + # Returns: |
| 50 | + # This method does not return anything. |
| 51 | + def convert_action_interaction(self, sorted_node, math_symbol): |
| 52 | + |
| 53 | + # append interaction comment to configuration file |
| 54 | + self.configuration_file.append(str("COM Action Interaction ") + str(sorted_node.node_interaction)) |
| 55 | + # append beginning of action interaction to conversion line |
| 56 | + conversion_line = str("INS ") + str(sorted_node.node_output) + str(" = ") |
| 57 | + |
| 58 | + # search for all input signal names within sorted node and put them into conversion line |
| 59 | + for i in range(0, len(sorted_node.node_input_list)): |
| 60 | + # find input signal name within sorted node |
| 61 | + node_input = sorted_node.node_input_list[i] |
| 62 | + # append input signal name to conversion line |
| 63 | + conversion_line = conversion_line + str(node_input) |
| 64 | + # if sorted node processing is not completed |
| 65 | + if i < len(sorted_node.node_input_list) - 1: |
| 66 | + # append math symbol to conversion line |
| 67 | + conversion_line = conversion_line + str(" ") + str(math_symbol) + str(" ") |
| 68 | + |
| 69 | + # append conversion line to configuration file |
| 70 | + self.configuration_file.append(conversion_line) |
| 71 | + |
| 72 | + # Method: |
| 73 | + # convert_signal_assignment() |
| 74 | + # |
| 75 | + # Description: |
| 76 | + # This method is responsible for conversion of signal assignment into configuration file. |
| 77 | + # |
| 78 | + # Returns: |
| 79 | + # This method does not return anything. |
| 80 | + def convert_signal_assignment(self, sorted_node): |
| 81 | + |
| 82 | + # find output signal name within sorted node |
| 83 | + node_output = sorted_node.node_output |
| 84 | + # find input signal name within sorted node |
| 85 | + node_input = sorted_node.node_input_list[0] |
| 86 | + |
| 87 | + # append input and output signal to conversion line |
| 88 | + conversion_line = str("INS ") + str(node_output) + str(" = ") + str(node_input) |
| 89 | + |
| 90 | + # append conversion line to configuration file |
| 91 | + self.configuration_file.append(conversion_line) |
| 92 | + |
| 93 | + # Method: |
| 94 | + # convert_component() |
| 95 | + # |
| 96 | + # Description: |
| 97 | + # This method is responsible for converting of component content into configuration file. |
| 98 | + # |
| 99 | + # Returns: |
| 100 | + # This method does not return anything. |
| 101 | + def convert_component(self): |
| 102 | + |
| 103 | + # component converter |
| 104 | + Logger.save_in_log_file(">>>>>>>>>>>>>>>>>>>>>>>>>>>> COMPONENT CONVERTER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n") |
| 105 | + |
| 106 | + # convert header |
| 107 | + Logger.save_in_log_file("*** convert header") |
| 108 | + |
| 109 | + # append start marker of new component section to configuration file |
| 110 | + self.configuration_file.append(str("COMPONENT START")) |
| 111 | + |
| 112 | + # append file name to configuration file |
| 113 | + self.configuration_file.append(str("COMPONENT SOURCE ") + str(self.activity_source)) |
| 114 | + |
| 115 | + # append component name to configuration file |
| 116 | + self.configuration_file.append(str("COMPONENT NAME ") + str(self.model_element_name)) |
| 117 | + |
| 118 | + # convert interface details to configuration file |
| 119 | + self.convert_interfaces() |
| 120 | + |
| 121 | + # convert body |
| 122 | + Logger.save_in_log_file("*** convert body") |
| 123 | + |
| 124 | + # append start marker of function body section to configuration file |
| 125 | + self.configuration_file.append(str("BODY START")) |
| 126 | + |
| 127 | + # repeat for all nodes from sorted node list |
| 128 | + for sorted_node in self.sorted_node_list: |
| 129 | + |
| 130 | + # if sorted node contains ADD action |
| 131 | + if "ADD " in sorted_node.node_interaction: |
| 132 | + # convert ADD action |
| 133 | + self.convert_action_interaction(sorted_node, "+") |
| 134 | + |
| 135 | + # if sorted node contains SUB action |
| 136 | + if "SUB " in sorted_node.node_interaction: |
| 137 | + # convert SUB action |
| 138 | + self.convert_action_interaction(sorted_node, "-") |
| 139 | + |
| 140 | + # if sorted node contains MUL action |
| 141 | + if "MUL " in sorted_node.node_interaction: |
| 142 | + # convert MUL action |
| 143 | + self.convert_action_interaction(sorted_node, "*") |
| 144 | + |
| 145 | + # if sorted node contains DIV action |
| 146 | + if "DIV " in sorted_node.node_interaction: |
| 147 | + # convert DIV action |
| 148 | + self.convert_action_interaction(sorted_node, "/") |
| 149 | + |
| 150 | + # if sorted node contains ASSIGNMENT action |
| 151 | + if "ASSIGNMENT" in sorted_node.node_interaction: |
| 152 | + # convert ASSIGNMENT action |
| 153 | + self.convert_signal_assignment(sorted_node) |
| 154 | + |
| 155 | + # append end marker of function body section to configuration file |
| 156 | + self.configuration_file.append(str("BODY END")) |
| 157 | + |
| 158 | + # append end marker of new component section to configuration file |
| 159 | + self.configuration_file.append(str("COMPONENT END")) |
| 160 | + |
| 161 | + # save configuration file |
| 162 | + self.save_in_configuration_file() |
| 163 | + |
| 164 | + # process completed |
| 165 | + Logger.save_in_log_file("PROCESS COMPLETED") |
| 166 | + |
| 167 | + # display additional details after component conversion |
| 168 | + Logger.save_in_log_file("") |
| 169 | + Logger.save_in_log_file("Configuration File:") |
| 170 | + for line in self.configuration_file: |
| 171 | + Logger.save_in_log_file(" " + str(line)) |
| 172 | + |
| 173 | + # end of component converter |
| 174 | + Logger.save_in_log_file("\n>>>>>>>>>>>>>>>>>>>>>>>> END OF COMPONENT CONVERTER <<<<<<<<<<<<<<<<<<<<<<<<<<<") |
0 commit comments