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