Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:54

0001 from __future__ import absolute_import
0002 from .SequenceTypes import *
0003 from .Modules import OutputModule, EDProducer, EDFilter, EDAnalyzer, Service, ESProducer, ESSource, _Module
0004 from .Mixins import _Labelable
0005 
0006 # Use this on Tasks in the Schedule
0007 class ScheduleTaskValidator(object):
0008     def __init__(self):
0009         pass
0010     def enter(self,visitee):
0011         if visitee.isLeaf():
0012             if isinstance(visitee, _Labelable):
0013                 if not visitee.hasLabel_():
0014                     raise ValueError("A task associated with the Schedule contains a module of type '"+visitee.type_()+"'\nwhich has no assigned label.")
0015             elif isinstance(visitee, Service):
0016                 if not visitee._inProcess:
0017                     raise ValueError("A task associated with the Schedule contains a service of type '"+visitee.type_()+"'\nwhich is not attached to the process.")
0018     def leave(self,visitee):
0019         pass
0020 
0021 # Use this on Paths
0022 class PathValidator(object):
0023     def __init__(self):
0024         self.__label = ''
0025     def setLabel(self,label:str):
0026         self.__label = "'"+label+"' "
0027     def enter(self,visitee):
0028         if isinstance(visitee,OutputModule):
0029             raise ValueError("Path "+self.__label+"cannot contain an OutputModule, '"+visitee.type_()+"', with label '"+visitee.label_()+"'")
0030         if visitee.isLeaf():
0031             if isinstance(visitee, _Labelable):
0032                 if not visitee.hasLabel_():
0033                     raise ValueError("Path "+self.__label+"contains a module of type '"+visitee.type_()+"' which has no assigned label.")
0034             elif isinstance(visitee, Service):
0035                 if not visitee._inProcess:
0036                     raise ValueError("Path "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
0037     def leave(self,visitee):
0038         pass
0039 
0040 # Use this on EndPaths
0041 class EndPathValidator(object):
0042     _presetFilters = ["TriggerResultsFilter", "HLTPrescaler"]
0043     def __init__(self):   
0044         self.filtersOnEndpaths = []
0045         self.__label = ''
0046         self._levelInTasks = 0
0047     def setLabel(self,label:str):
0048         self.__label = "'"+label+"' "
0049     def enter(self,visitee):
0050         if visitee.isLeaf():
0051             if isinstance(visitee, _Labelable):
0052                 if not visitee.hasLabel_():
0053                     raise ValueError("EndPath "+self.__label+"contains a module of type '"+visitee.type_()+"' which has\nno assigned label.")
0054             elif isinstance(visitee, Service):
0055                 if not visitee._inProcess:
0056                     raise ValueError("EndPath "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
0057         if isinstance(visitee, Task):
0058             self._levelInTasks += 1
0059         if self._levelInTasks > 0:
0060             return
0061         if isinstance(visitee,EDFilter):
0062             if (visitee.type_() in self._presetFilters):
0063                 if (visitee.type_() not in self.filtersOnEndpaths):
0064                     self.filtersOnEndpaths.append(visitee.type_())
0065     def leave(self,visitee):
0066         if self._levelInTasks > 0:
0067             if isinstance(visitee, Task):
0068                 self._levelInTasks -= 1
0069 
0070 # Use this on EndPaths
0071 class FinalPathValidator(object):
0072     def __init__(self):
0073         self.__label = ''
0074         self._levelInTasks = 0
0075         self.invalidModulesOnFinalpaths = []
0076     def setLabel(self,label:str):
0077         self.__label = "'"+label+"' "
0078     def enter(self,visitee):
0079         if visitee.isLeaf():
0080             if isinstance(visitee, _Labelable):
0081                 if not visitee.hasLabel_():
0082                     raise ValueError("FinalPath "+self.__label+"contains a module of type '"+visitee.type_()+"' which has\nno assigned label.")
0083             elif isinstance(visitee, Service):
0084                 if not visitee._inProcess:
0085                     raise ValueError("FinalPath "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
0086         if isinstance(visitee, Task):
0087             self._levelInTasks += 1
0088         if self._levelInTasks > 0:
0089             return
0090         if isinstance(visitee,(EDAnalyzer,EDProducer,EDFilter)):
0091             self.invalidModulesOnFinalpaths.append(visitee.type_())
0092     def leave(self,visitee):
0093         if self._levelInTasks > 0:
0094             if isinstance(visitee, Task):
0095                 self._levelInTasks -= 1
0096 
0097 class NodeVisitor(object):
0098     """Form sets of all modules, ESProducers, ESSources and Services in visited objects. Can be used
0099     to visit Paths, EndPaths, Sequences or Tasks. Includes in sets objects on sub-Sequences and sub-Tasks"""
0100     def __init__(self):
0101         self.modules = set()
0102         self.esProducers = set()
0103         self.esSources = set()
0104         self.services = set()
0105     def enter(self,visitee):
0106         if visitee.isLeaf():
0107             if isinstance(visitee, _Module):
0108                 self.modules.add(visitee)
0109             elif isinstance(visitee, ESProducer):
0110                 self.esProducers.add(visitee)
0111             elif isinstance(visitee, ESSource):
0112                 self.esSources.add(visitee)
0113             elif isinstance(visitee, Service):
0114                 self.services.add(visitee)
0115     def leave(self,visitee):
0116         pass
0117 
0118 class CompositeVisitor(object):
0119     """ Combines 3 different visitor classes in 1 so we only have to visit all the paths and endpaths once"""
0120     def __init__(self, validator, node, decorated, optional=None):
0121         self._validator = validator
0122         self._node = node
0123         self._decorated = decorated
0124         self._optional = optional
0125     def enter(self, visitee):
0126         self._validator.enter(visitee)
0127         self._node.enter(visitee)
0128         self._decorated.enter(visitee)
0129         if self._optional:
0130           self._optional.enter(visitee)
0131     def leave(self, visitee):
0132         self._validator.leave(visitee)
0133         # The node visitor leave function does nothing
0134         #self._node.leave(visitee)
0135         self._decorated.leave(visitee)
0136         if self._optional:
0137           self._optional.leave(visitee)
0138 
0139 class ModuleNamesFromGlobalsVisitor(object):
0140     """Fill a list with the names of Event module types in a sequence. The names are determined
0141     by using globals() to lookup the variable names assigned to the modules. This
0142     allows the determination of the labels before the modules have been attached to a Process."""
0143     def __init__(self,globals_,l):
0144         self._moduleToName = { v[1]:v[0] for v in globals_.items() if isinstance(v[1],_Module) }
0145         self._names =l
0146     def enter(self,node):
0147         if isinstance(node,_Module):
0148             self._names.append(self._moduleToName[node])
0149     def leave(self,node):
0150         return
0151 
0152 if __name__=="__main__":
0153     import unittest
0154     class TestModuleCommand(unittest.TestCase):
0155         def setUp(self):
0156             """Nothing to do """
0157             pass
0158         def testValidators(self):
0159             producer = EDProducer("Producer")
0160             analyzer = EDAnalyzer("Analyzer")
0161             output = OutputModule("Out")
0162             filter = EDFilter("Filter")
0163             unlabeled = EDAnalyzer("UnLabeled")
0164             producer.setLabel("producer")
0165             analyzer.setLabel("analyzer")
0166             output.setLabel("output")
0167             filter.setLabel("filter")
0168             s1 = Sequence(analyzer*producer)
0169             s2 = Sequence(output+filter)
0170             p1 = Path(s1)
0171             p2 = Path(s1*s2)
0172             p3 = Path(s1+unlabeled)
0173             ep1 = EndPath(producer+output+analyzer)
0174             ep2 = EndPath(filter+output)
0175             ep3 = EndPath(s2)
0176             ep4 = EndPath(unlabeled)
0177             pathValidator = PathValidator()
0178             endpathValidator = EndPathValidator()
0179             p1.visit(pathValidator)
0180             self.assertRaises(ValueError, p2.visit, pathValidator) 
0181             self.assertRaises(ValueError, p3.visit, pathValidator) 
0182             ep1.visit(endpathValidator) 
0183             ep2.visit(endpathValidator) 
0184             ep3.visit(endpathValidator) 
0185             self.assertRaises(ValueError, ep4.visit, endpathValidator) 
0186 
0187     unittest.main()
0188