def go(self, g, actor): # Test that all members of 'focal_point' have value 'value'. # If so, tag 'focal_point' AllMembersSameValue # HACK Need to find 'focal_point' node left by SeekAndGlom or whatever # process set up the node in which we should NoticeAllSameValue. #focal_point = first(g.prev_new_nodes) # if not self.focal_point: # self.focal_point = g.look_for(OfClass(Glom)) # if not self.focal_point: # return # TODO FAIL if not self.focal_point: raise NeedArg(ac=self, name='focal_point') if self.value is None: raise NeedArg(ac=self, name='value') if all( g.value_of(memberid) == self.value #for memberid in g.members_of(self.focal_point) for memberid in g.find_all(OfClass(Number), focal_point=self.focal_point)): #g.do(Build.maybe_make(g, AllMembersSameValue, [self.focal_point], {})) g.do(Build.maybe_make(g, AllMembersSameValue, self.focal_point)) g.new_state(self.actor, Completed) else: raise NotAllSameValue(self, value=self.value, focal_point=self.focal_point)
def go(self, g, actor): if not self.node1: raise NeedArg(self, 'node1') if not self.node2: raise NeedArg(self, 'node2') if g.value_of(self.node1) == g.value_of(self.node2): g.do( Build.maybe_make(g, SameValue, taggees=[self.node1, self.node2])) g.new_state(self.actor, Completed) else: raise NotSameValue(self, self.node1, self.node2)
def get(self, g: 'G', actor: MaybeNRef, env: AcEnv, name: str, default: Any = None) -> Any: '''Looks up name, searching first in actor's overrides, then in env, then in actor's attrs, and finally in this Ac's attrs. If the value found is itself a string, then looks up the string as 'name' (recursively). A value of Quote(s) will be returned as s, without recursive lookup, if you need to actually return a string. Raises NeedArg if the value found is None or not found.''' # TODO Document name_overrides_ # TODO Document special meaning of 'this' if name == 'this': return actor #print('ACNAME0', name, self.name_overrides_) name = self.name_overrides_.get(name, name) #print('ACNAME1', name) try: result = g.get_overrides(actor, name)[name] except KeyError: try: result = env[name] #print('ACGETENV', self.__class__.__name__, name, repr(result)) except KeyError: result = g.getattr(actor, name) if result is None: try: #print('GET', self, repr(name)) result = getattr(self, name) #print('GET1', result) except AttributeError: result = default #print('ACGET2', self.__class__.__name__, name, repr(result)) if result is None: #raise AcNeedArg(ac=self, name=name) raise NeedArg(ac=self, name=name) elif isinstance(result, str): # TODO Prevent infinite recursion: make sure we haven't tried # this key before. #print('GETRECUR', self, actor, name, result) result = self.get(g, actor, env, result) #print('GETRECUR2', repr(result)) return result elif Context.is_context(result): result = result.focal_point(g, actor) #print('GETCTX', result) return result elif is_seq_of(result, Criterion): #return [self.fix_criterion(g, c, actor, env) for c in result] return [c.replace_from_env(g, self, actor, env) for c in result] elif isinstance(result, Criterion): return result.replace_from_env(g, self, actor, env) else: return Quote.get(result)
def go(self, g, actor): if not self.focal_point: raise NeedArg(self, 'focal_point') operand = g.look_for(OfClass(Brick), focal_point=self.focal_point) if operand: g.add_node(Exclude, operand) g.new_state(self.actor, Completed) else: print('ExcludeOperand FAILED') #DEBUG
def test_ac_already_built_fillparamscout(self): g = NumboGraph(Numble([4, 5, 6], 15)) noticer = g.add_node(NoticeAllBricksAreAvail, member_of=g.ws) tag = g.add_tag( Blocked(reason=NeedArg(ac=noticer.action, name='focal_point')), noticer) scout = g.add_node(FillParamScout, behalf_of=noticer, problem=tag) self.assertTrue( g.already_built(FillParamScout, behalf_of=noticer, problem=tag))
def test_ac_already_built_acnode_without_args(self): class MyScout(AcNode): # There are no NodeParams here. This exposed a bug once. acs = [FindParamName()] g = NumboGraph(Numble([4, 5, 6], 15)) noticer = g.add_node(NoticeAllBricksAreAvail, member_of=g.ws) tag = g.add_tag( Blocked(reason=NeedArg(ac=noticer.action, name='focal_point')), noticer) scout = g.add_node(MyScout, behalf_of=noticer, problem=tag) self.assertTrue( g.already_built(MyScout, behalf_of=noticer, problem=tag))
def go(self, g, actor): if not self.glom: raise NeedArg(self, 'glom') #old_members = as_set(g.members_of(self.glom)) old_members = as_set( g.find_all(CTagged(Avail), subset=g.members_of(self.glom))) excluded = as_set(g.find_all(CTagged(Exclude), subset=old_members)) new_members = old_members - excluded g.remove_node(self.glom) if new_members: g.add_node(Glom, new_members) g.new_state(self.actor, Completed) else: # should signal failure TODO print('REGLOM FAILED', self.glom, old_members, excluded, new_members)
def go(self, g, actor): if not self.focal_point: raise NeedArg(self, 'focal_point') g.add_node( Proposal, ConsumeOperands(), consume_operands=g.find_all(OfClass(Number), CTagged(Avail), focal_point=self.focal_point), proposed_operator=g.look_for( OfClass(Plus), CTagged(Allowed), # TODO 'and' these criteria focal_point=g.ws), member_of=g.ws) g.new_state(self.actor, Completed)
def test_ac_fillparamscout(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) tag = g.add_tag( Blocked(reason=NeedArg(ac=noticer.action, name='focal_point')), noticer) scout = g.add_node(FillParamScout, behalf_of=noticer, problem=tag) g.do_timestep(actor=scout) # The FillParamScout should do the override: self.assertTrue(g.has_hop(noticer, 'focal_point', glom, 'overriding')) # and remove the Blocked tag: self.assertFalse(g.has_node(tag))
def test_do_notice_and_tag(self): # Tests running Ac objects directly. Normally, though, you only run # Ac objects from inside an AcNode. g = NumboGraph(Numble([4, 5, 6], 15)) targetid = 1 # HACK env = Ac.run(g, All(OfClass(Brick), focal_point=g.ws), actor=targetid) nodes = map(g.as_node, env['nodes']) self.assertCountEqual(nodes, [Brick(4), Brick(5), Brick(6)]) # Should fail because All is missing a 'focal_point' arg. try: env = Ac.run(g, All(OfClass(Brick)), actor=targetid) except NeedArg as exc: self.assertEqual( exc, NeedArg(ac=All(OfClass(Brick)), name='focal_point')) else: self.fail("Missing 'focal_point' failed to raise AcNeedArg.") env = Ac.run( g, [All(OfClass(Brick), focal_point=g.ws), AllAre(CTagged(Avail))], actor=targetid) nodes = map(g.as_node, env['nodes']) self.assertCountEqual(nodes, [Brick(4), Brick(5), Brick(6)]) env = Ac.run(g, [ All(OfClass(Brick), focal_point=g.ws), AllAre(CTagged(Avail)), TagWith(AllBricksAvail, taggees='nodes') ], actor=targetid) result = list(g.as_nodes(env['result'])) self.assertEqual(len(result), 1) tag = result[0] self.assertEqual(tag.__class__, AllBricksAvail) self.assertTrue(g.has_tag(nodes, tag))
def get_kwarg(self, name): try: return self.kwargs[name] except KeyError: raise NeedArg(self, name)
def go(self, g, actor): if not self.focal_point: raise NeedArg(self, 'focal_point') # TODO kws num_members = len(g.neighbors(self.focal_point, port_label='members')) g.add_node(Count, taggees=self.focal_point, value=num_members) g.new_state(self.actor, Completed)