\subsection{Linking}
\label{Linking}

When the Parser has read the model, it is necessary to link the variables to each other.

The attributes set in this phase are:
\begin{itemize}
\item In \textit{Entity} objects: the \textit{XCoordinate} and \textit{YCoordinate} 
\item In \textit{Variable} objects: the list \textit{depends} includes the variables the current variable depends on, that is, the variables that affect the current variable
\item In \textit{Variable} objects: the list \textit{affects} includes the variables the current variable affects, that is, the variables that depend on the current variable
\item In \textit{Distribution} objects: the reference parameters of the distribution, that is, the array \textit{variableParameter}
\item In \textit{Equation} objects: the parameters of the equation, that is, the array \textit{parameters}.
\end{itemize}

Note: We assume that the Parser creates the (unlinked) \textit{Distribution} objects for all the stochastic variables. The objects store the names of its parameters as \textit{Strings}. Similarly we assume that the Parser creates the (unlinked) \textit{Equation} objects for all the functional parameters. The objects store as \textit{Strings} the names of variables appearing in the equation.

The idea of having this kind of information is that when generating the acceptance probability calculation for a parameter, the \textit{affects} and \textit{depends} lists define which variables are part of the probability calculation formula. By using the \textit{Distribution} object we obtain parameters needed when the distribution is used in the acceptance probability formula.

When the variable is stochastic, the \textit{Distribution} object already has information of the variables which affect the current variable, so in that case the \textit{depends} contains redundant information but it is used anyway. When the variable is functional, the \textit{Equation} objects contains the same information.

%Note also that a variable is considered to affect another variable not only when there is a direct connection but also when there is a functional parameter or several functional parameters between them. 

The Parser has created the \textit{Variable} and \textit{Entity} instances which correspond to the variables in the model. In this phase no objects need to be created.

\subsubsection{Linking the Entity objects}

The linking of the Entity objects is done in the Parser.

\begin{itemize}
\item Put all \textit{Entity} objects into a \textit{HashMap}. The key of an \textit{Entity} is its \textit{name}.
\item Iterate the entityList (the data structure of the model where all the Entities are) to find the entities of the model
\item For each \textit{Entity} found:
\begin{itemize} 
\item If the entity doesn't describe a matrix, do nothing.
\item Otherwise check the attributes \textit{XCoordinateString} and \textit{YCoordinateString} that define the corresponding parent entities (x-coordinate and y-coordinate entities), find the corresponding \textit{Entity} objects from the \textit{HashMap}, and link them to attributes \textit{XCoordinate} and \textit{YCoordinate}.
\end{itemize}
\end{itemize}

\subsubsection{Linking the Variable, Distribution and Equation objects}

A \textit{Distribution} object has its parameters as \textit{Strings}. An \textit{Equation} object has the variable names appearing in it as \textit{Strings}.

\begin{itemize}
\item Put all \textit{Variable} objects into a \textit{HashMap}. The key of a variable is its \textit{name}.
\item Iterate the \textit{VariableList} in \textit{ComputationalModel} and the \textit{VariableList} objects in all the \textit{Entities} to find all the variables of the model. For each one the attribute \textit{functional} tells whether the variable is stochastic or functional.
\item For each stochastic variable x:
	\begin{itemize}
	\item Get the distribution of the variable as a \textit{String}
	\item For each variable name ``y'' that occurs in the distribution:
	\item Use the \textit{HashMap} to find out the corresponding \textit{Variable} object y
	\item Add y to the distribution parameter data structure
	\item Add x to y's affects-list
	\item Add y to x's depends-list
	\end{itemize}

\item For each functional parameter x:
	\begin{itemize}
	\item Get the equation of the parameter
	\item For each variable name ``y'' that occurs in the equation:
	\item Use the \textit{HashMap} to find out the corresponding \textit{Variable} object y
	\item Add y to the equation variables data structure
	\item Add x to y's affects-list
	\item Add y to x's depends-list
	\end{itemize}
\end{itemize}


 