Exemplo n.º 1
0
    def test_want_w_forced(self):
        fm = TestFM()
        ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)]))
        cr0 = fm.build(CellRef(ca, 0))
        cr1 = cr0.next_cellref()
        wa = fm.build(Want(target=15,
                           startcell=cr0,
                           sk=RaiseException(TestFoundIt)),
                      min_a=4.0)
        # TODO assert that cr1 got build as an Elem in the ws?
        self.assertEqual(fm.a(wa), 4.0)

        # The Want should build a Consume
        expected_co = Consume(plus, (5, 4))
        fm.run(wa, force_slipnet_result=[expected_co])
        co = fm.the(expected_co)
        self.assertEqual(co, Consume(plus, (5, 4), source=cr0, dest=cr1))
        self.assertEqual(fm.builder_of(co), wa)
        self.assertIn(co, fm.built_by(wa))
        self.assertTrue(fm.can_go(co))

        # The Consume should build a LitPainter
        fm.run(co)
        expected_value = SeqState((6, 9), ArithDelta((5, 4), 9, plus))
        lp = fm.the(LitPainter)
        self.assertEqual(lp, LitPainter(cr1, expected_value))
        self.assertTrue(fm.is_mutual_support(co, lp))
        self.assertFalse(fm.can_go(co))

        # The LitPainter shouldn't paint yet: activation is too low
        self.assertFalse(fm.ok_to_paint(lp, cr1))
        self.assertFalse(fm.can_go(lp))

        # Eventually, the LitPainter should be able to paint
        fm.propagate_a(num=30)
        self.assertTrue(fm.ok_to_paint(lp, cr1))
        self.assertTrue(fm.can_go(lp))
        self.assertEqual(fm.the(cr1), cr1)

        # When the LitPainter paints, it should be marked Succeeded
        fm.run(lp)
        self.assertEqual(fm.agent_state(lp), Succeeded())
        self.assertTrue(fm.has_succeeded(lp))
        self.assertFalse(fm.can_go(lp))

        # The Consume should be marked MustCheckIfSucceeded
        self.assertIsInstance(fm.agent_state(co), MustCheckIfSucceeded)

        # Running the Consume should make it see that it has succeeded
        fm.run(co)
        self.assertTrue(fm.has_succeeded(co))
        self.assertFalse(fm.can_go(co))
        self.assertIsInstance(fm.agent_state(wa), MustCheckIfSucceeded)

        # Running the Want should make it see that it has still not succeeded
        fm.run(wa)
        self.assertIsInstance(fm.agent_state(wa), Active)
        self.assertFalse(fm.has_succeeded(wa))
Exemplo n.º 2
0
    def test_blocked_makes_want(self):
        fm = TestFM(seed=1)
        ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)]))
        cr0 = fm.build(CellRef(ca, 0))
        cr1 = cr0.next_cellref()
        co69 = fm.build(Consume(plus, (6, 9), source=cr0, dest=cr1))

        fm.run(co69)
        bl = fm.the(Blocked)  # TODO query for built_by co69
        assert bl is not None

        fm.run(bl)
        wa = fm.the(Want)  # TODO query for built_by bl
        self.assertIsNotNone(wa)
        self.assertEqual(wa.target, 9)
        self.assertEqual(wa.startcell, cr0)
        self.assertEqual(wa.sk, RemoveBlocked(bl))

        fm.run(wa, force_slipnet_result=Consume(plus, (4, 5)))
        co45 = fm.the(Consume(plus, (4, 5)))
        self.assertEqual(co45, Consume(plus, (4, 5), source=cr0, dest=cr1))

        fm.run(co45)
        lp9 = fm.the(LitPainter)
        self.assertIsNotNone(lp9)

        fm.run(lp9)
        self.assertTrue(fm.has_succeeded(lp9))
        self.assertIsInstance(fm.agent_state(co45), MustCheckIfSucceeded)

        fm.run(co45)
        self.assertTrue(fm.has_succeeded(co45))
        self.assertIsInstance(fm.agent_state(wa), MustCheckIfSucceeded)

        fm.run(wa)
        self.assertTrue(fm.has_succeeded(wa))
        cr2 = cr1.next_cellref()
        co69b = fm.the(Consume(plus, (6, 9)))
        self.assertEqual(co69b.source, cr1)
        self.assertEqual(co69b.dest, cr2)
        self.assertFalse(fm.is_blocked(co69b))
        self.assertIsNone(fm.the(Blocked))
        self.assertIsInstance(fm.agent_state(co69b), Active)
        # TODO Make sure node for cr[2] gets built

        fm.run(co69b)
        lp15 = fm.the(LitPainter(cellref=cr2))
        self.assertEqual(lp15.value.avails, (15, ))

        fm.run(lp15)
        self.assertTrue(fm.has_succeeded(lp15))
Exemplo n.º 3
0
    def test_consume(self):
        fm = FARGModel()
        ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)]))
        cr0 = fm.build(CellRef(ca, 0))
        cr1 = fm.build(CellRef(ca, 1))

        self.assertCountEqual(cr0.avails, [4, 5, 6])
        self.assertCountEqual(cr1.avails, [])

        co = fm.build(Consume(plus, (4, 5), source=cr0, dest=cr1))
        fm.run(co)
        lp = fm.the(LitPainter)
        self.assertEqual(
            lp, LitPainter(cr1, SeqState((6, 9), ArithDelta((4, 5), 9, plus))))
        self.assertEqual(fm.builder_of(lp), co)
        self.assertEqual(fm.ae_weight(co, lp), fm.mutual_support_weight)
        #TODO UT behalf_of
        #self.assertCountEqual(fm.neighbors(co), [lp])
        self.assertIn(lp, fm.neighbors(co))
        self.assertIn(cr0, fm.neighbors(co))
        self.assertIn(cr1, fm.neighbors(co))

        self.assertCountEqual(cr1.avails, [])
        fm.run(lp)
        self.assertCountEqual(cr1.avails, [6, 9])
Exemplo n.º 4
0
 def test_make_table_consume(self):
     table = list(Consume.make_table([10, 20], [2, 3], [plus, minus]))
     self.maxDiff = None
     self.assertCountEqual(table, [
         Consume(plus, (10, 2)),
         Consume(plus, (10, 3)),
         Consume(plus, (20, 2)),
         Consume(plus, (20, 3)),
         Consume(minus, (10, 2)),
         Consume(minus, (10, 3)),
         Consume(minus, (20, 2)),
         Consume(minus, (20, 3))
     ])
Exemplo n.º 5
0
    def test_gettingcloser(self):
        fm = TestFM(seed=1)
        ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)]))
        cr0 = CellRef(ca, 0)
        cr1 = CellRef(ca, 1)
        co = fm.build(Consume(plus, (6, 5), source=cr0, dest=cr1))
        fm.run(co)
        lp = fm.the(LitPainter)
        self.assertGreater(GettingCloser.calc_weight(lp, 15), 0.1)

        tagger = fm.build(GettingCloser.Tagger(15))
        tagger.look(fm)
        self.assertTrue(fm.is_tagged(lp, GettingCloser))
Exemplo n.º 6
0
    def test_values_not_avail(self):
        fm = FARGModel()
        ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)]))
        cr0 = CellRef(ca, 0)
        cr1 = CellRef(ca, 1)

        # This must fail because there is only one 4 avail
        co = fm.build(Consume(plus, (4, 4), source=cr0, dest=cr1))
        self.assertTrue(fm.can_go(co))
        fm.run(co)
        self.assertIsNone(fm.the(LitPainter))
        # TODO fm.tags_of()
        self.assertTrue(fm.is_tagged(co, Blocked))
        self.assertTrue(fm.is_blocked(co))
        self.assertFalse(fm.can_go(co))
        bl = first(e for e in fm.built_by(co) if isinstance(e, Blocked))
        self.assertIsNotNone(bl)
        self.assertEqual(bl.reason.avails, (4, None))
        self.assertEqual(bl.reason.unavails, (None, 4))
        self.assertGreater(fm.ae_weight(co, bl), 0.0)
Exemplo n.º 7
0
 def fill_slipnet(self) -> None:
     self.slipnet.add_layer2_nodes(
         Consume(operator, (a, b)) for a in range(1, 21)
         for b in range(1, 11) for operator in [plus, times, minus]
         if a >= b)