def test_unique_node(self): g = Graph() self.assertIs(g.already_built(UniqueNumber(1)), None) # Make the nodes by getting g.add_node() to construct them u1 = g.add_node(UniqueNumber, 1) u1_a = g.add_node(UniqueNumber, 1) u2 = g.add_node(UniqueNumber, 2) self.assertIs(g.already_built(UniqueNumber, 1), u1) self.assertIs(u1, u1_a) self.assertIsNot(u1, u2) self.assertEqual(g.num_nodes(), 2)
def test_unique_node_via__init__(self): g = Graph() # Make the nodes by calling Node.__init__ u1 = g.add_node(UniqueNumber(1)) u1_a = g.add_node(UniqueNumber(1)) u2 = g.add_node(UniqueNumber(2)) self.assertIs(g.already_built(UniqueNumber(1)), u1) self.assertIs(u1, u1_a) self.assertIsNot(u1, u2) self.assertEqual(g.num_nodes(), 2)
def test_simple_action(self): g = Graph() node = g.add_node(Node) self.assertIs(g.value_of(node, 'attr'), None) g.do_action(SetAttr('attr', 1), node) self.assertEqual(g.value_of(node, 'attr'), 1)
def go(self, g: Graph, actor: NRef): g.set_attr(actor, self.name_, self.v)
def test_view(self): global g g = Graph(port_mates=port_mates) v = g.add_node(View(NodeCriterion(nodeclass=Number, tagclass=Avail))) dv = g.datum(v) n5 = g.add_node(Number(5)) g.add_tag(Avail, n5) n6 = g.add_node(Number(6)) # not tagged Avail g.do_touches() self.assertCountEqual(dv.viewing(), [n5.id]) g.remove_tag(n5, Avail) # Removing the tag should remove it from View g.do_touches() self.assertCountEqual(dv.viewing(), [])
def test_stdgraph_nodebuilding(self): g = Graph() # Make two Bricks b1 = g.add_node(Brick, value=1) b2 = g.add_node(Brick(2)) self.assertEqual(str(b1), 'Brick(1)') self.assertEqual(str(b2), 'Brick(2)') self.assertNotEqual(b1.id, b2.id) self.assertIs(as_node(g, b1.id), b1) self.assertIs(as_node(g, b2.id), b2) self.assertEqual(as_nodeid(b1), b1.id) self.assertEqual(as_nodeid(b2), b2.id) self.assertCountEqual(as_nodeids([b1, b2, None]), [b1.id, b2.id]) self.assertCountEqual(as_nodes(g, [b1.id, b2, None]), [b1, b2]) self.assertEqual(g.value_of(b1), 1) self.assertEqual(g.value_of(b2), 2) self.assertEqual(g.activation(b1), 0.72) # Tag one Brick Avail a1 = g.add_node(Avail, b1) self.assertTrue(g.has_edge(b1, 'tags', a1, 'taggees')) self.assertEqual(len(g.hops_from_port(b1, 'tags')), 1) alr = g.already_built(Avail, b1) self.assertIs(a1, alr) # Try to double-tag it: nothing should happen a1_a = g.add_node(Avail, b1) #pg(g) self.assertEqual(len(g.hops_from_port(b1, 'tags')), 1) self.assertIs(a1_a, a1)
self.assertTrue(g.has_edge(minus, 'operands', b10, 'source')) self.assertTrue(g.has_edge(minus, 'operands', b7, 'source')) self.assertTrue(g.has_edge(minus, 'minuend', b10, 'source')) self.assertFalse(g.has_edge(minus, 'minuend', b7, 'source')) self.assertFalse(g.has_edge(minus, 'subtrahend', b10, 'source')) self.assertTrue(g.has_edge(minus, 'subtrahend', b7, 'source')) self.assertTrue(g.has_edge(b10, 'source', minus, 'operands')) self.assertTrue(g.has_edge(b7, 'source', minus, 'operands')) self.assertTrue(g.has_edge(b10, 'source', minus, 'minuend')) self.assertFalse(g.has_edge(b7, 'source', minus, 'minuend')) self.assertFalse(g.has_edge(b10, 'source', minus, 'subtrahend')) self.assertTrue(g.has_edge(b7, 'source', minus, 'subtrahend')) #self.assertEqual(g.edge_weight(plus, 'operands', b1, 'source'), 1.0) def is_even(g, node: NRef): return g.value_of(node) & 1 == 0 if __name__ == '__main__': g = Graph() b1 = g.add_node(Brick, value=1) b2 = g.add_node(Brick(2)) a1 = g.add_node(Avail, [b1]) a11 = g.add_node(Avail, [b1]) pg(g) print(g.hops_from_node(b1))
def test_numble(self): Numble = make_numble_class(Brick, Target, Want, Avail, Allowed, [Plus, Times]) g = Graph(port_mates=port_mates) numble = Numble([4, 5, 6], 15) numble.build(g, None) #pg(g) ids = g.nodes_of_class(Target) self.assertEqual(len(ids), 1) targetid = ids[0] self.assertEqual(g.value_of(targetid), 15) ids = g.nodes_of_class(Want) self.assertEqual(len(ids), 1) wantid = ids[0] self.assertTrue(g.has_hop(wantid, 'taggees', targetid, 'tags')) brickids = g.nodes_of_class(Brick) self.assertEqual(len(brickids), 3) self.assertCountEqual([g.value_of(b) for b in brickids], [4, 5, 6]) ids = g.nodes_of_class(Avail) self.assertEqual(len(ids), 3) for b in brickids: self.assertTrue(g.has_tag(b, Avail)) operatorids = g.nodes_of_class(Operator) self.assertEqual(len(operatorids), 2) ids = g.nodes_of_class(Allowed) self.assertEqual(len(ids), 2) for o in operatorids: self.assertTrue(g.has_tag(o, Allowed))