Пример #1
0
 def _init_data_store(self):
     self._dataStore = DataStore()
     self._dataStore.put('nEvent', self._configuration['General_nEvent'],
                         ObjectLifetime.Application)
     self._dataStore.put('configuration', self._configuration,
                         ObjectLifetime.Application)
     self._dataStore.put('driftChamber', self, ObjectLifetime.Application)
class ParticlePrinterTest(unittest.TestCase):
    """
    Test class for the ParticlePrinter class
    """
    def setUp(self):
        self.datastore = DataStore()
        self.module = ParticlePrinter()
        self.datastore.put('Particles', ParticleContainer(),
                           ObjectLifetime.Application)
        particle = Particle(
            0.1, 'electron', 5, 0, 0.001, 0.005
        )  #Create Particle with determined specification for read out with the printer
        self.datastore.get('Particles').add_particle(particle, 'electron')

    def test_begin(self):
        with LogCapture() as l:
            self.module.begin(self.datastore)

            l.check(('root', 'INFO', "Begin of module 'ParticlePrinter'"))

    def test_event(self):
        with LogCapture() as l:
            self.module.event(self.datastore)

            l.check(
                ('root', 'INFO', "Particle: electron"),
                ('root', 'INFO', "Position\nx=5\ny=0"),
                ('root', 'INFO', "Momentum\np_x=0.001\np_y=0.005"),
            )

    def test_end(self):
        with LogCapture() as l:
            self.module.end(self.datastore)
            l.check(('root', 'INFO', "End of module 'ParticlePrinter'"))
 def setUp(self):
     self.pathToConfgiFiles = os.path.dirname(os.path.abspath(__file__))
     self.path_to_default_test_config_file = self.pathToConfgiFiles + '/particleGun_Electron.cfg'
     configuration = Configuration(self.path_to_default_test_config_file, configuration_specification) #Read configuration from config file
     self.datastore = DataStore()
     self.module = ParticleGun()
     self.datastore.put(self.module, configuration, ObjectLifetime.Application)
Пример #4
0
    def test_put_get_scalar(self):
        ds = DataStore()
        ds.put("int1", 10)
        ds.put("int2", 20)

        self.assertEqual(ds.get("int1"), 10)
        self.assertEqual(ds.get("int2"), 20)
