parameters

Class for reading simulation parameters

 1"""
 2    Class for reading simulation parameters
 3"""
 4import json
 5import math
 6import os
 7from datetime import datetime
 8
 9
10class Parameters(object):
11
12    def __init__(self, path_parameters: str, traces: int):
13        self.TRACES = traces
14        """TRACES: number of traces to generate"""
15        self.PATH_PARAMETERS = path_parameters
16        """PATH_PARAMETERS: path of json file for others parameters. """
17        self.read_metadata_file()
18
19    def read_metadata_file(self):
20        '''
21        Method to read parameters from json file, see *Simulation parameters* to get the whole list of simulation parameters.
22        '''
23        if os.path.exists(self.PATH_PARAMETERS):
24            with open(self.PATH_PARAMETERS) as file:
25                data = json.load(file)
26                roles_table = data['resource_table']
27                self.START_SIMULATION = self._check_default_parameters(data, 'start_timestamp')
28                self.SIM_TIME = self._check_default_parameters(data, 'duration_simulation')
29                self.PROBABILITY = data['probability'] if 'probability' in data.keys() else []
30                self.PROCESSING_TIME = data['processing_time']
31                self.WAITING_TIME = data['waiting_time'] if 'waiting_time' in data.keys() else []
32                self.INTER_TRIGGER = data["interTriggerTimer"]
33                self.ROLE_ACTIVITY = dict()
34                for elem in roles_table:
35                    self.ROLE_ACTIVITY[elem['task']] = elem['role']
36
37                if 'calendar' in data['interTriggerTimer'] and data['interTriggerTimer']['calendar']:
38                    self.ROLE_CAPACITY = {'TRIGGER_TIMER': [math.inf, {'days': data['interTriggerTimer']['calendar']['days'], 'hour_min': data['interTriggerTimer']['calendar']['hour_min'], 'hour_max': data['interTriggerTimer']['calendar']['hour_max']}]}
39                else:
40                    self.ROLE_CAPACITY = {'TRIGGER_TIMER': [math.inf, []]}
41                self._define_roles_resources(data['resource'])
42        else:
43            raise ValueError('Parameter file does not exist')
44
45    def _define_roles_resources(self, roles):
46        for idx, key in enumerate(roles):
47            self.ROLE_CAPACITY[key] = [roles[key]['resources'], {'days': roles[key]['calendar']['days'],
48                                                                      'hour_min': roles[key]['calendar']['hour_min'],
49                                                                      'hour_max': roles[key]['calendar']['hour_max']}]
50
51    def _check_default_parameters(self, data, type):
52        if type == 'start_timestamp':
53            value = datetime.strptime(data['start_timestamp'], '%Y-%m-%d %H:%M:%S') if type in data else datetime.now()
54        elif type == 'duration_simulation':
55            value = data['duration_simulation'] if type in data else 31536000
56        return value
class Parameters:
11class Parameters(object):
12
13    def __init__(self, path_parameters: str, traces: int):
14        self.TRACES = traces
15        """TRACES: number of traces to generate"""
16        self.PATH_PARAMETERS = path_parameters
17        """PATH_PARAMETERS: path of json file for others parameters. """
18        self.read_metadata_file()
19
20    def read_metadata_file(self):
21        '''
22        Method to read parameters from json file, see *Simulation parameters* to get the whole list of simulation parameters.
23        '''
24        if os.path.exists(self.PATH_PARAMETERS):
25            with open(self.PATH_PARAMETERS) as file:
26                data = json.load(file)
27                roles_table = data['resource_table']
28                self.START_SIMULATION = self._check_default_parameters(data, 'start_timestamp')
29                self.SIM_TIME = self._check_default_parameters(data, 'duration_simulation')
30                self.PROBABILITY = data['probability'] if 'probability' in data.keys() else []
31                self.PROCESSING_TIME = data['processing_time']
32                self.WAITING_TIME = data['waiting_time'] if 'waiting_time' in data.keys() else []
33                self.INTER_TRIGGER = data["interTriggerTimer"]
34                self.ROLE_ACTIVITY = dict()
35                for elem in roles_table:
36                    self.ROLE_ACTIVITY[elem['task']] = elem['role']
37
38                if 'calendar' in data['interTriggerTimer'] and data['interTriggerTimer']['calendar']:
39                    self.ROLE_CAPACITY = {'TRIGGER_TIMER': [math.inf, {'days': data['interTriggerTimer']['calendar']['days'], 'hour_min': data['interTriggerTimer']['calendar']['hour_min'], 'hour_max': data['interTriggerTimer']['calendar']['hour_max']}]}
40                else:
41                    self.ROLE_CAPACITY = {'TRIGGER_TIMER': [math.inf, []]}
42                self._define_roles_resources(data['resource'])
43        else:
44            raise ValueError('Parameter file does not exist')
45
46    def _define_roles_resources(self, roles):
47        for idx, key in enumerate(roles):
48            self.ROLE_CAPACITY[key] = [roles[key]['resources'], {'days': roles[key]['calendar']['days'],
49                                                                      'hour_min': roles[key]['calendar']['hour_min'],
50                                                                      'hour_max': roles[key]['calendar']['hour_max']}]
51
52    def _check_default_parameters(self, data, type):
53        if type == 'start_timestamp':
54            value = datetime.strptime(data['start_timestamp'], '%Y-%m-%d %H:%M:%S') if type in data else datetime.now()
55        elif type == 'duration_simulation':
56            value = data['duration_simulation'] if type in data else 31536000
57        return value
Parameters(path_parameters: str, traces: int)
13    def __init__(self, path_parameters: str, traces: int):
14        self.TRACES = traces
15        """TRACES: number of traces to generate"""
16        self.PATH_PARAMETERS = path_parameters
17        """PATH_PARAMETERS: path of json file for others parameters. """
18        self.read_metadata_file()
TRACES

