Class: SpotFeel::Dmn::Definitions

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, namespace:, exporter:, exporter_version:, execution_platform:, execution_platform_version:, decisions:) ⇒ Definitions

Returns a new instance of Definitions.



24
25
26
27
28
29
30
31
32
33
# File 'lib/spot_feel/dmn/definitions.rb', line 24

def initialize(id:, name:, namespace:, exporter:, exporter_version:, execution_platform:, execution_platform_version:, decisions:)
  @id = id
  @name = name
  @namespace = namespace
  @exporter = exporter
  @exporter_version = exporter_version
  @execution_platform = execution_platform
  @execution_platform_version = execution_platform_version
  @decisions = decisions
end

Instance Attribute Details

#decisionsObject (readonly)

Returns the value of attribute decisions.



7
8
9
# File 'lib/spot_feel/dmn/definitions.rb', line 7

def decisions
  @decisions
end

#execution_platformObject (readonly)

Returns the value of attribute execution_platform.



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

def execution_platform
  @execution_platform
end

#execution_platform_versionObject (readonly)

Returns the value of attribute execution_platform_version.



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

def execution_platform_version
  @execution_platform_version
end

#exporterObject (readonly)

Returns the value of attribute exporter.



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

def exporter
  @exporter
end

#exporter_versionObject (readonly)

Returns the value of attribute exporter_version.



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

def exporter_version
  @exporter_version
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

Class Method Details

.from_json(json) ⇒ Object



19
20
21
22
# File 'lib/spot_feel/dmn/definitions.rb', line 19

def self.from_json(json)
  decisions = Array.wrap(json[:decision]).map { |decision| Decision.from_json(decision) }
  Definitions.new(id: json[:id], name: json[:name], namespace: json[:namespace], exporter: json[:exporter], exporter_version: json[:exporter_version], execution_platform: json[:execution_platform], execution_platform_version: json[:execution_platform_version], decisions: decisions)
end

.from_xml(xml) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/spot_feel/dmn/definitions.rb', line 9

def self.from_xml(xml)
  XmlHasher.configure do |config|
    config.snakecase = true
    config.ignore_namespaces = true
    config.string_keys = false
  end
  json = XmlHasher.parse(xml)
  Definitions.from_json(json[:definitions])
end

Instance Method Details

#as_jsonObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/spot_feel/dmn/definitions.rb', line 54

def as_json
  {
    id: id,
    name: name,
    namespace: namespace,
    exporter: exporter,
    exporter_version: exporter_version,
    execution_platform: execution_platform,
    execution_platform_version: execution_platform_version,
    decisions: decisions.map(&:as_json),
  }
end

#evaluate(decision_id, variables: {}, already_evaluated_decisions: {}) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spot_feel/dmn/definitions.rb', line 35

def evaluate(decision_id, variables: {}, already_evaluated_decisions: {})
  decision = decisions.find { |d| d.id == decision_id }
  raise EvaluationError, "Decision #{decision_id} not found" unless decision

  # Evaluate required decisions recursively
  decision.required_decision_ids&.each do |required_decision_id|
    next if already_evaluated_decisions[required_decision_id]
    next if decisions.find { |d| d.id == required_decision_id }.nil?

    result = evaluate(required_decision_id, variables:, already_evaluated_decisions:)

    variables.merge!(result) if result.is_a?(Hash)

    already_evaluated_decisions[required_decision_id] = true
  end

  decision.evaluate(variables)
end