Class: SpotFeel::Dmn::DecisionTable
- Inherits:
-
Object
- Object
- SpotFeel::Dmn::DecisionTable
- Defined in:
- lib/spot_feel/dmn/decision_table.rb
Instance Attribute Summary collapse
-
#hit_policy ⇒ Object
readonly
Returns the value of attribute hit_policy.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #evaluate(variables = {}) ⇒ Object
-
#initialize(id:, hit_policy:, inputs:, outputs:, rules:) ⇒ DecisionTable
constructor
A new instance of DecisionTable.
Constructor Details
#initialize(id:, hit_policy:, inputs:, outputs:, rules:) ⇒ DecisionTable
Returns a new instance of DecisionTable.
15 16 17 18 19 20 21 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 15 def initialize(id:, hit_policy:, inputs:, outputs:, rules:) @id = id @hit_policy = hit_policy&.downcase&.to_sym || :unique @inputs = inputs @outputs = outputs @rules = rules end |
Instance Attribute Details
#hit_policy ⇒ Object (readonly)
Returns the value of attribute hit_policy.
6 7 8 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 6 def hit_policy @hit_policy end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 6 def id @id end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
6 7 8 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 6 def inputs @inputs end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
6 7 8 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 6 def outputs @outputs end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
6 7 8 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 6 def rules @rules end |
Class Method Details
.from_json(json) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 8 def self.from_json(json) inputs = Array.wrap(json[:input]).map { |input| Input.from_json(input) } outputs = Array.wrap(json[:output]).map { |output| Output.from_json(output) } rules = Array.wrap(json[:rule]).map { |rule| Rule.from_json(rule) } DecisionTable.new(id: json[:id], hit_policy: json[:hit_policy], inputs: inputs, outputs: outputs, rules: rules) end |
Instance Method Details
#as_json ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 42 def as_json { id: id, hit_policy: hit_policy, inputs: inputs.map(&:as_json), outputs: outputs.map(&:as_json), rules: rules.map(&:as_json), } end |
#evaluate(variables = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/spot_feel/dmn/decision_table.rb', line 23 def evaluate(variables = {}) output_values = [] input_values = inputs.map do |input| input.input_expression.evaluate(variables) end rules.each do |rule| results = rule.evaluate(input_values, variables) if results.all? output_value = rule.output_value(outputs, variables) return output_value if hit_policy == :first || hit_policy == :unique output_values << output_value end end output_values.empty? ? nil : output_values end |