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 
106 if __name__=="__main__":
107  import unittest
108  class TestModuleCommand(unittest.TestCase):
109  def setUp(self):
110  """Nothing to do """
111  pass
112  def testValidators(self):
113  producer = EDProducer("Producer")
114  analyzer = EDAnalyzer("Analyzer")
115  output = OutputModule("Out")
116  filter = EDFilter("Filter")
117  unlabeled = EDAnalyzer("UnLabeled")
118  producer.setLabel("producer")
119  analyzer.setLabel("analyzer")
120  output.setLabel("output")
121  filter.setLabel("filter")
122  s1 = Sequence(analyzer*producer)
123  s2 = Sequence(output+filter)
124  p1 = Path(s1)
125  p2 = Path(s1*s2)
126  p3 = Path(s1+unlabeled)
127  ep1 = EndPath(producer+output+analyzer)
128  ep2 = EndPath(filter+output)
129  ep3 = EndPath(s2)
130  ep4 = EndPath(unlabeled)
131  pathValidator = PathValidator()
132  endpathValidator = EndPathValidator()
133  p1.visit(pathValidator)
134  self.assertRaises(ValueError, p2.visit, pathValidator)
135  self.assertRaises(ValueError, p3.visit, pathValidator)
136  ep1.visit(endpathValidator)
137  ep2.visit(endpathValidator)
138  ep3.visit(endpathValidator)
139  self.assertRaises(ValueError, ep4.visit, endpathValidator)
140 
141  unittest.main()
142 
def __init__(self, validator, node, decorated)