Пример #5
0
    def test_order_of_execution(self):
        order_begin_list = []
        order_event_list = []
        order_end_list = []
        test_module0 = TestOrderOfExcecutionModule(0, order_begin_list,
                                                   order_event_list,
                                                   order_end_list)
        test_module1 = TestOrderOfExcecutionModule(1, order_begin_list,
                                                   order_event_list,
                                                   order_end_list)
        test_module2 = TestOrderOfExcecutionModule(2, order_begin_list,
                                                   order_event_list,
                                                   order_end_list)
        data_store = DataStore()
        data_store.put('nEvent', 3)

        run_engine = RunEngine([test_module0, test_module1, test_module2],
                               data_store)
        run_engine.run()
        self.assertEqual(order_begin_list, [0, 1, 2])
        self.assertEqual(order_event_list, [
            0,
            1,
            2,
            0,
            1,
            2,
            0,
            1,
            2,
        ])
        self.assertEqual(order_end_list, [0, 1, 2])
 def test_objectLifeTime(self):
     data_store = DataStore()
     self_wrapper = self
     test_module = TestObjectLifeTimeModule(lambda: self_wrapper.assertRaises(NotFoundInDataStore, data_store.get, 'EventLifetime'))
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module], data_store)
     run_engine.run()
     data_store.get('ApplicationLifetime')
 def setUp(self):
     self.datastore = DataStore()
     self.module = ParticlePrinter()
     self.datastore.put('Particles', ParticleContainer(),
                        ObjectLifetime.Application)
     particle = Particle(
         0.1, 'electron', 5, 0, 0.001, 0.005
     )  #Create Particle with determined specification for read out with the printer
     self.datastore.get('Particles').add_particle(particle, 'electron')
 def test_run(self):
     configuration = Configuration(self.path_to_default_test_config_file, configuration_specification)
     dataStore = DataStore()
     module = DetectorInitializer()
     dataStore.put(module, configuration, ObjectLifetime.Application)
     module.begin(dataStore)
     # now, there must be a detector present
     detector = dataStore.get("Detector")
     self.assertEqual(detector.width, 100)
     self.assertEqual(detector.nSuperLayers, 11)
     self.assertEqual(detector.nLayersList, [1, 5, 5, 2, 6, 6, 2, 7, 5, 7, 8])
 def test_run(self):
     configuration = Configuration(self.path_to_default_test_config_file, configuration_specification)
     dataStore = DataStore()
     dataStore.put('nEvent', 2)
     module = MultiModule()
     dataStore.put(module, configuration, ObjectLifetime.Application)
     module.begin(dataStore)
     module.event(dataStore)
     module.event(dataStore)
     module.end(dataStore)
     
 def test_run(self):
     configuration = Configuration(self.path_to_default_test_config_file, configuration_specification)
     dataStore = DataStore()
     module = DetectorInitializer()
     dataStore.put(module, configuration, ObjectLifetime.Application)
     module.begin(dataStore)
     # now, there must be a detector present
     detector = dataStore.get('Detector')
     self.assertEqual(detector.width, 100)
     self.assertEqual(detector.nSuperLayers, 11)
     self.assertEqual(detector.nLayersList, [1, 5, 5, 2, 6, 6, 2, 7, 5, 7, 8])
     
 def test_run(self):
     moduleFactory = ModuleFactory('TestModule', 
                                   self.path_to_default_test_config_file,
                                   self.path_to_test_modules,
                                   self.path_to_test_modules_py)
     module = moduleFactory.get_module_instance()
     data_store = DataStore()
     data_store.put('nEvent', 2)
     run_engine = RunEngineFactory([moduleFactory], data_store).get_run_engine()
     self.assertIsInstance(run_engine, RunEngine)
     run_engine.run()
     self.assertEqual(module.beginCalled, 1)
     self.assertEqual(module.eventCalled, 2)
     self.assertEqual(module.endCalled, 1)
 def test_run(self):
     moduleFactory = ModuleFactory('TestModule',
                                   self.path_to_default_test_config_file,
                                   self.path_to_test_modules,
                                   self.path_to_test_modules_py)
     module = moduleFactory.get_module_instance()
     data_store = DataStore()
     data_store.put('nEvent', 2)
     run_engine = RunEngineFactory([moduleFactory],
                                   data_store).get_run_engine()
     self.assertIsInstance(run_engine, RunEngine)
     run_engine.run()
     self.assertEqual(module.beginCalled, 1)
     self.assertEqual(module.eventCalled, 2)
     self.assertEqual(module.endCalled, 1)
 def test_order_of_execution(self):
     order_begin_list = []
     order_event_list = []
     order_end_list = []
     test_module0 = TestOrderOfExcecutionModule(0, order_begin_list, order_event_list, order_end_list)
     test_module1 = TestOrderOfExcecutionModule(1, order_begin_list, order_event_list, order_end_list)
     test_module2 = TestOrderOfExcecutionModule(2, order_begin_list, order_event_list, order_end_list)
     data_store = DataStore()
     data_store.put('nEvent', 3)
     
     run_engine = RunEngine([test_module0, test_module1, test_module2], data_store)
     run_engine.run()
     self.assertEqual(order_begin_list, [0, 1, 2])
     self.assertEqual(order_event_list, [0, 1, 2, 0, 1, 2, 0, 1, 2,])
     self.assertEqual(order_end_list, [0, 1, 2])
Пример #14
0
    def test_ref_assumption_val_type(self):
        ds = DataStore()
        ds.put("item", 20)
        _ = ds.get("item")
        _ = 40

        self.assertEqual(ds.get("item"), 20)
