Skip to content

Commit

Permalink
hold the nodes' information in the MergedEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevengre committed Nov 22, 2024
1 parent df97a5f commit 3eb8bbc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
25 changes: 20 additions & 5 deletions pyk/src/pyk/kcfg/kcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,36 @@ class MergedEdge(EdgeLike):
edges: tuple[KCFG.Edge, ...]

def to_dict(self) -> dict[str, Any]:

def _merged_edge_to_dict(edge: KCFG.Edge) -> dict[str, Any]:
return {
'source': edge.source.to_dict(),
'target': edge.target.to_dict(),
'depth': edge.depth,
'rules': list(edge.rules),
}

return {
'source': self.source.id,
'target': self.target.id,
'edges': [edge.to_dict() for edge in self.edges],
'edges': [_merged_edge_to_dict(e) for e in self.edges],
}

@staticmethod
def from_dict(dct: dict[str, Any], nodes: Mapping[int, KCFG.Node]) -> KCFG.Successor:

def _merged_edge_from_dict(dct: dict[str, Any]) -> KCFG.Edge:
return KCFG.Edge(
KCFG.Node.from_dict(dct['source']),
KCFG.Node.from_dict(dct['target']),
dct['depth'],
tuple(dct['rules']),
)

return KCFG.MergedEdge(
nodes[dct['source']],
nodes[dct['target']],
tuple(
KCFG.Edge(edge['source'], edge['target'], edge['depth'], tuple(edge['rules']))
for edge in dct['edges']
),
tuple(_merged_edge_from_dict(e) for e in dct['edges']),
)

def replace_source(self, node: KCFG.Node) -> KCFG.Successor:
Expand Down
12 changes: 11 additions & 1 deletion pyk/src/tests/unit/test_kcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,19 @@ def _make_edge_dict(i: int, j: int, depth: int = 1, rules: tuple[str, ...] = ())


def merged_edge_dicts(*merged_edges: Iterable) -> list[dict[str, Any]]:
def _make_edge_dicts(*edges: Iterable) -> list[dict[str, Any]]:
def _make_edge_dict(i: int, j: int, depth: int = 1, rules: tuple[str, ...] = ()) -> dict[str, Any]:
return {
'source': node_dicts(1, start=i)[0],
'target': node_dicts(1, start=j)[0],
'depth': depth,
'rules': list(rules),
}

return [_make_edge_dict(*edge) for edge in edges]

def _make_merged_edge_dict(s: int, t: int, *edges: Iterable) -> dict[str, Any]:
return {'source': s, 'target': t, 'edges': edge_dicts(*edges)}
return {'source': s, 'target': t, 'edges': _make_edge_dicts(*edges)}

return [_make_merged_edge_dict(*merged_edge) for merged_edge in merged_edges]

Expand Down

0 comments on commit 3eb8bbc

Please sign in to comment.