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