class ParticleGunTest(unittest.TestCase):
    """
    Test class for the ParticleGun class
    """
    def setUp(self):
        self.pathToConfgiFiles = os.path.dirname(os.path.abspath(__file__))
        self.path_to_default_test_config_file = self.pathToConfgiFiles + '/particleGun_Electron.cfg'
        configuration = Configuration(self.path_to_default_test_config_file, configuration_specification) #Read configuration from config file
        self.datastore = DataStore()
        self.module = ParticleGun()
        self.datastore.put(self.module, configuration, ObjectLifetime.Application)


    def test_begin(self):
        with LogCapture() as l:
            self.module.begin(self.datastore)

            l.check(
                    ('root', 'INFO', "Begin of module 'ParticleGun'"),
                    ('root', 'WARNING', "ParticleGun hasn't found detector, set cells to default."),
                    ('root', 'INFO', "Particle gun initialized that shoots a 'electron'.")
            )

    def test_event(self):
        self.module.begin(self.datastore) #Execute begin method to set variables needed to execute event method
        self.module.event(self.datastore)
        for particle in self.datastore.get('Particles').get_all_particles(): #Test outputs to be in expected range
            self.assertLessEqual(particle.position().pos()[0], self.module.cells)
            self.assertEqual(particle.position().pos()[1], 0)
            self.assertLessEqual(particle.momentum().mom()[0], self.module.particle_max_mom)
            self.assertLessEqual(particle.momentum().mom()[1], self.module.particle_max_mom)


    def test_end(self):
        with LogCapture() as l:
            self.module.end(self.datastore)

            l.check(
                    ('root', 'INFO', "End of module 'ParticleGun'")
            )
Пример #16
0
 def test_run(self):
     datastore = DataStore()
     module = HelloWorld()
     with LogCapture() as logCapture:
         module.begin(datastore)
         module.event(datastore)
         module.event(datastore)
         module.end(datastore)
         logCapture.check(
                 ('root', 'INFO', "Begin of module 'HelloWorld'"),
                 ('root', 'INFO', "Number of previous events in module 'HelloWorld': 1"),
                 ('root', 'INFO', "Number of previous events in module 'HelloWorld': 2"),
                 ('root', 'INFO', "End of module 'HelloWorld'")
         )
class ParticlePrinterTest(unittest.TestCase):
    """
    Test class for the ParticlePrinter class
    """
    def setUp(self):
        self.datastore = DataStore()
        self.module = ParticlePrinter()
        self.datastore.put('Particles', ParticleContainer(), ObjectLifetime.Application)
        particle = Particle(0.1, 'electron', 5, 0, 0.001, 0.005) #Create Particle with determined specification for read out with the printer
        self.datastore.get('Particles').add_particle(particle, 'electron')


    def test_begin(self):
        with LogCapture() as l:
            self.module.begin(self.datastore)

            l.check(
                    ('root', 'INFO', "Begin of module 'ParticlePrinter'")
            )

    def test_event(self):
        with LogCapture() as l:
            self.module.event(self.datastore)

            l.check(
                    ('root', 'INFO', "Particle: electron"),
                    ('root', 'INFO', "Position\nx=5\ny=0"),
                    ('root', 'INFO', "Momentum\np_x=0.001\np_y=0.005"),

            )

    def test_end(self):
        with LogCapture() as l:
            self.module.end(self.datastore)
            l.check(
                    ('root', 'INFO', "End of module 'ParticlePrinter'")
            )
