-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
113 lines (92 loc) · 3.35 KB
/
util.py
File metadata and controls
113 lines (92 loc) · 3.35 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
import logical_classes as lc
def is_var(var):
"""Check whether an element is a variable (either instance of Variable,
instance of Term (where .term is a Variable) or a string starting with
`'?'`, e.g. `'?d'`)
Args:
var (any): value to check
Returns:
bool
"""
if type(var) == str:
return var[0] == "?"
if isinstance(var, lc.Term):
return isinstance(var.term, lc.Variable)
return isinstance(var, lc.Variable)
def match(pred1, pred2, bindings=None):
"""Match two statements and return the associated bindings or False if there
is no binding
Args:
pred1 (Statement): statement to match with pred2
pred2 (Statement): statement to match with pred1
bindings (Bindings|None): already associated bindings
Returns:
Bindings|False: either associated bindings or no match found
"""
if len(pred1.args) != len(pred2.args) or pred1.name != pred2.name:
return False
if not bindings:
bindings = lc.Bindings()
return match_recursive(pred1.args, pred2.args, bindings)
def match_recursive(args1, args2, bindings): # recursive...
"""Recursive helper for match
Args:
args1 (listof Term): terms to match with args2
args2 (listof Term): terms to match with args1
bindings (Bindings): already associated bindings
Returns:
Bindings|False: either associated bindings or no match found
"""
if len(args1) == 0:
return bindings
if is_var(args1[0]):
if not bindings.test_and_bind(args1[0], args2[0]):
return False
elif is_var(args2[0]):
if not bindings.test_and_bind(args2[0], args1[0]):
return False
elif args1[0] != args2[0]:
return False
return match_recursive(args1[1:], args2[1:], bindings)
def instantiate(statement, bindings, value):
"""Generate Statement from given statement and bindings. Constructed statement
has bound values for variables if they exist in bindings.
Args:
statement (Statement): statement to generate new statement from
bindings (Bindings): bindings to substitute into statement
"""
def handle_term(term):
if is_var(term):
bound_value = bindings.bound_to(term.term)
return lc.Term(bound_value) if bound_value else term
else:
return term
new_terms = [handle_term(t) for t in statement.args]
return lc.Predicate(statement.name, new_terms, value)
def predq(element):
"""Check if element is a fact
Args:
element (any): element to check
Returns:
bool
"""
return isinstance(element, lc.Predicate)
def acteq(element):
"""Check if element is a rule
Args:
element (any): element to check
Returns:
bool
"""
return isinstance(element, lc.Action)
def printv(message, level, verbose, data=[]):
"""Prints given message formatted with data if passed in verbose flag is greater than level
Args:
message (str): message to print, if format string data should have values
to format with
level (int): value of verbose required to print
verbose (int): value of verbose flag
data (listof any): optional data to format message with
"""
if verbose > level:
print(message.format(*data) if data else message)