Class: DMN::Rule
- Inherits:
-
Object
- Object
- DMN::Rule
- Defined in:
- lib/dmn/rule.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#input_entries ⇒ Object
Returns the value of attribute input_entries.
-
#output_entries ⇒ Object
Returns the value of attribute output_entries.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #evaluate(input_values = [], variables = {}) ⇒ Object
-
#initialize(id:, input_entries:, output_entries:, description: nil) ⇒ Rule
constructor
A new instance of Rule.
- #output_value(outputs, variables) ⇒ Object
Constructor Details
#initialize(id:, input_entries:, output_entries:, description: nil) ⇒ Rule
Returns a new instance of Rule.
13 14 15 16 17 18 |
# File 'lib/dmn/rule.rb', line 13 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
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/dmn/rule.rb', line 5 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/dmn/rule.rb', line 5 def id @id end |
#input_entries ⇒ Object
Returns the value of attribute input_entries.
5 6 7 |
# File 'lib/dmn/rule.rb', line 5 def input_entries @input_entries end |
#output_entries ⇒ Object
Returns the value of attribute output_entries.
5 6 7 |
# File 'lib/dmn/rule.rb', line 5 def output_entries @output_entries end |
Class Method Details
.from_json(json) ⇒ Object
7 8 9 10 11 |
# File 'lib/dmn/rule.rb', line 7 def self.from_json(json) input_entries = Array.wrap(json[:input_entry]).map { |input_entry| FEEL::UnaryTests.from_json(input_entry) } output_entries = Array.wrap(json[:output_entry]).map { |output_entry| FEEL::LiteralExpression.from_json(output_entry) } Rule.new(id: json[:id], input_entries: input_entries, output_entries: output_entries, description: json[:description]) end |
Instance Method Details
#as_json ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/dmn/rule.rb', line 28 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
20 21 22 23 24 25 26 |
# File 'lib/dmn/rule.rb', line 20 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
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dmn/rule.rb', line 37 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 |