add factorial example'
[msc-thesis1617.git] / arch.example.tex
index 4b4f48e..db9a2a7 100644 (file)
@@ -1 +1,61 @@
-Here comes a description of the demo program.
+\subsection{Framework}
+Systems built with support for \gls{mTask} are often following the same design
+pattern. First the devices are created --- with or without the interaction of
+the user --- and they are then connected. When all devices are registered, the
+\gls{mTask}-\glspl{Task} can be sent and \gls{iTasks}-\glspl{Task} can be
+started to monitor the output. When everything is finished, the devices are
+removed and the system is powered off.
+
+\begin{lstlisting}[language=Clean,label={lst:framework},
+       caption={\gls{mTask} framework for building applications}]
+w :: Task ()
+w =         makeDevice "dev1" (...) >>= connectDevice
+       >>= \dev1->makeDevice "dev2" (...) >>= connectDevice
+       >>= \dev2->...
+       ...
+       >>* [OnAction (Action "Shutdown") $ always
+               $   deleteDevice dev1
+               >>| deleteDevice dev2
+               >>| ...
+               >>| shutDown 0
+       ]
+\end{lstlisting}
+
+\subsection{Thermostat}
+The thermostat is a classic example program for showing interactions between
+peripherals. The following program shows a system containing two devices. One
+device is connected via \gls{TCP} and contains a temperature sensor. The second
+
+\subsection[Lifting mTasks to iTasks-Tasks]%
+       {Lifting \gls{mTask}-\glspl{Task} to \gls{iTasks}-\glspl{Task}}
+If the user does not want to know where and when a \gls{mTask} is actually
+executed and is just interested in the results it can lift the \gls{mTask} to
+an \gls{iTasks}-\gls{Task}. The function is called with a name, \gls{mTask},
+device and interval specification and it will return a \gls{Task} that finishes
+if and only if the \gls{mTask} has returned.
+
+\begin{lstlisting}[caption={Starting up the devices}]
+liftmTask :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task [MTaskShare]
+liftmTask wta mTask c=:(dev, _)= sendTaskToDevice wta mTask c
+       >>= \(t, shs)->wait "Waiting for mTask to return" (taskRemoved t) (deviceShare dev)
+       >>| viewInformation "Done!" [] ()
+       >>| treturn shs
+where
+       taskRemoved t d = isNothing $ find (\t1->t1.ident==t.ident) d.deviceTasks
+\end{lstlisting}
+
+The factorial example can then be lifted to a real \gls{iTasks}-\gls{mTask}
+with the following code:
+\begin{lstlisting}[caption={Lifting the factorial \gls{Task} to \gls{iTasks}}]
+factorial :: MTaskDevice -> Task BCValue
+factorial dev = enterInformation "Factorial of ?" []
+       >>= \fac->liftmTask "fact" (fact fac) (dev, OnInterval 100)
+       @         fromJust o find (\x->x.humanName == "result")
+       @         \s->s.MTaskShare.value
+where
+       fact i = sds \y=i
+               In namedsds \x=(1 Named "result")
+               In {main = IF (y <=. lit 1)
+                       ( pub x :. retrn                  )
+                       ( x =. x *. y :.  y =. y -. lit 1 )}
+\end{lstlisting}