@@ -58,14 +58,43 @@ Due to the complexity of vensim2py, since it has the five functions in which PyS
![Simplified vensim2py module]
In [Classes of pysd grammars] and [Classes of pysd grammars2] are represented the classes associated to the grammars.
![Classes of pysd grammars]
![Classes of pysd grammars2]
The methods of each class are the visitor methods associated with the different grammar rules. There is not a visitor method for each rule, but there is a visitor method associated with a rule that serves to store certain information about the parsed model. Within the visitor method, that relevant information is stored in the attributes of each class, which are then returned as a result of the grammar.
Visitor methods always have three parameters: *self*, *n* and *vc*. *Self* represents the current instance of the class, *n* is of the type Node and is the node being visited, and *vc* or *visit children* is a list of all the results from the child nodes of that expression being parsed. From that last parameter, vc, the information is taken and saved in the attributes of the classes.
The **functions** module is represented in [Functions module]. It is one of the most important modules in PySD, since it has the classes that will instantiate the Python translation model and also has the necessary logic to execute the simulation. In that diagram, the classes it has and the relationships between them are represented.
![Simplified functions module]
The **functions** module with details is found in the diagram of [Functions module] as well as the **Time** class that is define in this module. In **functions**, we can find many functions that are used in Vensim but with the pertinent logic in Python, for example: PULSE, IF THEN ELSE, RANDOM UNIFORM, etc.
The **Time** class represents the time throughout the simulation. With the *t* attribute represents the current time, which is modified according as the simulation moves, and the *step* attribute the time increment that occurs in each iteration.
![Functions module]
![Functions module]
In the diagram [Functions module2] the classes of the functions module **Stateful**, **Integ**, **Macro** and **Model** are represented in details. The **Stateful** class is one of the most relevant classes of that module, since, except Time, all of the classes inherit from it. This class allows to represents the state evolution of a certain element models, recreating the simulation process in Vensim. To do this, it has an attributed called *state* that simulates the state of the elements and changes its value in each iteration of the simulation.
<!--Cambiar nombres-->
![Functions module2]
The **Integ** class simulates the Vensim stocks. It receives and stores an initial value and has the function from which the derivative necessary to carry out the integration is obtained.
The Model class stores all the information about the main code of the model (translated). An instance of this class is called a pysd model, it is the Python language representation of the Vensim file. That is, the Model class implements a representation of the model's stateful elements and has most of the methods for being able to access and modify the model's components. In addition, the Model class is in charge of instantiating time as a function of the model variables and is also in charge of carrying out the simulation through Euler integration.
The **initialize** function of that class initialize the model simulation. The **run** function allows to simulate the behaviour of the model through increasing the steps. And the **\_euler\_step** function allows to do the Euler integration in a single step, using the state of the Stateful elements and updating it.
The **Model** class inherits from Macro class. The logic for rendering Vensim macros is implemented in Macro class. This class gets the stateful objects which had been created in translation process and they are initialized to later obtain their derivates and the results of the execution. Model does the same functions of Macro, but Model is the root model object so it has more methods to facilitate execution.
Next, in [Builder module] figures the **builder** module. There is no class defined in this module, but it is charge in making the text model in Python, using the results obtained in the translation. It has the necessary code to assemble in a model pysd all the elements of both Vensim or XMILE and make, from these, a version compatible with Python.
![Builder module]
The main function of the builder module is **build**. That function builds and writes the Python representation of the model. It is called from vensim2py module after finishing the entire translation process of the Vensim model. As parameters are passed the different elements of the model that have been parsed, subscripts, namespace and the name of the file where the result of the representation in Python must be written. This function has certain lines of permanent code that always write in the created models, but then, there are certain lines of code that are completed with the translation generated before in the vensim2py model.
In image [Utils module] is found the utils module. The main purpose of utils is to join in a single module all the functions with great utility for the proyect. Many of these functions are used many times throughout the translation processs. So, as we already presented in [PySD relationships between modules], this module is used by builder, functions, external and vensim2py modules. In turn, the accesible names of the decorators, external and functions modules are imported into the utils modules to define a list with the names that hace already been used and that have a particular meaning in the model that is being translated.
![Utils module]
![Simplified external module]
![External module]
...
...
@@ -102,7 +131,7 @@ Two main scenarios can be distinguished throughout the
[Classes of pysd grammars2]:/images/logical-view/grammar2.png"Classes of pysd grammars"