Class: SpotFlow::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/spot_flow/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources = [], processes: [], decisions: [], services: {}) ⇒ Context

Returns a new instance of Context.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spot_flow/context.rb', line 8

def initialize(sources = [], processes:[], decisions:[], services: {})
  @sources = Array.wrap(sources)
  @processes = Array.wrap(processes)
  @bpmn_definitions = []
  @dmn_definitions = []
  @decisions = Array.wrap(decisions)
  @services = HashWithIndifferentAccess.new((SpotFlow.config.services || {}).merge(services))

  @sources.each do |source|
    if source.include?("http://www.omg.org/spec/DMN/20180521/DC/")
      definitions = SpotFeel.definitions_from_xml(source)
      @dmn_definitions << definitions
      @decisions += definitions.decisions
    else
      @processes += SpotFlow.processes_from_xml(source)
    end
  end

  @executions = []
end

Instance Attribute Details

#bpmn_definitionsObject (readonly)

Returns the value of attribute bpmn_definitions.



6
7
8
# File 'lib/spot_flow/context.rb', line 6

def bpmn_definitions
  @bpmn_definitions
end

#decisionsObject (readonly)

Returns the value of attribute decisions.



5
6
7
# File 'lib/spot_flow/context.rb', line 5

def decisions
  @decisions
end

#dmn_definitionsObject (readonly)

Returns the value of attribute dmn_definitions.



6
7
8
# File 'lib/spot_flow/context.rb', line 6

def dmn_definitions
  @dmn_definitions
end

#executionsObject (readonly)

Returns the value of attribute executions.



5
6
7
# File 'lib/spot_flow/context.rb', line 5

def executions
  @executions
end

#processesObject (readonly)

Returns the value of attribute processes.



5
6
7
# File 'lib/spot_flow/context.rb', line 5

def processes
  @processes
end

#servicesObject (readonly)

Returns the value of attribute services.



5
6
7
# File 'lib/spot_flow/context.rb', line 5

def services
  @services
end

#sourcesObject (readonly)

Returns the value of attribute sources.



5
6
7
# File 'lib/spot_flow/context.rb', line 5

def sources
  @sources
end

Instance Method Details

#decision_by_id(id) ⇒ Object



92
93
94
# File 'lib/spot_flow/context.rb', line 92

def decision_by_id(id)
  decisions.find { |d| d.id == id }
end

#default_processObject



61
62
63
64
# File 'lib/spot_flow/context.rb', line 61

def default_process
  raise "Multiple processes defined, must identify process" if processes.size > 1
  processes.first
end

#dmn_definitions_by_decision_id(decision_id) ⇒ Object



96
97
98
# File 'lib/spot_flow/context.rb', line 96

def dmn_definitions_by_decision_id(decision_id)
  dmn_definitions.find { |definitions| definitions.decisions.find { |decision| decision.id == decision_id } }
end

#element_by_id(id) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/spot_flow/context.rb', line 76

def element_by_id(id)
  processes.each do |process|
    element = process.element_by_id(id)
    return element if element
  end
  nil
end

#execution_by_id(id) ⇒ Object



84
85
86
# File 'lib/spot_flow/context.rb', line 84

def execution_by_id(id)
  executions.find { |e| e.id == id }
end

#execution_by_step_id(step_id) ⇒ Object



88
89
90
# File 'lib/spot_flow/context.rb', line 88

def execution_by_step_id(step_id)
  executions.find { |e| e.step.id == step_id }
end

#inspectObject



100
101
102
103
104
105
106
# File 'lib/spot_flow/context.rb', line 100

def inspect
  parts = ["#<Context"]
  parts << "@processes=#{processes.inspect}" if processes.present?
  parts << "@decisions=#{decisions.inspect}" if decisions.present?
  parts << "@executions=#{executions.inspect}" if executions.present?
  parts.join(" ") + ">"
end

#notify_listener(*args) ⇒ Object



57
58
59
# File 'lib/spot_flow/context.rb', line 57

def notify_listener(*args)
  SpotFlow.config.listener&.call(args)
end

#process_by_id(id) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/spot_flow/context.rb', line 66

def process_by_id(id)
  processes.each do |process|
    return process if process.id == id
    process.sub_processes.each do |sub_process|
      return sub_process if sub_process.id == id
    end
  end
  nil
end

#restore(execution_state) ⇒ Object



51
52
53
54
55
# File 'lib/spot_flow/context.rb', line 51

def restore(execution_state)
  Execution.deserialize(execution_state, context: self).tap do |execution|
    executions << execution
  end
end

#start(process_id: nil, start_event_id: nil, variables: {}) ⇒ Object

Raises:



29
30
31
32
33
34
35
# File 'lib/spot_flow/context.rb', line 29

def start(process_id: nil, start_event_id: nil, variables: {})
  process = process_id ? process_by_id(process_id) : default_process
  raise ExecutionError.new(process_id ? "Process #{process_id} not found." : "No default process found.") if process.blank?
  execution = Execution.start(context: self, process: process, start_event_id: start_event_id, variables: variables)
  executions << execution
  execution
end

#start_with_message(message_name:, variables: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spot_flow/context.rb', line 37

def start_with_message(message_name:, variables: {})
  [].tap do |executions|
    processes.map do |process|
      process.start_events.map do |start_event|
        start_event.message_event_definitions.map do |message_event_definition|
          if message_name == message_event_definition.message_name
            Execution.start(context: self, process: process, variables: variables, start_event_id: start_event.id).tap { |execution| executions.push execution }
          end
        end
      end
    end
  end
end