Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion automates/model_assembly/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,8 @@ def __init__(
self.lambdas = [n for n in self.nodes if isinstance(n, LambdaNode)]
self.types = T

self.omit_source_info = False # Boolean flag that determines whether or not we generate source reference information for the GrFN

# NOTE: removing detached variables from GrFN
del_indices = list()
for idx, var_node in enumerate(self.variables):
Expand Down Expand Up @@ -1741,7 +1743,7 @@ def to_dict(self) -> Dict:
:rtype: type
:raises ExceptionName: Why the exception is raised.
"""
return {
data = {
"uid": self.uid,
"entry_point": "::".join(
["@container", self.namespace, self.scope, self.name]
Expand All @@ -1760,6 +1762,46 @@ def to_dict(self) -> Dict:
],
"metadata": [m.to_dict() for m in self.metadata],
}
if self.omit_source_info:
for v in data["variables"]:
for e in v["metadata"]:
if e["type"] == "code_span_reference":
e["code_span"] = None

for v in data["functions"]:
for e in v["metadata"]:
if e["type"] == "code_span_reference":
e["code_span"] = None

for v in data["subgraphs"]:
for e in v["metadata"]:
if e["type"] == "code_span_reference":
e["code_span"] = None

for v in data["types"]:
for e in v["metadata"]:
if e["type"] == "code_span_reference":
e["code_span"] = None

for v in data["metadata"]:
for e in v["metadata"]:
if e["type"] == "code_span_reference":
e["code_span"] = None

return data

def set_source_flag(self, flag):
"""Toggles a flag in the GrFN object that tells us whether or not we need to omit the source reference
information in the GrFN JSON. At the moment the source reference information is the 'code_span' information.
By default the source_info flag is set to False,

Args:
flag (Boolean): Flag is set to either True or False depending on whether or not we want
the source information to be removed (True) or kept (False)
"""

self.omit_source_info = flag


def to_json(self) -> str:
"""Outputs the contents of this GrFN to a JSON object string.
Expand Down
4 changes: 3 additions & 1 deletion automates/program_analysis/CAST2GrFN/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ def to_AIR(self):

return AutoMATES_IR(GenericIdentifier.from_str(air["entrypoint"]), C, V, T, [], [], [])

def to_GrFN(self):
def to_GrFN(self,omit_source_ref=False):
air = self.to_AIR()
grfn = GroundedFunctionNetwork.from_AIR(air)
if omit_source_ref:
grfn.set_source_flag(omit_source_ref)
return grfn

def write_cast_object(self, cast_value):
Expand Down
7 changes: 6 additions & 1 deletion scripts/program_analysis/run_gcc_to_grfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def get_args(args=sys.argv[1:]):
parser.add_argument("-p", "--plugin", help="Custom path to gcc plugin")
parser.add_argument("-Cg", "--CASTgraph", help="Create CAST graphviz pdf",
action='store_true')
parser.add_argument("-S", "--source", help="Turns off source reference information in the GrFN",
action='store_true')
options = parser.parse_args(args)
return options

Expand Down Expand Up @@ -145,7 +147,10 @@ def run_gcc_pipeline():
# Set random seed to 0 for UUID generation for consistent results in
# GrFN generation for tests
misc.rd.seed(0)
grfn = cast.to_GrFN()
if args.source:
grfn = cast.to_GrFN(omit_source_ref=True)
else:
grfn = cast.to_GrFN(omit_source_ref=False)
grfn.to_json_file(f"{program_name}--GrFN.json")

print("Transforming GrFN into AGraph...")
Expand Down