upadtes
[phd-thesis.git] / top / int.tex
index 1ef5860..93a161e 100644 (file)
@@ -9,43 +9,48 @@
 \chapter{Integration with \texorpdfstring{\gls{ITASK}}{iTask}}%
 \label{chp:integration_with_itask}
 \begin{chapterabstract}
-       \noindent This chapter shows the integration of \gls{MTASK} with \gls{ITASK} by showing:
+       This chapter shows the integration of \gls{MTASK} with \gls{ITASK} by showing:
        \begin{itemize}
                \item an architectural overview \gls{MTASK} applications;
                \item on the interface for connecting devices;
                \item the interface for lifting \gls{MTASK} tasks to \gls{ITASK} tasks;
-               \item a interface for lifting \gls{ITASK} \glspl{SDS} to \gls{MTASK} \glspl{SDS};
+               \item a interface for lowering \gls{ITASK} \glspl{SDS} to \gls{MTASK} \glspl{SDS};
                \item and finishes with non-trivial home automation example application using all integration mechanisms;
        \end{itemize}
 \end{chapterabstract}
 
-The \gls{MTASK} language is a multi-view \gls{DSL}, i.e.\ there are multiple interpretations possible for a single \gls{MTASK} term.
-Using the byte code compiler (\cleaninline{BCInterpret}) \gls{DSL} interpretation, \gls{MTASK} tasks can be fully integrated in \gls{ITASK}.
-They are executed as if they are regular \gls{ITASK} tasks and they communicate may access \glspl{SDS} from \gls{ITASK} as well.
-\Gls{MTASK} devices contain a domain-specific \gls{OS} and are little \gls{TOP} engines in their own respect, being able to execute tasks.
+The \gls{MTASK} language is a multi-view \gls{DSL}, there are multiple interpretations possible for a single \gls{MTASK} term.
+Using the byte code compiler (\cleaninline{BCInterpret}) \gls{DSL} interpretation, \gls{MTASK} tasks are fully integrated in \gls{ITASK}.
+They are executed as if they are regular \gls{ITASK} tasks and they can access \glspl{SDS} from \gls{ITASK}.
+Devices in the \gls{MTASK} system contain a domain-specific \gls{OS} and are little \gls{TOP} engines in their own respect, being able to execute tasks.
+
 \Cref{fig:mtask_integration} shows the architectural layout of a typical \gls{IOT} system created with \gls{ITASK} and \gls{MTASK}.
 The entire system is written as a single \gls{CLEAN} specification where multiple tasks are executed at the same time.
 Tasks can access \glspl{SDS} according to many-to-many communication and multiple clients can work on the same task.
+The diagram contains three labelled arrows that denote the integration functions between \gls{ITASK} and \gls{MTASK}.
 Devices are integrated into the system using the \cleaninline{withDevice} function (see \cref{sec:withdevice}).
 Using \cleaninline{liftmTask}, \gls{MTASK} tasks are lifted to a device (see \cref{sec:liftmtask}).
-\Gls{ITASK} \glspl{SDS} are lifted to the \gls{MTASK} device using \cleaninline{liftsds} (see \cref{sec:liftsds}).
+\glspl{SDS} from \gls{ITASK} are lowered to the \gls{MTASK} device using \cleaninline{lowerSds} (see \cref{sec:liftsds}).
 
-\begin{figure}[ht]
+\begin{figure}
        \centering
        \includestandalone{mtask_integration}