TRACES: number of traces to generate

PATH_PARAMETERS

PATH_PARAMETERS: path of json file for others parameters.

def read_metadata_file(self):
20    def read_metadata_file(self):
21        '''
22        Method to read parameters from json file, see *Simulation parameters* to get the whole list of simulation parameters.
23        '''
24        if os.path.exists(self.PATH_PARAMETERS):
25            with open(self.PATH_PARAMETERS) as file:
26                data = json.load(file)
27                roles_table = data['resource_table']
28                self.START_SIMULATION = self._check_default_parameters(data, 'start_timestamp')
29                self.SIM_TIME = self._check_default_parameters(data, 'duration_simulation')
30                self.PROBABILITY = data['probability'] if 'probability' in data.keys() else []
31                self.PROCESSING_TIME = data['processing_time']
32                self.WAITING_TIME = data['waiting_time'] if 'waiting_time' in data.keys() else []
33                self.INTER_TRIGGER = data["interTriggerTimer"]
34                self.ROLE_ACTIVITY = dict()
35                for elem in roles_table:
36                    self.ROLE_ACTIVITY[elem['task']] = elem['role']
37
38                if 'calendar' in data['interTriggerTimer'] and data['interTriggerTimer']['calendar']:
39                    self.ROLE_CAPACITY = {'TRIGGER_TIMER': [math.inf, {'days': data['interTriggerTimer']['calendar']['days'], 'hour_min': data['interTriggerTimer']['calendar']['hour_min'], 'hour_max': data['interTriggerTimer']['calendar']['hour_max']}]}
40                else:
41                    self.ROLE_CAPACITY = {'TRIGGER_TIMER': [math.inf, []]}
42                self._define_roles_resources(data['resource'])
43        else:
44            raise ValueError('Parameter file does not exist')

Method to read parameters from json file, see Simulation parameters to get the whole list of simulation parameters.