Пример #18
0
    def test_run(self):
        test_module1 = Test()
        test_module2 = Test()
        data_store = DataStore()
        data_store.put('nEvent', 100)

        self.assertEqual(test_module1.beginCalled, 0)
        self.assertEqual(test_module1.eventCalled, 0)
        self.assertEqual(test_module1.endCalled, 0)

        self.assertEqual(test_module2.beginCalled, 0)
        self.assertEqual(test_module2.eventCalled, 0)
        self.assertEqual(test_module2.endCalled, 0)

        run_engine = RunEngine([test_module1, test_module2], data_store)
        run_engine.run()

        self.assertEqual(test_module1.beginCalled, 1)
        self.assertEqual(test_module1.eventCalled, 100)
        self.assertEqual(test_module1.endCalled, 1)

        self.assertEqual(test_module2.beginCalled, 1)
        self.assertEqual(test_module2.eventCalled, 100)
        self.assertEqual(test_module2.endCalled, 1)
    def test_run(self):
        test_module1 = Test()
        test_module2 = Test()
        data_store = DataStore()
        data_store.put('nEvent', 100)
        
        self.assertEqual(test_module1.beginCalled, 0)
        self.assertEqual(test_module1.eventCalled, 0)
        self.assertEqual(test_module1.endCalled, 0)
        
        self.assertEqual(test_module2.beginCalled, 0)
        self.assertEqual(test_module2.eventCalled, 0)
        self.assertEqual(test_module2.endCalled, 0)

        run_engine = RunEngine([test_module1, test_module2], data_store)
        run_engine.run()
        
        self.assertEqual(test_module1.beginCalled, 1)
        self.assertEqual(test_module1.eventCalled, 100)
        self.assertEqual(test_module1.endCalled, 1)
        
        self.assertEqual(test_module2.beginCalled, 1)
        self.assertEqual(test_module2.eventCalled, 100)
        self.assertEqual(test_module2.endCalled, 1)
Пример #20
0
 def test_objectLifeTime(self):
     data_store = DataStore()
     self_wrapper = self
     test_module = TestObjectLifeTimeModule(
         lambda: self_wrapper.assertRaises(NotFoundInDataStore, data_store.
                                           get, 'EventLifetime'))
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module], data_store)
     run_engine.run()
     data_store.get('ApplicationLifetime')
Пример #21
0
class DriftChamber:
    """
    Objects of this class represent an instance of the program.
    """
    def __init__(self, p_args=None):
        """
        Constructor.
        
        :param p_args     The command line arguments. Only use this for testing purposes and let it default to sys.argv otherwise.
        """

        # The configuration class also parses the command line arguments.
        # Hence, in order for the argument parser to be the first to stream to the standard output. No output should be streamed before the next line is executed.
        self._args = p_args
        self._init_commandline_argument_parser()
        self._init_confiuration()
        self._init_data_store()
        self._init_run_engine()

    def _init_commandline_argument_parser(self):
        self._commandLineKeyOfConfigurationFile = "config"
        self._commandLineFlagOfConfigurationFile = "--" + self._commandLineKeyOfConfigurationFile
        self._argumentParser = ArgumentParser(
            'Drift Chamber Project of Team A')
        self._argumentParser.add_argument(
            self._commandLineFlagOfConfigurationFile,
            type=str,
            help=
            'Path to a file that holds the configuration for this program run.'
        )
        self._parsedArgs = vars(self._argumentParser.parse_args(self._args))

    def _init_confiuration(self):
        self._configuration = Configuration(
            self._parsedArgs[self._commandLineKeyOfConfigurationFile],
            driftChamberConfig_generalSpecification)

    def _init_data_store(self):
        self._dataStore = DataStore()
        self._dataStore.put('nEvent', self._configuration['General_nEvent'],
                            ObjectLifetime.Application)
        self._dataStore.put('configuration', self._configuration,
                            ObjectLifetime.Application)
        self._dataStore.put('driftChamber', self, ObjectLifetime.Application)

    def _init_run_engine(self):
        self._run_engine = RunEngineFactory(
            self._configuration["Modules_moduleSequence"],
            self._dataStore).get_run_engine()

    def start_simulation(self):
        logging.info("'Drift Chamber Simulation' started.")
        self._run_engine.log_configuration()
        self._run_engine.run()
        logging.info("'Drift Chamber Simulation' done.")
Пример #22
0
 def test_run(self):
     configuration = Configuration(self.path_to_default_test_config_file,
                                   configuration_specification)
     dataStore = DataStore()
     dataStore.put('nEvent', 2)
     module = MultiModule()
     dataStore.put(module, configuration, ObjectLifetime.Application)
     module.begin(dataStore)
     module.event(dataStore)
     module.event(dataStore)
     module.end(dataStore)
