Ejemplo n.º 1
0
 def __setitem__(self, key, member):
     """
     In contrast to agent, which sets a val
     for setitem, for groups, we are going to set
     the 'key' member.
     """
     join(self, member)
Ejemplo n.º 2
0
 def test_join_group(self):
     """
     Test joining a group.
     This test is here rather than in agent because it requires Group!
     """
     join(self.calc, self.hardy)
     self.assertEqual(create_mem_str(self.calc), NL + H)
Ejemplo n.º 3
0
    def __iadd__(self, other):
        """
        Add other to set self.
        If other is a group, add all its members.
        If other is an atom, add it.
        """
        if other is None:
            return self

        if is_group(other):
            for key in other:
                join(self, other[key])
        else:
            join(self, other)
        return self
Ejemplo n.º 4
0
    def __init__(self,
                 name,
                 attrs=None,
                 members=None,
                 duration=INF,
                 action=None,
                 mbr_creator=None,
                 mbr_action=None,
                 color=None,
                 num_mbrs=None,
                 serial_obj=None,
                 exec_key=None,
                 **kwargs):

        self.num_mbrs_ever = 0
        self.members = OrderedDict()

        super().__init__(name,
                         attrs=attrs,
                         duration=duration,
                         action=action,
                         serial_obj=serial_obj,
                         exec_key=exec_key,
                         **kwargs)
        self.type = type(self).__name__

        if serial_obj is not None:
            self.restore(serial_obj)
        else:
            if members is not None:
                for member in members:
                    join(self, member)
            if num_mbrs is None:
                num_mbrs = 1  # A default if they forgot to pass this.
            self.num_mbrs_ever = num_mbrs
            self.mbr_creator = mbr_creator
            self.mbr_action = mbr_action
            self.color = color
            if mbr_creator is not None:
                # If we have a member creator function, call it
                # `num_mbrs` times to create group members.
                for i in range(num_mbrs):
                    join(
                        self,
                        mbr_creator(self.name,
                                    i,
                                    action=mbr_action,
                                    exec_key=self.exec_key))
Ejemplo n.º 5
0
 def handle_womb(self):
     """
     The structure of the womb is:
         {"group_name": #agents_to_create}
     """
     for grp_nm in self.womb:
         print(f"Going to add {self.womb[grp_nm]} members to {grp_nm}")
         grp = self.members[grp_nm]
         num_to_add = self.womb[grp_nm]
         mbr_num = grp.num_mbrs_ever
         while num_to_add > 0:
             new_agent = grp.mbr_creator(grp_nm, mbr_num,
                                         exec_key=self.exec_key)
             join(grp, new_agent)
             self.place_member(new_agent)
             num_to_add -= 1
             mbr_num += 1
Ejemplo n.º 6
0
def town_action(town):
    """
    Create big box store at appropriate turn.
    """
    bb_grp = get_group(BIG_BOX, town.exec_key)
    box = get_model(town.exec_key)
    bb_period = box.bb_period
    bb_init_capital = box.multiplier * AVG_MP_INIT_CAP
    # if no big box exists, make them:
    num_bbs = len(bb_grp)
    if num_bbs == 0:
        if town.get_periods() >= bb_period:
            new_bb = bb_grp.mbr_creator(BIG_BOX,
                                        num_bbs,
                                        bb_init_capital,
                                        exec_key=town.exec_key)
            join(bb_grp, new_bb)
            town.place_member(new_bb)