-       \caption{\Gls{MTASK}'s integration with \gls{ITASK}.}%
+\caption{An architectural view of an \imtask{} applications.}%
        \label{fig:mtask_integration}
 \end{figure}
 
 \section{Connecting edge devices}\label{sec:withdevice}
-When interpreted by the byte code compiler view, an \gls{MTASK} task produces a compiler.
-This compiler is exceuted at run time so that the resulting byte code can be sent to an edge device.
-All communication with this device happens through a so-called \emph{channels} \gls{SDS}.
+Edge devices in an \gls{MTASK} application are always coordinated by a server.
+This means that they wait for a server to connect to them and send work.
+The heavy lifting of connecting an \gls{MTASK} device to an \gls{ITASK} server is done with the \cleaninline{withDevice} function.
+This function is given a communication specification and a function producing an \gls{ITASK} task that does something with an \gls{MTASK} device, e.g.\ lift tasks.
+By using \gls{HOAS} like this, setting up and tearing down the connection to the device is fully controlled.
+
+In the implementation of the function, all communication with a device happens through a so-called \emph{channels} \gls{SDS}.
 The channels contain three fields, a queue of messages that are received, a queue of messages to send and a stop flag.
 Every communication method that implements the \cleaninline{channelSync} class can provide the communication with an \gls{MTASK} device.
 As of now, serial port communication, direct \gls{TCP} communication and \gls{MQTT} over \gls{TCP} are supported as communication providers (see \cref{lst:connection_types}).
-The \cleaninline{withDevice} function transforms such a communication provider and a task that does something with this device to an \gls{ITASK} task.
-Internally, the task sets up the communication, exchanges specifications with the device, executes the inner task while handling errors, and finally cleans up after closing.
+Internally, the \cleaninline{withDevice} task sets up the communication, exchanges specifications with the device, executes the inner task while handling errors, and finally cleans up after closing.
 \Cref{lst:mtask_device} shows the types and interface to connecting devices.
 
 \begin{lstClean}[label={lst:mtask_device},caption={Device communication interface in \gls{MTASK}.}]
@@ -63,7 +68,7 @@ withDevice :: a (MTDevice -> Task b)
 The \cleaninline{MTDevice} abstract type is internally represented as three \gls{ITASK} \gls{SDS} that contain all the current information about the tasks.
 The first \gls{SDS} is the information about the \gls{RTS} of the device, i.e.\ metadata on the tasks that are executing, the hardware specification and capabilities, and a list of fresh task identifiers.
 The second \gls{SDS} is a map storing downstream \gls{SDS} updates.
-When a lifted \gls{SDS} is updated on the device, a message is sent to the server.
+When a lowered \gls{SDS} is updated on the device, a message is sent to the server.
 This message is initially queued in the map to allow for asynchronous handling of multiple updates.
 Finally, the \cleaninline{MTDevices} type contains the communication channels.
 
@@ -74,7 +79,7 @@ Then, it performs the following four tasks in parallel to monitor the edge devic
                Errors that occur here are converted to the proper \gls{MTASK} exception.
        \item Watches the channels for the shutdown flag.
                If the connection is lost with the device unexpectedly, an \gls{MTASK} exception is thrown.
-       \item Watches the channels for messages and processes them accordingly by changing the device information \gls{SDS} or adding the lifted \gls{SDS} updates to the corresponding \gls{SDS} update queue.
+       \item Watches the channels for messages and processes them accordingly by changing the device information \gls{SDS} or adding the lowered \gls{SDS} updates to the corresponding \gls{SDS} update queue.
        \item Sends a request for a specification. Once the specification is received, the device task is run.
                The task value of this device task is then used as the task value of the \cleaninline{withDevice} task.
 \end{enumerate}
@@ -94,10 +99,11 @@ withDevice spec deviceTask =
 \end{lstClean}
 
 If at any stage an unrecoverable device error occurs, an \gls{ITASK} exception is thrown on the \cleaninline{withDevice} task.
-This exception can be caught in order to device some kind of fail-safe mechanism.
-For example, when a device fails, the tasks can be sent to another device as can be seen in \cref{lst:failover}.
-This function executes an \gls{MTASK} task on a pool of devices.
-If an error occurs during execution, the next device in the pool is tried until the pool is exhausted
+This exception can be caught in order to devise fail-safe mechanisms.
+For example, if a device fails, the task can be sent to another device as can be seen in \cref{lst:failover}.
+This function executes an \gls{MTASK} task on a pool of devices connected through \gls{TCP}.
+If a device error occurs during execution, the next device in the pool is tried until the pool is exhausted
+If another error occurs, it is rethrown for a parent task to catch.
 
 \begin{lstClean}[caption={An \gls{MTASK} failover combinator.},label={lst:failover}]
 failover :: [TCPSettings] (Main (MTask BCInterpret a)) -> Task a
@@ -108,9 +114,9 @@ where except MTEUnexpectedDisconnect = failover ds mtask
 \end{lstClean}
 
 \section{Lifting \texorpdfstring{\gls{MTASK}}{mTask} tasks}\label{sec:liftmtask}
-Once the connection with the device is established, \gls{MTASK} tasks can be lifted to \gls{MTASK} tasks using the \cleaninline{liftmTask} family of functions (see \cref{lst:liftmtask}).
+Once the connection with the device is established, \gls{MTASK} tasks are lifted to \gls{MTASK} tasks using the \cleaninline{liftmTask} family of functions (see \cref{lst:liftmtask}).
 Given an \gls{MTASK} task in the \cleaninline{BCInterpret} view and a device obtained from \cleaninline{withDevice}, an \gls{ITASK} task is returned.
-This \gls{ITASK} task tethers the \gls{MTASK} task that is executed on the microcontroller.
+This \gls{ITASK} task proxies the \gls{MTASK} task that is executed on the microcontroller.
 Hence, when for example observing the task value, the actual task value from the microcontroller is observed.
 
 \begin{lstClean}[label={lst:liftmtask},caption={The interface for lifting \gls{MTASK} tasks to \gls{ITASK} tasks.}]
@@ -123,8 +129,8 @@ The first argument is the task and the second argument is the device which is ju
 First a fresh identifier for the task is generated using the device state.
 With this identifier, the cleanup hook can be installed.
 This is done to assure the task is removed from the edge device if the \gls{ITASK} task coordinating it is destroyed.
-Tasks can be destroyed when for example a task executed in parallel and the parallel combinator terminates or when the condition to step holds in a sequential task combination.
-Then the \gls{MTASK} compiler is invoked, its only argument besides the task is a function doing something with the results of the compilation, i.e.\ the lifted \glspl{SDS} and the messages containing the compiled and serialised task.
+Tasks in \gls{ITASK} are destroyed when for example it is executed in parallel with another task and the parallel combinator terminates or when the condition to step holds in a sequential task combination.
+Then the \gls{MTASK} compiler is invoked, its only argument besides the task is a function doing something with the results of the compilation, i.e.\ the lowered \glspl{SDS} and the messages containing the compiled and serialised task.
 With the result of the compilation, the task can be executed.
 First the messages are put in the channels, sending them to the device.
 Then, in parallel:
@@ -145,39 +151,53 @@ liftmTask task (MTDevice dev sdsupdates channels)
                        -|| watchSharesUpstream mrefs channels tid)
 \end{lstClean}
 
-\todo{dis\-cuss pre\-loading}
+Sending the complete byte code to the device is not always a suitable option.
+For example, when the device is connected through an unstable or slow connection, sending the entire byte code may induce lots of delay.
+To mitigate this problem, \gls{MTASK} tasks can be preloaded on a device.
+Preloading means that the task is compiled and integrated into the device firmware.
+On receiving a \cleaninline{TaskPrep}, a hashed value of the task to be sent is included.
+The device then checks the preloaded task registry and uses the local version if the hash matches.
+Of course this only works for tasks that are not tailor made for the current work specification and not depend on run time information.
+The interface for task preloading can be found in \cref{lst:preload}.
+Given an \gls{MTASK} task, a header file is created that is placed in the source code directory of the \gls{RTS} to include it in the firmware.
+
+\begin{lstClean}[label={lst:preload},caption={Preloading tasks in \gls{MTASK}.}]
+preloadTask :: (Main (MTask BCInterpret u)) -> Task String
+\end{lstClean}
 
-\section{Lifting \texorpdfstring{\gls{ITASK}}{iTask} \texorpdfstring{\glsxtrlongpl{SDS}}{shared data sources}}\label{sec:liftsds}
-Lifting \gls{ITASK} \glspl{SDS} to \gls{MTASK} \glspl{SDS} is something that mostly happens at the compiler level using the \cleaninline{liftsds} function (see \cref{lst:mtask_itasksds}).
+\section{Lowering \texorpdfstring{\gls{ITASK}}{iTask} \texorpdfstring{\glsxtrlongpl{SDS}}{shared data sources}}\label{sec:liftsds}
+Lowering \gls{ITASK} \glspl{SDS} to \gls{MTASK} \glspl{SDS} is something that mostly happens at the compiler level using the \cleaninline{lowerSds} function (see \cref{lst:mtask_itasksds}).
 \Glspl{SDS} in \gls{MTASK} must always have an initial value.
