Showing
3 changed files
with
78 additions
and
13 deletions
cinzia.py
0 → 100644
1 | +import snakes.nets as snk | ||
2 | +from snakes.utils.simul import BaseSimulator | ||
3 | + | ||
4 | +class CinziaSimulator (BaseSimulator) : | ||
5 | + def __init__ (self, **system) : | ||
6 | + net = snk.PetriNet("counter machine") | ||
7 | + for var, spec in system.items() : | ||
8 | + init, expr = spec.split(":", 1) | ||
9 | + system[var] = snk.Expression(expr) | ||
10 | + net.add_place(snk.Place(var, [int(init)])) | ||
11 | + for var, expr in system.items() : | ||
12 | + trans = "update_" + var | ||
13 | + net.add_transition(snk.Transition(trans)) | ||
14 | + for v in set(expr.vars() + [var]) : | ||
15 | + net.add_input(v, trans, snk.Variable(v)) | ||
16 | + if v == var : | ||
17 | + net.add_output(v, trans, expr) | ||
18 | + else : | ||
19 | + net.add_output(v, trans, snk.Variable(v)) | ||
20 | + BaseSimulator.__init__(self, net) | ||
21 | + def getstate (self, state) : | ||
22 | + ret = BaseSimulator.getstate(self, state) | ||
23 | + marking = self.states[state] | ||
24 | + ret["variables"] = dict((place, tokens.items()[0]) | ||
25 | + for place, tokens in marking.items()) | ||
26 | + ret["modes"] = [] | ||
27 | + for i, (trans, binding) in enumerate(marking.modes) : | ||
28 | + if (state + i) % 5 == 0 : | ||
29 | + groups = ["timed"] | ||
30 | + else : | ||
31 | + groups = [] | ||
32 | + ret["modes"].append( | ||
33 | + {"state" : state, | ||
34 | + "mode" : i, | ||
35 | + "html" : "%s (%s)" % (trans.name[7:], binding), | ||
36 | + "groups" : groups + ["odd" if (state % 2) else "even"] | ||
37 | + }) | ||
38 | + return ret | ||
39 | + |
1 | -import tempfile, anydbm, os | 1 | +from snakes.utils.simul import BaseSimulator, BaseHTTPSimulator |
2 | -from snakes.utils.simul import * | ||
3 | import snakes.utils.abcd.html as html | 2 | import snakes.utils.abcd.html as html |
4 | 3 | ||
5 | class ABCDSimulator (BaseSimulator) : | 4 | class ABCDSimulator (BaseSimulator) : |
... | @@ -33,9 +32,19 @@ class ABCDSimulator (BaseSimulator) : | ... | @@ -33,9 +32,19 @@ class ABCDSimulator (BaseSimulator) : |
33 | return res | 32 | return res |
34 | def init_help (self) : | 33 | def init_help (self) : |
35 | help = BaseSimulator.init_help(self) | 34 | help = BaseSimulator.init_help(self) |
36 | - help.update({"#model .abcd" : "ABCD source code", | 35 | + help.update({ |
37 | - "#model .tree" : "hierarchy of ABCD objects", | 36 | + "#model .abcd" : { |
38 | - "#model .petrinet" : "Petri nets semantics"}) | 37 | + "title" : "Source", |
38 | + "content" : "ABCD source code" | ||
39 | + }, | ||
40 | + "#model .tree" : { | ||
41 | + "title" : "State", | ||
42 | + "content" : "hierarchy of ABCD objects", | ||
43 | + }, | ||
44 | + "#model .petrinet" : { | ||
45 | + "title" : "Net", | ||
46 | + "content" : "Petri nets semantics" | ||
47 | + }}) | ||
39 | return help | 48 | return help |
40 | def getstate (self, state) : | 49 | def getstate (self, state) : |
41 | marking = self.states[state] | 50 | marking = self.states[state] | ... | ... |
1 | import snakes | 1 | import snakes |
2 | from snakes.utils.simul.httpd import * | 2 | from snakes.utils.simul.httpd import * |
3 | from snakes.utils.simul.html import H | 3 | from snakes.utils.simul.html import H |
4 | -import multiprocessing, time, sys, json, os.path, signal, inspect, glob | 4 | +import multiprocessing, time, sys, os.path, signal, inspect, glob |
5 | import operator | 5 | import operator |
6 | 6 | ||
7 | class StateSpace (dict) : | 7 | class StateSpace (dict) : |
... | @@ -109,11 +109,28 @@ class BaseSimulator (object) : | ... | @@ -109,11 +109,28 @@ class BaseSimulator (object) : |
109 | state = self.states.succ(state, mode) | 109 | state = self.states.succ(state, mode) |
110 | return self.getstate(state) | 110 | return self.getstate(state) |
111 | def init_help (self) : | 111 | def init_help (self) : |
112 | - return {"#trace": "the states and transitions explored so far", | 112 | + return { |
113 | - "#model" : "the model being simulated", | 113 | + "#trace": { |
114 | - "#alive .ui #ui-quit" : "stop the simulator (server side)", | 114 | + "title" : "Trace", |
115 | - "#alive .ui #ui-help" : "show this help", | 115 | + "content" : "the states and transitions explored so far" |
116 | - "#alive .ui #ui-about" : "show information about the simulator"} | 116 | + }, |
117 | + "#model" : { | ||
118 | + "title" : "Model", | ||
119 | + "content" : "the model being simulated" | ||
120 | + }, | ||
121 | + "#alive .ui #ui-quit" : { | ||
122 | + "title" : "Stop", | ||
123 | + "content" : "stop the simulator (server side)" | ||
124 | + }, | ||
125 | + "#alive .ui #ui-help" : { | ||
126 | + "title" : "Help", | ||
127 | + "content" : "show this help" | ||
128 | + }, | ||
129 | + "#alive .ui #ui-about" : { | ||
130 | + "title" : "About", | ||
131 | + "content" : "show information about the simulator" | ||
132 | + }, | ||
133 | + } | ||
117 | 134 | ||
118 | class BaseHTTPSimulator (Node) : | 135 | class BaseHTTPSimulator (Node) : |
119 | def __init__ (self, net, port=8000, respatt=[], simulator=None) : | 136 | def __init__ (self, net, port=8000, respatt=[], simulator=None) : |
... | @@ -139,7 +156,7 @@ class BaseHTTPSimulator (Node) : | ... | @@ -139,7 +156,7 @@ class BaseHTTPSimulator (Node) : |
139 | while True : | 156 | while True : |
140 | try : | 157 | try : |
141 | httpd = HTTPServer(('', self.port), self) | 158 | httpd = HTTPServer(('', self.port), self) |
142 | - except Exception as err : | 159 | + except : |
143 | self.port += 1 | 160 | self.port += 1 |
144 | else : | 161 | else : |
145 | break | 162 | break |
... | @@ -206,7 +223,7 @@ class BaseHTTPSimulator (Node) : | ... | @@ -206,7 +223,7 @@ class BaseHTTPSimulator (Node) : |
206 | @http("application/json", state=int) | 223 | @http("application/json", state=int) |
207 | def init (self, state=-1) : | 224 | def init (self, state=-1) : |
208 | ret = {"ui" : self.init_ui(), | 225 | ret = {"ui" : self.init_ui(), |
209 | - "help" : self.init_help()} | 226 | + "help" : self.simul.init_help()} |
210 | ret.update(self.simul.init(state)) | 227 | ret.update(self.simul.init(state)) |
211 | return ret | 228 | return ret |
212 | @http("application/json", state=int, mode=int) | 229 | @http("application/json", state=int, mode=int) | ... | ... |
-
Please register or login to post a comment