def test_copy(self): original_numbers = self._get_original_numbers() simple = Graph(name='simple') simple.add_node(x=0.0, y=0.0, name='A') simple.add_node(x=0.0, y=1.0, name='B') simple.connect('A', 'B', name="I", directional=False) simple_copy = simple.copy(0.0, 0.0) nodes = [('A', Node(0.0, 0.0)), ('B', Node(0.0, 1.0))] edges = [('I', Edge('A', 'B', directional=False))] numbers = ( 2, # anchored_nodes 1, # anchored_edges 2, # unique_nodes 1, # unique_anchored_edges 0, # unique_unanchored_edges 0, # subgraphs 0 # anchored_subgraphs ) self._check_graph(simple_copy, 'simple(copy)', 'A', True, nodes, edges, numbers) self._check_numbers(original_numbers, 6, 3, 2) for node, node_copy in zip(simple.nodes.values(), simple_copy.nodes.values()): self.assertEqual(node, node_copy) self.assertIsNot(node, node_copy) for edge, edge_copy in zip(simple.edges.values(), simple_copy.edges.values()): self.assertEqual(edge, edge_copy) self.assertIsNot(edge, edge_copy)
def test_subgraphing(self): original_numbers = self._get_original_numbers() simple = Graph(name='simple') simple.add_node(x=0.0, y=0.0, name='A') simple.add_node(x=0.0, y=1.0, name='B') simple.connect('A', 'B', name="I", directional=False) simple_copy = simple.copy(1.0, 0.0) super_graph = Graph(name='super_graph', graphs=[simple, simple_copy]) super_graph.connect('simple:A', 'simple(copy):B', name='II', directional=True) nodes = [('simple:A', Node(0.0, 0.0)), ('simple:B', Node(0.0, 1.0)), ('simple(copy):A', Node(1.0, 0.0)), ('simple(copy):B', Node(1.0, 1.0))] edges = [('simple:I', Edge('simple:A', 'simple:B', directional=False)), ('simple(copy):I', Edge('simple(copy):A', 'simple(copy):B', directional=False)), ('II', Edge('simple:A', 'simple(copy):B', directional=True))] numbers = ( 4, # anchored_nodes 3, # anchored_edges 0, # unique_nodes 1, # unique_anchored_edges 0, # unique_unanchored_edges 2, # subgraphs 2 # anchored_subgraphs ) self._check_graph(super_graph, 'super_graph', 'simple:A', True, nodes, edges, numbers) self._check_numbers(original_numbers, 8, 6, 3)
def test_subtraction(self): original_count = len(Node._instances) node = Node(3, 5) tmp_node = node - node self.assertEqual(tmp_node.x, 0) self.assertEqual(tmp_node.y, 0) self.assertEqual(node.count, original_count + 2)
def add_node(self, x, y, **kwargs): ''' positional arguments: x (number) y (number) keyword arguments: name (str) anchor (bool): True - node is the anchor node. False - node is not the anchor node unless it is the first node added to the graph. ''' name = process_name("Node " + str(len(Node._instances)), kwargs) # location self.nodes[name] = Node(x, y, label_string=name, **kwargs) self.unique_nodes.append(name) # anchor if ('anchor' in kwargs): if kwargs['anchor']: self.anchor = name self.anchored_nodes = [self.anchor] elif self.anchor is None: self.anchor = name self.anchored_nodes = [self.anchor] return name
def test_addition(self): original_count = len(Node._instances) node = Node(3, 5) node = node + node self.assertEqual(node.x, 6) self.assertEqual(node.y, 10) self.assertEqual(node.count, original_count + 1)
def move(self, x, y): displacement = Node(x, y) - self.graph.nodes[self.graph.anchor] for node in self.graph.unique_nodes: self.graph.nodes[node].x += displacement.x self.graph.nodes[node].y += displacement.y for component in self.components: new_coords = component.center_of_rotation + displacement component.move(new_coords.x, new_coords.y)
def test_copy(self): node = Node(3, 5) original_count = node.count tmp_node = deepcopy(node) self.assertEqual(tmp_node, node) self.assertIsNot(tmp_node, node) self.assertEqual(tmp_node.count, original_count + 1) self.assertEqual(tmp_node.count, node.count)
def test_connect(self): original_numbers = self._get_original_numbers() simple = Graph(name='simple') simple.add_node(x=0.0, y=0.0, name='A') simple.add_node(x=0.0, y=1.0, name='B', anchor=True) simple.connect('A', 'B', name="I", directional=False) nodes = [('A', Node(0.0, 0.0)), ('B', Node(0.0, 1.0))] edges = [('I', Edge('A', 'B', directional=False))] numbers = ( 2, # anchored_nodes 1, # anchored_edges 2, # unique_nodes 1, # unique_anchored_edges 0, # unique_unanchored_edges 0, # subgraphs 0 # anchored_subgraphs ) self._check_graph(simple, 'simple', 'B', True, nodes, edges, numbers) self._check_numbers(original_numbers, 4, 2, 1)
def test_add_node(self): original_numbers = self._get_original_numbers() simple = Graph(name='simple') simple.add_node(x=0.0, y=1.0, name='A') nodes = [('A', Node(0.0, 1.0))] edges = [] numbers = ( 1, # anchored_nodes 0, # anchored_edges 1, # unique_nodes 0, # unique_anchored_edges 0, # unique_unanchored_edges 0, # subgraphs 0 # anchored_subgraphs ) self._check_graph(simple, 'simple', 'A', False, nodes, edges, numbers) self._check_numbers(original_numbers, 2, 0, 1)
def test_copying_with_subgraphing(self): original_numbers = self._get_original_numbers() pipe1 = Graph(name='pipe1') pipe1.add_node(x=0.0, y=0.0, name='inlet') pipe1.add_node(1.0, 0.0, name='outlet') pipe2 = pipe1.copy(x=1.5, y=0.0, name='pipe2') long_pipe1 = Graph(name='long_pipe1', graphs=[pipe1, pipe2]) long_pipe1.connect('pipe1:outlet', 'pipe2:inlet', name="junction") long_pipe1.connect('pipe1:inlet', 'pipe1:outlet', name="internal-connection1", directional=False) long_pipe1.connect('pipe2:inlet', 'pipe2:outlet', name="internal-connection2", directional=False) long_pipe2 = long_pipe1.copy(x=2.5, y=1.0, name='long_pipe2') long_pipe2.reflect(axis='y') circuit = Graph(name='circuit', graphs=[long_pipe1, long_pipe2]) circuit.connect('long_pipe1:pipe2:outlet', 'long_pipe2:pipe1:inlet', name="junction1") circuit.connect('long_pipe2:pipe2:outlet', 'long_pipe1:pipe1:inlet', name="junction2") nodes = [('long_pipe1:pipe1:inlet', Node(0.0, 0.0)), ('long_pipe1:pipe1:outlet', Node(1.0, 0.0)), ('long_pipe1:pipe2:inlet', Node(1.5, 0.0)), ('long_pipe1:pipe2:outlet', Node(2.5, 0.0)), ('long_pipe2:pipe1:inlet', Node(2.5, 1.0)), ('long_pipe2:pipe1:outlet', Node(1.5, 1.0)), ('long_pipe2:pipe2:inlet', Node(1.0, 1.0)), ('long_pipe2:pipe2:outlet', Node(0.0, 1.0))] edges = [('long_pipe1:junction', Edge('long_pipe1:pipe1:outlet', 'long_pipe1:pipe2:inlet', directional=True)), ('long_pipe1:internal-connection1', Edge('long_pipe1:pipe1:inlet', 'long_pipe1:pipe1:outlet', directional=False)), ('long_pipe1:internal-connection2', Edge('long_pipe1:pipe2:inlet', 'long_pipe1:pipe2:outlet', directional=False)), ('long_pipe2:junction', Edge('long_pipe2:pipe1:outlet', 'long_pipe2:pipe2:inlet', directional=True)), ('long_pipe2:internal-connection1', Edge('long_pipe2:pipe1:inlet', 'long_pipe2:pipe1:outlet', directional=False)), ('long_pipe2:internal-connection2', Edge('long_pipe2:pipe2:inlet', 'long_pipe2:pipe2:outlet', directional=False)), ('junction1', Edge('long_pipe1:pipe2:outlet', 'long_pipe2:pipe1:inlet', directional=True)), ('junction2', Edge('long_pipe2:pipe2:outlet', 'long_pipe1:pipe1:inlet', directional=True))] numbers = ( 8, # anchored_nodes 8, # anchored_edges 0, # unique_nodes 2, # unique_anchored_edges 0, # unique_unanchored_edges 2, # subgraphs 2 # anchored_subgraphs ) self._check_graph(circuit, 'circuit', 'long_pipe1:pipe1:inlet', True, nodes, edges, numbers) self._check_numbers(original_numbers, 16, 16, 7)
def move(self, x, y): displacement = Node(x, y) - self.nodes[self.anchor] for node in self.nodes.values(): node.move(displacement)
def test_instantiation(self): original_count = len(Node._instances) node = Node(3, 5) self.assertEqual(node.x, 3) self.assertEqual(node.y, 5) self.assertEqual(node.count, original_count + 1)
def test_inequality(self): node = Node(3, 5) tmp_node = Node() self.assertNotEqual(tmp_node, node)
def test_deletion(self): node = Node() original_count = len(Node._instances) del node self.assertEqual(len(Node._instances), original_count - 1)