Class: Hesabu::Solver
- Inherits:
-
Object
- Object
- Hesabu::Solver
- Defined in:
- lib/hesabu/solver.rb
Instance Method Summary collapse
- #add(name, raw_expression) ⇒ Object
- #handle_error(solution) ⇒ Object
-
#initialize ⇒ Solver
constructor
A new instance of Solver.
- #log_everything(exit_status, result) ⇒ Object
- #solve! ⇒ Object
Constructor Details
#initialize ⇒ Solver
Returns a new instance of Solver.
5 6 7 |
# File 'lib/hesabu/solver.rb', line 5 def initialize @equations = {} end |
Instance Method Details
#add(name, raw_expression) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/hesabu/solver.rb', line 9 def add(name, raw_expression) if raw_expression.nil? || name.nil? raise Hesabu::ArgumentError, "name or expression can't be nil : '#{name}', '#{raw_expression}'" end @equations[name] = EquationCleaner.clean(raw_expression.to_s) end |
#handle_error(solution) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/hesabu/solver.rb', line 32 def handle_error(solution) error = solution["errors"].first = "In equation #{error['source']} " + error["message"] + " #{error['source']} := #{error['expression']}" err = Hesabu::Error.new() err.errors = solution["errors"] raise err end |
#log_everything(exit_status, result) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/hesabu/solver.rb', line 41 def log_everything(exit_status, result) puts ["**************", "exit_status:#{exit_status}", @equations.to_json, "=> ", result].join("\n") end |
#solve! ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hesabu/solver.rb', line 16 def solve! return {} if @equations.empty? result = nil IO.popen(HESABUCLI, mode = "r+") do |io| io.write @equations.to_json io.close_write # let the process know you've given it all the data result = io.read end solution = JSON.parse(result) exit_status = $CHILD_STATUS.exitstatus log_everything(exit_status, result) if ENV["HESABU_DEBUG"] || exit_status != 0 handle_error(solution) if exit_status != 0 solution end |