Class: Dentaku::Calculator
- Inherits:
-
Object
- Object
- Dentaku::Calculator
- Defined in:
- lib/dentaku/calculator.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#rule_set ⇒ Object
readonly
Returns the value of attribute rule_set.
Instance Method Summary collapse
- #add_function(fn) ⇒ Object
- #add_functions(fns) ⇒ Object
- #clear ⇒ Object
- #dependencies(expression) ⇒ Object
- #empty? ⇒ Boolean
- #evaluate(expression, data = {}) ⇒ Object
- #evaluate!(expression, data = {}) ⇒ Object
-
#initialize ⇒ Calculator
constructor
A new instance of Calculator.
- #solve!(expression_hash) ⇒ Object
- #store(key_or_hash, value = nil) ⇒ Object (also: #bind)
Constructor Details
#initialize ⇒ Calculator
Returns a new instance of Calculator.
12 13 14 15 |
# File 'lib/dentaku/calculator.rb', line 12 def initialize clear @rule_set = RuleSet.new end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
10 11 12 |
# File 'lib/dentaku/calculator.rb', line 10 def result @result end |
#rule_set ⇒ Object (readonly)
Returns the value of attribute rule_set.
10 11 12 |
# File 'lib/dentaku/calculator.rb', line 10 def rule_set @rule_set end |
Instance Method Details
#add_function(fn) ⇒ Object
17 18 19 20 |
# File 'lib/dentaku/calculator.rb', line 17 def add_function(fn) rule_set.add_function(fn) self end |
#add_functions(fns) ⇒ Object
22 23 24 25 |
# File 'lib/dentaku/calculator.rb', line 22 def add_functions(fns) fns.each { |fn| add_function(fn) } self end |
#clear ⇒ Object
88 89 90 |
# File 'lib/dentaku/calculator.rb', line 88 def clear @memory = {} end |
#dependencies(expression) ⇒ Object
63 64 65 |
# File 'lib/dentaku/calculator.rb', line 63 def dependencies(expression) Expression.new(expression, @memory).identifiers end |
#empty? ⇒ Boolean
92 93 94 |
# File 'lib/dentaku/calculator.rb', line 92 def empty? @memory.empty? end |
#evaluate(expression, data = {}) ⇒ Object
27 28 29 30 31 |
# File 'lib/dentaku/calculator.rb', line 27 def evaluate(expression, data={}) evaluate!(expression, data) rescue UnboundVariableError yield expression if block_given? end |
#evaluate!(expression, data = {}) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/dentaku/calculator.rb', line 33 def evaluate!(expression, data={}) store(data) do expr = Expression.new(expression, @memory) raise UnboundVariableError.new(expr.identifiers) if expr.unbound? @evaluator ||= Evaluator.new(rule_set) @result = @evaluator.evaluate(expr.tokens) end end |
#solve!(expression_hash) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dentaku/calculator.rb', line 42 def solve!(expression_hash) expressions = Hash[expression_hash.map { |k,v| [k.to_s, v] }] # expression_hash: { variable_name: "string expression" } # TSort thru the expressions' dependencies, then evaluate all expression_dependencies = Hash[expressions.map do |var, expr| [var, dependencies(expr)] end] variables_in_resolve_order = DependencyResolver::find_resolve_order( expression_dependencies) results = variables_in_resolve_order.each_with_object({}) do |var_name, r| r[var_name] = evaluate!(expressions[var_name], r) end expression_hash.each_with_object({}) do |(k, _), r| r[k] = results[k.to_s] end end |
#store(key_or_hash, value = nil) ⇒ Object Also known as: bind
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dentaku/calculator.rb', line 67 def store(key_or_hash, value=nil) restore = @memory.dup if value.nil? key_or_hash.each do |key, val| @memory[key.downcase.to_s] = val end else @memory[key_or_hash.to_s] = value end if block_given? result = yield @memory = restore return result end self end |