1- # from typing import Generator
2- from typing import Optional
1+ from typing import TYPE_CHECKING
32
43from compas .datastructures import Graph
54from compas_model .elements import Element # noqa: F401
65
7- # Ideally, graph (and mesh) are rewritten to use dedicated classes for nodes and edges.
8- # This will allow more fine-grained control over the (types of) attributes added to nodes and edges.
9- # It will also provide a much more intuitive API.
6+ if TYPE_CHECKING :
7+ from compas_model .models import Model
108
119
1210class InteractionGraph (Graph ):
@@ -26,57 +24,16 @@ class InteractionGraph(Graph):
2624
2725 """
2826
29- @property
30- def __data__ (self ) -> dict :
31- data = super ().__data__
32-
33- for node , attr in data ["node" ].items ():
34- # this modifies the attributes in place
35- # as a consequence, after accessing the __data__ property of the graph,
36- # the graph is broken
37- # to prevent this, the attribute dict should be copied
38- attr = attr .copy ()
39- attr ["element" ] = str (attr ["element" ].guid )
40- data ["node" ][node ] = attr
41- return data
42-
43- @classmethod
44- def __from_data__ (cls , data : dict , guid_element : dict [str , Element ]) -> "InteractionGraph" :
45- graph = super (InteractionGraph , cls ).__from_data__ (data )
46- for node , attr in graph .nodes (data = True ):
47- element = guid_element [attr ["element" ]]
48- attr ["element" ] = element # type: ignore
49- element .graphnode = node
50- return graph
51-
52- def copy (self ) -> "InteractionGraph" :
53- # A custom implementation of copy is needed to allow passing the element dictionary to __from_data__.
54- guid_element = {}
55- for _ , node in self .nodes (data = True ):
56- element = node ["element" ]
57- guid_element [str (element .guid )] = element
58- return self .__from_data__ (self .__data__ , guid_element )
59-
60- def __init__ (
61- self ,
62- default_node_attributes : Optional [dict ] = None ,
63- default_edge_attributes : Optional [dict ] = None ,
64- name : Optional [str ] = None ,
65- ** kwargs ,
66- ) -> None :
67- super ().__init__ (
68- default_node_attributes = default_node_attributes ,
69- default_edge_attributes = default_edge_attributes ,
70- name = name ,
71- ** kwargs ,
72- )
27+ model : "Model"
7328
29+ def __init__ (self , ** kwargs ) -> None :
30+ super ().__init__ (** kwargs )
7431 self .update_default_node_attributes (element = None )
75- self .update_default_edge_attributes (interactions = None )
32+ self .update_default_edge_attributes (modifiers = None , contacts = None )
33+ self .model = None # type: ignore
7634
7735 def __str__ (self ) -> str :
7836 output = super ().__str__ ()
79-
8037 output += "\n "
8138 output += self ._build_interactions_str ()
8239 return output
@@ -91,23 +48,18 @@ def _build_interactions_str(self) -> str:
9148 edge = nbr , node
9249
9350 lines .append (
94- "- {}: {} {} {} " .format (
51+ "- {}: {} {}" .format (
9552 nbr ,
9653 self .edge_attribute (edge , "modifiers" ),
9754 self .edge_attribute (edge , "contacts" ),
98- self .edge_attribute (edge , "collisons" ),
9955 ) # type: ignore
10056 )
10157 return "\n " .join (lines ) + "\n "
10258
103- def clear_edges (self ):
104- """Clear all the edges and connectivity information of the graph."""
105- for u , v in list (self .edges ()):
106- del self .edge [u ][v ]
107- if v in self .adjacency [u ]:
108- del self .adjacency [u ][v ]
109- if u in self .adjacency [v ]:
110- del self .adjacency [v ][u ]
59+ def add_element (self , element : Element ) -> int :
60+ node = self .add_node (element = str (element .guid ))
61+ element .graphnode = node
62+ return node
11163
11264 def node_element (self , node : int ) -> Element :
11365 """Get the element associated with the node.
@@ -122,4 +74,14 @@ def node_element(self, node: int) -> Element:
12274 :class:`compas_model.elements.Element`
12375
12476 """
125- return self .node_attribute (node , "element" ) # type: ignore
77+ guid : str = self .node_attribute (node , "element" ) # type: ignore
78+ return self .model ._elements [guid ]
79+
80+ def clear_edges (self ):
81+ """Clear all the edges and connectivity information of the graph."""
82+ for u , v in list (self .edges ()):
83+ del self .edge [u ][v ]
84+ if v in self .adjacency [u ]:
85+ del self .adjacency [u ][v ]
86+ if u in self .adjacency [v ]:
87+ del self .adjacency [v ][u ]
0 commit comments