-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass.py
executable file
·218 lines (154 loc) · 4.26 KB
/
ass.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python2
#-*- coding: utf-8 -*-
#lol bootstrap, © koo5
import importlib
import pyke
from pyke import knowledge_engine
e = knowledge_engine.engine(__file__)
"""
knowledge base
top down:
goal: the gui
editor window is a kind of pygame window
pygame window is a kind of thing..
"""
def ass(verb, *args):
e.assert_('a', verb, args)
class Node(object):
def to_text(self):
return str(self)
class String():
pass
class Object(object):
@classmethod
def to_text(cls):
return "Object"
class Assertion(Node):
def ass(self):
if self.object != None:
ass(self.verb, self.subject, self.object)
else:
e.assert_('a', self.verb, (self.subject, ))
#Assertion(verb =
class List(Node):
def __init__(self, items):
self.items = items
for i in self.items:
i.parent = self
print self.to_text()
def to_text(self):
r=""
for i in self.items:
r+=i.to_text() + "\n"
return r
class Codes(List):
def __init__(self, items):
self.items = items
def ass(self):
for i in self.items:
i.ass()
class ClassDefinition(Node):
def __init__(self, name, superclass = None):
self.name = name
self.superclass = superclass
def ass(self):
e.assert_('a', 'is class', (self.name, self.superclass))
def to_text(self):
return self.name + " is a subclass of " + self.superclass.to_text()
class VariableDeclaration(Node):
def __init__(self, name, type):
self.name = name
self.type = type
def ass(self):
e.assert_('a', 'is an object of type', (self, self.type))
def to_text(self):
return self.name + " is an object of type " + self.type
class Import(Node):
def __init__(self, name):
self.name = name
self.loaded = None
def to_text(self):
return "import the python module "+self.name +(str(self.loaded) if self.loaded else "")
def run(self):
self.loaded = importlib.import_module(self.name)
def ass(self):
ass('is a python module import', self)
class Section(Node):
def __init__(self, name, codes):
self.name = name
self.codes = codes
def to_text(self):
return "section "+self.name+":\n" + self.codes.to_text()
class Program(Node):
def __init__(self, name, codes):
self.name = name
self.codes = codes
self.memory = dict()
def to_text(self):
return self.name+"\n"+self.codes.to_text()
def run(self):
self.ass()
e.activate('a')
def ass(self):
ass('is a kind of', self, Node)
self.codes.ass()
class PythonCall(Node):
def __init__(self, module, name, arguments):
self.module = module
self.name = name
self.arguments = arguments
class PythonTuple(Node):
def __init__(self, items):
self.items = items
class Rule(Node):
def __init__(self, time, action, codes):
self.time = time
self.action = action
class ActionParameter(Node):
def __init__(self, name, type):
self.name = name
self.type = type
class ActionSignature(Node):
def __init__(self, items):
self.items = items
class ActionDefinition(Node):
def __init__(self, signature):
self.signature = signature
class Resolver(Node):
def __init__(self, clues):
self.clues = clues
def resolve(self):
print "resolving", self.clues
items = program.codes.items
print items
if self.clues.has_key("type"):
print "by type", self.clues['type']
items = filter(lambda i: isinstance(i, self.clues['type']), items)
print "resolved to", items
if len(items) == 1:
return items
else:
raise BaseException("resolution failed")
program = Program(
name = "x", codes = Codes(
[
Section("builtin", codes = Codes(
[
ActionDefinition(ActionSignature(["importing", ActionParameter(name = 'module', type = String)]))
])),
ClassDefinition('pygame window'),
VariableDeclaration('window',
Resolver({'type': ClassDefinition, 'name':'pygame window'})),
Import('os'),
Rule('before',
Resolver({'list':Resolver({'type':Section, 'name':'builtin'}), 'index':0, 'type':ActionDefinition, 'key':"signature"}), Codes([])),
PythonCall(module = 'os', name = 'putenv', arguments = PythonTuple(["SDL_VIDEO_ALLOW_SCREENSAVER","1"])),
Import('pygame')
]
))
#program = Program(name = "x", codes=Codes([]))
#program.ass()
print Resolver({'type':Section, 'name':'builtin'}).resolve()
e.get_kb('a').dump_universal_facts()
e.get_kb('a').dump_specific_facts()
print program.to_text()