in experimental/piranha_playground/rule_inference/piranha_agent.py [0:0]
def infer_rules_statically(self) -> str:
"""This function creates the first pass of the rule inference process.
It statically infers rules from the example code and returns a TOML representation of the rule graph.
:return: str, string containing the rule in TOML format
"""
parser = TemplateParser(self.language)
source_tree = parser.get_tree_from_code(self.source_code)
target_tree = parser.get_tree_from_code(self.target_code)
rules = {}
finder = GraphParser(source_tree, target_tree)
pairs = finder.parse_templates()
for comment_name, (nodes_before, nodes_after) in pairs.items():
inference_engine = Inference(nodes_before, nodes_after, parser.template_holes)
rule = inference_engine.static_infer()
rules[comment_name] = rule
# build a dict using finder.edges but with the rule names from rule_names
edges = {
rules[from_name].name: [rules[to_name].name for to_name in to_names]
for from_name, to_names in finder.edges.items()
}
edges = [
{"from": k, "to": v, "scope": "File"} for k, v in edges.items() if v != []
]
graph = RawRuleGraph(list(rules.values()), edges)
return graph.to_toml()