Commit 833e4c02 authored by Georgios Dagkakis's avatar Georgios Dagkakis

EventGenerator to be defined in new way

parent 1d7a4aeb
......@@ -31,17 +31,22 @@ import simpy
from ObjectInterruption import ObjectInterruption
class EventGenerator(ObjectInterruption):
def __init__(self, id=id, name=None, start=None, stop=None, interval=None,
duration=None, method=None, argumentDict=None):
def __init__(self, id=id, name=None, start=0, stop=float('inf'), interval=1,
duration=0, method=None, argumentDict=None):
ObjectInterruption.__init__(self)
self.id=id
self.name=name
self.start=start #the time that the generator will be activated for the first time
self.stop=stop #the time that the generator will stop to trigger events
self.interval=interval #the interval that the generator sleeps
self.duration=duration #the duration that the generation is awake (this is not active for now)
self.start=float(start) #the time that the generator will be activated for the first time
self.stop=float(stop) #the time that the generator will stop to trigger events
self.interval=float(interval) #the interval that the generator sleeps
self.duration=float(duration) #the duration that the generation is awake (this is not active for now)
self.method=method #the method to be invoked
self.argumentDict=argumentDict #the arguments of the method given in a dict
from Globals import G
G.EventGeneratorList.append(self)
if method:
import Globals
self.method=Globals.getMethodFromName(method)
def run(self):
yield self.env.timeout(self.start) #wait until the start time
......
......@@ -165,6 +165,27 @@ def getClassFromName(dotted_name):
_class=__import__(class_name)
return getattr(_class,class_name)
# =======================================================================
# returns a method by its name. name should be given as Dream.ClassName.MethodName
# =======================================================================
def getMethodFromName(dotted_name):
name=dotted_name.split('.')
methodName=name[-1]
# if the method is in this script
if 'Globals' in name:
methodName=name[name.index('Globals')+1]
possibles = globals().copy()
possibles.update(locals())
method = possibles.get(methodName)
# if the method is in a class
else:
clsName=name[0]+'.'+name[1]
cls=getClassFromName(clsName)
method = getattr(cls, methodName)
if not method:
raise Exception("Method %s not implemented" % method_name)
return method
# =======================================================================
# method finding objects by ID
# =======================================================================
......
......@@ -45,7 +45,7 @@
"to": "E1"
},
"interval": "60",
"method": "Globals.moveExcess",
"method": "Dream.Globals.moveExcess",
"name": "ExcessEntitiesMover",
"start": "60"
},
......
......@@ -82,7 +82,7 @@
"_class": "Dream.EventGenerator",
"argumentDict": {},
"interval": "480",
"method": "Globals.countIntervalThroughput",
"method": "Dream.Globals.countIntervalThroughput",
"name": "calculateDailyAttainment",
"start": "480"
},
......
......@@ -424,7 +424,7 @@
"name": "attainment",
"start": "1440",
"interval": "1440",
"method": "Globals.countIntervalThroughput",
"method": "Dream.Globals.countIntervalThroughput",
"argumentDict": {
}
}
......
......@@ -697,29 +697,20 @@ def createObjectInterruptions():
# element itself
element['id'] = element_id # create a new entry for the element (dictionary)
# with key 'id' and value the the element_id
elementClass = element.get('_class', 'not found') # get the class type of the element
if elementClass=='Dream.EventGenerator': # check the object type
id = element.get('id', 'not found') # get the id of the element / default 'not_found'
name = element.get('name', 'not found') # get the name of the element / default 'not_found'
start = float(element.get('start') or 0)
stop = float(element.get('stop') or -1)
# infinity (had to be done to make it as float)
if stop<0:
stop=float('inf')
interval = float(element.get('interval') or 1)
duration = float(element.get('duration') or 0)
method = (element.get('method', None)) # get the method to be run / default None
method = method.split('.') #the method is given as 'Path.MethodName'
method=getattr(str_to_class(method[0]),method[1]) #and then parsed with getattr
argumentDict=(element.get('argumentDict', {})) # get the arguments of the method as a dict / default {}
EV = EventGenerator(id, name, start=start, stop=stop, interval=interval,
duration=duration, method=method, argumentDict=argumentDict) # create the EventGenerator object
objClass = element.get('_class', 'not found') # get the class type of the element
import Globals
objClass = element.pop('_class')
objectType=Globals.getClassFromName(objClass)
# from CoreObject import CoreObject
# if issubclass(objectType, CoreObject):
G.EventGeneratorList.append(EV)
G.ObjectInterruptionList.append(EV)
if objClass=='Dream.EventGenerator': # check the object type
inputDict=dict(element)
# create the CoreObject
objectInterruption=objectType(**inputDict)
G.ObjectInterruptionList.append(objectInterruption)
elif elementClass=='Dream.CapacityStationController': # check the object type
elif objClass=='Dream.CapacityStationController': # check the object type
id = element.get('id', 'not found') # get the id of the element / default 'not_found'
name = element.get('name', 'not found') # get the name of the element / default 'not_found'
start = float(element.get('start') or 0)
......@@ -792,14 +783,6 @@ def createObjectInterruptions():
G.ObjectInterruptionList.append(SS)
G.ShiftSchedulerList.append(SS)
# ===========================================================================
# used to convert a string read from the input to object type
# ===========================================================================
def str_to_class(str):
str = str.replace("Dream.", "") # XXX temporary.
# actuall this method can be dropped in favor of getClassFromName
return getattr(sys.modules[__name__], str)
# ===========================================================================
# the main script that is ran
# ===========================================================================
......
......@@ -38,6 +38,8 @@ class ObjectInterruption(object):
self.victim=victim
# variable used to hand in control to the objectInterruption
self.call=False
from Globals import G
# G.ObjectInterruptionList.append(self)
def initialize(self):
from Globals import G
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment