]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.sequences/scl/Simantics/Sequences.scl
Prevent undesirable UI code access when workbench is not running
[simantics/platform.git] / bundles / org.simantics.simulation.sequences / scl / Simantics / Sequences.scl
1
2 /// Actions ///////////////////////////////////////////////////////////////////
3
4 effect Action 
5     "sequenceAction"
6     "org.simantics.simulation.sequences.action.ActionContext"
7
8 importJava "org.simantics.simulation.sequences.action.ActionContext" where
9     data ActionContext
10     
11     @JavaName stop
12     stopActionContext :: ActionContext -> <Proc> ()
13
14     """Gives the current simulation time."""
15     time :: <Action> Double
16     @JavaName get
17     getVar_  :: String -> Binding a -> <Action> a
18     @JavaName set
19     setVar_  :: String -> a -> Binding a -> <Action> ()
20     
21     scheduleNow :: (() -> <Action,Proc> a) -> <Action> ()
22     scheduleNextStep :: (() -> <Action,Proc> a) -> <Action> ()
23     scheduleAt :: Double -> (() -> <Action,Proc> a) -> <Action> ()
24     @JavaName stop
25     stop_ :: <Action> ()
26
27 """Returns the current value of a variable"""
28 getVar :: Serializable a => String -> <Action> a
29 getVar variableName = getVar_ variableName binding
30
31 """Sets the value of a variable"""
32 setVar :: Serializable a => String -> a -> <Action> ()
33 setVar variableName value = setVar_ variableName value binding
34
35 /// Sequences /////////////////////////////////////////////////////////////////
36
37 """
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`.
41 """
42 data Sequence a = Sequence ( (a -> <Action,Proc> ()) -> <Action,Proc> () )
43
44 @private
45 @inline
46 execSequence (Sequence f) cont = f cont
47
48 instance Functor Sequence where
49     @inline
50     fmap f seq = Sequence (\cont -> execSequence seq (cont . f))
51
52 instance Monad Sequence where
53     @inline
54     return v = Sequence (\cont -> cont v)
55     @inline
56     seq >>= f = Sequence (\cont -> execSequence seq (\result -> execSequence (f result) cont)) 
57
58 """
59 The sequence `execute action` is an instantious sequence that executes the operation `action` in the simulator.
60 """
61 @inline
62 execute :: (<Action,Proc> a) -> Sequence a
63 execute action = Sequence (\cont -> cont action)
64
65 """
66 The sequence `fork seq` is an instantious sequence that creates a new sequence thread behaving like the sequence `seq`.
67 """
68 fork :: Sequence a -> Sequence ()
69 fork seq = Sequence (\cont -> do { scheduleNow cont ; execSequence seq ignore })
70
71 """
72 The sequence `halt` ends the current sequence thread and the sequence .
73 """
74 halt :: Sequence a
75 halt = Sequence (\cont -> ())
76
77 """
78 The sequence `stop` stops all sequence threads, stopping the simulation completely.
79 """
80 stop :: Sequence a
81 stop = Sequence (\cont -> stop_)
82
83 """
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.
87 """
88 waitStep :: Sequence ()
89 waitStep = Sequence (\cont -> scheduleNextStep cont)
90
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)
94
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)
98
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
103                  then cont ()
104                  else scheduleNextStep loop 
105     in  loop ())
106