2 /// Actions ///////////////////////////////////////////////////////////////////
\r
6 "org.simantics.simulation.sequences.action.ActionContext"
\r
8 importJava "org.simantics.simulation.sequences.action.ActionContext" where
\r
12 stopActionContext :: ActionContext -> <Proc> ()
\r
14 """Gives the current simulation time."""
\r
15 time :: <Action> Double
\r
17 getVar_ :: String -> Binding a -> <Action> a
\r
19 setVar_ :: String -> a -> Binding a -> <Action> ()
\r
21 scheduleNow :: (() -> <Action,Proc> a) -> <Action> ()
\r
22 scheduleNextStep :: (() -> <Action,Proc> a) -> <Action> ()
\r
23 scheduleAt :: Double -> (() -> <Action,Proc> a) -> <Action> ()
\r
25 stop_ :: <Action> ()
\r
27 """Returns the current value of a variable"""
\r
28 getVar :: Serializable a => String -> <Action> a
\r
29 getVar variableName = getVar_ variableName binding
\r
31 """Sets the value of a variable"""
\r
32 setVar :: Serializable a => String -> a -> <Action> ()
\r
33 setVar variableName value = setVar_ variableName value binding
\r
35 /// Sequences /////////////////////////////////////////////////////////////////
\r
38 `Sequence a` is a plan of some operations that may happen during the simulation and may take some
\r
39 (simulation) time. A sequence is initiated at a specific time and it may either finish at a specific
\r
40 time or operate forever. If it completes, it retuns a value of type `a`.
\r
42 data Sequence a = Sequence ( (a -> <Action,Proc> ()) -> <Action,Proc> () )
\r
46 execSequence (Sequence f) cont = f cont
\r
48 instance Functor Sequence where
\r
50 fmap f seq = Sequence (\cont -> execSequence seq (cont . f))
\r
52 instance Monad Sequence where
\r
54 return v = Sequence (\cont -> cont v)
\r
56 seq >>= f = Sequence (\cont -> execSequence seq (\result -> execSequence (f result) cont))
\r
59 The sequence `execute action` is an instantious sequence that executes the operation `action` in the simulator.
\r
62 execute :: (<Action,Proc> a) -> Sequence a
\r
63 execute action = Sequence (\cont -> cont action)
\r
66 The sequence `fork seq` is an instantious sequence that creates a new sequence thread behaving like the sequence `seq`.
\r
68 fork :: Sequence a -> Sequence ()
\r
69 fork seq = Sequence (\cont -> do { scheduleNow cont ; execSequence seq ignore })
\r
72 The sequence `halt` ends the current sequence thread and the sequence .
\r
75 halt = Sequence (\cont -> ())
\r
78 The sequence `stop` stops all sequence threads, stopping the simulation completely.
\r
81 stop = Sequence (\cont -> stop_)
\r
84 The sequence `waitStep` waits that the simulator takes one simulation step.
\r
85 It is a primitive mechanism that can be used to implement other events by
\r
86 inspecting the simulator state after each time step.
\r
88 waitStep :: Sequence ()
\r
89 waitStep = Sequence (\cont -> scheduleNextStep cont)
\r
91 """The sequence `waitUntil time` waits until the simulation time is at least the given `time`."""
\r
92 waitUntil :: Double -> Sequence ()
\r
93 waitUntil t = Sequence (\cont -> scheduleAt t cont)
\r
95 """The sequence `wait duration` waits that `duration` seconds elapses from the current simulation time."""
\r
96 wait :: Double -> Sequence ()
\r
97 wait duration = Sequence (\cont -> scheduleAt (time+duration) cont)
\r
99 """The sequence `waitCondition condition` waits until the `condition` is satisfied."""
\r
100 waitCondition :: (<Action,Proc> Boolean) -> Sequence ()
\r
101 waitCondition condition = Sequence (\cont ->
\r
102 let loop _ = if condition
\r
104 else scheduleNextStep loop
\r