git @ Cat's Eye Technologies Samovar / de5a4bd
Python 3 support seems complete (CAVEAT: random hash differently!) Chris Pressey 6 years ago
5 changed file(s) with 45 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
5353 a given action has been taken previously or not.
5454 * (+) Consider macros.
5555 * Output scenarios to JSON.
56 * Python 3 support.
5756 * Consider a simple equality rule.
5857 * Consider allowing `∨`.
5958 * Take AST code from SixtyPical or ALPACA (or try to do better, perhaps with
191191 item(oilcan).
192192 goal [holding(Ignatz,brick)].
193193 }
194 ===> Ignatz picks up the brick.
195 ===> Ignatz puts down the brick.
194196 ===> Ignatz picks up the oilcan.
195 ===> Ignatz puts down the oilcan.
196 ===> Ignatz picks up the brick.
197 ===> Ignatz picks up the oilcan.
197 ===> Ignatz picks up the brick.
198198
199199 chairs
200200 ------
228228
229229 goal [].
230230 }
231 ===> Wembley sits down in the recliner.
232 ===> Wembley leans back in the recliner.
231233 ===> Hastings sits down in the chair.
232 ===> Hastings leans back in the chair.
233234 ===> Petersen sits down in the sofa.
234 ===> Wembley sits down in the recliner.
235235
236236
237237 no need for functions
259259
260260 goal [].
261261 }
262 ===> Bob scratches his head.
263 ===> Bob scratches his head.
264 ===> Alice scratches her head.
265 ===> Alice scratches her head.
262 ===> Alice scratches her head.
263 ===> Alice scratches her head.
264 ===> Bob scratches his head.
265 ===> Bob scratches his head.
266266
267267 This loses the nice property of the function name being a readable
268268 placeholder in the sentence, but you can now use named variables
281281
282282 goal [].
283283 }
284 ===> Bob scratches his head.
285 ===> Bob scratches his head.
286 ===> Alice scratches her head.
287 ===> Alice scratches her head.
284 ===> Alice scratches her head.
285 ===> Alice scratches her head.
286 ===> Bob scratches his head.
287 ===> Bob scratches his head.
22 from copy import deepcopy
33 from pprint import pprint
44 import random
5
6
7 # Python 2/3
8 try:
9 unicode = unicode
10 except NameError:
11 unicode = str
512
613
714 class AST(object):
66 from samovar.ast import Assert, Retract
77 from samovar.query import match_all
88 from samovar.terms import Term
9
10
11 # Python 2/3
12 try:
13 xrange = xrange
14 except NameError:
15 xrange = range
916
1017
1118 class Event(object):
88
99
1010 class AbstractTerm(object):
11
12 def __ne__(self, other):
13 return not self.__eq__(other)
14
15 def __hash__(self):
16 return hash(unicode(self))
11 pass
1712
1813
1914 class Term(AbstractTerm):
2015 def __init__(self, constructor, *subterms):
21 self.constructor = constructor
22 self.subterms = subterms
16 self.t = tuple([constructor] + list(subterms))
17
18 @property
19 def constructor(self):
20 return self.t[0]
21
22 @property
23 def subterms(self):
24 return self.t[1:]
2325
2426 def __str__(self):
2527 if len(self.subterms) == 0:
3739 )
3840
3941 def __eq__(self, other):
40 if not isinstance(other, Term):
41 return False
42 if self.constructor != other.constructor:
43 return False
44 if len(self.subterms) != len(other.subterms):
45 return False
46 for (st1, st2) in zip(self.subterms, other.subterms):
47 if st1 != st2:
48 return False
49 return True
42 return isinstance(other, Term) and self.t == other.t
43
44 def __hash__(self):
45 return hash(self.t)
5046
5147 def is_atom(self):
5248 return len(self.subterms) == 0
9288 return "%s(%r)" % (self.__class__.__name__, self.name)
9389
9490 def __eq__(self, other):
95 if not isinstance(other, Var):
96 return False
97 return self.name == other.name
91 return isinstance(other, Var) and self.name == other.name
92
93 def __hash__(self):
94 return hash(self.name)
9895
9996 def is_atom(self):
10097 return False