CMS 3D CMS Logo

SequenceVisitors.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 from .SequenceTypes import *
3 from .Modules import OutputModule, EDProducer, EDFilter, EDAnalyzer, Service, ESProducer, ESSource, _Module
4 from .Mixins import _Labelable
5 import six
6 
7 # Use this on Tasks in the Schedule
9  def __init__(self):
10  pass
11  def enter(self,visitee):
12  if visitee.isLeaf():
13  if isinstance(visitee, _Labelable):
14  if not visitee.hasLabel_():
15  raise ValueError("A task associated with the Schedule contains a module of type '"+visitee.type_()+"'\nwhich has no assigned label.")
16  elif isinstance(visitee, Service):
17  if not visitee._inProcess:
18  raise ValueError("A task associated with the Schedule contains a service of type '"+visitee.type_()+"'\nwhich is not attached to the process.")
19  def leave(self,visitee):
20  pass
21 
22 # Use this on Paths
23 class PathValidator(object):
24  def __init__(self):
25  self.__label = ''
26  def setLabel(self,label):
27  self.__label = "'"+label+"' "
28  def enter(self,visitee):
29  if isinstance(visitee,OutputModule):
30  raise ValueError("Path "+self.__label+"cannot contain an OutputModule, '"+visitee.type_()+"', with label '"+visitee.label_()+"'")
31  if visitee.isLeaf():
32  if isinstance(visitee, _Labelable):
33  if not visitee.hasLabel_():
34  raise ValueError("Path "+self.__label+"contains a module of type '"+visitee.type_()+"' which has no assigned label.")
35  elif isinstance(visitee, Service):
36  if not visitee._inProcess:
37  raise ValueError("Path "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
38  def leave(self,visitee):
39  pass
40 
41 # Use this on EndPaths
42 class EndPathValidator(object):
43  _presetFilters = ["TriggerResultsFilter", "HLTPrescaler"]
44  def __init__(self):
46  self.__label = ''
47  self._levelInTasks = 0
48  def setLabel(self,label):
49  self.__label = "'"+label+"' "
50  def enter(self,visitee):
51  if visitee.isLeaf():
52  if isinstance(visitee, _Labelable):
53  if not visitee.hasLabel_():
54  raise ValueError("EndPath "+self.__label+"contains a module of type '"+visitee.type_()+"' which has\nno assigned label.")
55  elif isinstance(visitee, Service):
56  if not visitee._inProcess:
57  raise ValueError("EndPath "+self.__label+"contains a service of type '"+visitee.type_()+"' which is not attached to the process.\n")
58  if isinstance(visitee, Task):
59  self._levelInTasks += 1
60  if self._levelInTasks > 0:
61  return
62  if isinstance(visitee,EDFilter):
63  if (visitee.type_() in self._presetFilters):
64  if (visitee.type_() not in self.filtersOnEndpaths):
65  self.filtersOnEndpaths.append(visitee.type_())
66  def leave(self,visitee):
67  if self._levelInTasks > 0:
68  if isinstance(visitee, Task):
69  self._levelInTasks -= 1
70 
72  """Form sets of all modules, ESProducers, ESSources and Services in visited objects. Can be used
73  to visit Paths, EndPaths, Sequences or Tasks. Includes in sets objects on sub-Sequences and sub-Tasks"""
74  def __init__(self):
75  self.modules = set()
76  self.esProducers = set()
77  self.esSources = set()
78  self.services = set()
79  def enter(self,visitee):
80  if visitee.isLeaf():
81  if isinstance(visitee, _Module):
82  self.modules.add(visitee)
83  elif isinstance(visitee, ESProducer):
84  self.esProducers.add(visitee)
85  elif isinstance(visitee, ESSource):
86  self.esSources.add(visitee)
87  elif isinstance(visitee, Service):
88  self.services.add(visitee)
89  def leave(self,visitee):
90  pass
91 
92 class CompositeVisitor(object):
93  """ Combines 3 different visitor classes in 1 so we only have to visit all the paths and endpaths once"""
94  def __init__(self, validator, node, decorated):
95  self._validator = validator
96  self._node = node
97  self._decorated = decorated
98  def enter(self, visitee):
99  self._validator.enter(visitee)
100  self._node.enter(visitee)
101  self._decorated.enter(visitee)
102  def leave(self, visitee):
103  self._validator.leave(visitee)
104  # The node visitor leave function does nothing
105  #self._node.leave(visitee)
106  self._decorated.leave(visitee)
107 
109  """Fill a list with the names of Event module types in a sequence. The names are determined
110  by using globals() to lookup the variable names assigned to the modules. This
111  allows the determination of the labels before the modules have been attached to a Process."""
112  def __init__(self,globals_,l):
113  self._moduleToName = { v[1]:v[0] for v in six.iteritems(globals_) if isinstance(v[1],_Module) }
114  self._names =l
115  def enter(self,node):
116  if isinstance(node,_Module):
117  self._names.append(self._moduleToName[node])
118  def leave(self,node):
119  return
120 
121 if __name__=="__main__":
122  import unittest
123  class TestModuleCommand(unittest.TestCase):
124  def setUp(self):
125  """Nothing to do """
126  pass
127  def testValidators(self):
128  producer = EDProducer("Producer")
129  analyzer = EDAnalyzer("Analyzer")
130  output = OutputModule("Out")
131  filter = EDFilter("Filter")
132  unlabeled = EDAnalyzer("UnLabeled")
133  producer.setLabel("producer")
134  analyzer.setLabel("analyzer")
135  output.setLabel("output")
136  filter.setLabel("filter")
137  s1 = Sequence(analyzer*producer)
138  s2 = Sequence(output+filter)
139  p1 = Path(s1)
140  p2 = Path(s1*s2)
141  p3 = Path(s1+unlabeled)
142  ep1 = EndPath(producer+output+analyzer)
143  ep2 = EndPath(filter+output)
144  ep3 = EndPath(s2)
145  ep4 = EndPath(unlabeled)
146  pathValidator = PathValidator()
147  endpathValidator = EndPathValidator()
148  p1.visit(pathValidator)
149  self.assertRaises(ValueError, p2.visit, pathValidator)
150  self.assertRaises(ValueError, p3.visit, pathValidator)
151  ep1.visit(endpathValidator)
152  ep2.visit(endpathValidator)
153  ep3.visit(endpathValidator)
154  self.assertRaises(ValueError, ep4.visit, endpathValidator)
155 
156  unittest.main()
157 
SequenceTypes.TestModuleCommand
Definition: SequenceTypes.py:1653
SequenceVisitors.NodeVisitor
Definition: SequenceVisitors.py:71
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
SequenceVisitors.ModuleNamesFromGlobalsVisitor.leave
def leave(self, node)
Definition: SequenceVisitors.py:118
SequenceVisitors.PathValidator.__label
__label
Definition: SequenceVisitors.py:25
SequenceTypes.Path
Definition: SequenceTypes.py:644
SequenceVisitors.EndPathValidator.__init__
def __init__(self)
Definition: SequenceVisitors.py:44
SequenceVisitors.PathValidator.setLabel
def setLabel(self, label)
Definition: SequenceVisitors.py:26
SequenceVisitors.NodeVisitor.enter
def enter(self, visitee)
Definition: SequenceVisitors.py:79
SequenceVisitors.PathValidator.enter
def enter(self, visitee)
Definition: SequenceVisitors.py:28
SequenceVisitors.EndPathValidator.leave
def leave(self, visitee)
Definition: SequenceVisitors.py:66
SequenceVisitors.NodeVisitor.esProducers
esProducers
Definition: SequenceVisitors.py:76
SequenceVisitors.CompositeVisitor.leave
def leave(self, visitee)
Definition: SequenceVisitors.py:102
SequenceVisitors.EndPathValidator.__label
__label
Definition: SequenceVisitors.py:46
SequenceVisitors.CompositeVisitor.__init__
def __init__(self, validator, node, decorated)
Definition: SequenceVisitors.py:94
SequenceVisitors.CompositeVisitor.enter
def enter(self, visitee)
Definition: SequenceVisitors.py:98
Modules.EDAnalyzer
Definition: Modules.py:180
SequenceVisitors.CompositeVisitor._decorated
_decorated
Definition: SequenceVisitors.py:97
SequenceVisitors.PathValidator.leave
def leave(self, visitee)
Definition: SequenceVisitors.py:38
SequenceVisitors.EndPathValidator._levelInTasks
_levelInTasks
Definition: SequenceVisitors.py:47
SequenceVisitors.EndPathValidator.setLabel
def setLabel(self, label)
Definition: SequenceVisitors.py:48
SequenceVisitors.ModuleNamesFromGlobalsVisitor.__init__
def __init__(self, globals_, l)
Definition: SequenceVisitors.py:112
SequenceTypes.Sequence
Definition: SequenceTypes.py:656
SequenceVisitors.NodeVisitor.leave
def leave(self, visitee)
Definition: SequenceVisitors.py:89
Modules.EDFilter
Definition: Modules.py:172
SequenceVisitors.ModuleNamesFromGlobalsVisitor
Definition: SequenceVisitors.py:108
SequenceTypes.TestModuleCommand.setUp
def setUp(self)
Definition: SequenceTypes.py:1654
SequenceVisitors.NodeVisitor.services
services
Definition: SequenceVisitors.py:78
SequenceVisitors.CompositeVisitor._validator
_validator
Definition: SequenceVisitors.py:95
SequenceVisitors.EndPathValidator
Definition: SequenceVisitors.py:42
PVValHelper::add
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
Definition: PVValidationHelpers.cc:12
SequenceVisitors.NodeVisitor.__init__
def __init__(self)
Definition: SequenceVisitors.py:74
SequenceVisitors.ScheduleTaskValidator
Definition: SequenceVisitors.py:8
mps_setup.append
append
Definition: mps_setup.py:85
SequenceVisitors.NodeVisitor.esSources
esSources
Definition: SequenceVisitors.py:77
SequenceVisitors.PathValidator.__init__
def __init__(self)
Definition: SequenceVisitors.py:24
SequenceVisitors.PathValidator
Definition: SequenceVisitors.py:23
SequenceVisitors.ScheduleTaskValidator.enter
def enter(self, visitee)
Definition: SequenceVisitors.py:11
Modules.EDProducer
Definition: Modules.py:164
SequenceVisitors.EndPathValidator.filtersOnEndpaths
filtersOnEndpaths
Definition: SequenceVisitors.py:45
Modules.OutputModule
Definition: Modules.py:187
SequenceVisitors.ModuleNamesFromGlobalsVisitor._names
_names
Definition: SequenceVisitors.py:114
SequenceVisitors.EndPathValidator.enter
def enter(self, visitee)
Definition: SequenceVisitors.py:50
SequenceVisitors.CompositeVisitor._node
_node
Definition: SequenceVisitors.py:96
SequenceVisitors.ScheduleTaskValidator.__init__
def __init__(self)
Definition: SequenceVisitors.py:9
SequenceVisitors.ModuleNamesFromGlobalsVisitor._moduleToName
_moduleToName
Definition: SequenceVisitors.py:113
SequenceTypes.EndPath
Definition: SequenceTypes.py:650
SequenceVisitors.ModuleNamesFromGlobalsVisitor.enter
def enter(self, node)
Definition: SequenceVisitors.py:115
SequenceVisitors.NodeVisitor.modules
modules
Definition: SequenceVisitors.py:75
SequenceVisitors.ScheduleTaskValidator.leave
def leave(self, visitee)
Definition: SequenceVisitors.py:19
SequenceVisitors.EndPathValidator._presetFilters
_presetFilters
Definition: SequenceVisitors.py:43