Class: FlowCore::Definition::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_core/definition/transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net, tag, attributes = {}, &block) ⇒ Transition

Returns a new instance of Transition.

Raises:

  • (TypeError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/flow_core/definition/transition.rb', line 9

def initialize(net, tag, attributes = {}, &block)
  raise TypeError unless net.is_a? FlowCore::Definition::Net
  raise TypeError unless tag.is_a? Symbol

  @net = net
  @tag = tag
  @input_tags = []
  @output_tags = []
  @callbacks = []

  input = attributes.delete(:input)
  output = attributes.delete(:output)
  raise ArgumentError, "Require `input`" unless input

  inputs(*input)
  outputs(*output) if output

  trigger = attributes.delete :with_trigger
  @trigger =
    if trigger
      FlowCore::Definition::Trigger.new trigger
    end

  callbacks = []
  callbacks.concat Array.wrap(attributes.delete(:with_callbacks))
  callbacks.concat Array.wrap(attributes.delete(:with_callback))
  @callbacks = callbacks.compact.map { |cb| FlowCore::Definition::Callback.new cb }

  @attributes = attributes.with_indifferent_access.except(FlowCore::Transition::FORBIDDEN_ATTRIBUTES)
  @attributes[:name] ||= tag.to_s
  @attributes[:tag] ||= tag.to_s

  block&.call(self)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def attributes
  @attributes
end

#callbacksObject (readonly)

Returns the value of attribute callbacks.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def callbacks
  @callbacks
end

#input_tagsObject (readonly)

Returns the value of attribute input_tags.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def input_tags
  @input_tags
end

#output_tagsObject (readonly)

Returns the value of attribute output_tags.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def output_tags
  @output_tags
end

#tagObject (readonly)

Returns the value of attribute tag.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def tag
  @tag
end

#triggerObject (readonly)

Returns the value of attribute trigger.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def trigger
  @trigger
end

Instance Method Details

#compileObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/flow_core/definition/transition.rb', line 108

def compile
  {
    tag: @tag,
    attributes: @attributes,
    trigger: @trigger&.compile,
    callbacks: @callbacks.map(&:compile),
    input_tags: @input_tags,
    output_tags: @output_tags.map { |output| { tag: output[:tag], guards: output[:guards].map(&:compile) } }
  }
end

#input(tag, attributes = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/flow_core/definition/transition.rb', line 58

def input(tag, attributes = {})
  unless net.places.find { |place| place.tag == tag }
    net.add_place(tag, attributes)
  end

  @input_tags << tag
end

#inputs(*tags) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/flow_core/definition/transition.rb', line 66

def inputs(*tags)
  tags.each do |tag|
    case tag
    when Symbol
      input(tag)
    when Array # Expect `[:p1, {name: "Place 1"}]`
      input(*tag)
    else
      raise TypeError, "Unknown pattern - #{place}"
    end
  end
end

#output(tag, attributes = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/flow_core/definition/transition.rb', line 79

def output(tag, attributes = {})
  guards = []
  guards.concat Array.wrap(attributes.delete(:with_guards))
  guards.concat Array.wrap(attributes.delete(:with_guard))
  guards.compact!
  guards.map! { |guard| FlowCore::Definition::Guard.new guard }

  fallback = attributes.delete(:fallback) || false

  unless net.places.find { |place| place.tag == tag }
    net.add_place(tag, attributes)
  end

  @output_tags << { tag: tag, guards: guards, fallback: fallback }
end

#outputs(*tags) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/flow_core/definition/transition.rb', line 95

def outputs(*tags)
  tags.each do |tag|
    case tag
    when Symbol
      output(tag)
    when Array # Expect `[:p1, {name: "Place 1"}]`
      output(*tag)
    else
      raise TypeError, "Unknown pattern - #{place}"
    end
  end
end

#with_callback(type, attributes = {}) ⇒ Object



48
49
50
# File 'lib/flow_core/definition/transition.rb', line 48

def with_callback(type, attributes = {})
  @callbacks << FlowCore::Definition::Callback.new(attributes.merge(type: type))
end

#with_callbacks(*callbacks) ⇒ Object



52
53
54
55
56
# File 'lib/flow_core/definition/transition.rb', line 52

def with_callbacks(*callbacks)
  callbacks.each do |cb|
    with_callback(*cb)
  end
end

#with_trigger(type, attributes = {}) ⇒ Object



44
45
46
# File 'lib/flow_core/definition/transition.rb', line 44

def with_trigger(type, attributes = {})
  @trigger = FlowCore::Definition::Trigger.new attributes.merge(type: type)
end