-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
242 lines (167 loc) · 6.19 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from event import Event
from rule import Rule
from assignment import Assignment
import pdb
import copy
class Monitor:
def __init__(self, monitorName: str):
self.monitorName = monitorName
self.ruleVector = []
self.assignmentVector = []
self.currentTime = 0
def handleProcessEvent(self, e: Event):
print("Handle process event")
print(e)
print()
newAssignments = []
for rule in self.ruleVector:
print("Check if event creates new body assignment\n")
for bodyProcessAtom in rule.bodyProcessAtoms:
if (e.eventName == bodyProcessAtom.processName):
print("Create new body assignment\n")
variablesDefinedFlags = {x:False for x in rule.bodyVariables}
values = {}
for i,v in enumerate(bodyProcessAtom.variables):
values[v] = e.values[i]
variablesDefinedFlags[v]=True
seenProcessAtoms = [];
seenProcessAtoms.append(bodyProcessAtom)
missingProcessAtoms = []
for ruleBodyProcessAtom in rule.bodyProcessAtoms:
if bodyProcessAtom != ruleBodyProcessAtom:
missingProcessAtoms.append(ruleBodyProcessAtom)
matchingAssignments = []
a = Assignment(rule.bodyVariables,
values,variablesDefinedFlags,"body",
rule, missingProcessAtoms,seenProcessAtoms,[],[e])
print("Newly created body assignment:\n")
print(a)
self.assignmentVector.append(a)
newAssignments.append(a)
print("Check if event creates new head assignment\n")
for headProcessAtom in rule.headProcessAtoms:
if (e.eventName == headProcessAtom.processName):
print("Create new head assignment\n")
variablesDefinedFlags = {x:False for x in rule.headVariables}
values = {}
for i,v in enumerate(headProcessAtom.variables):
values[v] = e.values[i]
variablesDefinedFlags[v]=True
seenProcessAtoms = [];
seenProcessAtoms.append(headProcessAtom)
missingProcessAtoms = []
for ruleHeadProcessAtom in rule.headProcessAtoms:
if headProcessAtom != ruleHeadProcessAtom:
missingProcessAtoms.append(ruleHeadProcessAtom)
matchingAssignments = []
a = Assignment(rule.headVariables,
values,variablesDefinedFlags,"head",
rule, missingProcessAtoms,seenProcessAtoms,[],[e])
print("Newly created head assignment\n")
print(a)
self.assignmentVector.append(a)
newAssignments.append(a)
print("Search for assignments this event will extend\n")
# loop through existing assignment vector
for a in self.assignmentVector:
assignmentDefinedVariables = []
for v in a.variables:
if a.variablesDefinedFlags[v]:
assignmentDefinedVariables.append(v)
for m in a.missingProcessAtoms:
if (e.eventName == m.processName):
eventValuesForVariables = {}
for i,v in enumerate(m.variables):
eventValuesForVariables[v] = e.values[i]
match = True
for v in m.variables:
if a.variablesDefinedFlags[v]:
if (a.values[v] != eventValuesForVariables[v]):
print("Disagreement with "+v+" variable")
match = False;
break;
if not match:
continue;
if a.typeOfAssignment == "body":
constraints = a.rulePointer.bodyGapAtoms
else:
constraints = a.rulePointer.headGapAtoms
mergedMap = a.values.copy()
for v in eventValuesForVariables.keys():
if not v in assignmentDefinedVariables:
assignmentDefinedVariables.append(v)
mergedMap[v] = eventValuesForVariables[v]
print("New mapping for variables:")
print(mergedMap)
print('Check if new assignment is consistent with ' + a.typeOfAssignment +' gap atoms\n')
consistentWithGapAtoms = True
for c in constraints:
if ((c.lhs in mergedMap.keys()) and
(c.rhs in mergedMap.keys())):
print("Check gap atom")
print(c)
if not c.checkTruthValueWithAssignment(mergedMap):
print("disagreement\n")
consistentWithGapAtoms = False
if consistentWithGapAtoms:
print("Create a new assignment\n")
b = copy.deepcopy(a)
for v in mergedMap.keys():
b.values[v] = mergedMap[v]
b.variablesDefinedFlags[v] = True
for x in b.missingProcessAtoms:
if str(x)==str(m):
b.missingProcessAtoms.remove(x)
b.seenProcessAtoms.append(m)
b.seenEvents.append(e)
b.complete = len(b.missingProcessAtoms)==0
b.expirationTime = b.computeExpirationTime()
self.assignmentVector.append(b)
return newAssignments
def findMatches(self, r: Rule):
for b in self.assignmentVector:
if ((b.rulePointer.ruleName == r.ruleName) and (b.complete) and b.typeOfAssignment == 'body'):
for h in self.assignmentVector:
if ((h.rulePointer.ruleName == r.ruleName) and (h.typeOfAssignment == 'head') and (h.complete) and h not in b.matchingAssignments):
if(self.doAssignmentsMatch(b,h)):
print("Found a match\n")
b.matchingAssignments.append(h)
def expired(self, a: Assignment):
return a.expirationTime < self.currentTime
def removeExpiredData(self, e: Event):
return
print("Removing data that expires before time "+str(e.values[-1])+"\n")
latestEventTime = int(e.values[-1])
unexpiredData = []
expiredData = []
for a in self.assignmentVector:
if a.expirationTime < latestEventTime:
unexpiredData.append(a)
else:
expiredData.append(a)
self.assignmentVector = unexpiredData
def printAssignments(self):
for i,a in enumerate(self.assignmentVector):
print("assignmentVector["+str(i)+"]")
print(a)
def doAssignmentsMatch(self, a, b):
for v in a.variables:
if v in b.variables:
print("Check agreement with variable "+v)
if (a.values[v] != b.values[v]):
print("Disagreement between a: "+a.values[v]+" and b: "+b.values[v])
return False
mergedMap = a.values.copy()
for k,v in b.values.items():
mergedMap[k] = v
constraints = []
if (a.typeOfAssignment == 'body'):
constraints += a.rulePointer.bodyGapAtoms
constraints += b.rulePointer.headGapAtoms
else:
constraints += a.rulePointer.headGapAtoms
constraints += b.rulePointer.bodyGapAtoms
for x in constraints:
if (not x.checkTruthValueWithAssignment(mergedMap)):
return False
return True