Skip to content

Commit

Permalink
Fixing bugs, adding exception management
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoSix committed May 12, 2020
1 parent 7af6c03 commit 19fa9dc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
21 changes: 10 additions & 11 deletions blade/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ def create_app(configfile=None):

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

@app.route("/results", methods=["POST"])
def post_recommendation():
result = request.form
res = get_request_from_dict(result)
try:
s = solve_from_dict(res)
except:
s="failed to compute valid solution"
r=yaml.dump(res)
return render_template('pages/results.html', solution=s,request=r)

@app.route('/')
def index():
return render_template('pages/index.html')
Expand All @@ -28,10 +17,20 @@ def index():
def display_publications():
return render_template('pages/publications.html')

# RECOMMENDATIONS
@app.route('/recommendation/get')
def get_recommandation():
return render_template('pages/get_recommendation.html')

@app.route("/recommendation/results", methods=["POST"])
def post_recommendation():
result = request.form
res = get_request_from_dict(result)
s = solve_from_dict(res)
r=yaml.dump(res)
return render_template('pages/results.html', solution=s["msg"],request=r)

# KNOWLEDGE BASE
@app.route('/knowledge_base/')
def display_knowledge_base():
alternatives = get_alternatives()
Expand Down
45 changes: 33 additions & 12 deletions blade/app/classes/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def return_topsis_res(self, scores=[]):
"""Display results in CLI"""
res = ""
if self.results["optimum_id"] is None:
res += "Alternatives cannot be ranked if weights are null.\n"
res += "Alternatives cannot be ranked if weights are null or considered alternatives identical on chosen requirements.\n"
res += "However, blockchains have been filtered according to your requirements.\n"
res += "Suitable alternatives: \n"
for a in self.results["considered"]:
Expand All @@ -141,20 +141,41 @@ def return_topsis_res(self, scores=[]):
best_altr["name"] + " (" + best_altr["infoAttributes"]["consensusAlgorithm"] + ")\n")
res += "Scores: %s \n" % scores

return res
return {
"success": True,
"msg": res
}

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

self.filter_unsuitable_alternatives()
self.gen_alternatives_values_array()

if(sum(abs(x) for x in self.weights) > 0):
decision = topsis(self.alternatives_values, self.weights, self.costs)
decision.calc()

self.results['optimum_id'] = decision.optimum_choice

return self.return_topsis_res(decision.C)

return self.return_topsis_res()
if len(self.results["considered"]) == 1:
return {
"success": True,
"msg": "Only one alternative is compatible with your input: " + self.results["considered"][0]
}
elif len(self.results["considered"]) > 1:
try:
self.gen_alternatives_values_array()

if(sum(abs(x) for x in self.weights) > 0):
decision = topsis(self.alternatives_values, self.weights, self.costs)
decision.calc()

self.results['optimum_id'] = decision.optimum_choice

return self.return_topsis_res(decision.C)

return self.return_topsis_res()
except:
return {
"error": True,
"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!"
}
else:
return {
"success": True,
"msg": "No alternative compatible with this input. Please change your requirements and retry."
}
1 change: 1 addition & 0 deletions blade/app/classes/topsis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class topsis:
optimum_choice = None

def __init__(self, a, w, I):
print(a, w, I)
""" Initialise topsis object with alternatives (a), weighting (w),
and benefit/cost indicator (i). Validate the user input for correct
dimensions etc.
Expand Down
9 changes: 2 additions & 7 deletions blade/app/templates/pages/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
{{ super() }}
<div class="container">
<label for="story">Your personalized recommendation</label><br>
<textarea id="story" name="recommendation"
rows="20" cols="120">
{{ solution }}
</textarea>
<textarea id="story" name="recommendation" rows="20" cols="120">{{ solution }}</textarea>
<br>
<label for="story">Your Request as yaml</label><br>
<textarea id="story" name="recommendation"
rows="20" cols="120">
{{ request }}
<textarea id="story" name="recommendation"rows="20" cols="120">{{ request }}
</textarea>
</div>
{% endblock %}

0 comments on commit 19fa9dc

Please sign in to comment.