-
Notifications
You must be signed in to change notification settings - Fork 6
/
Snippets.hs
53 lines (48 loc) · 1.56 KB
/
Snippets.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Snippets where
decideAction =
unlines
[ ""
, "def decide_action(oracle, variables, actions, step) do"
, " different_states = Enum.uniq(Enum.map(actions, fn(action) -> action[:transition].(variables) end))"
, ""
, " cond do"
, " Enum.count(different_states) == 1 ->"
, " Enum.at(different_states, 0)"
, " true ->"
, " send oracle, {:choose, self(), different_states}"
, ""
, " receive do"
, " {:ok, n} -> Enum.at(different_states, n)"
, " {:cancel} -> variables"
, " {:stop} -> exit(0)"
, " end"
, " end"
, "end"
]
mainFunction =
unlines
[ "def main(oracle, private_variables, step) do"
, " shared_state = wait_lock(oracle)"
, " variables = Map.merge(private_variables, shared_state)"
, ""
, " actions = next(variables)"
, ""
, " next_variables = decide_action(oracle, variables, actions, step)"
, " send(oracle, {:notify, self(), variables, next_variables})"
, " Process.sleep(2000)"
, ""
, " main(oracle, next_variables, step + 1)"
, "end"
]
waitLockFunction =
unlines
[ "def wait_lock(oracle) do"
, " send(oracle, {:lock, self()})"
, " receive do"
, " {:lock_acquired, state} -> IO.puts(\"Lock acquired\"); {map, _} = Map.split(state, shared_variables); map"
, " {:already_locked, _} -> IO.puts(\"Lock refused\"); Process.sleep(1000); wait_lock(oracle)"
, " end"
, "end"
]
logState = "IO.puts (inspect variables)\n\n"
oracleDelaration = "require Oracle\n\n"