Exemplo n.º 1
0
def run_model_put(payload, run_time):
    """
    We create a dummy env that fills itself in to create
    the real env from the payload.
    """
    env = Env(name='temp name', serial_obj=payload)
    env.runN(periods=run_time)
    return json_converter(env)
Exemplo n.º 2
0
 def test_runN(self):
     """
     Test running for N turns.
     """
     new_env = Env("Test1 env", action=env_action, members=[self.newton])
     num_periods = 10
     acts = new_env.runN(num_periods)
     self.assertEqual(acts, num_periods)
Exemplo n.º 3
0
def run_model_put(payload, run_time):
    """
    We create a dummy env that fills itself in to create
    the real env from the payload.
    """
    execution_key = payload.get(EXEC_KEY)
    print("Got execution key", execution_key)
    env = Env(name="temp_env", serial_obj=payload, execution_key=execution_key)
    user_log_notif("Searching env attributes for " + env.name)
    if env.name in env_attrs:
        user_log_notif("Loading env attributes for " + env.name)
        env_attrs[env.name](execution_key=execution_key)
        user_log_notif("Attributes for " + env.name + repr(env.attrs))
    else:
        user_log_notif(env.name + " not found in env_attrs")
    try:
        env.runN(periods=run_time, execution_key=execution_key)
    except Exception as excp:
        env.user.tell(str(excp))
        env.user.tell("Halting Model!")
        user_log_notif(str(excp))
        user_log_notif("Halting Model!")
    return json_converter(env)
Exemplo n.º 4
0
class EnvTestCase(TestCase):
    def setUp(self):
        self.newton = create_newton()
        self.calcs = create_calcguys()
        self.cambs = create_cambguys()
        self.pop_hist = PopHist()
        self.env = Env("Test env")

    def tearDown(self):
        self.newton = None
        self.calcs = None
        self.cambs = None
        self.pop_hist = None
        self.env = None

    def fill_pop_hist(self):
        self.pop_hist.record_pop(GRP1, 10)
        self.pop_hist.record_pop(GRP2, 10)
        self.pop_hist.record_pop(GRP1, 20)
        self.pop_hist.record_pop(GRP2, 20)

    def test_user_type(self):
        self.assertEqual(self.env.user_type, TEST)

    def test_runN(self):
        NUM_PERIODS = 10
        self.env += self.newton
        acts = self.env.runN(NUM_PERIODS)
        self.assertEqual(acts, NUM_PERIODS)

    def test_str_pop(self):
        self.fill_pop_hist()
        s = str(self.pop_hist)
        self.assertEqual(s, POP_HIST_HDR + GRP1 + POP_SEP + GRP2 + POP_SEP)

    def test_record_pop(self):
        self.assertTrue(True)

    def test_add_child(self):
        self.env.add_child(self.newton, self.calcs)
        self.assertIn((self.newton, self.calcs), self.env.womb)

    def test_add_switch(self):
        self.env.add_switch(self.newton, self.calcs, self.cambs)
        self.assertIn((self.newton, self.calcs, self.cambs), self.env.switches)

    def test_has_disp(self):
        if not disp.plt_present:
            self.assertTrue(not self.env.has_disp())
        else:
            self.assertTrue(self.env.has_disp())

    # def test_line_data(self):
    #     self.fill_pop_hist()
    #     ret = self.env.line_data()
    #     self.assertEqual(ret, (1, {GRP1: {"data": [10, 20]}, GRP2: {"data": [10, 20]}}))
    #
    # def test_plot_data(self):
    #     self.pop_hist.record_pop(GRP1, 10)
    #     ret = self.env.plot_data()
    #     current_var = self.env.members[GRP1]
    #     current_agent_pos = current_var[GRP1].pos
    #     (x, y) = current_agent_pos
    #     self.assertEqual(ret, {GRP1: {X: [x], Y: [y]}})

    def test_headless(self):
        bool = (self.env.user_type == WEB) or (self.env.user_type == TEST)
        if bool:
            self.assertTrue(self.env.headless())
        else:
            self.assertTrue(not self.env.headless())
Exemplo n.º 5
0
class EnvTestCase(TestCase):
    def setUp(self):
        self.newton = create_newton()
        self.calcs = create_calcguys()
        self.cambs = create_cambguys()
        self.pop_hist = PopHist()
        self.env = Env("Test env", action=env_action)

    def tearDown(self):
        self.newton = None
        self.calcs = None
        self.cambs = None
        self.pop_hist = None
        self.env = None

    def fill_pop_hist(self):
        self.pop_hist.record_pop(GRP1, 10)
        self.pop_hist.record_pop(GRP2, 10)
        self.pop_hist.record_pop(GRP1, 20)
        self.pop_hist.record_pop(GRP2, 20)
        return self.pop_hist

    def test_user_type(self):
        """
        Make sure our user type is test.
        """
        self.assertEqual(self.env.user_type, TEST)

    def test_runN(self):
        """
        Test running for N turns.
        """
        NUM_PERIODS = 10
        self.env += self.newton
        acts = self.env.runN(NUM_PERIODS)
        self.assertEqual(acts, NUM_PERIODS)

    def test_str_pop(self):
        """
        Test converting the pop history to a string.
        """
        self.fill_pop_hist()
        s = str(self.pop_hist)
        self.assertEqual(s, POP_HIST_HDR + GRP1 + POP_SEP + GRP2 + POP_SEP)

    def test_record_pop(self):
        self.assertTrue(True)

    def test_add_child(self):
        self.env.add_child(self.newton, self.calcs)
        self.assertIn((self.newton, self.calcs), self.env.womb)

    def test_add_switch(self):
        self.env.add_switch(self.newton, self.calcs, self.cambs)
        self.assertIn((self.newton, self.calcs, self.cambs), self.env.switches)

    def test_has_disp(self):
        if not disp.plt_present:
            self.assertTrue(not self.env.has_disp())
        else:
            self.assertTrue(self.env.has_disp())

    def test_line_data(self):
        """
        Test the construction of line graph data.
        This test must be changed to handle new color param!
        Commented out for the moment.
        """
        global travis
        travis = os.getenv("TRAVIS")
        if not travis:
            self.env.pop_hist = self.fill_pop_hist()
            ret = self.env.line_data()
            self.assertEqual(ret, (2, {
                GRP1: {
                    "color": "navy",
                    "data": [10, 20]
                },
                GRP2: {
                    "color": "b",
                    "data": [10, 20]
                }
            }))

    def test_plot_data(self):
        """
        Test the construction of scatter plot data.
        """
        global travis
        travis = os.getenv("TRAVIS")
        if not travis:
            our_grp = Composite(GRP1, members=[self.newton])
            self.env = Env("Test env", members=[our_grp])
            ret = self.env.plot_data()
            (x, y) = self.newton.pos
            self.assertEqual(ret, {GRP1: {X: [x], Y: [y], 'color': None}})

    def test_headless(self):
        if (self.env.user_type == WEB) or (self.env.user_type == TEST):
            self.assertTrue(self.env.headless())
        else:
            self.assertTrue(not self.env.headless())

    def test_env_action(self):
        self.env()
        # must debug this test!
        self.assertEqual(self.env.name, "Monjur")