Skip to content

Commit

Permalink
Merge pull request #125 from inz/feature/unsatisfiable-error-parser
Browse files Browse the repository at this point in the history
Parse error message for unsatisfiable recommendations (#122)
  • Loading branch information
gmazlami authored Sep 1, 2016
2 parents b37f478 + 08a56d2 commit abcde49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
27 changes: 24 additions & 3 deletions app/models/deployment_recommendation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def generate
ingredients.write self.ingredients_data
ingredients.close

command = "minizinc -G or-tools -f fzn-or-tools #{Rails.root}/lib/stove.mzn #{resources.path} #{ingredients.path}"
command = "minizinc -G or-tools -f fzn-or-tools #{minizinc_model} #{resources.path} #{ingredients.path}"
stdout, stderr, status = Open3.capture3(command)
if status.success?
parse_result(stdout)
parse_result(stdout, stderr)
else
fail [ 'Error executing MiniZinc!',
'----------stdout----------',
Expand All @@ -65,7 +65,11 @@ def generate
ingredients.unlink
end

def parse_result(stdout)
def minizinc_model
"#{Rails.root}/lib/stove.mzn"
end

def parse_result(stdout, stderr)
result = extract_result(stdout)
if satisfiable?(result)
self.more_attributes = result
Expand All @@ -85,10 +89,27 @@ def parse_result(stdout)
self.save!
else
self.status = UNSATISFIABLE
self.more_attributes = unsatisfiable_msg(stderr)
self.save!
end
end

def unsatisfiable_msg(stderr)
line = line_with_error(stderr)
{ unsatisfiable_message: line_from_minizinc_model(line) }
rescue NoMethodError
{ unsatisfiable_message: 'Could not localize MiniZinc error!' }
end

def line_from_minizinc_model(line)
File.readlines(minizinc_model)[line - 1].strip
end

MINIZINC_ERROR_REGEX = /lib\/stove\.mzn:(\d+):/
def line_with_error(stderr)
MINIZINC_ERROR_REGEX.match(stderr)[1].to_i
end

def extract_result(output)
output.gsub!(', ]', ']')
results = output.split(SOLN_SEP)
Expand Down
1 change: 1 addition & 0 deletions test/models/deployment_recommendation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ def load_seed(name)
recommendation = DeploymentRecommendation.construct(rails_app)
# Google provider factories have no instance available to satisfy the 4G RAM constraint
assert_equal 'unsatisfiable', recommendation.status
assert_equal 'constraint forall(i in Ingredients)(ram[assignments[i]] >= min_ram[i]);', recommendation.more_attributes['unsatisfiable_message']
end
end

0 comments on commit abcde49

Please sign in to comment.