Class: SpotFeel::Dmn::DecisionTable

Inherits:
Object
  • Object
show all
Defined in:
lib/spot_feel/dmn/decision_table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_policyObject (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

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/spot_feel/dmn/decision_table.rb', line 6

def id
  @id
end

#inputsObject (readonly)

Returns the value of attribute inputs.



6
7
8
# File 'lib/spot_feel/dmn/decision_table.rb', line 6

def inputs
  @inputs
end

#outputsObject (readonly)

Returns the value of attribute outputs.



6
7
8
# File 'lib/spot_feel/dmn/decision_table.rb', line 6

def outputs
  @outputs
end

#rulesObject (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_jsonObject



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