-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsa.py
311 lines (249 loc) · 10.1 KB
/
lsa.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""Sampling functions"""
# System imports
import json
import os
import random
import re
def check(product, data):
"""Check if product does not exist in the data"""
if type(data) == dict:
data = data.values()
for d in data:
if product == d:
return False
return True
def get_all_features(features, data):
"""Return all features from data"""
for fid, feature in features.items():
if feature["type"] == "Feature":
data.update({fid: {"name": feature["name"],
"attr": feature["attr"]}})
children = feature.get("children", {})
get_all_features(children, data)
def original(model):
"""Create original data"""
features_data = {}
get_all_features(model, features_data)
data = {"features": features_data}
features = list(data["features"].keys())
def one_enabled():
"""One Enabled sampling algorithm"""
data['one_enabled'] = {}
for index, feature in enumerate(features):
data['one_enabled'].update({index+1: [feature]})
def one_disabled():
"""One Disabled sampling algorithm"""
data['one_disabled'] = {}
for index, feature in enumerate(features):
features_temp = features.copy()
features_temp.remove(feature)
data['one_disabled'].update({index+1: features_temp})
def most_enabled_disabled():
"""Most Enabled Disabled sampling algorithm"""
data['most_enabled_disabled'] = {}
data['most_enabled_disabled'].update({"1": features})
data['most_enabled_disabled'].update({"2": []})
one_enabled()
one_disabled()
most_enabled_disabled()
return data
def rand_original(model):
"""Create random original data"""
original_data = original(model)
temp_products = []
for strategy in ['one_enabled', 'one_disabled', 'most_enabled_disabled']:
for product in original_data[strategy].values():
temp_products.append(product)
data = {
'features': original_data['features'],
'rand': {}
}
for index, product in enumerate(random.choices(temp_products, k=3)):
data['rand'].update({index+1: product})
return data
def any(model):
"""Create any data"""
data = {"features": {}}
def get_features(features):
for fid, feature in features.items():
children = feature.get("children", {})
if feature["type"] == "Feature":
data["features"].update({fid: {"name": feature["name"],
"attr": feature["attr"]}})
elif feature["type"] == "Group" and feature["attr"] == "AND":
target = random.choice(list(children.keys()))
children = {target: children[target]}
get_features(children)
get_features(model)
features = list(data["features"].keys())
def one_enabled():
"""One Enabled sampling algorithm"""
def get_mandatory(fid):
mandatory = []
for feature in features:
attr = data["features"][feature]["attr"]
prefix = feature[:-2]
if (re.match(feature, fid) or
(attr == "mandatory" and prefix in mandatory)):
mandatory.append(feature)
return mandatory
count = len(data['one_enabled'])
for feature in features:
mandatory = get_mandatory(feature)
if check(mandatory, data['one_enabled']):
data['one_enabled'].update({count+1: mandatory})
count += 1
def one_disabled():
"""One Disabled sampling algorithm"""
def get_children(fid):
children = []
for feature in features:
attr = data["features"][feature]["attr"]
prefix = feature[:-2]
if re.match(fid, feature) or (prefix in children and
attr == "mandatory"):
children.append(feature)
return children
count = len(data['one_disabled'])
for feature in features:
attr = data["features"][feature]["attr"]
if attr == "mandatory":
continue
features_copy = features.copy()
children = get_children(feature)
features_temp = [f for f in features_copy if f not in children]
if check(features_temp, data['one_disabled']):
data['one_disabled'].update({count+1: features_temp})
count += 1
def most_enabled_disabled():
"""Most Enabled Disabled sampling algorithm"""
if check([], data['most_enabled_disabled']):
data['most_enabled_disabled'].update({len(data['most_enabled_disabled'])+1: []})
if check(features, data['most_enabled_disabled']):
data['most_enabled_disabled'].update({len(data['most_enabled_disabled'])+1: features})
for strategy in ['one_enabled', 'one_disabled', 'most_enabled_disabled']:
data[strategy] = {}
one_enabled()
one_disabled()
most_enabled_disabled()
return data
def rand_any(model):
"""Create random any data"""
any_data = any(model)
temp_products = []
for strategy in ['one_enabled', 'one_disabled', 'most_enabled_disabled']:
for product in any_data[strategy].values():
temp_products.append(product)
data = {
'features': any_data['features'],
'rand': {}
}
for index, product in enumerate(random.choices(temp_products, k=3)):
data['rand'].update({index+1: product})
return data
def all(model):
"""Create all data"""
blend = []
data = {"features": {}}
get_all_features(model, data["features"])
def get_features(features, features_data):
for fid, feature in features.items():
if feature["type"] == "Group" and feature["attr"] == "AND":
continue
if feature["type"] == "Feature":
features_data.update({fid: {"name": feature["name"],
"attr": feature["attr"]}})
children = feature.get("children", {})
get_features(children, features_data)
def get_blend(features, base, flag=False):
child_data = {}
for fid, feature in features.items():
children = feature.get("children", {})
if feature["type"] == "Group" and feature["attr"] == "AND":
for cid, child in children.items():
temp = base.copy()
temp.update({cid: {"name": child["name"],
"attr": child["attr"]}})
temp.update(get_blend({cid: child}, temp, True))
blend.append(temp)
elif flag and feature["type"] == "Feature":
child_data.update({fid: {"name": feature["name"],
"attr": feature["attr"]}})
child_data.update(get_blend(children, base, flag))
return child_data
features_data = {}
get_features(model, features_data)
get_blend(model, features_data)
def one_enabled(features):
"""One Enabled sampling algorithm"""
def get_mandatory(fid):
mandatory = []
for feature in features:
attr = data["features"][feature]["attr"]
prefix = feature[:-2]
if (re.match(feature, fid) or
(attr == "mandatory" and prefix in mandatory)):
mandatory.append(feature)
return mandatory
count = len(data['one_enabled'])
for feature in features:
mandatory = get_mandatory(feature)
if check(mandatory, data['one_enabled']):
data['one_enabled'].update({count+1: mandatory})
count += 1
def one_disabled(features):
"""One Disabled sampling algorithm"""
def get_children(fid):
children = []
for feature in features:
attr = data["features"][feature]["attr"]
prefix = feature[:-2]
if re.match(fid, feature) or (prefix in children and
attr == "mandatory"):
children.append(feature)
return children
count = len(data['one_disabled'])
for feature in features:
attr = data["features"][feature]["attr"]
if attr == "mandatory":
continue
features_copy = features.copy()
children = get_children(feature)
features_temp = [f for f in features_copy if f not in children]
if check(features_temp, data['one_disabled']):
data['one_disabled'].update({count+1: features_temp})
count += 1
def most_enabled_disabled(features):
"""Most Enabled Disabled sampling algorithm"""
if check([], data['most_enabled_disabled']):
data['most_enabled_disabled'].update({len(data['most_enabled_disabled'])+1: []})
if check(features, data['most_enabled_disabled']):
data['most_enabled_disabled'].update({len(data['most_enabled_disabled'])+1: features})
for strategy in ['one_enabled', 'one_disabled', 'most_enabled_disabled']:
data[strategy] = {}
if blend:
for b in blend:
features = list(b.keys())
one_enabled(features)
one_disabled(features)
most_enabled_disabled(features)
else:
features = list(features_data.keys())
one_enabled(features)
one_disabled(features)
most_enabled_disabled(features)
return data
def rand_all(model):
"""Create random all data"""
all_data = all(model)
temp_products = []
for strategy in ['one_enabled', 'one_disabled', 'most_enabled_disabled']:
for product in all_data[strategy].values():
temp_products.append(product)
data = {
'features': all_data['features'],
'rand': {}
}
for index, product in enumerate(random.choices(temp_products, k=3)):
data['rand'].update({index+1: product})
return data