Example #1
0
 def test_and_not(self):
     g = TestGraph(Numble([4, 5, 15, 6], 15))
     targetid = NodeOfClass(Target).see_one(g)
     spec = And(Not(HasSameValue(targetid)), NodeWithTag(Number, Avail))
     expect = [Brick(4), Brick(5), Brick(6)]
     got = [g.datum(nodeid) for nodeid in spec.see_all(g)]
     self.assertCountEqual(got, expect)
Example #2
0
    def test_node_of_class(self):
        spec = NodeOfClass(Number)
        g = TestGraph(Numble([4, 5, 6], 15))
        expect = [Brick(4), Brick(5), Brick(6), Target(15)]
        got = [g.datum(nodeid) for nodeid in spec.see_all(g)]
        self.assertCountEqual(got, expect)

        got = g.datum(spec.see_one(g))
Example #3
0
    def test_nodespec(self):
        g = TestGraph(Numble([4, 5, 6], 15))

        spec = NodeSpec()  # All nodes
        expect = [
            Brick(4),
            Brick(5),
            Brick(6),
            Target(15),
            Workspace(),
            Want(),
            Plus(),
            Times(),
            Allowed(),
            Allowed(),
            Avail(),
            Avail(),
            Avail()
        ]
        got = [g.datum(nodeid) for nodeid in spec.see_all(g)]
        self.assertCountEqual(got, expect)

        spec = NodeSpec(nodeclass=Number)
        expect = [Brick(4), Brick(5), Brick(6), Target(15)]
        got = [g.datum(nodeid) for nodeid in spec.see_all(g)]
        self.assertCountEqual(got, expect)

        spec = NodeSpec(tagclass=Avail)
        expect = [Brick(4), Brick(5), Brick(6)]
        got = [g.datum(nodeid) for nodeid in spec.see_all(g)]
        self.assertCountEqual(got, expect)
Example #4
0
 def test_cartesian_product(self):
     g = TestGraph(Numble([4, 5, 6], 15))
     finder = CartesianProduct(NodeWithTag(Number, Avail),
                               NodeWithTag(Number, Avail))
     got = [
         tuple(g.datum(nodeid) for nodeid in tup)
         for tup in finder.see_all(g)
     ]
     bricks = [Brick(4), Brick(5), Brick(6)]
     expect = list(itertools.product(bricks, bricks))
     self.assertCountEqual(got, expect)
Example #5
0
 def test_has_same_value(self):
     g = TestGraph(Numble([4, 5, 15, 6], 15))
     targetid = NodeOfClass(Target).see_one(g)
     spec = HasSameValue(targetid)
     expect = [Brick(15)]
     got = [g.datum(nodeid) for nodeid in spec.see_all(g)]
     self.assertCountEqual(got, expect)
Example #6
0
 def test_node_with_tag(self):
     spec = NodeWithTag(Number, Avail)
     g = TestGraph(Numble([4, 5, 6], 15))
     expect = [Brick(4), Brick(5), Brick(6)]
     got = [g.datum(nodeid) for nodeid in spec.see_all(g)]
     self.assertCountEqual(got, expect)
Example #7
0
 def test_cartesian_product_not_linked_to_same_no_dups(self):
     g = TestGraph(Numble([4, 5, 6], 15))
     plus = g.node_of_class(Plus)
     times = g.node_of_class(Plus)
     b4 = NodeWithValue(4).see_one(g)
     b5 = NodeWithValue(5).see_one(g)
     b6 = NodeWithValue(6).see_one(g)
     g.add_node(ConsumeOperands, plus, b4, b5)
     g.add_node(ConsumeOperands, plus, b4, b6)
     # Now 4+5 and 4+6 are each linked to a ConsumeOperands node.
     finder = CartesianProduct(
         NodeWithTag(Number, Avail),
         NodeWithTag(Number, Avail),
         NodeOfClass(Operator),
         whole_tuple_criterion=TupAnd(
             NotLinkedToSame(*ConsumeOperands.defined_roles()), no_dups))
     got = [
         tuple(g.datum(nodeid) for nodeid in tup)
         for tup in finder.see_all(g)
     ]
     # We see all possibilities except 4+5 and 4+6.
     # HMM, this assumes a sort of commutativity, which won't be
     # appropriate when we search for ways to build a Minus expression.
     expect = [(Brick(4), Brick(5), Times()), (Brick(4), Brick(6), Times()),
               (Brick(5), Brick(6), Plus()), (Brick(5), Brick(4), Times()),
               (Brick(5), Brick(6), Times()), (Brick(6), Brick(5), Plus()),
               (Brick(6), Brick(4), Times()), (Brick(6), Brick(5), Times())]
     # Thanks to no_dups, no possibility proposes consuming the same
     # Brick more than once.
     self.assertCountEqual(got, expect)
Example #8
0
 def test_cartesian_product_not_linked_to_same(self):
     g = TestGraph(Numble([4, 5, 6], 15))
     plus = g.node_of_class(Plus)
     times = g.node_of_class(Plus)
     b4 = NodeWithValue(4).see_one(g)
     b5 = NodeWithValue(5).see_one(g)
     b6 = NodeWithValue(6).see_one(g)
     #ConsumeOperands.build_and_link(g, b4, b5, plus)
     #ConsumeOperands.build_and_link(g, b4, b6, plus)
     co45 = g.add_node(ConsumeOperands,
                       consume_operand=[b4, b5],
                       proposed_operator=plus)
     #The next line tests passing args rather than kwargs
     co46 = g.add_node(ConsumeOperands, plus, b4, b6)
     #pg(g)
     # Now 4+5 and 4+6 are each linked to a ConsumeOperands node.
     finder = CartesianProduct(NodeWithTag(Number, Avail),
                               NodeWithTag(Number, Avail),
                               NodeOfClass(Operator),
                               whole_tuple_criterion=NotLinkedToSame(
                                   *ConsumeOperands.defined_roles()))
     got = [
         tuple(g.datum(nodeid) for nodeid in tup)
         for tup in finder.see_all(g)
     ]
     # We see all possibilities except 4+5 and 4+6.
     # HMM, this assumes a sort of commutativity, which won't be
     # appropriate when we search for ways to build a Minus expression.
     expect = [(Brick(4), Brick(4), Times()), (Brick(4), Brick(5), Times()),
               (Brick(4), Brick(6), Times()), (Brick(5), Brick(6), Plus()),
               (Brick(5), Brick(4), Times()), (Brick(5), Brick(5), Times()),
               (Brick(5), Brick(6), Times()), (Brick(6), Brick(5), Plus()),
               (Brick(6), Brick(4), Times()), (Brick(6), Brick(5), Times()),
               (Brick(6), Brick(6), Times())]
     # Since we did not pass no_dups to CartesianProduct, some of the
     # found possibilities include the same Brick twice.
     self.maxDiff = None
     self.assertCountEqual(got, expect)
Example #9
0
 def test_cartesian_product_no_dups(self):
     g = TestGraph(Numble([4, 5, 6], 15))
     finder = CartesianProduct(NodeWithTag(Number, Avail),
                               NodeWithTag(Number, Avail),
                               whole_tuple_criterion=no_dups)
     got = [
         tuple(g.datum(nodeid) for nodeid in tup)
         for tup in finder.see_all(g)
     ]
     expect = [(Brick(4), Brick(5)), (Brick(4), Brick(6)),
               (Brick(5), Brick(4)), (Brick(5), Brick(6)),
               (Brick(6), Brick(4)), (Brick(6), Brick(5))]
     self.assertCountEqual(got, expect)