Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathproduction.py
More file actions
Latest commit
126 lines (104 loc) · 4.06 KB
/
Copy pathproduction.py
File metadata and controls
126 lines (104 loc) · 4.06 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from . importmodel
importinspect
importre
from . importpattern
try:
Set=set
except:
fromsetsimportSet
classProductionException(Exception):
pass
classProduction:
def__init__(self,system,name,func):
self.system=system
self.name=name
self.base_utility=0
a,va,hk,d,kwa,kwd,ant=inspect.getfullargspec(func)
self.keys=a
patterns={}
fori,nameinenumerate(a[:]):
ifname=='utility':
self.base_utility=d[i]
dela[i]
else:
patterns[name]=d[i]
self.pattern_specs=patterns
self.pattern=pattern.Pattern(patterns)
self.bound=None
self.original_func=func
code=inspect.getsource(func)
m=re.match(r'[^(]+\([^(]*\):',code)
self.code=code[m.end():]
code='if True:'+code[m.end():]
self.func=compile(code,'<production-%s>'%self.name,'exec')
defmatch(self,obj):
b=self.pattern.match(obj)
ifbisNone: returnFalse
self.bound=b
returnTrue
deffire(self,context):
self.system.sch.bound=self.bound
exec(self.func, context,self.bound)
classProductionSystem(model.Model):
production_time=0.05
production_match_delay=0
_auto_run_start=False
def_convert_info(self,objects,methods):
self._productions=[]
self._initializers=[]
self._keys_used=Set()
fork,vinlist(methods.items()):
a,va,hk,d,kwa,kwd,ant=inspect.getfullargspec(v)
ifvaisNoneandhkisNone:
ifdisNoneandlen(a)==0:
p=Production(self,k,v)
self._initializers.append(p)
ifdisnotNoneandaisnotNoneandlen(a)==len(d):
p=Production(self,k,v)
self._keys_used.update(p.keys)
self._productions.append(p)
self.sch.add(self._process_productions)
def_calc_context(self):
context={}
keys=Set(self._keys_used)
if'self'inkeys: keys.remove('self')
iflen(keys)==0: top=self
m=self
whilemisnotNone:
fork,vinlist(m.__dict__.items()):
ifknotincontextandk[0]!='_'andk!='parent'andisinstance(v,object) andnotisinstance(v,model.MethodWrapper):
context[k]=v
ifkinkeys:
keys.remove(k)
iflen(keys)==0: top=m
if'top'inkeys: top=m
m=m.parent
if'top'inkeys: keys.remove('top')
iflen(keys)>0:
raiseProductionException("Production is matching on an unknown module '%s'"%(keys))
#TODO: rethink this. top should refer to the highest level in the tree.
# but it would be nice if we had something to refer to the highest level
# that could cause a matching change, to optimize the yield._top.changes
# call. This was the original idea and may impact the logic calculating
# top above. However, any fix to this should make sure not to break the
# rock paper scissors tutorials.
whilehasattr(top,'parent') andtop.parentisnotNone: top=top.parent
context['self']=self
context['top']=top
self._top=top
self._context=context
def_process_productions(self):
self._calc_context()
foriinself._initializers:
i.fire(self._context)
whileTrue:
ifself.production_match_delay>0: yieldself.production_match_delay
match=[pforpinself._productionsifp.match(self._context)]
iflen(match)==0:
yieldself._top.changes
else:
choice=self.random.choice(match)
self.log.production=choice.name
yieldself.production_time-self.production_match_delay
self.log.production=None
choice.fire(self._context)