Exemplo n.º 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)
Exemplo n.º 2
0
 def test_registry_saved_to_disk(self, dump, load):
     indra_dir = get_indra_home(PA_INDRA_NET)
     file_path = os.path.join(indra_dir, 'registry', 'db',
                              '{}-reg.json'.format(self.exec_key))
     registry[self.exec_key]["name"] = "Abhinav"
     registry.save_reg(self.exec_key)
     self.assertTrue(os.path.exists(file_path))
Exemplo n.º 3
0
 def test_del_exec_env(self, dump, load):
     registry[self.exec_key]["name"] = "Abhinav"
     registry.save_reg(self.exec_key)
     registry.del_exec_env(self.exec_key)
     self.already_cleared = True
     self.assertRaises(KeyError, registry.del_exec_env, self.exec_key)
     self.assertTrue(not os.path.exists(self.get_action_pkl_file()))
Exemplo n.º 4
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)
Exemplo n.º 5
0
 def test_registry_saved_to_disk(self, dump, load):
     indra_dir = os.getenv("INDRA_HOME", "/home/indrasnet/indras_net")
     file_path = os.path.join(indra_dir, 'registry', 'db',
                              '{}-reg.json'.format(self.exec_key))
     registry[self.exec_key]["name"] = "Abhinav"
     registry.save_reg(self.exec_key)
     self.assertTrue(os.path.exists(file_path))
Exemplo n.º 6
0
 def put(self, model_id):
     """
     Put a revised list of parameters for a model back to the server.
     This should return a new model with the revised props.
     """
     exec_key = api.payload['exec_key'].get('val')
     model = json_converter(create_model(model_id, api.payload, indra_dir))
     registry.save_reg(exec_key)
     return model
Exemplo n.º 7
0
 def get(self, model_id):
     """
     Get the list of properties (parameters) for a model.
     """
     props = PropArgs.create_props(str(model_id),
                                   prop_dict=get_props(model_id, indra_dir))
     exec_key = create_exec_env(save_on_register=True)
     props["exec_key"] = exec_key
     registry.save_reg(exec_key)
     return props.to_json()
Exemplo n.º 8
0
 def put(self, run_time):
     """
     Put a model env to the server and run it `run_time` periods.
     Catch all possible exceptions to keep the server responsive.
     """
     try:
         exec_key = api.payload['exec_key']
         print(f'Executing for key {exec_key}')
         model = run_model(api.payload, run_time, indra_dir)
         if model is None:
             return err_return(f"Model not found: {api.payload['module']}")
         registry.save_reg(exec_key)
         return json_converter(model)
     except Exception as err:
         raise wz.InternalServerError(f"Server error: {str(err)}")
Exemplo n.º 9
0
    def test_should_only_pickle_once(self, dump, load):
        agent1 = Agent("test_agent1", action=self.agent_action,
                       exec_key=self.exec_key)
        agent2 = Agent("test_agent2", action=self.agent_action,
                       exec_key=self.exec_key)
        registry.save_reg(self.exec_key)
        self.assertTrue(registry[self.exec_key]['functions'] is not None)

        pickle_files = list(
            filter(lambda file: 'agent-agent_action.pkl' in file,
                   [value for value in registry[self.exec_key]
                   ['functions'].values()]))

        self.assertTrue(len(pickle_files) != 0)
        self.assertTrue(len(pickle_files) == 1)
Exemplo n.º 10
0
 def test_agent_save_to_disk(self, dump):
     agent = Agent("test_agent",
                   action=self.agent_action,
                   exec_key=self.exec_key)
     registry[self.exec_key][agent.name] = agent
     registry.save_reg(self.exec_key)
Exemplo n.º 11
0
 def test_registry_fetch_from_disk(self, dump, load):
     registry[self.exec_key]["name"] = "Abhinav"
     registry.save_reg(self.exec_key)
     loaded_object_name = get_agent("name", self.exec_key)
     self.assertTrue("Abhinav" == loaded_object_name)
Exemplo n.º 12
0
 def test_del_exec_env(self, dump, load):
     registry[self.exec_key]["name"] = "Abhinav"
     registry.save_reg(self.exec_key)
     registry.del_exec_env(self.exec_key)
     self.already_cleared = True
     self.assertRaises(KeyError, registry.del_exec_env, self.exec_key)