\section{Generator}

The functionality of generator can be divided into following parts:

\begin{enumerate}
\item Create the DistributionFactory
\item Use the ComputationalModelParser to read input files
\item Link the objects
\item Generate the data structures and variable definitions
\item Generate the setting of initial values, reading of data and spatial matrix
\item Generate the printing of variables
\item Generate the proposal distribution module
\item Generate the main module
\item Generate the acceptance formulas for variables
\end{enumerate}

With "generate" we mean generating and writing Fortran code using FortranWriter class.

Each part in more detail.

1. Create the DistributionFactory

A DistributionFactory object is created. The parser needs the object to determine which proposal distribution to set to a variable. DistributionFactory's constructor reads 
the user defined distributions file and constructs an object for each user-defined distribution.

2. Use the ComputationalModelParser to read input files

The model file, which defined variables and structures, is read first one line at a time. If the line describes a structure, an Entity object will be created. A structure definition ends with '\}'. Name, file (if any), spatial matrix and some other data are set when an Entity object is constructed. The object will then be added to a collection. A variable can be global, or it can belong to an entity. Variables are created the same way as are the entities. The Parser processes the formula of each variable, creating either a Distribution or an Equation, accordingly.

Simulation parameters are read next. Global variables thinning, burn-in, etc are set. Proposal distributions are set to each variable. The parses uses DistributionFactory to determine the right distribution. A new Distribution is then created and added to the variable in question.

Lastly the data files are processed. All missing values are replaced with '0' and the program checks that each missing value has a value set in the initial values file. The Parser also writes a missing value matrix for each data file. The lengths of data files are saved in the related Entity objects. With the information gathered, the Parser creates a ComputationalModel instance and passes it to the Generator. 

3. Link the objects

See \ref{Linking}.

4. Generate the data structures and variable definitions

The generateDefinitions() method is called. The Entity and Variable objects are examined and the data structures needed are coded into a Fortran module. 

5. Generate the setting of initial values, reading of data and the spatial matrix

The generateInput() method is called. This method calls generateSetInitialValues(), generateReadData(), generateSetSpatial() which read the initial values, data and spatial 
matrices.

6. Generate the printing of variables

The generateOutput() method is called. This method calls generateWriteSummary(), and generateWriteOutput() methods. These methods generate the needed Fortran code for printing 
of variables.

7. Generate the proposal distribution module

The generateProposal() method is called. The method generates the proposal distribution module.

8. Generate the main module

The generateMain() method is called. The method generates the subroutines in the main program, the initialization of data structures and calls to other modules.

9. Generate the acceptance formulas for variables

This phase is done in the main module generation. The generateMain() method calls either generateUpdateAll() or generateUpdateOne() depending on the update strategy used. The methods code the update of variables. Both methods call the generateAcceptanceFormula() method, which generates the acceptance formula for a variable.


\subsection{Operations of Generator}


\begin{tabular}[]{| p{5.5cm} | l | p{7.5cm} |}
\hline
\textbf{Operation}&\textbf{Return type}&\textbf{Description} \\
\hline
generate() & void & The method doesn't generate any code itself. It calls other generate methods.\\
\hline
generateDefinitions() & void & This method generates the Fortran module Definitions and writes it into a source code file.\\
\hline
generateInput() & void & This method generates the Fortran module Input and writes it into a source code file.\\
\hline
generateOutput() & void & This method generates the Fortran module Output and writes it into a source code file. It calls the generateWriteSummary() and generateWriteOutput() methods.\\
\hline
generateProposal() & void & This method generates the Fortran module Proposal and writes it into a source code file.\\
\hline
generateMain() & void & This method generates the Fortran module Main and writes it into a source code file.\\
\hline
generateSetInitialValues() & String[] & The method generates the subroutine set\_initial\_values. \\
\hline
generateReadData() & String[] & The method generates the subroutine read\_data. \\
\hline
generateSetSpatial() & String[] & The method generates the subroutine set\_spatial. \\
\hline
generateWriteSummary() & String[] & The method generates the subroutine write\_summary. \\
\hline
generateWriteOutput() & String[] & The method generates the subroutine write\_output. \\
\hline
generateUpdateAll() & String[] & The method generates the subroutine update\_all.\\
\hline
generateUpdateOne() & String[] & The method generates the subroutine update\_one.\\
\hline
generateAcceptanceFormula (Variable variable) & String[] & The method generates and returns the acceptance formula for a given Variable object.\\
\hline
\end{tabular}


