Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 19fa9dc

Browse files
committed
Fixing bugs, adding exception management
1 parent 7af6c03 commit 19fa9dc

4 files changed

Lines changed: 46 additions & 30 deletions

File tree

blade/app/__init__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@ def create_app(configfile=None):
99

1010
app.config['SECRET_KEY'] = 'devkey'
1111

12-
@app.route("/results", methods=["POST"])
13-
def post_recommendation():
14-
result = request.form
15-
res = get_request_from_dict(result)
16-
try:
17-
s = solve_from_dict(res)
18-
except:
19-
s="failed to compute valid solution"
20-
r=yaml.dump(res)
21-
return render_template('pages/results.html', solution=s,request=r)
22-
2312
@app.route('/')
2413
def index():
2514
return render_template('pages/index.html')
@@ -28,10 +17,20 @@ def index():
2817
def display_publications():
2918
return render_template('pages/publications.html')
3019

20+
# RECOMMENDATIONS
3121
@app.route('/recommendation/get')
3222
def get_recommandation():
3323
return render_template('pages/get_recommendation.html')
3424

25+
@app.route("/recommendation/results", methods=["POST"])
26+
def post_recommendation():
27+
result = request.form
28+
res = get_request_from_dict(result)
29+
s = solve_from_dict(res)
30+
r=yaml.dump(res)
31+
return render_template('pages/results.html', solution=s["msg"],request=r)
32+
33+
# KNOWLEDGE BASE
3534
@app.route('/knowledge_base/')
3635
def display_knowledge_base():
3736
alternatives = get_alternatives()

blade/app/classes/solver.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def return_topsis_res(self, scores=[]):
122122
"""Display results in CLI"""
123123
res = ""
124124
if self.results["optimum_id"] is None:
125-
res += "Alternatives cannot be ranked if weights are null.\n"
125+
res += "Alternatives cannot be ranked if weights are null or considered alternatives identical on chosen requirements.\n"
126126
res += "However, blockchains have been filtered according to your requirements.\n"
127127
res += "Suitable alternatives: \n"
128128
for a in self.results["considered"]:
@@ -141,20 +141,41 @@ def return_topsis_res(self, scores=[]):
141141
best_altr["name"] + " (" + best_altr["infoAttributes"]["consensusAlgorithm"] + ")\n")
142142
res += "Scores: %s \n" % scores
143143

144-
return res
144+
return {
145+
"success": True,
146+
"msg": res
147+
}
145148

146149
def solve(self):
147150
"""Executes the 2-step solving process : filter unsuitable alternatives, then run TOPSIS to find the best alternative"""
148151

149152
self.filter_unsuitable_alternatives()
150-
self.gen_alternatives_values_array()
151-
152-
if(sum(abs(x) for x in self.weights) > 0):
153-
decision = topsis(self.alternatives_values, self.weights, self.costs)
154-
decision.calc()
155153

156-
self.results['optimum_id'] = decision.optimum_choice
157-
158-
return self.return_topsis_res(decision.C)
159-
160-
return self.return_topsis_res()
154+
if len(self.results["considered"]) == 1:
155+
return {
156+
"success": True,
157+
"msg": "Only one alternative is compatible with your input: " + self.results["considered"][0]
158+
}
159+
elif len(self.results["considered"]) > 1:
160+
try:
161+
self.gen_alternatives_values_array()
162+
163+
if(sum(abs(x) for x in self.weights) > 0):
164+
decision = topsis(self.alternatives_values, self.weights, self.costs)
165+
decision.calc()
166+
167+
self.results['optimum_id'] = decision.optimum_choice
168+
169+
return self.return_topsis_res(decision.C)
170+
171+
return self.return_topsis_res()
172+
except:
173+
return {
174+
"error": True,
175+
"msg": "An unexpected error happened. Please copy your generated YAML file and send it to one of the researcher in charge of the project. Thanks!"
176+
}
177+
else:
178+
return {
179+
"success": True,
180+
"msg": "No alternative compatible with this input. Please change your requirements and retry."
181+
}

blade/app/classes/topsis.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class topsis:
1616
optimum_choice = None
1717

1818
def __init__(self, a, w, I):
19+
print(a, w, I)
1920
""" Initialise topsis object with alternatives (a), weighting (w),
2021
and benefit/cost indicator (i). Validate the user input for correct
2122
dimensions etc.

blade/app/templates/pages/results.html

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
{{ super() }}
55
<div class="container">
66
<label for="story">Your personalized recommendation</label><br>
7-
<textarea id="story" name="recommendation"
8-
rows="20" cols="120">
9-
{{ solution }}
10-
</textarea>
7+
<textarea id="story" name="recommendation" rows="20" cols="120">{{ solution }}</textarea>
118
<br>
129
<label for="story">Your Request as yaml</label><br>
13-
<textarea id="story" name="recommendation"
14-
rows="20" cols="120">
15-
{{ request }}
10+
<textarea id="story" name="recommendation"rows="20" cols="120">{{ request }}
1611
</textarea>
1712
</div>
1813
{% endblock %}

0 commit comments

Comments
 (0)