-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_classes.py
430 lines (351 loc) · 14.6 KB
/
logical_classes.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from util import is_var
class Predicate(object):
"""Represents a predicate in our problem definition or a domain file
Attributes:
name (str): the name of the predicate
args (triple): the arguments of the predicate
value (boolean): false represents 'not'
"""
def __init__(self, name, args, value=None):
"""Constructor for Fact setting up useful flags and generating appropriate statement
Args:
name (str): The name of the predicate
args (of arguements): The variable arguments or the actual arguments of the predicate.
"""
super(Predicate, self).__init__()
self.name = name
self.args = [t if isinstance(t, Term) else Term(t) for t in args]
# self.name = name # 'in-room'
# self.args = args # ['noah', 'room1']
self.value = value # True
def __repr__(self):
"""Define internal string representation
"""
return '(PREDICATE: {!r}, {!r}, {!r})'.format(
self.name, self.args, self.value)
def __str__(self):
"""Define external representation when printed
"""
return '(PREDICATE: {!r}, {!r}, {!r})'.format(
self.name, self.args, self.value)
def __eq__(self, other):
"""Define behavior of == when applied to this object
"""
return isinstance(other, Predicate) and self.name == other.name and self.args == other.args and other.value == self.value
def __ne__(self, other):
"""Define behavior of != when applied to this object
"""
return not self == other
class Action(object):
"""Represents an action from our domain file
Attributes:
name (str): the name of the action to be taken
parameters (list of variables): the parameters for the action.
precondition (list of Predicates): the preconditions of an action (LHS of a rule almost)
effect (list of Predicates): the effect of the action (RHS of a rule almost)
"""
def __init__(self, name, parameters, precondition, effect):
"""
Args:
name (str): the name of the action to be taken
parameters (list of variables): the parameters for the action.
precondition (list of Predicates): the preconditions of an action (LHS of a rule almost)
effect (list of Predicates): the effect of the action (RHS of a rule almost)
"""
super(Action, self).__init__()
self.name = name
self.parameters = parameters
self.precondition = precondition
self.effect = effect
def __repr__(self):
"""Define internal string representation
"""
return '(ACTION: Name: {!r}, Parameters: {!r}, Precondition: {!r}, Effect: {!r})'.format(
self.name, self.parameters, self.precondition,
self.effect)
def __str__(self):
"""Define external representation when printed
"""
return '(ACTION: Name: {!r}, Parameters: {!r}, Precondition: {!r}, Effect: {!r})'.format(
self.name, self.parameters, self.precondition,
self.effect)
def __eq__(self, other):
"""Define behavior of == when applied to this object
"""
is_action = isinstance(other, Action)
return is_action and self.name == other.name and self.rhs == other.rhs
def __ne__(self, other):
"""Define behavior of != when applied to this object
"""
return not self == other
# class Statement(object):
# """
# Attributes:
# terms (listof Term): List of terms (Variable or Constant) in the
# statement, e.g. 'Nosliw' or '?d'
# predicate (str): The predicate of the statement, e.g. isa, hero, needs
# """
# def __init__(self, statement_list=[]):
# """Constructor for Statements with optional list of Statements that are
# converted to appropriate terms (and one predicate)
# Args:
# statement_list (mostly listof str|Term, first element is str): The element at
# index 0 is the predicate of the statement (a str) while the rest of
# the list is either instantiated Terms or strings to be passed to the
# Term constructor
# """
# super(Statement, self).__init__()
# self.terms = []
# self.predicate = ""
# if statement_list:
# self.predicate = statement_list[0]
# self.terms = [t if isinstance(t, Term) else Term(t) for t in statement_list[1:]]
# def __repr__(self):
# """Define internal string representation
# """
# return 'Statement({!r}, {!r})'.format(self.predicate, self.terms)
# def __str__(self):
# """Define external representation when printed
# """
# return "(" + self.predicate + " " + ' '.join((str(t) for t in self.terms)) + ")"
# def __eq__(self, other):
# """Define behavior of == when applied to this object
# """
# if self.predicate != other.predicate:
# return False
# for self_term, other_term in zip(self.terms, other.terms):
# if self_term != other_term:
# return False
# return True
# def __ne__(self, other):
# """Define behavior of != when applied to this object
# """
# return not self == other
class Term(object):
"""Represents a term (a Variable or Constant) in our knowledge base. Can
sorta be thought of as a super class of Variable and Constant, though
there is no inheritance implemented in the code.
Attributes:
term (Variable|Constant): The Variable or Constant that this term holds (represents)
"""
def __init__(self, term):
"""Constructor for Term which converts term to appropriate form
Args:
term (Variable|Constant|string): Either an instantiated Variable or
Constant, or a string to be passed to the appropriate constructor
"""
super(Term, self).__init__()
is_var_or_const = isinstance(term, Variable) or isinstance(term, Constant)
self.term = term if is_var_or_const else (Variable(term) if is_var(term) else Constant(term))
def __repr__(self):
"""Define internal string representation
"""
return 'Term({!r})'.format(self.term)
def __str__(self):
"""Define external representation when printed
"""
return str(self.term)
def __eq__(self, other):
"""Define behavior of == when applied to this object
"""
return (self is other
or isinstance(other, Term) and self.term.element == other.term.element
or ((isinstance(other, Variable) or isinstance(other, Constant))
and self.term.element == other.element))
def __ne__(self, other):
"""Define behavior of != when applied to this object
"""
return not self == other
class Variable(object):
"""Represents a variable used in statements
Attributes:
element (str): The name of the variable, e.g. '?x'
"""
def __init__(self, element):
"""Constructor for Variable
Args:
element (str): The name of the variable, e.g. '?x'
"""
super(Variable, self).__init__()
self.element = element
def __repr__(self):
"""Define internal string representation
"""
return 'Variable({!r})'.format(self.element)
def __str__(self):
"""Define external representation when printed
"""
return str(self.element)
def __eq__(self, other):
"""Define behavior of == when applied to this object
"""
return (self is other
or isinstance(other, Term) and self.term.element == other.term.element
or ((isinstance(other, Variable) or isinstance(other, Constant))
and self.term.element == other.element))
def __ne__(self, other):
"""Define behavior of != when applied to this object
"""
return not self == other
class Constant(object):
"""Represents a constant used in statements
Attributes:
element (str): The value of the constant, e.g. 'Nosliw'
"""
def __init__(self, element):
"""Constructor for Constant
Args:
element (str): The value of the constant, e.g. 'Nosliw'
"""
super(Constant, self).__init__()
self.element = element
def __repr__(self):
"""Define internal string representation
"""
return 'Constant({!r})'.format(self.element)
def __str__(self):
"""Define external representation when printed
"""
return str(self.element)
def __eq__(self, other):
"""Define behavior of == when applied to this object
"""
return (self is other
or isinstance(other, Term) and self.term.element == other.term.element
or ((isinstance(other, Variable) or isinstance(other, Constant))
and self.element == other.element))
def __ne__(self, other):
"""Define behavior of != when applied to this object
"""
return not self == other
class Binding(object):
"""Represents a binding of a constant to a variable, e.g. 'Nosliw' might be
bound to'?d'
Attributes:
variable (Variable): The name of the variable associated with this binding
constant (Constant): The value of the variable
"""
def __init__(self, variable, constant):
"""Constructor for Binding
Args:
variable (Variable): The name of the variable associated with this binding
constant (Constant): The value of the variable
"""
super(Binding, self).__init__()
self.variable = variable
self.constant = constant
def __repr__(self):
"""Define internal string representation
"""
return 'Binding({!r}, {!r})'.format(self.variable, self.constant)
def __str__(self):
"""Define external representation when printed
"""
return self.variable.term.element.upper() + " : " + self.constant.term.element
class Bindings(object):
"""Represents Binding(s) used while matching two statements
Attributes:
bindings (listof Bindings): bindings involved in match
bindings_dict (dictof Bindings): bindings involved in match where key is
bound variable and value is bound value,
e.g. some_bindings.bindings_dict['?d'] => 'Nosliw'
"""
def __init__(self):
"""Constructor for Bindings creating initially empty instance
"""
self.bindings = []
self.bindings_dict = {}
def __repr__(self):
"""Define internal string representation
"""
return 'Bindings({!r}, {!r})'.format(self.bindings_dict, self.bindings)
def __str__(self):
"""Define external representation when printed
"""
if self.bindings == []:
return "No bindings"
return ", ".join(((str(binding) + " : " + str(self.bindings_dict[binding])) for binding in self.bindings_dict))
def __getitem__(self,key):
"""Define behavior for indexing, e.g. random_bindings[key] returns
random_bindings.bindings_dict[key] when the dictionary is not empty
and the key exists, otherwise None
"""
return (self.bindings_dict[key]
if (self.bindings_dict and key in self.bindings_dict)
else None)
def add_binding(self, variable, value):
"""Add a binding from a variable to a value
Args:
variable (Variable): the variable to bind to
value (Constant): the value to bind to the variable
"""
self.bindings_dict[variable.element] = value.element
self.bindings.append(Binding(variable, value))
def bound_to(self, variable):
"""Check if variable is bound. If so return value bound to it, else False.
Args:
variable (Variable): variable to check for binding
Returns:
Variable|Constant|False: returns bound term if variable is bound else False
"""
if variable.element in self.bindings_dict.keys():
value = self.bindings_dict[variable.element]
if value:
return Variable(value) if is_var(value) else Constant(value)
return False
def test_and_bind(self, variable_term, value_term):
"""Check if variable_term already bound. If so return whether or not passed
in value_term matches bound value. If not, add binding between
variable_terma and value_term and return True.
Args:
value_term (Term): value to maybe bind
variable_term (Term): variable to maybe bind to
Returns:
bool: if variable bound returns whether or not bound value matches value_term,
else True
"""
bound = self.bound_to(variable_term.term)
if bound:
return value_term.term == bound
self.add_binding(variable_term.term, value_term.term)
return True
class ListOfBindings(object):
"""Container for multiple Bindings
Attributes:
list_of_bindings (listof Bindings): collects Bindings
"""
def __init__(self):
"""Constructor for ListOfBindings
"""
super(ListOfBindings, self).__init__()
self.list_of_bindings = []
def __repr__(self):
"""Define internal string representation
"""
return 'ListOfBindings({!r})'.format(self.list_of_bindings)
def __str__(self):
"""Define external representation when printed
"""
string = ""
for binding in self.list_of_bindings:
string += "Bindings for Predicates and Terms: " + str(binding) + "\n"
return string
def __len__(self):
"""Define behavior of len, when called on this class,
e.g. len(ListOfBindings([])) == 0
"""
return len(self.list_of_bindings)
def __getitem__(self,key):
"""Define behavior for indexing, e.g. random_list_of_bindings[i] returns
random_list_of_bindings[i][0]
"""
return self.list_of_bindings[key][0]
def add_bindings(self, bindings, facts_rules=[]):
"""Add given bindings to list of Bindings along with associated rules or facts
Args:
bindings (Bindings): bindings to add
facts_rules (listof Fact|Rule): rules or facts associated with bindings
Returns:
Nothing
"""
self.list_of_bindings.append((bindings, facts_rules))