Module: Trailblazer::Developer::Generate

Defined in:
lib/trailblazer/developer/generate.rb

Overview

Computes an Intermediate data structure from a TRB-editor.js file.

Defined Under Namespace

Modules: Representer Classes: Arrow, Element

Class Method Summary collapse

Class Method Details

.call(hash) ⇒ Object



32
33
34
35
36
# File 'lib/trailblazer/developer/generate.rb', line 32

def call(hash)
  elements = transform_from_hash(hash)

  compute_intermediate(elements)
end

.compute_intermediate(elements, find_start_events: method(:find_start_events)) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/trailblazer/developer/generate.rb', line 46

def compute_intermediate(elements, find_start_events: method(:find_start_events))
  start_events = find_start_events.(elements)
  end_events   = elements.find_all { |el| el.type == "EndEventTerminate" } # DISCUSS: is it really called TERMINATE?

  inter = Activity::Schema::Intermediate

  wiring = elements.collect { |el| [inter.TaskRef(el.id, el.data), el.linksTo.collect { |arrow| inter.Out(semantic_for(arrow.to_h), arrow.target) } ] }
  wiring = Hash[wiring]

  # end events need this stupid special handling
  # DISCUSS: currently, the END-SEMANTIC is read from the event's label.
  wiring = wiring.merge(Hash[
    end_events.collect do |_end|
      ref, outputs = wiring.find { |ref, _| ref.id == _end.id }

      [ref, [inter.Out(semantic_for(_end.to_h)|| raise, nil)]] # TODO: test the raise, happens when the semantic of an End can't be distinguished. # TODO: don't extract semantic from :label but from :data.
    end
  ])
  # pp wiring

  inter.new(wiring, end_events.collect(&:id), start_events.collect(&:id))
end

.extract_semantic(label) ⇒ Object



79
80
81
# File 'lib/trailblazer/developer/generate.rb', line 79

def extract_semantic(label)
  label.to_sym
end

.find_start_events(elements) ⇒ Object



42
43
44
# File 'lib/trailblazer/developer/generate.rb', line 42

def find_start_events(elements)
  elements.find_all { |el| el.type == "Event" }
end

.semantic_for(label: nil) ⇒ Object

We currently use the :label field of an arrow to encode an output semantic. The :symbol_style part will be filtered out as semantic. Defaults to :success.



73
74
75
76
77
# File 'lib/trailblazer/developer/generate.rb', line 73

def semantic_for(label:nil, **)
  return :success unless label

  extract_semantic(label)
end

.transform_from_hash(hash, parser: Representer::Activity) ⇒ Object



38
39
40
# File 'lib/trailblazer/developer/generate.rb', line 38

def transform_from_hash(hash, parser: Representer::Activity)
  parser.new(OpenStruct.new).from_hash(hash).elements
end