Example #1
0
    def test_ac_add_all_in_glom(self):
        g = NumboGraph(Numble([4, 5, 6], 15))
        glom = g.add_node(Glom, g.find_all(OfClass(Brick)))
        proposer = g.add_node(AddAllInGlom, focal_point=glom)

        a_brick = g.look_for(OfClass(Brick), focal_point=g.ws)
        self.assertEqual(g.containers_of_recursive(a_brick),
                         {glom.id, g.ws.id})

        g.do_timestep(actor=proposer)

        proposal = g.as_node(g.neighbor(proposer, 'built'))
        self.assertEqual(g.class_of(proposal), Proposal)

        g.do_timestep(actor=proposal)

        new_plus = g.neighbor(proposal, 'built', neighbor_class=Plus)
        self.assertTrue(new_plus, 'Proposal did not build Plus.')
        self.assertTrue(g.is_member(new_plus, g.ws))
        self.assertFalse(g.is_member(new_plus, glom))
        self.assertTrue(g.is_dormant(proposal))

        block = g.as_node(g.neighbor(proposal, 'built', neighbor_class=Block))
        self.assertEqual(g.as_node(block), Block(15))
        self.assertTrue(g.is_member(block, g.ws))
        self.assertFalse(g.is_member(block, glom))
Example #2
0
    def test_override(self):
        g = NumboGraph(Numble([4, 5, 6], 15))
        glom = g.add_node(Glom, g.find_all(OfClass(Brick)))

        noticer = g.add_node(NoticeAllBricksAreAvail, member_of=g.ws)
        self.assertIn(noticer.id, g.as_nodeids(g.active_nodes()))

        g.do_timestep(actor=noticer)

        # Since the NoticeAllBricksAreAvail can't run, it should get tagged
        # Blocked(NeedArg('focal_point')).
        tag = g.as_node(g.tag_of(noticer, Blocked))
        self.assertTrue(
            tag,
            "Failed to create Blocked tag for missing 'focal_point' argument.")
        reason = g.getattr(tag, 'reason')
        self.assertTrue(isinstance(reason, NeedArg))
        self.assertEqual(reason.name, 'focal_point')

        self.assertTrue(g.is_blocked(noticer))

        # Now we manually override the 'focal_point' argument.
        g.add_override_node(noticer, 'focal_point', glom)
        g.remove_tag(noticer, Blocked)

        g.do_timestep(actor=noticer)
        bricks = g.find_all(OfClass(Brick))
        self.assertTrue(
            g.has_tag(bricks, AllBricksAvail),
            "Did not tag the Bricks even when 'focal_point' was overridden.")
Example #3
0
    def test_oom_and_gt(self):
        class ActivationTrap(Node):
            '''Does nothing but receive activation.'''
            pass

        g = NumboGraph(Numble([10, 5], 15))
        b10 = g.look_for(Brick(10))
        b5 = g.look_for(Brick(5))
        t15 = g.look_for(Target(15))
        assert b10, "Couldn't find Brick(10)"
        assert b5, "Couldn't find Brick(5)"
        assert t15, "Couldn't find Target(15)"
        atrap = g.add_node(ActivationTrap)
        oomtagger = g.add_node(OoMTagger, member_of=g.ws)
        oomgttagger = g.add_node(OoMGreaterThanTagger, member_of=g.ws)
        oom1btagger = g.add_node(OoMSmallGapToWantedTagger, member_of=g.ws)

        # Every OoM node should give activation to the ActivationTrap
        g.add_activation_autolinks((OoM, atrap))

        g.do_timestep(actor=oomtagger)
        g.do_timestep(actor=oomtagger)
        g.do_timestep(actor=oomtagger)

        self.assertEqual(g.as_node(g.tag_of(b10, OoM)), OoM(value=1.0))
        self.assertEqual(g.as_node(g.tag_of(b5, OoM)), OoM(value=log10(5)))
        self.assertEqual(g.as_node(g.tag_of(t15, OoM)), OoM(value=log10(15)))

        # This tests .do_activation_autolinks().
        for oom in g.find_all(OoM):
            self.assertEqual(g.activation_from_to(oom, atrap), 1.0)

        # TODO Assert that this fizzles.
        g.do_timestep(actor=oomtagger)

        g.do_timestep(actor=oomgttagger, num=6)
        self.assertTrue(g.has_tag(t15, OoMGreaterThan, lesser=b5, greater=t15))

        #ShowPrimitives.start_logging()
        g.do_timestep(actor=oom1btagger, num=6)
        self.assertTrue(
            g.has_tag(t15, OoMSmallGapToWanted, lesser=b5, wanted=t15))
Example #4
0
    def test_ac_notice_solved(self):
        class NoticeSolved(AcNode):
            acs = [
                LookFor(CTagged(Avail), cond=EqualValue('node', 'target')),
                Raise(NumboSuccess, node='node', target='target')
            ]

        g = NumboGraph(Numble([4, 5, 6, 15], 15))
        target = g.look_for(OfClass(Target))
        glom = g.add_node(Glom, g.find_all(OfClass(Brick)))

        noticer = g.add_node(NoticeSolved, focal_point=glom, target=target)

        g.do_timestep(actor=noticer)

        got = g.done()
        self.assertEqual(got.__class__, NumboSuccess)
        self.assertEqual(g.as_node(got.node), Brick(15))
        self.assertEqual(g.as_node(got.target), Target(15))
        self.assertTrue(g.succeeded())
Example #5
0
    def test_noticer(self):
        g = NumboGraph(Numble([4, 5, 6], 15))

        noticer = g.add_node(AdHocAcNode, [
            All(OfClass(Brick), focal_point=g.ws),
            AllAre(CTagged(Avail)),
            TagWith(AllBricksAvail, taggees='nodes')
        ],
                             member_of=g.ws)

        self.assertEqual(noticer.state, Start)
        self.assertTrue(noticer.can_go())
        self.assertIn(noticer.id, g.as_nodeids(g.active_nodes()))
        g.do_timestep(actor=noticer)
        tag = g.as_node(first(g.new_nodes))
        self.assertEqual(tag.__class__, AllBricksAvail)
        self.assertEqual(noticer.state, Completed)
        self.assertFalse(noticer.can_go())
        self.assertNotIn(noticer.id, g.as_nodeids(g.active_nodes()))
Example #6
0
    def test_sleep_and_wake(self):
        g = NumboGraph(Numble([4, 5, 6], 15))
        assert g.t == 0, f't == {g.t}, should == 0'
        node = g.as_node(g.look_for(NoticeSolved))
        assert node, "We need an ActiveNode for this unit test."
        self.assertTrue(node.id in g.active_nodes())
        node.state = Sleeping(Start, until=4)
        self.assertFalse(node.id in g.active_nodes())

        g.do_timestep()
        assert g.t == 1
        self.assertFalse(node.id in g.active_nodes())

        g.do_timestep()
        assert g.t == 2
        self.assertFalse(node.id in g.active_nodes())

        g.do_timestep()
        assert g.t == 3
        self.assertTrue(node.id in g.active_nodes())
Example #7
0
    def test_ac_build_op_result(self):
        class Builder(AcNode):
            acs = [
                LookForTup([Brick(4), Brick(5)],
                           focal_point=InWorkspace,
                           asgn_to='operands'),
                BuildOpResult(operands='operands')
            ]

        g = NumboGraph(Numble([4, 5, 6], 15))
        plus = g.look_for(Plus, focal_point=g.ws)
        b4 = g.look_for(Brick(4), focal_point=g.ws)
        b5 = g.look_for(Brick(5), focal_point=g.ws)
        builder = g.add_node(Builder, member_of=g.ws, opclass=plus)

        g.do_timestep(actor=builder)

        block = g.as_node(g.look_for(Block, focal_point=g.ws))
        new_plus = g.neighbor(block, 'source')
        self.assertTrue(g.is_of_class(new_plus, Plus))
        self.assertEqual(block, Block(9))
        self.assertCountEqual(g.neighbors(new_plus, 'operands'),
                              as_nodeids([b4, b5]))