This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
78 lines (66 loc) · 2.23 KB
/
state.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from continuation import PropContinuation, EndContinuation, Continuation
from pypy.rlib import jit
from pypy.rlib.objectmodel import specialize
from pypy.rlib import rarithmetic
def get_printable_location(prop):
if prop:
l = str(prop) + str(prop.label())
else:
l = 'No Prop'
return l
jitdriver = jit.JitDriver(
reds=["cont", "f", "state"],
greens=["prop"],
get_printable_location=get_printable_location,
)
def state_eq(self, other):
assert isinstance(other, type(self))
for x in range(len(self.tokens)):
if self.tokens[x] != other.tokens[x]:
return False
return True
def state_hash(state):
return state.hash()
class State(object):
_immutable_ = True
__slots__ = ('tokens', 'net','labels', '_hash')
def __init__(self, tokens=None, net=None):
if tokens is None:
tokens = []
self.tokens = tokens
self.net = net
self._hash = 0
self.labels = {}
def successors(self):
return [self.net._get_state_from_cache(t.fire(self)) for t in self.net.enabled_transitions(self)]
def get(self, i):
return self.tokens[i]
def evaluate(self, prop, default = False):
f = EndContinuation(False)
cont = PropContinuation(prop, EndContinuation(True), f)
state = self
while not cont.is_done():
prop = cont.prop
if cont.__class__ is not PropContinuation:
jitdriver.can_enter_jit(cont=cont, f=f, state=state, prop=prop)
jitdriver.jit_merge_point(cont=cont, f=f, state=state, prop=prop)
cont, f, state = cont.activate(state)
assert isinstance(cont, EndContinuation)
return cont.result
@jit.purefunction
def hash(self):
if self._hash:
return self._hash
x = 0x345678
for i in range(len(self.tokens)):
y = self.tokens[i]
x = rarithmetic.intmask((1000003 * x) ^ y)
self._hash = x
return x
equals = state_eq
__eq__ = equals
__hash__ = hash
def __ne__(self, other):
return not self == other
def __repr__(self):
return 'State(%r)' % self.tokens