Franck Pommereau

separated base simulator from HTTP server

...@@ -2,15 +2,9 @@ import tempfile, anydbm, os ...@@ -2,15 +2,9 @@ import tempfile, anydbm, os
2 from snakes.utils.simul import * 2 from snakes.utils.simul import *
3 import snakes.utils.abcd.html as html 3 import snakes.utils.abcd.html as html
4 4
5 -class Simulator (BaseHTTPSimulator) : 5 +class ABCDSimulator (BaseSimulator) :
6 - def __init__ (self, abcd, node, net, gv) : 6 + def __init__ (self, net, a2html, n2html, gv) :
7 - BaseHTTPSimulator.__init__(self, net) 7 + BaseSimulator.__init__(self, net)
8 - a2html = html.ABCD2HTML(node)
9 - n2html = html.Net2HTML(net, gv, a2html)
10 - self.info = {"filename" : node.st.filename,
11 - "abcd" : a2html.html(),
12 - "tree" : n2html.html(),
13 - "net" : n2html.svg()}
14 self.tree = {} 8 self.tree = {}
15 for node in net.node() : 9 for node in net.node() :
16 nid = gv.nodemap[node.name] 10 nid = gv.nodemap[node.name]
...@@ -26,11 +20,6 @@ class Simulator (BaseHTTPSimulator) : ...@@ -26,11 +20,6 @@ class Simulator (BaseHTTPSimulator) :
26 if nid in n2html.n2a : 20 if nid in n2html.n2a :
27 self.abcd[trans.name] = ", ".join("#" + i for i in 21 self.abcd[trans.name] = ", ".join("#" + i for i in
28 n2html.n2a[nid]) 22 n2html.n2a[nid])
29 - # persistency
30 - self.tmp = tempfile.NamedTemporaryFile()
31 - self.tmp.file.close()
32 - os.unlink(self.tmp.name)
33 - self.store = anydbm.open(self.tmp.name, "c")
34 def getstate (self, state) : 23 def getstate (self, state) :
35 marking = self.states[state] 24 marking = self.states[state]
36 modes = dict((t, []) for t in self.transid) 25 modes = dict((t, []) for t in self.transid)
...@@ -61,6 +50,17 @@ class Simulator (BaseHTTPSimulator) : ...@@ -61,6 +50,17 @@ class Simulator (BaseHTTPSimulator) :
61 "items" : items} 50 "items" : items}
62 for trans, items in modes.items()], 51 for trans, items in modes.items()],
63 } 52 }
53 +
54 +class Simulator (BaseHTTPSimulator) :
55 + def __init__ (self, abcd, node, net, gv) :
56 + a2html = html.ABCD2HTML(node)
57 + n2html = html.Net2HTML(net, gv, a2html)
58 + simul = ABCDSimulator(net, a2html, n2html, gv)
59 + BaseHTTPSimulator.__init__(self, net, simulator=simul)
60 + self.info = {"filename" : node.st.filename,
61 + "abcd" : a2html.html(),
62 + "tree" : n2html.html(),
63 + "net" : n2html.svg()}
64 def init_model (self) : 64 def init_model (self) :
65 return self.res["model.html"] % self.info 65 return self.res["model.html"] % self.info
66 def init_ui (self) : 66 def init_ui (self) :
...@@ -76,15 +76,3 @@ class Simulator (BaseHTTPSimulator) : ...@@ -76,15 +76,3 @@ class Simulator (BaseHTTPSimulator) :
76 "#model .tree" : "hierarchy of ABCD objects", 76 "#model .tree" : "hierarchy of ABCD objects",
77 "#model .petrinet" : "Petri nets semantics"}) 77 "#model .petrinet" : "Petri nets semantics"})
78 return help 78 return help
79 - @http("text/plain", key=str)
80 - def get (self, key) :
81 - try :
82 - print self.store[key]
83 - return self.store[key]
84 - except KeyError :
85 - raise HTTPError(httplib.NOT_FOUND)
86 - @http("text/plain", key=str, value=str)
87 - def put (self, key, value) :
88 - print "store[%r] = %r" % (key, value)
89 - self.store[key] = value
90 - return "OK"
......
...@@ -72,8 +72,45 @@ class WatchDog (multiprocessing.Process) : ...@@ -72,8 +72,45 @@ class WatchDog (multiprocessing.Process) :
72 finally : 72 finally :
73 shutdown.set() 73 shutdown.set()
74 74
75 +class BaseSimulator (object) :
76 + def __init__ (self, net) :
77 + self.states = StateSpace(net)
78 + def getstate (self, state) :
79 + marking = self.states[state]
80 + places = ["%s = %s" % (H.span(place.name, class_="place"),
81 + H.span(marking(place.name), class_="token"))
82 + for place in sorted(self.states.net.place(),
83 + key=operator.attrgetter("name"))]
84 + modes = [{"state" : state,
85 + "mode" : i,
86 + "html" : "%s : %s" % (H.span(trans.name, class_="trans"),
87 + H.span(binding, class_="binding"))}
88 + for i, (trans, binding) in enumerate(marking.modes)]
89 + return {"id" : state,
90 + "states" : [{"do" : "sethtml",
91 + "select" : "#net",
92 + "html" : H.i(self.states.net)},
93 + {"do" : "settext",
94 + "select" : "#state",
95 + "text" : state},
96 + {"do" : "setlist",
97 + "select" : "#marking",
98 + "items" : places},
99 + ],
100 + "modes" : [{"select" : "#modes",
101 + "items" : modes},
102 + ],
103 + }
104 + def init (self, state=-1) :
105 + if state < 0 :
106 + state = self.states.current
107 + return {"state" : self.getstate(state)}
108 + def succ (self, state, mode) :
109 + state = self.states.succ(state, mode)
110 + return self.getstate(state)
111 +
75 class BaseHTTPSimulator (Node) : 112 class BaseHTTPSimulator (Node) :
76 - def __init__ (self, net, port=8000, respatt=[]) : 113 + def __init__ (self, net, port=8000, respatt=[], simulator=None) :
77 self.res = {} 114 self.res = {}
78 dirs = {} 115 dirs = {}
79 for cls in reversed(inspect.getmro(self.__class__)[:-2]) : 116 for cls in reversed(inspect.getmro(self.__class__)[:-2]) :
...@@ -106,7 +143,10 @@ class BaseHTTPSimulator (Node) : ...@@ -106,7 +143,10 @@ class BaseHTTPSimulator (Node) :
106 self.url = "http://127.0.0.1:%s/%s/" % (port, httpd.key) 143 self.url = "http://127.0.0.1:%s/%s/" % (port, httpd.key)
107 self._alive = self.res["alive.txt"].splitlines() 144 self._alive = self.res["alive.txt"].splitlines()
108 self._ping = 0 145 self._ping = 0
109 - self.states = StateSpace(net) 146 + if simulator is None :
147 + self.simul = BaseSimulator(net)
148 + else :
149 + self.simul = simulator
110 def start (self) : 150 def start (self) :
111 log("starting at %r" % self.url) 151 log("starting at %r" % self.url)
112 shutdown.clear() 152 shutdown.clear()
...@@ -129,32 +169,6 @@ class BaseHTTPSimulator (Node) : ...@@ -129,32 +169,6 @@ class BaseHTTPSimulator (Node) :
129 if self.watchdog.pid : 169 if self.watchdog.pid :
130 os.kill(self.watchdog.pid, sig) 170 os.kill(self.watchdog.pid, sig)
131 log("bye!") 171 log("bye!")
132 - def getstate (self, state) :
133 - marking = self.states[state]
134 - places = ["%s = %s" % (H.span(place.name, class_="place"),
135 - H.span(marking(place.name), class_="token"))
136 - for place in sorted(self.states.net.place(),
137 - key=operator.attrgetter("name"))]
138 - modes = [{"state" : state,
139 - "mode" : i,
140 - "html" : "%s : %s" % (H.span(trans.name, class_="trans"),
141 - H.span(binding, class_="binding"))}
142 - for i, (trans, binding) in enumerate(marking.modes)]
143 - return {"id" : state,
144 - "states" : [{"do" : "sethtml",
145 - "select" : "#net",
146 - "html" : H.i(self.states.net)},
147 - {"do" : "settext",
148 - "select" : "#state",
149 - "text" : state},
150 - {"do" : "setlist",
151 - "select" : "#marking",
152 - "items" : places},
153 - ],
154 - "modes" : [{"select" : "#modes",
155 - "items" : modes},
156 - ],
157 - }
158 def init_index (self) : 172 def init_index (self) :
159 return {"res" : "%sr" % self.url, 173 return {"res" : "%sr" % self.url,
160 "url" : self.url, 174 "url" : self.url,
...@@ -191,15 +205,13 @@ class BaseHTTPSimulator (Node) : ...@@ -191,15 +205,13 @@ class BaseHTTPSimulator (Node) :
191 "#alive .ui #ui-about" : "show information about the simulator"} 205 "#alive .ui #ui-about" : "show information about the simulator"}
192 @http("application/json", state=int) 206 @http("application/json", state=int)
193 def init (self, state=-1) : 207 def init (self, state=-1) :
194 - if state < 0 : 208 + ret = {"ui" : self.init_ui(),
195 - state = self.states.current 209 + "help" : self.init_help()}
196 - return {"ui" : self.init_ui(), 210 + ret.update(self.simul.init(state))
197 - "state" : self.getstate(state), 211 + return ret
198 - "help" : self.init_help()}
199 @http("application/json", state=int, mode=int) 212 @http("application/json", state=int, mode=int)
200 def succ (self, state, mode) : 213 def succ (self, state, mode) :
201 - state = self.states.succ(state, mode) 214 + return self.simul.succ(state, mode)
202 - return self.getstate(state)
203 @http("text/plain") 215 @http("text/plain")
204 def ping (self) : 216 def ping (self) :
205 ping.set() 217 ping.set()
......