Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Franck Pommereau
/
snakes
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Franck Pommereau
2014-04-25 14:14:07 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
4085f00c06ee0b6cda9272aa980c03b8320c7f30
4085f00c
1 parent
f6dc262d
separated base simulator from HTTP server
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
61 deletions
snakes/utils/abcd/simul.py
snakes/utils/simul/__init__.py
snakes/utils/abcd/simul.py
View file @
4085f00
...
...
@@ -2,15 +2,9 @@ import tempfile, anydbm, os
from
snakes.utils.simul
import
*
import
snakes.utils.abcd.html
as
html
class
Simulator
(
BaseHTTPSimulator
)
:
def
__init__
(
self
,
abcd
,
node
,
net
,
gv
)
:
BaseHTTPSimulator
.
__init__
(
self
,
net
)
a2html
=
html
.
ABCD2HTML
(
node
)
n2html
=
html
.
Net2HTML
(
net
,
gv
,
a2html
)
self
.
info
=
{
"filename"
:
node
.
st
.
filename
,
"abcd"
:
a2html
.
html
(),
"tree"
:
n2html
.
html
(),
"net"
:
n2html
.
svg
()}
class
ABCDSimulator
(
BaseSimulator
)
:
def
__init__
(
self
,
net
,
a2html
,
n2html
,
gv
)
:
BaseSimulator
.
__init__
(
self
,
net
)
self
.
tree
=
{}
for
node
in
net
.
node
()
:
nid
=
gv
.
nodemap
[
node
.
name
]
...
...
@@ -26,11 +20,6 @@ class Simulator (BaseHTTPSimulator) :
if
nid
in
n2html
.
n2a
:
self
.
abcd
[
trans
.
name
]
=
", "
.
join
(
"#"
+
i
for
i
in
n2html
.
n2a
[
nid
])
# persistency
self
.
tmp
=
tempfile
.
NamedTemporaryFile
()
self
.
tmp
.
file
.
close
()
os
.
unlink
(
self
.
tmp
.
name
)
self
.
store
=
anydbm
.
open
(
self
.
tmp
.
name
,
"c"
)
def
getstate
(
self
,
state
)
:
marking
=
self
.
states
[
state
]
modes
=
dict
((
t
,
[])
for
t
in
self
.
transid
)
...
...
@@ -61,6 +50,17 @@ class Simulator (BaseHTTPSimulator) :
"items"
:
items
}
for
trans
,
items
in
modes
.
items
()],
}
class
Simulator
(
BaseHTTPSimulator
)
:
def
__init__
(
self
,
abcd
,
node
,
net
,
gv
)
:
a2html
=
html
.
ABCD2HTML
(
node
)
n2html
=
html
.
Net2HTML
(
net
,
gv
,
a2html
)
simul
=
ABCDSimulator
(
net
,
a2html
,
n2html
,
gv
)
BaseHTTPSimulator
.
__init__
(
self
,
net
,
simulator
=
simul
)
self
.
info
=
{
"filename"
:
node
.
st
.
filename
,
"abcd"
:
a2html
.
html
(),
"tree"
:
n2html
.
html
(),
"net"
:
n2html
.
svg
()}
def
init_model
(
self
)
:
return
self
.
res
[
"model.html"
]
%
self
.
info
def
init_ui
(
self
)
:
...
...
@@ -76,15 +76,3 @@ class Simulator (BaseHTTPSimulator) :
"#model .tree"
:
"hierarchy of ABCD objects"
,
"#model .petrinet"
:
"Petri nets semantics"
})
return
help
@http
(
"text/plain"
,
key
=
str
)
def
get
(
self
,
key
)
:
try
:
print
self
.
store
[
key
]
return
self
.
store
[
key
]
except
KeyError
:
raise
HTTPError
(
httplib
.
NOT_FOUND
)
@http
(
"text/plain"
,
key
=
str
,
value
=
str
)
def
put
(
self
,
key
,
value
)
:
print
"store[
%
r] =
%
r"
%
(
key
,
value
)
self
.
store
[
key
]
=
value
return
"OK"
...
...
snakes/utils/simul/__init__.py
View file @
4085f00
...
...
@@ -72,8 +72,45 @@ class WatchDog (multiprocessing.Process) :
finally
:
shutdown
.
set
()
class
BaseSimulator
(
object
)
:
def
__init__
(
self
,
net
)
:
self
.
states
=
StateSpace
(
net
)
def
getstate
(
self
,
state
)
:
marking
=
self
.
states
[
state
]
places
=
[
"
%
s =
%
s"
%
(
H
.
span
(
place
.
name
,
class_
=
"place"
),
H
.
span
(
marking
(
place
.
name
),
class_
=
"token"
))
for
place
in
sorted
(
self
.
states
.
net
.
place
(),
key
=
operator
.
attrgetter
(
"name"
))]
modes
=
[{
"state"
:
state
,
"mode"
:
i
,
"html"
:
"
%
s :
%
s"
%
(
H
.
span
(
trans
.
name
,
class_
=
"trans"
),
H
.
span
(
binding
,
class_
=
"binding"
))}
for
i
,
(
trans
,
binding
)
in
enumerate
(
marking
.
modes
)]
return
{
"id"
:
state
,
"states"
:
[{
"do"
:
"sethtml"
,
"select"
:
"#net"
,
"html"
:
H
.
i
(
self
.
states
.
net
)},
{
"do"
:
"settext"
,
"select"
:
"#state"
,
"text"
:
state
},
{
"do"
:
"setlist"
,
"select"
:
"#marking"
,
"items"
:
places
},
],
"modes"
:
[{
"select"
:
"#modes"
,
"items"
:
modes
},
],
}
def
init
(
self
,
state
=-
1
)
:
if
state
<
0
:
state
=
self
.
states
.
current
return
{
"state"
:
self
.
getstate
(
state
)}
def
succ
(
self
,
state
,
mode
)
:
state
=
self
.
states
.
succ
(
state
,
mode
)
return
self
.
getstate
(
state
)
class
BaseHTTPSimulator
(
Node
)
:
def
__init__
(
self
,
net
,
port
=
8000
,
respatt
=
[])
:
def
__init__
(
self
,
net
,
port
=
8000
,
respatt
=
[]
,
simulator
=
None
)
:
self
.
res
=
{}
dirs
=
{}
for
cls
in
reversed
(
inspect
.
getmro
(
self
.
__class__
)[:
-
2
])
:
...
...
@@ -106,7 +143,10 @@ class BaseHTTPSimulator (Node) :
self
.
url
=
"http://127.0.0.1:
%
s/
%
s/"
%
(
port
,
httpd
.
key
)
self
.
_alive
=
self
.
res
[
"alive.txt"
]
.
splitlines
()
self
.
_ping
=
0
self
.
states
=
StateSpace
(
net
)
if
simulator
is
None
:
self
.
simul
=
BaseSimulator
(
net
)
else
:
self
.
simul
=
simulator
def
start
(
self
)
:
log
(
"starting at
%
r"
%
self
.
url
)
shutdown
.
clear
()
...
...
@@ -129,32 +169,6 @@ class BaseHTTPSimulator (Node) :
if
self
.
watchdog
.
pid
:
os
.
kill
(
self
.
watchdog
.
pid
,
sig
)
log
(
"bye!"
)
def
getstate
(
self
,
state
)
:
marking
=
self
.
states
[
state
]
places
=
[
"
%
s =
%
s"
%
(
H
.
span
(
place
.
name
,
class_
=
"place"
),
H
.
span
(
marking
(
place
.
name
),
class_
=
"token"
))
for
place
in
sorted
(
self
.
states
.
net
.
place
(),
key
=
operator
.
attrgetter
(
"name"
))]
modes
=
[{
"state"
:
state
,
"mode"
:
i
,
"html"
:
"
%
s :
%
s"
%
(
H
.
span
(
trans
.
name
,
class_
=
"trans"
),
H
.
span
(
binding
,
class_
=
"binding"
))}
for
i
,
(
trans
,
binding
)
in
enumerate
(
marking
.
modes
)]
return
{
"id"
:
state
,
"states"
:
[{
"do"
:
"sethtml"
,
"select"
:
"#net"
,
"html"
:
H
.
i
(
self
.
states
.
net
)},
{
"do"
:
"settext"
,
"select"
:
"#state"
,
"text"
:
state
},
{
"do"
:
"setlist"
,
"select"
:
"#marking"
,
"items"
:
places
},
],
"modes"
:
[{
"select"
:
"#modes"
,
"items"
:
modes
},
],
}
def
init_index
(
self
)
:
return
{
"res"
:
"
%
sr"
%
self
.
url
,
"url"
:
self
.
url
,
...
...
@@ -191,15 +205,13 @@ class BaseHTTPSimulator (Node) :
"#alive .ui #ui-about"
:
"show information about the simulator"
}
@http
(
"application/json"
,
state
=
int
)
def
init
(
self
,
state
=-
1
)
:
if
state
<
0
:
state
=
self
.
states
.
current
return
{
"ui"
:
self
.
init_ui
(),
"state"
:
self
.
getstate
(
state
),
"help"
:
self
.
init_help
()}
ret
=
{
"ui"
:
self
.
init_ui
(),
"help"
:
self
.
init_help
()}
ret
.
update
(
self
.
simul
.
init
(
state
))
return
ret
@http
(
"application/json"
,
state
=
int
,
mode
=
int
)
def
succ
(
self
,
state
,
mode
)
:
state
=
self
.
states
.
succ
(
state
,
mode
)
return
self
.
getstate
(
state
)
return
self
.
simul
.
succ
(
state
,
mode
)
@http
(
"text/plain"
)
def
ping
(
self
)
:
ping
.
set
()
...
...
Please
register
or
login
to post a comment