forked from JamesPane-Joyce/DCEC_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototypes.py
More file actions
executable file
·296 lines (266 loc) · 10.8 KB
/
Copy pathprototypes.py
File metadata and controls
executable file
·296 lines (266 loc) · 10.8 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
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
"""
"""
from __future__ import print_function
from six import string_types
from six.moves import input # pylint: disable=locally-disabled,redefined-builtin
# We need to use the first type of import if running this script directly and the second type of
# import if we're using it in a package (such as for within Talos)
try:
import cleaning
except ImportError:
import DCEC_Library.cleaning as cleaning
class Namespace:
"""
>>> namespace = Namespace()
>>> namespace.add_basic_dcec()
>>> namespace.add_basic_logic()
>>> namespace.add_text_function("typedef Greeting Action")
>>> namespace.add_text_function("Greeting hello Agent")
True
>>> namespace.add_text_function("Boolean greet Agent Greeting")
True
"""
def __init__(self):
self.functions = {}
self.atomics = {}
self.sorts = {}
self.quant_map = {"TEMP": 0}
def add_code_sort(self, name, inheritance=None):
"""
Add a new sort to the namespace
:param name:
:param inheritance:
:return:
"""
if inheritance is None:
inheritance = []
if not (isinstance(name, string_types) and isinstance(inheritance, list)):
print("ERROR: function addCodeSort takes arguments of the form, string, "
"list of strings")
return False
for thing in inheritance:
if thing not in self.sorts.keys():
print("ERROR: sort " + thing + " is not previously defined")
return False
if name in self.sorts.keys():
return True
self.sorts[name] = inheritance
return True
def add_text_sort(self, expression):
"""
:param expression:
:return:
"""
temp = expression.replace("(", " ")
temp = temp.replace(")", " ")
temp = cleaning.strip_white_space(temp)
temp = temp.replace("`", "")
args = temp.split(",")
if len(args) == 2:
self.add_code_sort(args[1])
elif len(args) > 2:
self.add_code_sort(args[1], args[2:])
else:
print("ERROR: Cannot define the sort")
return False
def find_atomic_type(self, name):
"""
:param name:
:return:
"""
if name in self.atomics.keys():
return self.atomics[name]
def add_code_function(self, name, return_type, args_types):
"""
:param name:
:param return_type:
:param args_types:
:return:
"""
item = [return_type, args_types]
if name in self.functions.keys():
if item in self.functions[name]:
pass
else:
self.functions[name].append(item)
else:
self.functions[name] = [item]
return True
def add_text_function(self, expression):
"""
:param expression:
:return:
"""
temp = expression.replace("(", " ")
temp = temp.replace(")", " ")
temp = cleaning.strip_white_space(temp)
temp = temp.replace("`", "")
args = temp.split(",")
if args[0].lower() == "typedef":
return self.add_text_sort(expression)
elif len(args) == 2:
return self.add_text_atomic(expression)
return_type = ""
func_name = ""
func_args = []
# Find the return type
if args[0] in self.sorts.keys():
return_type = args[0]
args.remove(args[0])
# Find the function name
for arg in args:
if arg not in self.sorts.keys():
func_name = arg
args.remove(arg)
break
# Find the function args
for arg in args:
if arg in self.sorts.keys():
func_args.append(arg)
# Error Checking
if return_type == "" or func_name == "" or func_args == []:
print("ERROR: The function prototype was not formatted correctly.")
return False
# Add the function
return self.add_code_function(func_name, return_type, func_args)
def add_code_atomic(self, name, atomic):
"""
:param name:
:param atomic:
:return:
"""
if name in self.atomics.keys():
if atomic in self.atomics[name]:
return True
else:
print("ERROR: item " + name + " was previously defined as "
"an " + self.atomics[name] + ", you cannot overload "
"atomics.")
return False
else:
self.atomics[name] = atomic
return True
def add_text_atomic(self, expression):
"""
:param expression:
:return:
"""
temp = expression.replace("(", " ")
temp = temp.replace(")", " ")
temp = cleaning.strip_white_space(temp)
temp = temp.replace("`", "")
args = temp.split(",")
return_type = ""
func_name = ""
# Find the return type
for arg in args:
if arg in self.sorts.keys():
return_type = arg
args.remove(arg)
break
# Find the function name
for arg in args:
if arg not in self.sorts.keys():
func_name = arg
args.remove(arg)
break
return self.add_code_atomic(func_name, return_type)
def add_basic_dcec(self):
"""
This adds the DCEC* sorts and functions to the current namespace
"""
# The Basic DCEC Sorts
self.add_code_sort("Object")
self.add_code_sort("Agent", ["Object"])
self.add_code_sort("Self", ["Object", "Agent"])
self.add_code_sort("ActionType", ["Object"])
self.add_code_sort("Event", ["Object"])
self.add_code_sort("Action", ["Object", "Event"])
self.add_code_sort("Moment", ["Object"])
self.add_code_sort("Boolean", ["Object"])
self.add_code_sort("Fluent", ["Object"])
self.add_code_sort("Numeric", ["Object"])
self.add_code_sort("Set", ["Object"])
# The Basic DCEC Modal Functions
self.add_code_function("C", "Boolean", ["Moment", "Boolean"])
self.add_code_function("B", "Boolean", ["Agent", "Moment", "Boolean"])
self.add_code_function("K", "Boolean", ["Agent", "Moment", "Boolean"])
self.add_code_function("P", "Boolean", ["Agent", "Moment", "Boolean"])
self.add_code_function("I", "Boolean", ["Agent", "Moment", "Boolean"])
self.add_code_function("D", "Boolean", ["Agent", "Moment", "Boolean"])
self.add_code_function("S", "Boolean", ["Agent", "Agent", "Moment", "Boolean"])
self.add_code_function("O", "Boolean", ["Agent", "Moment", "Boolean", "Boolean"])
# Fluent Functions
self.add_code_function("action", "Action", ["Agent", "ActionType"])
self.add_code_function("initially", "Boolean", ["Fluent"])
self.add_code_function("holds", "Boolean", ["Fluent", "Moment"])
self.add_code_function("happens", "Boolean", ["Event", "Moment"])
self.add_code_function("clipped", "Boolean", ["Moment", "Fluent", "Moment"])
self.add_code_function("initiates", "Boolean", ["Event", "Fluent", "Moment"])
self.add_code_function("terminates", "Boolean", ["Event", "Fluent", "Moment"])
self.add_code_function("prior", "Boolean", ["Moment", "Moment"])
self.add_code_function("interval", "Fluent", ["Moment", "Boolean"])
self.add_code_function("self", "Self", ["Agent"])
self.add_code_function("payoff", "Numeric", ["Agent", "ActionType", "Moment"])
# Time Functions
self.add_code_function("lessOrEqual", "Boolean", ["Moment", "Moment"])
def add_basic_logic(self):
"""
Adds some basic logic operators to the namespace
"""
# Logical Functions
self.add_code_function("implies", "Boolean", ["Boolean", "Boolean"])
self.add_code_function("iff", "Boolean", ["Boolean", "Boolean"])
self.add_code_function("not", "Boolean", ["Boolean"])
self.add_code_function("and", "Boolean", ["Boolean", "Boolean"])
self.add_code_function("or", "Boolean", ["Boolean", "Boolean"])
self.add_code_function("xor", "Boolean", ["Boolean", "Boolean"])
def add_basic_numerics(self):
"""
Adds some functions for use for numerics. However, you still need to define
each number you might want to use explicitly before you could use it.
"""
# Numerical Functions
self.add_code_function("negate", "Numeric", ["Numeric"])
self.add_code_function("add", "Numeric", ["Numeric", "Numeric"])
self.add_code_function("sub", "Numeric", ["Numeric", "Numeric"])
self.add_code_function("multiply", "Numeric", ["Numeric", "Numeric"])
self.add_code_function("divide", "Numeric", ["Numeric", "Numeric"])
self.add_code_function("exponent", "Numeric", ["Numeric", "Numeric"])
# Comparison Functions
self.add_code_function("greater", "Boolean", ["Numeric", "Numeric"])
self.add_code_function("greaterOrEqual", "Boolean", ["Numeric", "Numeric"])
self.add_code_function("less", "Boolean", ["Numeric", "Numeric"])
self.add_code_function("lessOrEqual", "Boolean", ["Numeric", "Numeric"])
self.add_code_function("equals", "Boolean", ["Numeric", "Numeric"])
def no_conflict(self, type1, type2, level):
if type1 == "?":
return True, level
elif type1 == type2:
return True, level
elif type2 in self.sorts[type1]:
return True, level + 1
else:
returnlist = []
for i in self.sorts[type1]:
recurse_return = self.no_conflict(i, type2, level + 1)
if recurse_return[0]:
returnlist.append([recurse_return[1]])
if len(returnlist) > 0:
return True, min(returnlist)[0]
else:
return False, level
def print_namespace(self):
"""
Outputs the current namespace and it's various sorts, functions, and atomics
"""
for item in self.sorts.keys():
print(item, self.sorts[item])
for item in self.functions:
print(item, self.functions[item])
for item in self.atomics:
print(item, self.atomics[item])
if __name__ == "__main__":
# pylint: disable=wrong-import-position
import doctest
doctest.testmod()