Пример #23
0
    def test_ref_assumption_ref_type(self):
        ds = DataStore()

        class WrappedInt(object):
            def __int__(self):
                self.i = 20

        ds.put("item", WrappedInt())
        item = ds.get("item")
        item.i = 40

        self.assertEqual(ds.get("item").i, 40)
class DriftChamber:
    """
    Objects of this class represent an instance of the program.
    """
    def __init__(self, p_args = None):
        """
        Constructor.
        
        :param p_args     The command line arguments. Only use this for testing purposes and let it default to sys.argv otherwise.
        """
        
        # The configuration class also parses the command line arguments.
        # Hence, in order for the argument parser to be the first to stream to the standard output. No output should be streamed before the next line is executed.
        self._args = p_args
        self._init_commandline_argument_parser()
        self._init_confiuration()
        self._init_data_store()
        self._init_run_engine()      
        
        
    def _init_commandline_argument_parser(self):
        self._commandLineKeyOfConfigurationFile = "config"
        self._commandLineFlagOfConfigurationFile = "--" + self._commandLineKeyOfConfigurationFile
        self._argumentParser = ArgumentParser('Drift Chamber Project of Team A')
        self._argumentParser.add_argument(
            self._commandLineFlagOfConfigurationFile, 
            type=str,
            help='Path to a file that holds the configuration for this program run.')
        self._parsedArgs = vars(self._argumentParser.parse_args(self._args))
        
        
    def _init_confiuration(self):
        self._configuration = Configuration(
            self._parsedArgs[self._commandLineKeyOfConfigurationFile],
            driftChamberConfig_generalSpecification)
        
        
    def _init_data_store(self):
        self._dataStore = DataStore()
        self._dataStore.put('nEvent', self._configuration['General_nEvent'], ObjectLifetime.Application)
        self._dataStore.put('configuration', self._configuration, ObjectLifetime.Application)
        self._dataStore.put('driftChamber', self, ObjectLifetime.Application)
        
            
    def _init_run_engine(self):
        self._run_engine = RunEngineFactory(self._configuration["Modules_moduleSequence"], self._dataStore).get_run_engine()
            
        
    def start_simulation(self):
        logging.info("'Drift Chamber Simulation' started.")
        self._run_engine.log_configuration()
        self._run_engine.run()
        logging.info("'Drift Chamber Simulation' done.")
 def test_impossibility_to_get_run_engine_in_invalid_state(self):
     test_module1 = Test()
     test_module2 = Test()
     data_store = DataStore()
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module1, test_module2], data_store)
     self.assertRaises(RunEngineInInconsistentState, run_engine.call_next_event_methods)
     
     test_module1 = Test()
     test_module2 = Test()
     data_store = DataStore()
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module1, test_module2], data_store)
     self.assertRaises(RunEngineInInconsistentState, run_engine.call_all_end_methods)
     
     test_module1 = Test()
     test_module2 = Test()
     data_store = DataStore()
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module1, test_module2], data_store)
     run_engine.call_all_begin_methods()
     self.assertRaises(RunEngineInInconsistentState, run_engine.call_all_begin_methods)
     
     test_module1 = Test()
     test_module2 = Test()
     data_store = DataStore()
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module1, test_module2], data_store)
     run_engine.call_all_begin_methods()
     run_engine.call_next_event_methods()
     run_engine.clear_event_based_storage()
     run_engine.call_next_event_methods()
     run_engine.clear_event_based_storage()
     # can not call call_next_event_methods() method more often than nEvent times
     self.assertRaises(RunEngineInInconsistentState, run_engine.call_next_event_methods)
     
     test_module1 = Test()
     test_module2 = Test()
     data_store = DataStore()
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module1, test_module2], data_store)
     run_engine.call_all_begin_methods()
     run_engine.call_next_event_methods()
     run_engine.clear_event_based_storage()
     run_engine.call_next_event_methods()
     run_engine.clear_event_based_storage()
     run_engine.call_all_end_methods()
     self.assertRaises(RunEngineInInconsistentState, run_engine.call_all_end_methods)
 def setUp(self):
     self.datastore = DataStore()
     self.module = ParticlePrinter()
     self.datastore.put('Particles', ParticleContainer(), ObjectLifetime.Application)
     particle = Particle(0.1, 'electron', 5, 0, 0.001, 0.005) #Create Particle with determined specification for read out with the printer
     self.datastore.get('Particles').add_particle(particle, 'electron')
