Class: ReDuxml::Evaluator

Inherits:
Object
  • Object
show all
Includes:
Math, ReDuxml, Symbolic
Defined in:
lib/re_duxml/evaluate.rb

Constant Summary collapse

LOGIC_FILE =
File.expand_path(File.dirname(__FILE__) + '/../../xml/logic.xml')

Instance Attribute Summary collapse

Attributes included from ReDuxml

#e, #src_doc

Instance Method Summary collapse

Methods included from Symbolic

#%, #-@, #not

Methods included from AST

#new_ast

Methods included from ReDuxml

#resolve

Constructor Details

#initialize(logic = nil) ⇒ Evaluator

Returns a new instance of Evaluator.



17
18
19
20
# File 'lib/re_duxml/evaluate.rb', line 17

def initialize(logic=nil)
  @parser = Parser.new(logic || LOGIC_FILE)
  @param_hash = {}
end

Instance Attribute Details

#param_hashObject (readonly)

Returns the value of attribute param_hash.



15
16
17
# File 'lib/re_duxml/evaluate.rb', line 15

def param_hash
  @param_hash
end

#parserObject (readonly)

Returns the value of attribute parser.



15
16
17
# File 'lib/re_duxml/evaluate.rb', line 15

def parser
  @parser
end

Instance Method Details

#evaluate(_expr, _param_hash = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/re_duxml/evaluate.rb', line 26

def evaluate(_expr, _param_hash={})
  _param_hash.each do |key, val|
    @param_hash[key.to_s] = _param_hash[key]
  end

  expr = resolve_params _expr
  return expr if expr.parameterized?
  return expr if Regexp.identifier.match(expr).to_s == expr
  return expr.to_i if expr.to_i.to_s == expr
  result = reduce parser.parse expr
  case
    when result.respond_to?(:imaginary), result.class == TrueClass, result.class == FalseClass then result
    when result.respond_to?(:name) then result.name
    when result.respond_to?(:to_sexp) then result.print(parser.logic)
    else result.to_s
  end
end

#to_sObject



22
23
24
# File 'lib/re_duxml/evaluate.rb', line 22

def to_s
  "#<Evaluator: param_hash: '#{param_hash.to_s}' parser_logic: '#{parser}'>"
end