1
2 """Analyze OpenFOAM logs"""
3
4 from TimeLineAnalyzer import TimeLineAnalyzer
5 from PyFoam.Basics.LineReader import LineReader
6 from PyFoam.Error import error
7
9 """Base class for all analyzers
10
11 Administrates and calls a number of LogLineAnlayzers for each
12 line"""
13
28
30 """Remove reference to self in children (hoping to remove
31 circular dependencies)"""
32
33 for a in self.analyzers.values():
34 a.tearDown()
35 a.setParent(None)
36
38 """Sets the time and alert all the LineAnalyzers that the time has changed
39 @param time: the new value of the time
40 """
41 if time!=self.time:
42 self.time=time
43 for listener in self.timeListeners:
44 listener.timeChanged()
45 for nm in self.analyzers:
46 self.analyzers[nm].timeChanged()
47 self.checkTriggers()
48
50 """@param listener: An object that is notified when the time changes. Has to
51 implement a timeChanged method"""
52 if not 'timeChanged' in dir(listener):
53 error("Error. Object has no timeChanged-method:"+str(listener))
54 else:
55 self.timeListeners.append(listener)
56
58 """@returns: A list with the names of the Analyzers"""
59 return self.analyzers.keys()
60
62 """Get the LogLineAnalyzer name"""
63 if self.analyzers.has_key(name):
64 return self.analyzers[name]
65 else:
66 return None
67
69 """Adds an analyzer
70
71 obj - A LogLineAnalyzer
72 name - the name of the analyzer"""
73
74 obj.setParent(self)
75 self.analyzers[name]=obj
76
78 """Calls all the anlyzers for a line"""
79 for nm in self.analyzers:
80 self.analyzers[nm].doAnalysis(line)
81
83 """Analyzes a file (one line at a time)
84
85 fh - handle of the file"""
86 while(self.line.read(fh)):
87 self.analyzeLine(self.line.line)
88
90 """Checks with all the analyzers
91
92 If one analyzer returns False it returns False"""
93 result=True
94
95 for nm in self.analyzers:
96
97 result=result and self.analyzers[nm].goOn()
98
99 return result
100
102 """Gets the current time"""
103 return str(self.time)
104
106 """Sets the output directory for all the analyzers"""
107 self.oDir=d
108 for nm in self.analyzers:
109 self.analyzers[nm].setDirectory(self.oDir)
110
112 """Gets the output directory"""
113 return self.oDir
114
115 - def addTrigger(self,time,func,once=True,until=None):
116 """Adds a trigger function that is to be called as soon as
117 the simulation time exceeds a certain value
118 @param time: the time at which the function should be triggered
119 @param func: the trigger function
120 @param once: Should this function be called once or at every time-step
121 @param until: The time until which the trigger should be called"""
122
123 data={}
124 data["time"]=float(time)
125 data["func"]=func
126 if until!=None:
127 data["until"]=float(until)
128 once=False
129 data["once"]=once
130
131 self.timeTriggers.append(data)
132
134 """Check for and execute the triggered functions"""
135
136 remove=[]
137 for i in range(len(self.timeTriggers)):
138 t=self.timeTriggers[i]
139 if t["time"]<=self.time:
140 t["func"]()
141 if t["once"]:
142 remove.append(i)
143 elif "until" in t:
144 if t["until"]<=self.time:
145 remove.append(i)
146
147 remove.reverse()
148
149 for i in remove:
150 self.timeTriggers.pop(i)
151