Пример #27
0
    def test_impossibility_to_get_run_engine_in_invalid_state(self):
        test_module1 = Test()
        test_module2 = Test()
        data_store = DataStore()
        data_store.put('nEvent', 2)
        run_engine = RunEngine([test_module1, test_module2], data_store)
        self.assertRaises(RunEngineInInconsistentState,
                          run_engine.call_next_event_methods)

        test_module1 = Test()
        test_module2 = Test()
        data_store = DataStore()
        data_store.put('nEvent', 2)
        run_engine = RunEngine([test_module1, test_module2], data_store)
        self.assertRaises(RunEngineInInconsistentState,
                          run_engine.call_all_end_methods)

        test_module1 = Test()
        test_module2 = Test()
        data_store = DataStore()
        data_store.put('nEvent', 2)
        run_engine = RunEngine([test_module1, test_module2], data_store)
        run_engine.call_all_begin_methods()
        self.assertRaises(RunEngineInInconsistentState,
                          run_engine.call_all_begin_methods)

        test_module1 = Test()
        test_module2 = Test()
        data_store = DataStore()
        data_store.put('nEvent', 2)
        run_engine = RunEngine([test_module1, test_module2], data_store)
        run_engine.call_all_begin_methods()
        run_engine.call_next_event_methods()
        run_engine.clear_event_based_storage()
        run_engine.call_next_event_methods()
        run_engine.clear_event_based_storage()
        # can not call call_next_event_methods() method more often than nEvent times
        self.assertRaises(RunEngineInInconsistentState,
                          run_engine.call_next_event_methods)

        test_module1 = Test()
        test_module2 = Test()
        data_store = DataStore()
        data_store.put('nEvent', 2)
        run_engine = RunEngine([test_module1, test_module2], data_store)
        run_engine.call_all_begin_methods()
        run_engine.call_next_event_methods()
        run_engine.clear_event_based_storage()
        run_engine.call_next_event_methods()
        run_engine.clear_event_based_storage()
        run_engine.call_all_end_methods()
        self.assertRaises(RunEngineInInconsistentState,
                          run_engine.call_all_end_methods)
 def _init_data_store(self):
     self._dataStore = DataStore()
     self._dataStore.put('nEvent', self._configuration['General_nEvent'], ObjectLifetime.Application)
     self._dataStore.put('configuration', self._configuration, ObjectLifetime.Application)
     self._dataStore.put('driftChamber', self, ObjectLifetime.Application)
Пример #29
0
    def test_get_invalid(self):
        ds = DataStore()

        with self.assertRaises(NotFoundInDataStore):
            ds.get("not_set")
Пример #30
0
 def test_put_twice(self):
     ds = DataStore()
     ds.put("item", 20)
     with self.assertRaises(AlreadyInDataStore):
         ds.put("item", 10)
Пример #31
0
    def test_lifetime(self):
        ds = DataStore()
        # implicit event lifetime
        ds.put("item1", 20)
        # explicit event lifetime
        ds.put("item2", 30, ObjectLifetime.Event)
        # explicit application lifetime
        ds.put("item3", 40, ObjectLifetime.Application)

        ds.clear(ObjectLifetime.Event)

        with self.assertRaises(NotFoundInDataStore):
            ds.get("item1")
        with self.assertRaises(NotFoundInDataStore):
            ds.get("item2")

        self.assertIsNotNone(ds.get("item3"))

        ds.clear(ObjectLifetime.Application)

        with self.assertRaises(NotFoundInDataStore):
            ds.get("item3")