예제 #1
0
    def test_model_save_load_run_from_disk(self, dump, load):
        DEF_GRP[GRP_ACTION] = self.complex_agent_action
        DEF_GRP[MBR_CREATOR] = self.complex_agent_create
        SECOND_GRP = DEF_GRP.copy()
        SECOND_GRP[COLOR] = RED
        GRP_STRUCT = {
            "COMPLEX_RED_GRP": SECOND_GRP,
            "COMPLEX_BLUE_GRP": DEF_GRP
        }
        complexModel = Model(grp_struct=GRP_STRUCT, model_nm="Basic")
        complexModel.run(5)
        registry.save_reg(key=complexModel.exec_key)
        registry.load_reg(complexModel.exec_key)
        loaded_object = get_model(complexModel.exec_key)
        self.assertTrue(type(loaded_object) == Model)
        self.assertTrue("Basic" == loaded_object.module)
        all_red_members_have_attribute_5 = True
        all_blue_memebrs_have_attribute_10 = True
        deserialized_model = loaded_object
        deserialized_model.run(5)
        for grp in deserialized_model.groups:
            for member in grp.members:
                if grp.color == BLUE:
                    all_blue_memebrs_have_attribute_10 = \
                        all_blue_memebrs_have_attribute_10 and (
                                grp[member].get_attr("value") != 5)
                else:
                    all_red_members_have_attribute_5 = \
                        all_red_members_have_attribute_5 and (
                                grp[member].get_attr("value") == 5)

        self.assertTrue(all_red_members_have_attribute_5)
        self.assertTrue(all_blue_memebrs_have_attribute_10)
예제 #2
0
def run_model(serial_model, periods, indra_dir):
    """
    We get passed `serial_model` and run it `periods` times.
    `model_rec` refers to the record from the model db.
    `model` refers to an instance of the Python Model class.

    The passed serial model is not in the registry so calls for
    registry.get_model will fail. Also calls for model.group will fail
    because the api serial_obj does not contain serialized groups. They are
    present in model.env.members.
    Since we always rely on the api response to construct the model at run time
    we need to sync the registry with the model restored from the api response.

    NOTE: Maybe we should only use the registry to restore - could decrease the
    api payload size and give performance boost.
    """
    model_rec = get_model_by_mod(serial_model["module"], indra_dir=indra_dir)
    if model_rec is not None:
        module = module_from_model(model_rec)
        model = module.create_model(serial_obj=serial_model)
        registry.load_reg(model.exec_key)
        sync_api_restored_model_with_registry(model, model.exec_key)
        model.runN(periods)
        return model
    else:
        return None
예제 #3
0
 def test_agent_load_from_disk(self, dump, load):
     registry.save_reg(self.exec_key)
     registry.load_reg(self.exec_key)
     loaded_agent = get_agent(TEST_AGENT_NM, self.exec_key)
     (acted, moved) = loaded_agent()
     self.assertTrue(acted)
     self.assertTrue(not moved)