def setUp(self) -> None: """Set ups tests""" self.dynamic_system = DynamicSystemMock() self.model_1 = BaseModelMock(self.dynamic_system, "model 1") self.model_2 = BaseModelMock(self.dynamic_system, "model 2") self.path = Path(self.model_1, self.model_2, ExpressionProperty(Value(1)))
class DiscreteEventModelTest(unittest.TestCase): """Discrete event model tests""" dynamic_system: DiscreteEventDynamicSystem model: DiscreteEventModel def setUp(self) -> None: """Sets up tests""" self.dynamic_system = DynamicSystemMock(Scheduler()) self.model = ModelMock(self.dynamic_system, "model") def tearDown(self) -> None: """Remove changes of the tests.""" static_entity_manager._saved_names = set() def test_schedule(self): """Should schedule an event.""" self.model.schedule(Decimal(5)) self.assertEqual(5, self.dynamic_system.get_time_of_next_events()) def test_get_time(self): """Should retrieve the time evaluating the time advance function""" self.model._time_advance_function = lambda s: 10 self.assertEqual(10, self.model.get_time()) def test_get_output(self): """Should retrieve the output evaluating the time output function""" self.model._output_function = lambda s: 42 self.assertEqual(42, self.model.get_output()) def test_internal_transition(self): """Should execute autonomous, external and confluent transitions.""" self.model.set_up_state(0) self.model._time_advance_function = lambda s: 10 self.model._external_state_transition_function = lambda s, i, t: 1 self.model._internal_state_transition_function = lambda s: s + 2 self.model.state_transition(event_time=Decimal(0)) self.assertEqual(2, self.model.get_state()) def test_external_transition(self): """Should execute autonomous, external and confluent transitions.""" self.model.set_up_state(0) self.model._time_advance_function = lambda s: 10 self.model._external_state_transition_function = lambda s, i, t: s + i self.model._internal_state_transition_function = lambda s: s + 2 self.model.state_transition(inputs=19, event_time=Decimal(2)) self.assertEqual(19, self.model.get_state()) def test_confluent_transition(self): """Should execute autonomous, external and confluent transitions.""" self.model.set_up_state(0) self.model._time_advance_function = lambda s: 10 self.model._external_state_transition_function = lambda s, i, t: s + i self.model._internal_state_transition_function = lambda s: s + 2 self.model.state_transition(inputs=19, event_time=Decimal(0)) self.assertEqual(21, self.model.get_state())
def setUp(self) -> None: """Sets up tests""" self.dynamic_system = DynamicSystemMock(Scheduler())
class DiscreteEventDynamicSystemTest(unittest.TestCase): """Base dynamic system tests""" dynamic_system: DiscreteEventDynamicSystem def setUp(self) -> None: """Sets up tests""" self.dynamic_system = DynamicSystemMock(Scheduler()) def tearDown(self) -> None: """Remove changes of the tests.""" Entity._saved_names = set() def test_schedule(self): """Should schedule a model""" m = ModelMock(self.dynamic_system) self.dynamic_system.schedule(m, Decimal(10)) self.assertEqual(10, self.dynamic_system.get_time_of_next_events()) def test_get_next_models(self): """Should retrieve the next models""" m1 = ModelMock(self.dynamic_system) m2 = ModelMock(self.dynamic_system) m3 = ModelMock(self.dynamic_system) self.dynamic_system.schedule(m1, Decimal(10)) self.dynamic_system.schedule(m2, Decimal(10)) self.dynamic_system.schedule(m3, Decimal(11)) self.assertEqual({m1, m2}, self.dynamic_system.get_next_models()) def test_get_output(self): """Should retrieve the next models""" m1 = ModelMock(self.dynamic_system) self.dynamic_system.schedule(m1, Decimal(10)) m1.get_output = lambda: 5 self.assertDictEqual({m1: 5}, self.dynamic_system.get_output()) def test_get_effective_paths_ones(self): """Should get the valid paths for each output""" m1 = ModelMock(self.dynamic_system) m2 = ModelMock(self.dynamic_system) m3 = ModelMock(self.dynamic_system) m12 = Path(m1, m2, ExpressionProperty(Value(1))) m13 = Path(m1, m3, ExpressionProperty(Value(1))) m23 = Path(m2, m3, ExpressionProperty(Value(1))) self.dynamic_system.link(m12) self.dynamic_system.link(m13) self.dynamic_system.link(m23) m1.get_output = lambda: 5 m2.get_output = lambda: 15 m3.get_output = lambda: 25 self.dynamic_system.schedule(m1, Decimal(10)) self.dynamic_system.schedule(m2, Decimal(10)) self.dynamic_system.schedule(m3, Decimal(10)) self.dynamic_system.get_output() self.assertEqual({m12, m13}, self.dynamic_system._get_effective_paths(m1)) self.assertEqual({m23}, self.dynamic_system._get_effective_paths(m2)) def test_get_effective_paths_probability(self): """Should get the valid paths for each output probability""" m1 = ModelMock(self.dynamic_system) m2 = ModelMock(self.dynamic_system) m3 = ModelMock(self.dynamic_system) m11 = Path(m1, m1, ExpressionProperty(Value(0.1))) m12 = Path(m1, m2, ExpressionProperty(Value(0.3))) m13 = Path(m1, m3, ExpressionProperty(Value(0.6))) m23 = Path(m2, m3, ExpressionProperty(Value(1))) self.dynamic_system.link(m11) self.dynamic_system.link(m12) self.dynamic_system.link(m13) self.dynamic_system.link(m23) m1.get_output = lambda: 5 m2.get_output = lambda: 15 m3.get_output = lambda: 25 self.dynamic_system.schedule(m1, Decimal(10)) self.dynamic_system.schedule(m2, Decimal(10)) self.dynamic_system.schedule(m3, Decimal(10)) self.dynamic_system.get_output() self.assertEqual(1, len(self.dynamic_system._get_effective_paths(m1))) self.assertEqual({m23}, self.dynamic_system._get_effective_paths(m2)) def test_get_affected_models_and_its_inputs(self): """Should get the affected models and its inputs""" m1 = ModelMock(self.dynamic_system) m2 = ModelMock(self.dynamic_system) m3 = ModelMock(self.dynamic_system) m12 = Path(m1, m2, ExpressionProperty(Value(1))) m13 = Path(m1, m3, ExpressionProperty(Value(1))) m23 = Path(m2, m3, ExpressionProperty(Value(1))) np.random.seed(42) self.dynamic_system.link(m12) self.dynamic_system.link(m13) self.dynamic_system.link(m23) m1.get_output = lambda: 5 m2.get_output = lambda: 15 m3.get_output = lambda: 25 self.dynamic_system.schedule(m1, Decimal(10)) self.dynamic_system.schedule(m2, Decimal(10)) self.dynamic_system.schedule(m3, Decimal(10)) self.dynamic_system.get_output() ( affected_models, inputs, ) = self.dynamic_system._get_affected_models_and_its_inputs() self.assertEqual({m2, m3}, affected_models) self.assertDictEqual( { m2: { m1.get_id(): 5 }, m3: { m1.get_id(): 5, m2.get_id(): 15 } }, inputs)
class BaseDynamicSystemTest(unittest.TestCase): """Base dynamic system tests""" dynamic_system: BaseDynamicSystem def setUp(self) -> None: """Sets up tests""" self.dynamic_system = DynamicSystemMock(Scheduler()) def tearDown(self) -> None: """Remove changes of the tests.""" Entity._saved_names = set() def test_add_model(self) -> None: """Adds models to the dynamic system.""" models = set() for i in range(3): model = BaseModelMock(self.dynamic_system) models.add(model) self.assertEqual(models, self.dynamic_system._models) def test_link(self) -> None: """Adds paths to the dynamic system.""" m1 = BaseModelMock(self.dynamic_system) m2 = BaseModelMock(self.dynamic_system) m3 = BaseModelMock(self.dynamic_system) m1.add(m2) m2.add(m3) m12 = self.dynamic_system._paths[m1].pop() m23 = self.dynamic_system._paths[m2].pop() self.assertEqual(m12.get_source_model(), m1) self.assertEqual(m12.get_destination_model(), m2) self.assertEqual(m23.get_source_model(), m2) self.assertEqual(m23.get_destination_model(), m3) def test_remove_model(self) -> None: """Removes a model of the dynamic system.""" m1 = BaseModelMock(self.dynamic_system) m2 = BaseModelMock(self.dynamic_system) m3 = BaseModelMock(self.dynamic_system) self.dynamic_system.remove(m2) self.assertEqual({m1, m3}, self.dynamic_system._models) def test_remove_path(self) -> None: """Removes a model of the dynamic system.""" m1 = BaseModelMock(self.dynamic_system) m2 = BaseModelMock(self.dynamic_system) m3 = BaseModelMock(self.dynamic_system) m12 = Path(m1, m2, ExpressionProperty(Value(1))) m23 = Path(m2, m3, ExpressionProperty(Value(1))) self.dynamic_system.link(m12) self.dynamic_system.link(m23) self.assertEqual({m12}, self.dynamic_system._paths[m1]) self.dynamic_system.unlink(m12) self.assertTrue(m1 not in self.dynamic_system._paths) def test_remove_model_and_path(self) -> None: """Removes a model of the dynamic system.""" m1 = BaseModelMock(self.dynamic_system) m2 = BaseModelMock(self.dynamic_system) m3 = BaseModelMock(self.dynamic_system) m12 = Path(m1, m2, ExpressionProperty(Value(1))) m23 = Path(m2, m3, ExpressionProperty(Value(1))) m13 = Path(m1, m3, ExpressionProperty(Value(1))) m31 = Path(m3, m1, ExpressionProperty(Value(1))) m32 = Path(m3, m2, ExpressionProperty(Value(1))) self.dynamic_system.link(m12) self.dynamic_system.link(m23) self.dynamic_system.link(m13) self.dynamic_system.link(m31) self.dynamic_system.link(m32) self.assertEqual({m12, m13}, self.dynamic_system._paths[m1]) self.assertEqual({m31, m32}, self.dynamic_system._paths[m3]) self.dynamic_system.remove(m1) self.assertTrue(m1 not in self.dynamic_system._paths) self.assertEqual({m32}, self.dynamic_system._paths[m3]) def test_show(self) -> None: """Shows a diagram of the dynamic system""" m1 = BaseModelMock(self.dynamic_system) m2 = BaseModelMock(self.dynamic_system) m3 = BaseModelMock(self.dynamic_system) m1.add(m2) m2.add(m3) m3.add(m1) m1.add(m1) self.dynamic_system.show()
def setUp(self) -> None: """Sets up tests""" self.dynamic_system = DynamicSystemMock(Scheduler()) self.model = BaseModelMock(self.dynamic_system, "model")
def setUp(self) -> None: """Sets up tests""" dynamic_system = DynamicSystemMock(Scheduler()) self.experiment = DiscreteEventExperiment(dynamic_system)