Skip to content

Commit

Permalink
[core] Add Graph.copy method
Browse files Browse the repository at this point in the history
Add a new method to create a copy of a graph instance, relying on
chaining serialization and deserialization operations.
Add test suite to validate its behavior, and the underlying serialization processes.
  • Loading branch information
yann-lty committed Dec 9, 2024
1 parent e3eee47 commit 5d455fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions meshroom/core/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,12 @@ def toDict(self):
def asString(self):
return str(self.toDict())

def copy(self) -> "Graph":
"""Create a copy of this Graph instance."""
graph = Graph("")
graph._deserialize(self.serialize())
return graph

def serialize(self, asTemplate: bool = False) -> dict:
"""Serialize this Graph instance.
Expand Down
27 changes: 27 additions & 0 deletions tests/test_graphIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,30 @@ def test_serializeSingleNodeWithInputConnection(self):
assert len(otherGraph.compatibilityNodes) == 0
assert len(otherGraph.nodes) == 1
assert len(otherGraph.edges) == 0


class TestGraphCopy:
def test_graphCopyIsIdenticalToOriginalGraph(self):
graph = Graph("")

with registeredNodeTypes([SimpleNode]):
nodeA = graph.addNewNode(SimpleNode.__name__)
nodeB = graph.addNewNode(SimpleNode.__name__)

graph.addEdge(nodeA.output, nodeB.input)

graphCopy = graph.copy()
assert compareGraphsContent(graph, graphCopy)

def test_graphCopyWithUnknownNodeTypesDiffersFromOriginalGraph(self):
graph = Graph("")

with registeredNodeTypes([SimpleNode]):
nodeA = graph.addNewNode(SimpleNode.__name__)
nodeB = graph.addNewNode(SimpleNode.__name__)

graph.addEdge(nodeA.output, nodeB.input)

graphCopy = graph.copy()
assert not compareGraphsContent(graph, graphCopy)

0 comments on commit 5d455fb

Please sign in to comment.