2 /// Actions ///////////////////////////////////////////////////////////////////
6 "org.simantics.simulation.sequences.action.ActionContext"
8 importJava "org.simantics.simulation.sequences.action.StopReason" where
11 SIMULATION_DID_NOT_START :: StopReason
12 DIVERGED :: StopReason
13 INTERRUPTED :: StopReason
15 importJava "org.simantics.simulation.sequences.action.ActionContext" where
19 stopActionContext :: ActionContext -> <Proc> ()
21 """Gives the current simulation time."""
22 time :: <Action> Double
24 getVar_ :: String -> Binding a -> <Action> a
26 setVar_ :: String -> a -> Binding a -> <Action> ()
28 scheduleNow :: (() -> <Action,Proc> a) -> <Action> ()
29 scheduleNextStep :: (() -> <Action,Proc> a) -> <Action> ()
30 scheduleAt :: Double -> (() -> <Action,Proc> a) -> <Action> ()
31 scheduleWhenStopped :: (StopReason -> <Action,Proc> a) -> <Action> ()
35 """Returns the current value of a variable"""
36 getVar :: Serializable a => String -> <Action> a
37 getVar variableName = getVar_ variableName binding
39 """Sets the value of a variable"""
40 setVar :: Serializable a => String -> a -> <Action> ()
41 setVar variableName value = setVar_ variableName value binding
43 /// Sequences /////////////////////////////////////////////////////////////////
46 `Sequence a` is a plan of some operations that may happen during the simulation and may take some
47 (simulation) time. A sequence is initiated at a specific time and it may either finish at a specific
48 time or operate forever. If it completes, it retuns a value of type `a`.
50 data Sequence a = Sequence ( (a -> <Action,Proc> ()) -> <Action,Proc> () )
54 execSequence (Sequence f) cont = f cont
56 instance Functor Sequence where
58 fmap f seq = Sequence (\cont -> execSequence seq (cont . f))
60 instance Monad Sequence where
62 return v = Sequence (\cont -> cont v)
64 seq >>= f = Sequence (\cont -> execSequence seq (\result -> execSequence (f result) cont))
67 The sequence `execute action` is an instantious sequence that executes the operation `action` in the simulator.
70 execute :: (<Action,Proc> a) -> Sequence a
71 execute action = Sequence (\cont -> cont action)
74 executeWhenStopped :: (StopReason -> <Action,Proc> a) -> Sequence ()
75 executeWhenStopped handler = execute (scheduleWhenStopped handler)
78 The sequence `fork seq` is an instantious sequence that creates a new sequence thread behaving like the sequence `seq`.
80 fork :: Sequence a -> Sequence ()
81 fork seq = Sequence (\cont -> do { scheduleNow cont ; execSequence seq ignore })
84 The sequence `halt` ends the current sequence thread and the sequence .
87 halt = Sequence (\cont -> ())
90 The sequence `stop` stops all sequence threads, stopping the simulation completely.
93 stop = Sequence (\cont -> stop_)
96 The sequence `waitStep` waits that the simulator takes one simulation step.
97 It is a primitive mechanism that can be used to implement other events by
98 inspecting the simulator state after each time step.
100 waitStep :: Sequence ()
101 waitStep = Sequence (\cont -> scheduleNextStep cont)
103 """The sequence `waitUntil time` waits until the simulation time is at least the given `time`."""
104 waitUntil :: Double -> Sequence ()
105 waitUntil t = Sequence (\cont -> scheduleAt t cont)
107 """The sequence `wait duration` waits that `duration` seconds elapses from the current simulation time."""
108 wait :: Double -> Sequence ()
109 wait duration = Sequence (\cont -> scheduleAt (time+duration) cont)
111 """The sequence `waitCondition condition` waits until the `condition` is satisfied."""
112 waitCondition :: (<Action,Proc> Boolean) -> Sequence ()
113 waitCondition condition = Sequence (\cont ->
114 let loop _ = if condition
116 else scheduleNextStep loop