Class: FlowCore::Workflow

Inherits:
ApplicationRecord show all
Includes:
WorkflowCallbacks
Defined in:
app/models/flow_core/workflow.rb

Constant Summary collapse

FORBIDDEN_ATTRIBUTES =
%i[verified verified_at created_at updated_at].freeze

Instance Method Summary collapse

Methods included from WorkflowCallbacks

#on_instance_activate, #on_instance_cancel, #on_instance_finish, #on_instance_task_enable, #on_instance_task_errored, #on_instance_task_finish, #on_instance_task_rescue, #on_instance_task_resume, #on_instance_task_suspend, #on_instance_task_terminate, #on_instance_terminate

Instance Method Details

#build_instance(attributes = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'app/models/flow_core/workflow.rb', line 22

def build_instance(attributes = {})
  if force_workflow_verified_on_create_instance && invalid?
    raise FlowCore::UnverifiedWorkflow, "Workflow##{id} didn't do soundness check yet."
  end

  instances.build attributes.with_indifferent_access.except(FlowCore::Instance::FORBIDDEN_ATTRIBUTES)
end

#create_instance(attributes = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'app/models/flow_core/workflow.rb', line 30

def create_instance(attributes = {})
  instance = build_instance(attributes)
  return unless instance

  instance.save
  instance
end

#create_instance!(attributes = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'app/models/flow_core/workflow.rb', line 38

def create_instance!(attributes = {})
  instance = build_instance(attributes)
  unless instance
    raise RecordNotSaved.new("Failed to create workflow instance", instance)
  end

  instance.save!
  instance
end

#forkObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/flow_core/workflow.rb', line 129

def fork
  new_workflow = dup
  transaction do
    yield new_workflow if block_given?
    new_workflow.save!

    new_transitions = transitions.includes(:trigger, :callbacks).map do |t|
      new_transition = t.dup
      new_transition.workflow = new_workflow
      new_transition.save!

      if t.trigger
        new_trigger = t.trigger.dup
        new_trigger.workflow = new_workflow
        new_trigger.transition = new_transition
        new_trigger.save!
      end

      t.callbacks.find_each do |cb|
        new_cb = cb.dup
        new_cb.workflow = new_workflow
        new_cb.transition = new_transition
        new_cb.save!
      end

      [t.id, new_transition.id]
    end.to_h

    new_places = places.map do |p|
      new_place = p.dup
      new_place.workflow = new_workflow
      new_place.save!

      [p.id, new_place.id]
    end.to_h

    arcs.includes(:guards).find_each do |a|
      new_arc = a.dup
      new_arc.workflow = new_workflow
      new_arc.place_id = new_places[a.place_id]
      new_arc.transition_id = new_transitions[a.transition_id]
      new_arc.save!

      a.guards.find_each do |g|
        new_guard = g.dup
        new_guard.workflow = new_workflow
        new_guard.arc = new_arc
        new_guard.save!
      end
    end

    new_workflow.verify!
  end
  new_workflow
end

#invalid?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/flow_core/workflow.rb', line 48

def invalid?
  !verified?
end

#reset_workflow_verification!Object



125
126
127
# File 'app/models/flow_core/workflow.rb', line 125

def reset_workflow_verification!
  update! verified: false, verified_at: nil
end

#verify!Object



120
121
122
123
# File 'app/models/flow_core/workflow.rb', line 120

def verify!
  update! verified: verify?, verified_at: Time.zone.now
  violations.empty?
end

#verify?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/flow_core/workflow.rb', line 52

def verify?
  violations.clear

  unless start_place
    violations.add(:start_place, :presence)
  end
  unless end_place
    violations.add(:end_place, :presence)
  end

  return false unless start_place && end_place

  # TODO: Naive implementation for now, Help wanted!

  rgl = to_rgl

  start_place_code = "P_#{start_place.id}"
  end_place_code = "P_#{end_place.id}"

  unless rgl.path?(start_place_code, end_place_code)
    violations.add :end_place, :unreachable
  end

  places.find_each do |p|
    next if p == start_place
    next if p == end_place

    place_code = "P_#{p.id}"

    unless rgl.path?(start_place_code, place_code)
      violations.add p, :unreachable
    end

    unless rgl.path?(place_code, end_place_code)
      violations.add p, :impassable
    end
  end
  transitions.includes(:trigger).find_each do |t|
    transition_code = "T_#{t.id}"

    unless rgl.path?(start_place_code, transition_code)
      violations.add t, :unreachable
    end

    unless rgl.path?(transition_code, end_place_code)
      violations.add t, :impassable
    end

    t.verify violations: violations
  end

  violations.empty?
end

#verify_statusObject



110
111
112
113
114
115
116
117
118
# File 'app/models/flow_core/workflow.rb', line 110

def verify_status
  if verified_at.blank?
    :unverified
  elsif verified?
    :verified
  else
    :invalid
  end
end

#violationsObject



106
107
108
# File 'app/models/flow_core/workflow.rb', line 106

def violations
  @violations ||= FlowCore::Violations.new
end