Skip to content

Commit b80dc47

Browse files
titomeisterVincent Raymond
and
Vincent Raymond
authored
Gromet Import PR (#324)
* Starting import implementation * In progress changes for GroMEt Imports * More updates for importing * Fixed import * bug so now that it can properly read modules * Updated CAST nodes for model import * Pushing current additional changes for Vincent * Adding required modules list file for generating gromet. * Adding Gromet Import Reference to importer * updating files needed to generate GroMEt for Bucky simplified * Fixing issue with unescaped characters Co-authored-by: Vincent Raymond <vraymond6@arizona.edu>
1 parent a1a47f0 commit b80dc47

20 files changed

+613
-351
lines changed

automates/gromet/metadata/gromet_creation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class GrometCreation(Metadata):
4242
if hasattr(Metadata, "attribute_map"):
4343
attribute_map.update(Metadata.attribute_map)
4444

45-
def __init__(self, metadata_type='gromet_creation', gromet_version='0.1.2', *args, **kwargs): # noqa: E501
45+
def __init__(self, metadata_type='gromet_creation', gromet_version='0.1.4', *args, **kwargs): # noqa: E501
4646
"""GrometCreation - a model defined in Swagger""" # noqa: E501
4747
self._metadata_type = None
4848
self._gromet_version = None

automates/program_analysis/CAST2GrFN/ann_cast/ann_cast_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def func_container_name_from_name_node(node) -> str:
369369
if isinstance(node, AnnCastAttribute):
370370
return f"{node.attr.name}_id{node.attr.id}"
371371
else:
372-
return f"{node.name}_id{node.id}"
372+
return f"{node.name}_id{node.id}"
373373

374374
def func_def_argument_name(node, arg_index: int) -> str:
375375
"""

automates/program_analysis/CAST2GrFN/ann_cast/annotated_cast.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
)
3434

3535
from automates.model_assembly.networks import GroundedFunctionNetwork, VariableNode
36+
from automates.program_analysis.CAST2GrFN.model.cast.model_import import ModelImport
3637

3738
class PipelineState:
3839
def __init__(self, ann_nodes: typing.List, grfn2_2: bool):
@@ -659,6 +660,27 @@ def equiv(self, other):
659660
def __str__(self):
660661
return ModelContinue.__str__(self)
661662

663+
class AnnCastModelImport(AnnCastNode):
664+
def __init__(self, node: ModelImport):
665+
super().__init__(self)
666+
self.name = node.name
667+
self.alias = node.alias
668+
self.symbol = node.symbol
669+
self.all = node.all
670+
self.source_refs = node.source_refs
671+
672+
def to_dict(self):
673+
result = super().to_dict()
674+
return result
675+
676+
def equiv(self, other):
677+
if not isinstance(other, AnnCastModelImport):
678+
return False
679+
return self.to_dict() == other.to_dict()
680+
681+
def __str__(self):
682+
return ModelImport.__str__(self)
683+
662684
class AnnCastModelIf(AnnCastNode):
663685
def __init__(self, expr, body, orelse, source_refs):
664686
super().__init__(self)

automates/program_analysis/CAST2GrFN/ann_cast/cast_to_annotated_cast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ def visit_model_break(self, node: ModelBreak):
166166
def visit_model_continue(self, node: ModelContinue):
167167
return AnnCastModelContinue(node.source_refs)
168168

169+
@_visit.register
170+
def visit_model_import(self, node: ModelImport):
171+
return AnnCastModelImport(node)
172+
169173
@_visit.register
170174
def visit_model_if(self, node: ModelIf):
171175
expr = self.visit(node.expr)

automates/program_analysis/CAST2GrFN/ann_cast/container_scope_pass.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def visit_assignment(
234234
right_src_ref = self.visit(node.right, base_func_scopestr, enclosing_con_scope, AssignSide.RIGHT)
235235
# The AnnCastTuple is added to handle scenarios where an assignment
236236
# is made by assigning to a tuple of values, as opposed to one singular value
237-
assert isinstance(node.left, AnnCastVar) or isinstance(node.left, AnnCastTuple) or isinstance(node.left, AnnCastAttribute), f"container_scope: visit_assigment: node.left is not AnnCastVar or AnnCastTuple it is {type(node.left)}"
237+
assert isinstance(node.left, AnnCastVar) or isinstance(node.left, AnnCastTuple) or isinstance(node.left, AnnCastAttribute), f"container_scope: visit_assigment: node.left is {type(node.left)}"
238238
left_src_ref = self.visit(node.left, base_func_scopestr, enclosing_con_scope, AssignSide.LEFT)
239239

240240
return combine_grfn_con_src_refs([right_src_ref, left_src_ref])
@@ -312,7 +312,7 @@ def visit_record_def(self, node: AnnCastRecordDef, base_func_scopestr, enclosing
312312
assert(assign_side == AssignSide.NEITHER)
313313
# we do not visit the name because it is a string
314314
assert isinstance(node.name, str)
315-
classscope = enclosing_con_scope + [node.name]
315+
classscope = enclosing_con_scope # + [node.name]
316316
# NOTE:
317317
# node.bases is a list of strings
318318
# node.funcs is a list of Vars
@@ -395,7 +395,8 @@ def visit_literal_value(self, node: AnnCastLiteralValue, base_func_scopestr, enc
395395
@_visit.register
396396
def visit_loop(self, node: AnnCastLoop, base_func_scopestr, enclosing_con_scope, assign_side):
397397
# we believe the start of the container should not be on either side of an assignment
398-
assert(assign_side == AssignSide.NEITHER)
398+
# (NOTE: In the case of list/dict comprehensions, they could be on the right hand side)
399+
assert(assign_side == AssignSide.NEITHER or assign_side == AssignSide.RIGHT)
399400
# store the base_func_scopestr for this container
400401
node.base_func_scopestr = base_func_scopestr
401402

@@ -435,10 +436,15 @@ def visit_model_break(self, node: AnnCastModelBreak, assign_side):
435436
def visit_model_continue(self, node: AnnCastModelContinue, assign_side):
436437
pass
437438

439+
@_visit.register
440+
def visit_model_import(self, node: AnnCastModelImport, base_func_scopestr, enclosing_con_scope, assign_side):
441+
pass
442+
438443
@_visit.register
439444
def visit_model_if(self, node: AnnCastModelIf, base_func_scopestr, enclosing_con_scope, assign_side):
440445
# we believe the start of the container should not be on either side of an assignment
441-
assert(assign_side == AssignSide.NEITHER)
446+
# (NOTE: In the case of list/dict comprehensions, they could be on the right hand side)
447+
assert(assign_side == AssignSide.NEITHER or assign_side == AssignSide.RIGHT)
442448
# store the base_func_scopestr for this container
443449
node.base_func_scopestr = base_func_scopestr
444450
# want orig enclosing
@@ -540,7 +546,7 @@ def visit_string(self, node: AnnCastString, base_func_scopestr, enclosing_con_sc
540546
pass
541547

542548
@_visit.register
543-
def visit_subscript(self, node: AnnCastSubscript, assign_side):
549+
def visit_subscript(self, node: AnnCastSubscript, base_func_scoptr, enclosing_con_scope, assign_side):
544550
pass
545551

546552
@_visit.register

automates/program_analysis/CAST2GrFN/ann_cast/grfn_assignment_pass.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ def visit_model_break(self, node: AnnCastModelBreak, add_to: typing.Dict):
224224
def visit_model_continue(self, node: AnnCastModelContinue, add_to: typing.Dict):
225225
pass
226226

227+
@_visit.register
228+
def visit_model_import(self, node: AnnCastModelImport, add_to: typing.Dict):
229+
pass
230+
227231
@_visit.register
228232
def visit_model_if(self, node: AnnCastModelIf, add_to: typing.Dict):
229233
self.visit(node.expr, add_to)

automates/program_analysis/CAST2GrFN/ann_cast/grfn_var_creation_pass.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def visit_assignment(self, node: AnnCastAssignment):
425425
self.visit(node.right)
426426
# The AnnCastTuple is added to handle scenarios where an assignment
427427
# is made by assigning to a tuple of values, as opposed to one singular value
428-
assert isinstance(node.left, AnnCastVar) or isinstance(node.left, AnnCastTuple), f"container_scope: visit_assigment: node.left is not AnnCastVar or AnnCastTuple it is {type(node.left)}"
428+
assert isinstance(node.left, AnnCastVar) or isinstance(node.left, AnnCastTuple) or isinstance(node.left, AnnCastAttribute), f"container_scope: visit_assigment: node.left is not AnnCastVar or AnnCastTuple it is {type(node.left)}"
429429
self.visit(node.left)
430430

431431
@_visit.register
@@ -529,6 +529,10 @@ def visit_model_break(self, node: AnnCastModelBreak):
529529
def visit_model_continue(self, node: AnnCastModelContinue):
530530
pass
531531

532+
@_visit.register
533+
def visit_model_import(self, node: AnnCastModelImport):
534+
pass
535+
532536
@_visit.register
533537
def visit_model_if(self, node: AnnCastModelIf):
534538
self.create_grfn_vars_model_if(node)

automates/program_analysis/CAST2GrFN/ann_cast/id_collapse_pass.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from re import A
12
import typing
23
from collections import defaultdict
34
from functools import singledispatchmethod
@@ -90,7 +91,7 @@ def visit_assignment(self, node: AnnCastAssignment, at_module_scope):
9091
self.visit(node.right, at_module_scope)
9192
# The AnnCastTuple is added to handle scenarios where an assignment
9293
# is made by assigning to a tuple of values, as opposed to one singular value
93-
assert isinstance(node.left, AnnCastVar) or isinstance(node.left, AnnCastTuple) or isinstance(node.left, AnnCastAttribute), f"id_collapse: visit_assigment: node.left is not AnnCastVar it is {type(node.left)}"
94+
assert isinstance(node.left, AnnCastVar) or isinstance(node.left, AnnCastTuple) or isinstance(node.left, AnnCastAttribute), f"id_collapse: visit_assigment: node.left is {type(node.left)}"
9495
self.visit(node.left, at_module_scope)
9596

9697
@_visit.register
@@ -108,16 +109,27 @@ def visit_binary_op(self, node: AnnCastBinaryOp, at_module_scope):
108109

109110
@_visit.register
110111
def visit_call(self, node: AnnCastCall, at_module_scope):
111-
assert isinstance(node.func, AnnCastName) or isinstance(node.func, AnnCastAttribute)
112+
if isinstance(node.func, AnnCastLiteralValue):
113+
return
114+
115+
assert isinstance(node.func, AnnCastName) or isinstance(node.func, AnnCastAttribute), f"node.func is type f{type(node.func)}"
112116
if isinstance(node.func, AnnCastName):
113117
node.func.id = self.collapse_id(node.func.id)
114118
node.invocation_index = self.next_function_invocation(node.func.id)
115119
else:
120+
if isinstance(node.func.value, AnnCastCall):
121+
self.visit(node.func.value, at_module_scope)
122+
elif isinstance(node.func.value, AnnCastAttribute):
123+
self.visit(node.func.value, at_module_scope)
124+
elif isinstance(node.func.value, AnnCastSubscript):
125+
self.visit(node.func.value, at_module_scope)
126+
elif isinstance(node.func.value, AnnCastBinaryOp):
127+
self.visit(node.func.value, at_module_scope)
128+
else:
129+
node.func.value.id = self.collapse_id(node.func.value.id)
116130
node.func.attr.id = self.collapse_id(node.func.attr.id)
117-
node.func.value.id = self.collapse_id(node.func.value.id)
118131
node.invocation_index = self.next_function_invocation(node.func.attr.id)
119132

120-
121133
# cache Call node to later determine if this Call has a FunctionDef
122134
call_name = call_container_name(node)
123135
self.cached_call_nodes[call_name] = node
@@ -193,6 +205,10 @@ def visit_model_if(self, node: AnnCastModelIf, at_module_scope):
193205
def visit_return(self, node: AnnCastModelReturn, at_module_scope):
194206
self.visit(node.value, at_module_scope)
195207

208+
@_visit.register
209+
def visit_model_import(self, node: AnnCastModelImport, at_module_scope):
210+
pass
211+
196212
@_visit.register
197213
def visit_module(self, node: AnnCastModule, at_module_scope):
198214
# we cache the module node in the AnnCast object

automates/program_analysis/CAST2GrFN/ann_cast/lambda_expression_pass.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ def visit_model_break(self, node: AnnCastModelBreak) -> str:
394394
def visit_model_continue(self, node: AnnCastModelContinue) -> str:
395395
return node.expr_str
396396

397+
@_visit.register
398+
def visit_model_import(self, node: AnnCastModelImport) -> str:
399+
pass
400+
397401
@_visit.register
398402
def visit_model_if(self, node: AnnCastModelIf) -> str:
399403
# top interface lambda

0 commit comments

Comments
 (0)