-For regular \gls{SDS} this value is given in the source code, for lifted \gls{ITASK} \glspl{SDS} this value is obtained by reading the values once just before sending the task to the edge device.
-On the device itself, there is just one difference between lifted \glspl{SDS} and regular \glspl{SDS}: after changing \pgls{SDS}, a message is sent to the server containing this new value.
+For regular \gls{SDS} this value is given in the source code, for lowered \gls{ITASK} \glspl{SDS} this value is obtained by reading the values once just before sending the task to the edge device.
+On the device itself, there is just one difference between lowered \glspl{SDS} and regular \glspl{SDS}: after changing \pgls{SDS}, a message is sent to the server containing this new value.
 The \cleaninline{withDevice} task (see \cref{sec:withdevice}) receives and processes this message by writing to the \gls{ITASK} \gls{SDS}.
 Tasks watching this \gls{SDS} get notified then through the normal notification mechanism of \gls{ITASK}.
 
-\begin{lstClean}[label={lst:mtask_itasksds},caption={Lifted \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
-class liftsds v where
-       liftsds :: ((v (Sds t)) -> In (Shared sds t) (Main (MTask v u)))
+\begin{lstClean}[label={lst:mtask_itasksds},caption={Lowered \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
+class lowerSds v where
+       lowerSds :: ((v (Sds t)) -> In (Shared sds t) (Main (MTask v u)))
                -> Main (MTask v u) | RWShared sds
 \end{lstClean}
 
-As an example, \cref{lst:mtask_liftsds_ex} shows a lightswitch function producing an \imtask{} task.
-Given an \cleaninline{MTDevice} type, a device handle, an \gls{ITASK} \gls{SDS} of the type boolean is created.
+As an example, \cref{lst:mtask_liftsds_ex} shows a light switch function producing an \imtask{} task when given a device handle.
+First an \gls{ITASK} \gls{SDS} of the type boolean is created.
 This boolean represents the state of the light.
 The \gls{MTASK} task uses this \gls{SDS} to turn on or off the light.
-An \gls{ITASK} task that runs in parallel allows interactive updating of this state.
+The \gls{ITASK} task that runs in parallel allows interactive updating of this state.
 
-\todo{dit voor\-beeld aan\-hou\-den of al\-leen die gro\-te ge\-brui\-ken}
 \begin{lstClean}[label={lst:mtask_liftsds_ex},caption={Interactive light switch program.}]
+lightswitch :: MTDevice -> Task Bool
 lightswitch dev  =
        withShared False \sh->
                    liftmTask (mtask sh) dev
                -|| updateSharedInformation [] sh
                <<@ Hint "Light switch"
 where
+       mtask :: (Shared sds Bool) -> Main (MTask v Bool)
+               | mtask, lowerSds v & RWShared sds
        mtask sh =
                declarePin D13 PMOutput \d13->
-               liftsds \ls=sh
+               lowerSds \ls=sh
                In fun \f=(\st->
                             getSds ls
                        >>*. [IfValue ((!=.)st) (\v->writeD d13 v)]
@@ -190,15 +210,15 @@ The compilation of the code and the serialisation of the data throws away all ty
 \Glspl{SDS} are stored in the compiler state as a map from identifiers to either an initial value or an \cleaninline{MTLens}.
 The \cleaninline{MTLens} is a type synonym for a \gls{SDS} that represents the typeless serialised value of the underlying \gls{SDS}.
 This is done so that the \cleaninline{withDevice} task can write the received \gls{SDS} updates to the according \gls{SDS} independently.
-\Gls{ITASK}'s notification mechanism then takes care of the rest.
-Such a \gls{SDS} is created by using the \cleaninline{mapReadWriteError} which, given a pair of read and write functions with error handling, produces a \gls{SDS} with the lens embedded.
-The read function transforms, the function that converts a typed value to a typeless serialised value, just applies the serialisation.
-The write function, the function that, given the new serialised value and the old typed value, produces a new typed value.
+The \gls{ITASK} notification mechanism then takes care of the rest.
+Such \pgls{SDS} is created by using the \cleaninline{mapReadWriteError} which, given a pair of read and write functions with error handling, produces \pgls{SDS} with the lens embedded.
+The read function transforms converts the typed value to a typeless serialised value.
+The write function will, given the new serialised value and the old typed value, produce a new typed value.
 It tries to decode the serialised value, if that succeeds, it is written to the underlying \gls{SDS}, an error is thrown otherwise.
 \Cref{lst:mtask_itasksds_lens} provides the implementation for this:
 
 % VimTeX: SynIgnore on
-\begin{lstClean}[label={lst:mtask_itasksds_lens},caption={Lens applied to lifted \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
+\begin{lstClean}[label={lst:mtask_itasksds_lens},caption={Lens applied to lowered \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
 lens :: (Shared sds a) -> MTLens | type a & RWShared sds
 lens sds = mapReadWriteError
        ( \r->   Ok (fromString (toByteCode{|*|} r)
@@ -207,28 +227,30 @@ lens sds = mapReadWriteError
 \end{lstClean}
 % VimTeX: SynIgnore off
 
-\Cref{lst:mtask_itasksds_lift} shows the code for the implementation of \cleaninline{liftsds} that uses the \cleaninline{lens} function shown earlier.
-First, the \gls{SDS} to be lifted is extracted from the expression by bootstrapping the fixed point with a dummy value.
+\Cref{lst:mtask_itasksds_lift} shows the code for the implementation of \cleaninline{lowerSds} that uses the \cleaninline{lens} function shown earlier.
+First, the \gls{SDS} to be lowered is extracted from the expression by bootstrapping the fixed point with a dummy value.
 This is safe because the expression on the right-hand side of the \cleaninline{In} is never evaluated.
 Then, using \cleaninline{addSdsIfNotExist}, the identifier for this particular \gls{SDS} is either retrieved from the compiler state or generated freshly.
 This identifier is then used to provide a reference to the \cleaninline{def} definition to evaluate the main expression.
 
 % VimTeX: SynIgnore on
-\begin{lstClean}[label={lst:mtask_itasksds_lift},caption={Lens applied to lifted \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
-       liftsds def = {main =
-                       let (t In _) = def (abort "liftsds: expression too strict")
+\begin{lstClean}[label={lst:mtask_itasksds_lift},caption={Lens applied to lowered \gls{ITASK} \glspl{SDS} in \gls{MTASK}.}]
+instance lowerSds BCInterpret where
+       lowerSds def = {main =
+                       let (t In _) = def (abort "lowerSds: expression too strict")
                        in addSdsIfNotExist (Right $ lens t)
                                >>= \sdsi->let (_ In e) = def (pure (Sds sdsi)) in e.main
                }\end{lstClean}
 % VimTeX: SynIgnore off
 
 \section{Home automation}
+\todo[inline]{Staat dit hier goed of moet dit naar een andere sectie?}
 This section presents a interactive home automation program (\Cref{lst:example_home_automation}) to illustrate \gls{MTASK}'s integration with \gls{ITASK}.
-It consists of a web interface for the user to control which tasks may be executed on either of two connected devices: an \gls{ARDUINO} UNO, connected via a serial port; and an ESP8266 based prototyping board called NodeMCU, connected via \gls{TCP} over WiFi.
+It consists of a web interface for the user to control which tasks may be executed on either of two connected devices: an \gls{ARDUINO} UNO, connected via a serial port; and an ESP8266 based prototyping board called NodeMCU, connected via \gls{TCP} over \gls{WIFI}.
 
 \Crefrange{lst:example:spec1}{lst:example:spec2} show the specification for the devices.
 The UNO is connected via serial using the unix filepath \path{/dev/ttyACM0} and the default serial port settings.
-The NodeMCU is connected via WiFi and hence the \cleaninline{TCPSettings} record is used.
+The NodeMCU is connected via \gls{WIFI} and hence the \cleaninline{TCPSettings} record is used.
 Both types have \cleaninline{channelSync} instances.
 
 The code consists of an \gls{ITASK} part and several \gls{MTASK} parts.
@@ -247,7 +269,7 @@ This task just sends a simple temperature monitoring task to the device using \c
 \footnotetext{\cleaninline{(>\&>) infixl 1 :: (Task a) ((SDSLens () (? a) ()) -> Task b) -> Task b \| iTask a \& iTask b}}
 The light switch task at \crefrange{lst:example:ls1}{lst:example:ls2} is a task that has bidirectional interaction using the definition of \cleaninline{lightswitch} shown in \cref{lst:mtask_liftsds_ex}.
 Using \cleaninline{liftsds}, the status of the light switch is synchronised with the user.
-The task on the edge device continuously monitors the value of the lifted \gls{SDS}.
+The task on the edge device continuously monitors the value of the lowered \gls{SDS}.
 If it is different from the current state, the new value is written to the digital \gls{GPIO} pin 13 and the monitoring function is recursively called.
 
 \begin{figure}[ht]
@@ -278,12 +300,14 @@ If it is different from the current state, the new value is written to the digit
 \end{figure}
 
 \section{Conclusion}
-\Gls{MTASK} edge devices run little \gls{TOP} engines of their own.
-Using only a couple of \gls{ITASK} functions, \gls{MTASK} tasks can be integrated in \gls{ITASK} seamlessly.
+When \gls{IOT} edge devices run the \gls{MTASK} \gls{RTS}, they become little \gls{TOP} engines of their own.
+Using just three \gls{ITASK} functions, \gls{MTASK} devices are integrated in \gls{ITASK} seamlessly.
 Devices, using any supported type of connection, are integrated in \gls{ITASK} using the \cleaninline{withDevice} function.
-Once connected, \gls{MTASK} tasks can be sent to the device for execution using \cleaninline{liftmTask}, lifting them to full-fledged \gls{ITASK} tasks.
-Furthermore, the \gls{MTASK} tasks can interact with \gls{ITASK} \glspl{SDS} using the \cleaninline{liftsds} construct.
-This together allows entire \gls{IOT} systems to be programmed from a single source.
+Once connected, \gls{MTASK} tasks are sent to the device for execution using \cleaninline{liftmTask}, lifting them to full-fledged \gls{ITASK} tasks.
+To lower the bandwidth, tasks can also be preloaded.
+Furthermore, the \gls{MTASK} tasks interact with \gls{ITASK} \glspl{SDS} using the \cleaninline{lowerSds} construct.
+All of this together allows programming all layers of an \gls{IOT} system from a single source and in a single paradigm.
+All details regarding interoperation are automatically taken care of.
 
 \input{subfilepostamble}
 \end{document}