Class: SpotFeel::Dmn::Rule

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, input_entries:, output_entries:, description: nil) ⇒ Rule

Returns a new instance of Rule.



14
15
16
17
18
19
# File 'lib/spot_feel/dmn/rule.rb', line 14

def initialize(id:, input_entries:, output_entries:, description: nil)
  @id = id
  @input_entries = input_entries
  @output_entries = output_entries
  @description = description
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#input_entriesObject

Returns the value of attribute input_entries.



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

def input_entries
  @input_entries
end

#output_entriesObject

Returns the value of attribute output_entries.



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

def output_entries
  @output_entries
end

Class Method Details

.from_json(json) ⇒ Object



8
9
10
11
12
# File 'lib/spot_feel/dmn/rule.rb', line 8

def self.from_json(json)
  input_entries = Array.wrap(json[:input_entry]).map { |input_entry| UnaryTests.from_json(input_entry) }
  output_entries = Array.wrap(json[:output_entry]).map { |output_entry| LiteralExpression.from_json(output_entry) }
  Rule.new(id: json[:id], input_entries:, output_entries:, description: json[:description])
end

Instance Method Details

#as_jsonObject



29
30
31
32
33
34
35
36
# File 'lib/spot_feel/dmn/rule.rb', line 29

def as_json
  {
    id: id,
    input_entries: input_entries.map(&:as_json),
    output_entries: output_entries.map(&:as_json),
    description: description,
  }
end

#evaluate(input_values = [], variables = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/spot_feel/dmn/rule.rb', line 21

def evaluate(input_values = [], variables = {})
  [].tap do |test_results|
    input_entries.each_with_index do |input_entry, index|
      test_results.push input_entry.test(input_values[index], variables)
    end
  end
end

#output_value(outputs, variables) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/spot_feel/dmn/rule.rb', line 38

def output_value(outputs, variables)
  HashWithIndifferentAccess.new.tap do |ov|
    output_entries.each_with_index do |output_entry, index|
      if output_entry.valid?
        val = output_entry.evaluate(variables)
        nested_hash_value(ov, outputs[index].name, val)
      end
    end
  end
end