Ejemplo n.º 1
0
Archivo: alex.py Proyecto: nextsw/alex
 def __init__(self, name='alex', nevts=100, evtfreq=100):
     """ Alex class
     It is the main driver application
     It has a list of algorithms that runs in sequence for a given number of events 
     It has a dictionary of services to be set into each algorithm
     It has a dictionary with run information and event information (clean each event) to be set into in each alorithm
     """
     self.name = name # name of the application 
     self.ievt = 0 # current number of event processed
     self.nevts = nevts # number of events to run
     self.evtfreq = evtfreq # frequency to print event number in log
     self.counters = {}
     self.algs = [] # list of algorithms to run
     self.svcs = {} # dictionary with the services
     # primordial msg service
     self.msg = Messenger(name='msg') 
     self.addsvc(self.msg) # add msg service 
     # primordial request service (handle errrors, abort/end of job requets)
     self.req = RequestSvc(name='req',al=self) 
     self.addsvc(self.req) # add request service
     # event data service
     self.evt = DataSvc(name='evt')
     self.addsvc(self.evt) # add event data service
     self.exports = ['msg','req','evt'] # services to export to algorithms
     return 
Ejemplo n.º 2
0
Archivo: alex.py Proyecto: nextsw/alex
class Alex():
    
    def __init__(self, name='alex', nevts=100, evtfreq=100):
        """ Alex class
        It is the main driver application
        It has a list of algorithms that runs in sequence for a given number of events 
        It has a dictionary of services to be set into each algorithm
        It has a dictionary with run information and event information (clean each event) to be set into in each alorithm
        """
        self.name = name # name of the application 
        self.ievt = 0 # current number of event processed
        self.nevts = nevts # number of events to run
        self.evtfreq = evtfreq # frequency to print event number in log
        self.counters = {}
        self.algs = [] # list of algorithms to run
        self.svcs = {} # dictionary with the services
        # primordial msg service
        self.msg = Messenger(name='msg') 
        self.addsvc(self.msg) # add msg service 
        # primordial request service (handle errrors, abort/end of job requets)
        self.req = RequestSvc(name='req',al=self) 
        self.addsvc(self.req) # add request service
        # event data service
        self.evt = DataSvc(name='evt')
        self.addsvc(self.evt) # add event data service
        self.exports = ['msg','req','evt'] # services to export to algorithms
        return 

    def _declare(self,obj):
        """ add services and stores to the object 
        """    
        if (not hasattr(obj,'imports')): return
        nsvcs = obj.imports
        if (not isinstance(nsvcs,list)):
            self.msg.error(self.name,' no list of importing svc ',nsvcs)
            return
        if (isinstance(obj,ISvc)):
            if (not 'msg' in nsvcs): nsvcs.append('msg')
        elif (isinstance(obj,IAlg)):
            for svc in self.exports:
                if (not svc in nsvcs): nsvcs.append(svc)
        #self.msg.info(self.name,' importing services to ', obj.name)
        for nsvc in nsvcs:
            if (not self.svcs.has_key(nsvc)):
                self.msg.error(self.name,' no service ',nsvc,' to import ')
            else:
                svc = self.svcs[nsvc]
                if (svc.name != obj.name): setattr(obj,svc.name,svc) 
        return 
    
    def addsvc(self,svc):
        """ add a service by its name.
        Each service hass access to the msg svc and run store
        """
        self.msg.cinfo(self.name,' adding service ',svc.name)
        self.svcs[svc.name] = svc
        setattr(self,svc.name,svc)
        self._declare(svc)
        return

    def addalg(self,alg):
        """ add an algorithm to the list of algorithms to run.
        Services are set into the algorithm.
        It has access to the run store and event store.
        """
        self.msg.cinfo(self.name,' algorithm to run ',alg.name)
        self.algs.append(alg)
        self._declare(alg)
        # decorate the objects underneath
        if (hasattr(alg,'algs')):
            map(self._declare,alg.algs)
        return

    def initialize(self):
        """ initilize the services and the algorithms
        """
        self.msg.cinfo(' initilize ')            
        svcs = self.svcs.values()
        map(self._declare, svcs)
        oks1 = map(lambda svc: svc.initialize(), svcs)
        if (len(self.algs) == 0):
            self.msg.warning(self.name,' No algorithms to run')
            return True
        for alg in self.algs: self.counters[alg.name] = 0
        oks2 = map(lambda alg: alg.initialize(), self.algs)
        return (isok(oks1) and isok(oks2))

    def execute(self):
        """ do the execute method over the list of algorithms
        """
        if (self.ievt%self.evtfreq==0): 
            self.msg.cinfo(' event ', self.ievt)
        self.evt.clear()
        if (len(self.algs)==0): 
            self.ievt += 1; return True
        oks = map(lambda alg: alg.execute(), self.algs)
        def count(alg,ok):
            if (ok): self.counters[alg.name]+=1
        map(count,self.algs,oks)
        self.ievt+=1
        return isok(oks)
        
    def finalize(self):
        """ finalize the list of algorithms and the sercices
        """
        self.msg.cinfo(' finalize ')
        self.msg.cinfo(' events ',self.ievt)
        def infocounter(alg):
            self.msg.info(' counter algorithm ',alg.name,
                          self.counters[alg.name])
        if (len(self.algs)>0): 
            map(infocounter,self.algs)
            oks1 = map(lambda alg: alg.finalize(), self.algs)
        svcs = self.svcs.values()
        oks2 = map(lambda svc: svc.finalize(), svcs)
        self.msg.cinfo(' end!')
        exit()

    def loop(self,nevts):
        """  do execute in a loop with range nevts
        """
        self.msg.cinfo(' loop events ',nevts)
        for i in range(nevts): self.execute()
        return

    def run(self):
        """ run alex: initialize, loop nevents (do execute to each one) 
        and finalize
        """
        self.msg.cinfo(' run ')
        self.initialize()
        self.loop(self.nevts)
        self.finalize()
        return