-
Notifications
You must be signed in to change notification settings - Fork 1
/
matchmaker.py
180 lines (163 loc) · 6.25 KB
/
matchmaker.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
import copy
import sys
IDENTITY = 0x23420509
class MultiFactor:
def __init__(self, f=None):
self.factors = f
def __str__(self):
tn = type(self).__name__
tn += "[" + str(self.factors) + "]"
return tn
def __repr__(self):
return str(self)
class Resource(MultiFactor):
pass
class Artefact(MultiFactor):
pass
class Matchmaker:
# must be called explicitly (e.g. by generator with many synthetic combinations)
def setrec(self, limit):
sys.setrecursionlimit(limit + 3)
# dep_rules: deployment rules {a.factor: (f.factor, op, value)} (special value: IDENTITY):
# acc_rules: None (for combinatorial search), or {a.factor: op} for recursive search
def match(self, app, res, dep_rules, acc_rules, level=0, skip=None):
print("/// enter mapping", app, "X", res, "@", level)
if skip:
a_copy = {}
r_copy = {}
for f in skip:
for a in app:
if f in a.factors:
a_copy.setdefault(a, {})[f] = a.factors[f]
del a.factors[f]
print("! filter", a, f)
for r in res:
if f in r.factors:
r_copy.setdefault(r, {})[f] = r.factors[f]
del r.factors[f]
print("! filter", r, f)
mapping = {}
if acc_rules:
rescopy = copy.deepcopy(res)
res = rescopy
breakres = False
for a in app:
if breakres:
break
for r in res:
print("match", a, "X", r)
if acc_rules:
rforig = copy.deepcopy(r.factors)
valid = True
if a.factors:
for f in a.factors:
print(" -", f)
if f in dep_rules:
reval = False
rf, op, val = dep_rules[f]
if not r.factors or not rf in r.factors:
print(" !! factor absent from resource")
else:
rfval = r.factors[rf]
if val == IDENTITY:
val = a.factors[f]
reval = self.matchop(rfval, op, val)
print(" ->", self.printablerules(dep_rules[f]), reval)
if not reval:
valid = False
if acc_rules and valid:
if f in acc_rules:
if f in r.factors and f in a.factors:
op = acc_rules[f]
if op == "-":
print("# reduce", f, "in", r, "by", a.factors[f])
r.factors[f] -= a.factors[f]
else:
print("!! factor absent in resource or app")
print("= valid", valid)
if valid:
mapping[a] = r
if not acc_rules:
break
else:
print("//> partial mapping", a, "->", r)
# recursion starts here
remres = []
for rr in res:
if r != rr:
remres.append(rr)
remapp = []
for ra in app:
if a != ra:
remapp.append(ra)
if remapp:
# shortcut, not strictly necessary
rmapping = self.match(remapp, remres, dep_rules, acc_rules, level + 1)
if not rmapping:
valid = False
else:
for a in rmapping:
mapping[a] = rmapping[a]
if valid:
breakres = True
break
if not valid:
if acc_rules:
r.factors = rforig
if not a in mapping:
print("!! mapping failed")
return
if skip:
for a in a_copy:
a.factors.update(a_copy[a])
for r in res:
r.factors.update(r_copy[r])
print("/// leave mapping:", mapping, "@", level)
return mapping
def printablerules(self, r):
rx = r[2]
if rx == IDENTITY:
rx = "*"
return f"({r[0]} {r[1]} {rx})"
def matchop(self, rfval, op, val):
r = False
if op == "=":
if rfval == val:
r = True
elif op == ">=":
if rfval >= val:
r = True
elif op == "<=":
if rfval <= val:
r = True
elif op == "<>" or op == "!=":
if rfval != val:
r = True
return r
if __name__ == "__main__":
dep_rules = {
"vulnerability": ("location", "=", "dmz"),
"memory": ("memory", ">=", IDENTITY)
}
acc_rules = {
"memory": "-"
}
skip_rules = {
"memory": True
}
m = Matchmaker()
print("---")
print("Combinatorial mapping...")
app = [Artefact({"vulnerability": 0.8, "memory": 200}), Artefact()]
res = [Resource({"location": "intranet"}), Resource({"memory": 800, "location": "dmz"})]
mapping = m.match(app, res, dep_rules, None)
print("---")
print("Recursive mapping... needs tree search!")
app = [Artefact({"memory": 400}), Artefact({"memory": 800})]
res = [Resource({"memory": 900}), Resource({"memory": 500})]
mapping = m.match(app, res, dep_rules, acc_rules)
print("---")
print("Mapping with skipping rules - for a test")
app = [Artefact({"memory": 400}), Artefact({"memory": 800})]
res = [Resource({"memory": 900}), Resource({"memory": 500})]
mapping = m.match(app, res, dep_rules, None, skip=skip_rules)