Commit d654e1e3 by jasl

refactor fire

parent 72f52db2
...@@ -52,6 +52,6 @@ gem "form_core" ...@@ -52,6 +52,6 @@ gem "form_core"
gem "duck_record" gem "duck_record"
gem "closure_tree" gem "closure_tree"
gem "cocoon" gem "cocoon"
gem "script_core", github: "rails-engine/script_core", submodules: true gem "script_core"
gem "graphviz" gem "graphviz"
GIT
remote: https://github.com/rails-engine/script_core.git
revision: f086b63c03061732c8a760b97d54a9d2e7cbec68
submodules: true
specs:
script_core (0.0.4)
msgpack (~> 1.0)
rake-compiler (~> 1.0)
PATH PATH
remote: . remote: .
specs: specs:
...@@ -180,7 +171,7 @@ GEM ...@@ -180,7 +171,7 @@ GEM
rainbow (>= 2.2.2, < 4.0) rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1) unicode-display_width (~> 1.0, >= 1.0.1)
rubocop-rails_config (0.2.3) rubocop-rails_config (0.2.5)
railties (>= 3.0) railties (>= 3.0)
rubocop (~> 0.56) rubocop (~> 0.56)
ruby-progressbar (1.10.0) ruby-progressbar (1.10.0)
...@@ -196,6 +187,9 @@ GEM ...@@ -196,6 +187,9 @@ GEM
sprockets (>= 2.8, < 4.0) sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0) sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3) tilt (>= 1.1, < 3)
script_core (0.0.4)
msgpack (~> 1.0)
rake-compiler (~> 1.0)
selectize-rails (0.12.5) selectize-rails (0.12.5)
sprockets (4.0.0.beta8) sprockets (4.0.0.beta8)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
...@@ -248,7 +242,7 @@ DEPENDENCIES ...@@ -248,7 +242,7 @@ DEPENDENCIES
rubocop rubocop
rubocop-rails_config rubocop-rails_config
sass-rails sass-rails
script_core! script_core
selectize-rails selectize-rails
sprockets (~> 4.0.0.beta4) sprockets (~> 4.0.0.beta4)
sqlite3 sqlite3
......
...@@ -11,8 +11,22 @@ module WorkflowCore ...@@ -11,8 +11,22 @@ module WorkflowCore
has_many :output_places, dependent: :destroy, has_many :output_places, dependent: :destroy,
foreign_key: "input_transition_id", class_name: "WorkflowCore::Place" foreign_key: "input_transition_id", class_name: "WorkflowCore::Place"
def fire(_token) def fire(token, transaction_options: {requires_new: true}, **options)
transaction(**transaction_options) do
on_fire(token, transaction_options, options)
end
rescue => ex
rescue_fire_error ex
end
protected
def on_fire(_token, _transaction_options, **_options)
raise NotImplementedError raise NotImplementedError
end end
def rescue_fire_error(ex)
raise ex
end
end end
end end
...@@ -3,14 +3,6 @@ ...@@ -3,14 +3,6 @@
class Transition < WorkflowCore::Transition class Transition < WorkflowCore::Transition
serialize :options, Transitions::Options::Common serialize :options, Transitions::Options::Common
def auto_forward(next_token)
transition = next_token.place.output_transition
return unless transition
return unless transition.auto_forwardable?
transition.fire(next_token)
end
def auto_forwardable? def auto_forwardable?
false false
end end
...@@ -45,6 +37,16 @@ class Transition < WorkflowCore::Transition ...@@ -45,6 +37,16 @@ class Transition < WorkflowCore::Transition
g g
end end
protected
def auto_forward(next_token, transaction_options, **options)
transition = next_token.place.output_transition
return unless transition
return unless transition.auto_forwardable?
transition.on_fire(next_token, transaction_options, options)
end
end end
require_dependency "transitions" require_dependency "transitions"
# frozen_string_literal: true # frozen_string_literal: true
class Transitions::End < Transition class Transitions::End < Transition
def fire(token) def on_fire(token, _transaction_options, **_options)
p = output_places.first # assume only one output place p = output_places.first # assume only one output place
token.completed! token.completed!
......
...@@ -3,24 +3,24 @@ ...@@ -3,24 +3,24 @@
class Transitions::ExclusiveChoice < Transition class Transitions::ExclusiveChoice < Transition
serialize :options, Transitions::Options::ExclusiveChoice serialize :options, Transitions::Options::ExclusiveChoice
def fire(token) def on_fire(token, transaction_options, **options)
instance = token.instance instance = token.instance
next_place_id = options.conditions.select do |condition| next_place_id = self.options.conditions.select do |condition|
r = ScriptCore.run input: {payload: instance.payload}, r = ScriptCore.run input: {payload: instance.payload},
sources: [["expression", "@output = #{condition.condition_expression}"]] sources: [["expression", "@output = #{condition.condition_expression}"]]
if r.errors.any? if r.errors.any?
raise r.errors.map(&:message).join("; ") raise r.errors.map(&:message).join("; ")
end end
r.output r.output
end.first&.place_id || options.default_next_place_id end.first&.place_id || self.options.default_next_place_id
next_place = workflow.places.find(next_place_id) next_place = workflow.places.find(next_place_id)
token.completed! token.completed!
next_token = next_place.tokens.create! previous: token, type: "Token", next_token = next_place.tokens.create! previous: token, type: "Token",
instance: token.instance, workflow: workflow instance: token.instance, workflow: workflow
auto_forward(next_token) auto_forward(next_token, transaction_options, options)
end end
def auto_forwardable? def auto_forwardable?
......
# frozen_string_literal: true # frozen_string_literal: true
class Transitions::ParallelSplit < Transition class Transitions::ParallelSplit < Transition
def fire(token) def on_fire(token, transaction_options, **options)
token.completed! token.completed!
output_places.each do |p| output_places.each do |p|
next_token = p.tokens.create! previous: token, type: "Token", next_token = p.tokens.create! previous: token, type: "Token",
instance: token.instance, workflow: workflow instance: token.instance, workflow: workflow
auto_forward(next_token) auto_forward(next_token, transaction_options, options)
end end
def auto_forwardable? def auto_forwardable?
......
# frozen_string_literal: true # frozen_string_literal: true
class Transitions::Sequence < Transition class Transitions::Sequence < Transition
def fire(token) def on_fire(token, transaction_options, **options)
p = output_places.first # assume only one output place p = output_places.first # assume only one output place
token.completed! token.completed!
next_token = p.tokens.create! previous: token, type: "Token", next_token = p.tokens.create! previous: token, type: "Token",
instance: token.instance, workflow: workflow instance: token.instance, workflow: workflow
auto_forward(next_token) auto_forward(next_token, transaction_options, options)
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
class Transitions::SimpleMerge < Transition class Transitions::SimpleMerge < Transition
def fire(token) def on_fire(token, transaction_options, **options)
p = output_places.first # assume only one output place p = output_places.first # assume only one output place
# return unless p.tokens.size.zero? # return unless p.tokens.size.zero?
...@@ -14,7 +14,7 @@ class Transitions::SimpleMerge < Transition ...@@ -14,7 +14,7 @@ class Transitions::SimpleMerge < Transition
if completed_tokens.size == 1 if completed_tokens.size == 1
next_token = p.tokens.create! previous: token, type: "Token", next_token = p.tokens.create! previous: token, type: "Token",
instance: token.instance, workflow: workflow instance: token.instance, workflow: workflow
auto_forward(next_token) auto_forward(next_token, transaction_options, options)
end end
end end
......
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
class Transitions::Start < Transition class Transitions::Start < Transition
# http://workflowpatterns.com/patterns/control/basic/wcp1.php # http://workflowpatterns.com/patterns/control/basic/wcp1.php
def fire(token) def on_fire(token, transaction_options, **options)
p = output_places.first # assume only one output place p = output_places.first # assume only one output place
token.completed! token.completed!
next_token = p.tokens.create! previous: token, type: "Token", next_token = p.tokens.create! previous: token, type: "Token",
instance: token.instance, workflow: workflow instance: token.instance, workflow: workflow
auto_forward(next_token) auto_forward(next_token, transaction_options, options)
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
class Transitions::Synchronization < Transition class Transitions::Synchronization < Transition
def fire(token) def on_fire(token, transaction_options, **options)
p = output_places.first # assume only one output place p = output_places.first # assume only one output place
# return unless p.tokens.size.zero? # return unless p.tokens.size.zero?
...@@ -14,7 +14,7 @@ class Transitions::Synchronization < Transition ...@@ -14,7 +14,7 @@ class Transitions::Synchronization < Transition
if completed_tokens.size == input_places.size if completed_tokens.size == input_places.size
next_token = p.tokens.create! previous: token, type: "Token", next_token = p.tokens.create! previous: token, type: "Token",
instance: token.instance, workflow: workflow instance: token.instance, workflow: workflow
auto_forward(next_token) auto_forward(next_token, transaction_options, options)
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment