CMS 3D CMS Logo

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