state_machines-graphviz 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90020c1871d32a31894619ff9883ae27e08210499cd4a1c0361b18ad503c04b1
4
- data.tar.gz: 9f206b3af3589f1072c8514b182d57c066e1de9179effafb6351a63471f8d61f
3
+ metadata.gz: 7e305290570ffa9518655ebfed7a506115fd97235ab030484bf4c955c4ecef70
4
+ data.tar.gz: 570796a514e5ac230626db3841f323130700140d7a0ad046323d1c21c2d6532f
5
5
  SHA512:
6
- metadata.gz: cabca272749f9e4e5928cea279b45503e78268b5060e6acb6b50e61fcd5ef31800537ad5da64e7730afceeffaf3f560f18d8e0e96104ab8e00cab15acc27371f
7
- data.tar.gz: d410ac462911bd82ae5827c71045cef33b4e1413d0abb079f1e1124c1f58fe337b2c4313d2281aabc39bf9a51e92cb7666c191972c365badd83f4feea9749b90
6
+ metadata.gz: 17be51e4e45e4b1a80119eb98b233ff2c5e088e9f3cf9a028bc8c3e021b4feb3633aa22f8f75e355bbfe80d5c2070329c8b2ed5ef8ee2b14a02683a30f277098
7
+ data.tar.gz: 91e74085ed70e0efd6a0431c1a610356403ce4da205840bda345ce0a4136ac035567f53f4fc7b83c9248982a8788805db8342b2f0e12884f7dc50b9f325975ec
data/README.md CHANGED
@@ -19,6 +19,20 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
+ Requiring the gem changes no global state. Choose the GraphViz renderer for
23
+ `Machine#draw` (and `State`, `Event` and `Branch#draw`), as the core intends:
24
+
25
+ ```ruby
26
+ require 'state_machines-graphviz'
27
+
28
+ StateMachines::Machine.renderer = StateMachines::Graphviz::Renderer
29
+ Vehicle.state_machine(:state).draw(path: 'doc/state_machines', format: 'svg')
30
+ ```
31
+
32
+ or draw with it directly, whatever renderer is chosen:
33
+ `StateMachines::Graphviz::Renderer.draw_machine(machine)`,
34
+ `StateMachines::Graphviz.draw('Vehicle')`, or the rake task below.
35
+
22
36
  The default output folder for the images is `doc/state_machines`.
23
37
 
24
38
  #### Examples
@@ -69,6 +83,21 @@ in the class. The generated files will use an output filename of the format
69
83
  For examples of actual images generated using this task, see those under the
70
84
  examples folder.
71
85
 
86
+ #### What is drawn
87
+
88
+ The graph is built by [state_machines-diagram](https://github.com/state-machines/state_machines-diagram),
89
+ so it shows what the core performs:
90
+
91
+ * the initial state gets an edge from a start point, final states a double circle
92
+ * a state's value shows after its name when the two differ: `parked (1)`, `idling (*)` for a lambda
93
+ * each edge reads `event` and, when guarded, its conditions as the core checks them:
94
+ `[if engine_started? && unless braking? && if_any_state (gate=open, alarm=on)]`
95
+ * callbacks are listed in the order the core runs them: before and around callbacks
96
+ at an edge's tail in declaration order (an around where it enters), after callbacks
97
+ at its head, a conditional one with its conditions: `log_start (if logging?)`
98
+ * from one source an event takes its first matching branch, so a branch an unguarded
99
+ one before it always takes over is not drawn
100
+
72
101
  #### Example output
73
102
 
74
103
  Generate the sample diagram used in this README:
@@ -27,8 +27,7 @@ module StateMachines
27
27
  options = { path: 'doc/state_machines', format: 'png', font: 'Arial', orientation: 'portrait' }.merge(options)
28
28
  StateMachines::OptionsValidator.assert_valid_keys!(options, :path, :format, :font, :orientation)
29
29
 
30
- # TODO: fail if path cannot be created or readonly
31
- FileUtils.mkpath(options[:path]) unless Dir.exist? options[:path]
30
+ FileUtils.mkpath(options[:path])
32
31
  @font = options[:font]
33
32
  @file_path = File.join(options[:path], "#{name}.#{options[:format]}")
34
33
  @file_format = options[:format]
@@ -4,151 +4,136 @@ require 'state_machines-diagram'
4
4
 
5
5
  module StateMachines
6
6
  module Graphviz
7
+ # Draws a machine, or the part of it one state, event or branch involves,
8
+ # with GraphViz. What is drawn comes from StateMachines::Diagram::Builder:
9
+ # its states and markers, the transitions the core performs, their guards
10
+ # and the callbacks that run on them.
7
11
  module Renderer
12
+ # The start point's node id: the diagram's terminal pseudo-state, which
13
+ # Builder.state_id never gives a state
14
+ START_NODE_ID = ::Diagrams::StateDiagram::TERMINAL
15
+ # Characters DOT reads as escapes or breaks in a quoted id; ruby-graphviz
16
+ # writes ids holding them as they are
17
+ UNSAFE_ID = /[\\[:cntrl:]]/
18
+ # Characters ruby-graphviz writes into a label as they are, so DOT would
19
+ # read them as escapes (\N, \n, ...) or the label as HTML (<...>)
20
+ LABEL_ESCAPES = /[\\<]/
21
+
8
22
  extend self
9
23
 
10
- def draw_machine(machine, io: $stdout, **options)
11
- diagram, builder = StateMachines::Diagram::Renderer.build_state_diagram(machine, options)
24
+ # Draws the whole machine and writes the graph's file
25
+ def draw_machine(machine, **options)
12
26
  graph_options = options.dup
13
27
  name = graph_options.delete(:name) || "#{machine.owner_class.name}_#{machine.name}"
14
-
15
- draw_options = { human_name: false }
16
- draw_options[:human_name] = graph_options.delete(:human_names) if graph_options.include?(:human_names)
17
-
28
+ human_names = graph_options.delete(:human_names)
18
29
  graph = Graph.new(name, graph_options)
19
- render_diagram(graph, diagram, builder, draw_options)
30
+
31
+ builder = Diagram::Builder.new(machine, human_names: human_names)
32
+ builder.build
33
+ draw_built(graph, builder)
20
34
  graph.output
21
35
  graph
22
36
  end
23
37
 
24
- def draw_state(state, graph, options = {}, io = $stdout)
25
- node = graph.add_nodes(
26
- state.name ? state.name.to_s : 'nil',
27
- label: state.description(options),
28
- width: '1',
29
- height: '1',
30
- shape: state.final? ? 'doublecircle' : 'ellipse'
31
- )
32
-
33
- graph.add_edges(graph.add_nodes('starting_state', shape: 'point'), node) if state.initial?
34
- true
38
+ # Adds the state's node, and the start point if it is the initial state
39
+ def draw_state(state, graph, options = {}, _io = $stdout)
40
+ builder = Diagram::Builder.new(state.machine, human_names: options[:human_name])
41
+ builder.build_state(state)
42
+ add_state_node(graph, builder, state)
43
+ graph
35
44
  end
36
45
 
37
- def draw_event(event, graph, options = {}, io = $stdout)
38
- machine = event.machine
39
- valid_states = machine.states.by_priority.map(&:name)
40
- event_label = options[:human_name] ? event.human_name(machine.owner_class) : event.name.to_s
41
-
42
- event.branches.each do |branch|
43
- draw_branch_with_label(branch, graph, event_label, machine, valid_states)
44
- end
46
+ # Adds the transitions the event performs and the states they connect
47
+ def draw_event(event, graph, options = {}, _io = $stdout)
48
+ builder = Diagram::Builder.new(event.machine, human_names: options[:human_name])
49
+ builder.build_event(event)
50
+ draw_built(graph, builder)
51
+ graph
52
+ end
45
53
 
46
- true
54
+ # Adds the transitions one branch performs and the states they connect.
55
+ # The core passes no options (Branch#draw).
56
+ def draw_branch(branch, graph, event, _valid_states, _io = $stdout)
57
+ builder = Diagram::Builder.new(event.machine)
58
+ builder.build_branch(branch, event)
59
+ draw_built(graph, builder)
60
+ graph
47
61
  end
48
62
 
49
- def draw_branch(branch, graph, event, valid_states, io = $stdout)
50
- machine = event.machine
51
- draw_branch_with_label(branch, graph, event.name.to_s, machine, valid_states)
52
- true
63
+ # The node id of the state with diagram id +state_id+. An id DOT would
64
+ # misread is drawn by its bytes in hex, as Builder.state_id escapes ids:
65
+ # the builder only escapes names that hold none of those characters,
66
+ # so the two never meet.
67
+ def node_id(state_id)
68
+ state_id.match?(UNSAFE_ID) ? "%#{state_id.unpack1('H*')}" : state_id
53
69
  end
54
70
 
55
71
  private
56
72
 
57
- def render_diagram(graph, diagram, builder, draw_options)
58
- state_lookup = build_state_lookup(builder.machine)
59
- state_metadata = builder&.state_metadata || {}
60
- start_node = nil
61
-
62
- diagram.states.each do |state_node|
63
- metadata = state_metadata[state_node.id] || {}
64
- state = state_lookup[state_node.id]
65
- label = state ? state.description(human_name: draw_options[:human_name]) : (state_node.label || state_node.id)
66
- shape = metadata[:type] == 'final' ? 'doublecircle' : 'ellipse'
67
-
68
- node = graph.add_nodes(
69
- graph_node_id(state_node.id),
70
- label: label,
71
- width: '1',
72
- height: '1',
73
- shape: shape
74
- )
75
-
76
- if metadata[:type] == 'initial'
77
- start_node ||= graph.add_nodes('starting_state', shape: 'point')
78
- graph.add_edges(start_node, node)
79
- end
80
- end
81
-
82
- transition_metadata = transition_metadata_map(builder)
83
- diagram.transitions.each do |transition|
84
- metadata = transition_metadata[transition] || {}
85
- callbacks = metadata[:callbacks] || {}
86
-
87
- graph.add_edges(
88
- graph_node_id(transition.source_state_id),
89
- graph_node_id(transition.target_state_id),
90
- label: transition.label.to_s,
91
- labelfontsize: 10,
92
- taillabel: Array(callbacks[:before]).compact.join("\n"),
93
- headlabel: Array(callbacks[:after]).compact.join("\n")
94
- )
95
- end
73
+ # The guards a transition runs under, as one bracketed condition:
74
+ # "[if ready? && if_state gate=open]"
75
+ def guard_label(conditions)
76
+ phrases = Diagram::Labels.condition_phrases(conditions)
77
+ "[#{phrases.join(' && ')}]" unless phrases.empty?
96
78
  end
97
79
 
98
- def build_state_lookup(machine)
99
- machine.states.each_with_object({}) do |state, memo|
100
- key = state.name ? state.name.to_s : 'nil_state'
101
- memo[key] = state
80
+ def draw_built(graph, builder)
81
+ states = Diagram::Introspection.states_of(builder.machine).to_h { [Diagram::Builder.state_id(it.name), it] }
82
+ nodes = builder.diagram.states.to_h { [it.id, add_state_node(graph, builder, states.fetch(it.id))] }
83
+
84
+ builder.transition_metadata.each do |metadata|
85
+ transition = metadata[:transition]
86
+ add_transition_edge(graph, nodes.fetch(transition.source_state_id),
87
+ nodes.fetch(transition.target_state_id), metadata)
102
88
  end
103
89
  end
104
90
 
105
- def graph_node_id(diagram_state_id)
106
- diagram_state_id == 'nil_state' ? 'nil' : diagram_state_id
91
+ # The state's node: a double circle for a final state, an edge from
92
+ # the start point for the initial one (a state can be both)
93
+ def add_state_node(graph, builder, state)
94
+ id = Diagram::Builder.state_id(state.name)
95
+ metadata = builder.state_metadata.fetch(id)
96
+ node = graph.add_nodes(
97
+ node_id(id),
98
+ label: dot_text([node_label(state, metadata[:label])]),
99
+ width: '1',
100
+ height: '1',
101
+ shape: metadata[:final] ? 'doublecircle' : 'ellipse'
102
+ )
103
+ graph.add_edges(graph.add_nodes(START_NODE_ID, shape: 'point'), node) if metadata[:initial]
104
+ node
107
105
  end
108
106
 
109
- def transition_metadata_map(builder)
110
- Array(builder&.transition_metadata).each_with_object({}) do |metadata, memo|
111
- transition = metadata[:transition]
112
- memo[transition] = metadata if transition
113
- end
107
+ # The state's label and, as State#description shows it, its value
108
+ # when that is not its name: "parked (1)", "idling (*)" for a lambda
109
+ def node_label(state, label)
110
+ value = state.value(false)
111
+ return label if state.name.to_s == value.to_s
112
+
113
+ "#{label} (#{value.is_a?(Proc) ? '*' : value.inspect})"
114
114
  end
115
115
 
116
- def draw_branch_with_label(branch, graph, event_label, machine, valid_states)
117
- branch.state_requirements.each do |state_requirement|
118
- from_states = state_requirement[:from].filter(valid_states)
119
-
120
- if state_requirement[:to].values.empty?
121
- loopback = true
122
- else
123
- to_state = state_requirement[:to].values.first
124
- to_state = to_state ? to_state.to_s : 'nil'
125
- loopback = false
126
- end
127
-
128
- from_states.each do |from_state|
129
- from_state = from_state ? from_state.to_s : 'nil'
130
- graph.add_edges(
131
- from_state,
132
- loopback ? from_state : to_state,
133
- label: event_label,
134
- labelfontsize: 10,
135
- taillabel: callback_method_names(machine, branch, :before).join("\n"),
136
- headlabel: callback_method_names(machine, branch, :after).join("\n")
137
- )
138
- end
139
- end
116
+ # The event and its guards over the edge. The callbacks in the order the
117
+ # core runs them (Introspection.callback_sequence): those that run
118
+ # before the state changes at the edge's tail, before and around
119
+ # callbacks in declaration order with an around where it enters (as
120
+ # Labels.action_list lists it), the after callbacks at its head
121
+ def add_transition_edge(graph, from, to, metadata)
122
+ sequence = metadata[:callback_sequence].reject { it[:phase] == :exit }
123
+ after, before = sequence.partition { it[:kind] == :after }
124
+ graph.add_edges(
125
+ from,
126
+ to,
127
+ label: dot_text([metadata[:label], guard_label(metadata[:conditions])]),
128
+ labelfontsize: 10,
129
+ taillabel: dot_text(before.map { it[:text] }),
130
+ headlabel: dot_text(after.map { it[:text] })
131
+ )
140
132
  end
141
133
 
142
- def callback_method_names(machine, branch, type)
143
- event_name = branch.event_requirement.values.first
144
- machine.callbacks[type].select do |callback|
145
- callback.branch.matches?(branch,
146
- from: branch.state_requirements.map { |req| req[:from] },
147
- to: branch.state_requirements.map { |req| req[:to] },
148
- on: event_name)
149
- end.flat_map do |callback|
150
- callback.instance_variable_get('@methods')
151
- end.compact
134
+ # Lines of label text as DOT shows them as they are
135
+ def dot_text(lines)
136
+ lines.compact.map { |line| line.gsub(LABEL_ESCAPES) { "\\#{it}" } }.join("\n")
152
137
  end
153
138
  end
154
139
  end
@@ -3,6 +3,6 @@
3
3
  module StateMachines
4
4
  # Gem version
5
5
  module Graphviz
6
- VERSION = '0.1.0'
6
+ VERSION = '0.2.0'
7
7
  end
8
8
  end
@@ -10,26 +10,18 @@ module StateMachines
10
10
  module Graphviz
11
11
  module_function
12
12
 
13
+ # Draws every state machine of each class in +class_names+ (a
14
+ # comma-separated list) with GraphViz, whichever renderer machines use
13
15
  def draw(class_names, options = {})
14
- if class_names.nil? || class_names.to_s.split(',').empty?
15
- raise ArgumentError, 'At least one class must be specified'
16
- end
17
-
18
- # Load any files
19
- if (files = options.delete(:file))
20
- files.split(',').each { |file| require file }
21
- end
16
+ names = class_names.to_s.split(',').map(&:strip).reject(&:empty?)
17
+ raise ArgumentError, 'At least one class must be specified' if names.empty?
22
18
 
23
- class_names.to_s.split(',').each do |class_name|
24
- # Navigate through the namespace structure to get to the class
25
- klass = Object
26
- class_name.split('::').each do |name|
27
- klass = klass.const_defined?(name) ? klass.const_get(name) : klass.const_missing(name)
28
- end
19
+ options = options.dup
20
+ options.delete(:file).to_s.split(',').each { |file| require file.strip }
29
21
 
30
- # Draw each of the class's state machines
31
- klass.state_machines.each_value do |machine|
32
- machine.draw(**options)
22
+ names.each do |class_name|
23
+ Object.const_get(class_name).state_machines.each_value do |machine|
24
+ Renderer.draw_machine(machine, **options)
33
25
  end
34
26
  end
35
27
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'state_machines/graphviz'
4
+
5
+ # Test with:
6
+ # rake state_machines:draw FILE=./test/files/switch.rb CLASS=Switch
7
+ namespace :state_machines do
8
+ desc 'Draws state machines using GraphViz (options: CLASS=User,Vehicle; FILE=user.rb,vehicle.rb ' \
9
+ '[not required in Rails]; TARGET=doc/state_machines; FONT=Arial; FORMAT=png; ORIENTATION=portrait; ' \
10
+ 'HUMAN_NAMES=true)'
11
+ task :draw do
12
+ # Build drawing options
13
+ options = {}
14
+ options[:file] = ENV['FILE'] if ENV['FILE']
15
+ options[:path] = ENV['TARGET'] if ENV['TARGET']
16
+ options[:format] = ENV['FORMAT'] if ENV['FORMAT']
17
+ options[:font] = ENV['FONT'] if ENV['FONT']
18
+ options[:orientation] = ENV['ORIENTATION'] if ENV['ORIENTATION']
19
+ options[:human_names] = ENV['HUMAN_NAMES'] == 'true' if ENV['HUMAN_NAMES']
20
+
21
+ if defined?(Rails)
22
+ puts 'Files are automatically loaded in Rails; ignoring FILE option' if options.delete(:file)
23
+ Rake::Task['environment'].invoke
24
+ end
25
+
26
+ StateMachines::Graphviz.draw(ENV.fetch('CLASS', nil), options)
27
+ end
28
+ end
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'state_machines-diagram'
4
3
  require 'state_machines/graphviz'
5
- require 'state_machines/graphviz/renderer'
6
4
 
7
- # Set the renderer to use graphviz
8
- StateMachines::Machine.renderer = StateMachines::Graphviz::Renderer
5
+ # Loading the gem changes no global state. Machine#draw (and State, Event
6
+ # and Branch#draw) use this renderer once it is chosen:
7
+ #
8
+ # StateMachines::Machine.renderer = StateMachines::Graphviz::Renderer
9
+ #
10
+ # or draw with it directly: StateMachines::Graphviz::Renderer.draw_machine(machine),
11
+ # StateMachines::Graphviz.draw('Vehicle'), or the state_machines:draw task.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machines-graphviz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 1980-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: state_machines
14
+ name: ruby-graphviz
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,33 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: state_machines-diagram
28
+ name: state_machines
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.0
33
+ version: 0.303.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.1.0
40
+ version: 0.303.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: ruby-graphviz
42
+ name: state_machines-diagram
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.3.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.3.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '6.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '6.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -81,19 +95,61 @@ dependencies:
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
- name: minitest
98
+ name: rubocop
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - '='
101
+ - - "~>"
88
102
  - !ruby/object:Gem::Version
89
- version: 5.27.0
103
+ version: '1.59'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - '='
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.59'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-minitest
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
95
116
  - !ruby/object:Gem::Version
96
- version: 5.27.0
117
+ version: '0.34'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.34'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-performance
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.20'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.20'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-rake
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.6'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.6'
97
153
  description: Graphviz module for state machines
98
154
  email:
99
155
  - terminale@gmail.com
@@ -110,28 +166,12 @@ files:
110
166
  - lib/state_machines/graphviz/version.rb
111
167
  - lib/state_machines/tasks/railtie.rb
112
168
  - lib/state_machines/tasks/state_machines.rake
113
- - test/files/switch.rb
114
- - test/graph_default_test.rb
115
- - test/graph_edges_test.rb
116
- - test/graph_nodes_test.rb
117
- - test/graph_output_test.rb
118
- - test/machine_class_drawing_test.rb
119
- - test/machine_drawing_test.rb
120
- - test/machine_drawing_with_dynamic_states_test.rb
121
- - test/machine_drawing_with_integer_states_test.rb
122
- - test/machine_drawing_with_nil_states_test.rb
123
- - test/state_drawing_final_test.rb
124
- - test/state_drawing_initial_test.rb
125
- - test/state_drawing_lambda_value_test.rb
126
- - test/state_drawing_nil_name_test.rb
127
- - test/state_drawing_non_final_test.rb
128
- - test/state_drawing_test.rb
129
- - test/state_drawing_with_human_name_test.rb
130
- - test/test_helper.rb
169
+ - lib/state_machines/tasks/state_machines.rb
131
170
  homepage: https://github.com/state-machines/state_machines-graphviz
132
171
  licenses:
133
172
  - MIT
134
- metadata: {}
173
+ metadata:
174
+ rubygems_mfa_required: 'true'
135
175
  rdoc_options: []
136
176
  require_paths:
137
177
  - lib
@@ -139,32 +179,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
179
  requirements:
140
180
  - - ">="
141
181
  - !ruby/object:Gem::Version
142
- version: '0'
182
+ version: '4.0'
143
183
  required_rubygems_version: !ruby/object:Gem::Requirement
144
184
  requirements:
145
185
  - - ">="
146
186
  - !ruby/object:Gem::Version
147
187
  version: '0'
148
188
  requirements: []
149
- rubygems_version: 4.0.3
189
+ rubygems_version: 4.0.20
150
190
  specification_version: 4
151
191
  summary: Drawing module for state machines
152
- test_files:
153
- - test/files/switch.rb
154
- - test/graph_default_test.rb
155
- - test/graph_edges_test.rb
156
- - test/graph_nodes_test.rb
157
- - test/graph_output_test.rb
158
- - test/machine_class_drawing_test.rb
159
- - test/machine_drawing_test.rb
160
- - test/machine_drawing_with_dynamic_states_test.rb
161
- - test/machine_drawing_with_integer_states_test.rb
162
- - test/machine_drawing_with_nil_states_test.rb
163
- - test/state_drawing_final_test.rb
164
- - test/state_drawing_initial_test.rb
165
- - test/state_drawing_lambda_value_test.rb
166
- - test/state_drawing_nil_name_test.rb
167
- - test/state_drawing_non_final_test.rb
168
- - test/state_drawing_test.rb
169
- - test/state_drawing_with_human_name_test.rb
170
- - test/test_helper.rb
192
+ test_files: []
data/test/files/switch.rb DELETED
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Switch
4
- def self.name
5
- @name ||= "Switch_#{rand(1_000_000)}"
6
- end
7
-
8
- state_machine do
9
- event :turn_on do
10
- transition all => :on
11
- end
12
-
13
- event :turn_off do
14
- transition all => :off
15
- end
16
- end
17
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require './test/test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @graph = StateMachines::Graph.new('test')
7
- end
8
-
9
- def test_should_have_a_default_font
10
- assert_equal('Arial', @graph.font)
11
- end
12
-
13
- def test_should_use_current_directory_for_filepath
14
- assert_equal('doc/state_machines/test.png', @graph.file_path)
15
- end
16
-
17
- def test_should_have_a_default_file_format
18
- assert_equal('png', @graph.file_format)
19
- end
20
-
21
- def test_should_have_a_default_orientation
22
- assert_equal('TB', @graph[:rankdir].source)
23
- end
24
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @graph = StateMachines::Graph.new('test')
7
- @graph.add_nodes('parked', shape: 'ellipse')
8
- @graph.add_nodes('idling', shape: 'ellipse')
9
- @edge = @graph.add_edges('parked', 'idling', label: 'ignite')
10
- end
11
-
12
- def test_should_return_generated_edge
13
- refute_nil(@edge)
14
- end
15
-
16
- def test_should_use_specified_nodes
17
- assert_equal('parked', @edge.node_one(false))
18
- assert_equal('idling', @edge.node_two(false))
19
- end
20
-
21
- def test_should_use_specified_options
22
- assert_equal('ignite', @edge['label'].to_s.gsub('"', ''))
23
- end
24
-
25
- def test_should_set_default_font
26
- assert_equal('Arial', @edge['fontname'].to_s.gsub('"', ''))
27
- end
28
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @graph = StateMachines::Graph.new('test')
7
- @node = @graph.add_nodes('parked', shape: 'ellipse')
8
- end
9
-
10
- def test_should_return_generated_node
11
- refute_nil @node
12
- end
13
-
14
- def test_should_use_specified_name
15
- assert_equal(@node, @graph.get_node('parked'))
16
- end
17
-
18
- def test_should_use_specified_options
19
- assert_equal('ellipse', @node['shape'].to_s.gsub('"', ''))
20
- end
21
-
22
- def test_should_set_default_font
23
- assert_equal('Arial', @node['fontname'].to_s.gsub('"', ''))
24
- end
25
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @graph_name = "test_#{rand(1_000_000)}"
7
- @graph = StateMachines::Graph.new(@graph_name)
8
- @graph.add_nodes('parked', shape: 'ellipse')
9
- @graph.add_nodes('idling', shape: 'ellipse')
10
- @graph.add_edges('parked', 'idling', label: 'ignite')
11
- @graph.output
12
- end
13
-
14
- def test_should_save_file
15
- assert File.exist?("doc/state_machines/#{@graph_name}.png")
16
- end
17
-
18
- def teardown
19
- FileUtils.rm Dir["doc/state_machines/#{@graph_name}.png"]
20
- end
21
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- describe StateMachines::Machine do
6
- def switch_file
7
- File.expand_path("#{File.dirname(__FILE__)}/files/switch.rb")
8
- end
9
-
10
- def switch_machine
11
- require switch_file
12
- Switch.state_machines.values.first
13
- end
14
-
15
- def test_should_load_files
16
- machine = switch_machine
17
- assert(defined?(::Switch))
18
- assert(machine)
19
- end
20
-
21
- def test_should_allow_path_and_format_to_be_customized
22
- machine = switch_machine
23
- output_path = File.dirname(__FILE__)
24
- machine.draw(path: output_path, format: 'jpg')
25
- assert(File.exist?("#{output_path}/#{Switch.name}_state.jpg"))
26
- FileUtils.rm Dir["{.,#{output_path}}/#{Switch.name}_state.{jpg,png}"]
27
- end
28
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- describe StateMachines::Graph do
6
- def setup
7
- @klass = Class.new do
8
- def self.name
9
- @name ||= "Vehicle_#{rand(1_000_000)}"
10
- end
11
- end
12
-
13
- @machine = StateMachines::Machine.new(@klass, initial: :parked)
14
- @machine.event :ignite do
15
- transition parked: :idling
16
- end
17
- end
18
-
19
- def test_should_raise_exception_if_invalid_option_specified
20
- assert_raises(ArgumentError) { @machine.draw(invalid: true) }
21
- end
22
-
23
- def test_should_save_file_with_class_name_by_default
24
- @machine.draw
25
- assert File.exist?("doc/state_machines/#{@klass.name}_state.png"), 'Failed to save file with class name'
26
- end
27
-
28
- def test_should_allow_base_name_to_be_customized
29
- name = "@machine_#{rand(1_000_000)}"
30
- @machine.draw(name: name)
31
- @path = "doc/state_@machines/#{name}.png"
32
- end
33
-
34
- def test_should_allow_format_to_be_customized
35
- @machine.draw(format: 'jpg')
36
- @path = "doc/state_machines/#{@klass.name}_state.jpg"
37
- assert File.exist?(@path), 'allow format to be custom'
38
- end
39
-
40
- def test_should_allow_path_to_be_customized
41
- @machine.draw(path: "#{File.dirname(__FILE__)}/")
42
- @path = "#{File.dirname(__FILE__)}/#{@klass.name}_state.png"
43
- assert(File.exist?(@path))
44
- end
45
-
46
- def test_should_allow_orientation_to_be_landscape
47
- @graph = @machine.draw(orientation: 'landscape')
48
- assert_equal(@graph['rankdir'].to_s.gsub('"', ''), 'LR')
49
- end
50
-
51
- def test_should_allow_orientation_to_be_portrait
52
- @graph = @machine.draw(orientation: 'portrait')
53
- assert_equal(@graph['rankdir'].to_s.gsub('"', ''), 'TB')
54
- end
55
-
56
- def test_should_allow_human_names_to_be_displayed
57
- @machine.event :ignite, human_name: 'Ignite'
58
- @machine.state :parked, human_name: 'Parked'
59
- @machine.state :idling, human_name: 'Idling'
60
- @graph = @machine.draw(human_names: true)
61
-
62
- parked_node = @graph.get_node('parked')
63
- assert_equal(parked_node['label'].to_s.gsub('"', ''), 'Parked')
64
-
65
- idling_node = @graph.get_node('idling')
66
- assert_equal(idling_node['label'].to_s.gsub('"', ''), 'Idling')
67
- end
68
-
69
- def teardown
70
- FileUtils.rm Dir[@path || "doc/state_@machines/#{@klass.name}_state.png"]
71
- end
72
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- describe StateMachines::Graph do
6
- def setup
7
- @klass = Class.new do
8
- def self.name
9
- @name ||= "Vehicle_#{rand(1_000_000)}"
10
- end
11
- end
12
-
13
- @machine = StateMachines::Machine.new(@klass, initial: :parked)
14
- @machine.event :activate do
15
- transition parked: :idling
16
- end
17
-
18
- @machine.state :idling, value: -> { Time.now }
19
-
20
- @graph = @machine.draw
21
- end
22
-
23
- def test_should_draw_all_states
24
- assert(@graph.node_count, 3)
25
- end
26
-
27
- def test_should_draw_all_events
28
- assert(@graph.edge_count, 2)
29
- end
30
-
31
- def test_should_draw_machine
32
- assert(File.exist?("doc/state_machines/#{@klass.name}_state.png"))
33
- end
34
-
35
- def teardown
36
- FileUtils.rm Dir["doc/state_machines/#{@klass.name}_state.png"]
37
- end
38
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- describe StateMachines::Graph do
6
- def setup
7
- @klass = Class.new do
8
- def self.name
9
- @name ||= "Vehicle_#{rand(1_000_000)}"
10
- end
11
- end
12
-
13
- @machine = StateMachines::Machine.new(@klass, :state_id, initial: :parked)
14
- @machine.event :ignite do
15
- transition parked: :idling
16
- end
17
- @machine.state :parked, value: 1
18
- @machine.state :idling, value: 2
19
-
20
- @graph = @machine.draw
21
- end
22
-
23
- def test_should_draw_all_states
24
- assert_equal(@graph.node_count, 3)
25
- end
26
-
27
- def test_should_draw_all_events
28
- assert_equal(@graph.edge_count, 2)
29
- end
30
-
31
- def test_should_draw_machine
32
- assert(File.exist?("doc/state_machines/#{@klass.name}_state_id.png"))
33
- end
34
-
35
- def teardown
36
- assert(File.exist?("doc/state_machines/#{@klass.name}_state_id.png"))
37
- end
38
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- describe StateMachines::Graph do
6
- def setup
7
- @klass = Class.new do
8
- def self.name
9
- @name ||= "Vehicle_#{rand(1_000_000)}"
10
- end
11
- end
12
-
13
- @machine = StateMachines::Machine.new(@klass, initial: :parked)
14
- @machine.event :ignite do
15
- transition parked: :idling
16
- end
17
- @machine.state :parked, value: nil
18
-
19
- @graph = @machine.draw
20
- end
21
-
22
- def test_should_draw_all_states
23
- assert(@graph.node_count, 3)
24
- end
25
-
26
- def test_should_draw_all_events
27
- assert(@graph.edge_count, 2)
28
- end
29
-
30
- def test_should_draw_machine
31
- assert(File.exist?("doc/state_machines/#{@klass.name}_state.png"))
32
- end
33
-
34
- def teardown
35
- assert(FileUtils.rm(Dir["doc/state_machines/#{@klass.name}_state.png"]))
36
- end
37
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @machine = StateMachines::Machine.new(Class.new)
7
- @machine.states << @state = StateMachines::State.new(@machine, :parked)
8
-
9
- graph = StateMachines::Graph.new('test')
10
- @state.draw(graph)
11
- @node = graph.get_node('parked')
12
- end
13
-
14
- def test_should_use_doublecircle_as_shape
15
- assert_equal('doublecircle', @node['shape'].to_s.gsub('"', ''))
16
- end
17
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @machine = StateMachines::Machine.new(Class.new)
7
- @machine.states << @state = StateMachines::State.new(@machine, :parked, initial: true)
8
- @machine.event :ignite do
9
- transition parked: :idling
10
- end
11
-
12
- @graph = StateMachines::Graph.new('test')
13
- @state.draw(@graph)
14
- @node = @graph.get_node('parked')
15
- end
16
-
17
- def test_should_use_ellipse_as_shape
18
- assert_equal 'ellipse', @node['shape'].to_s.gsub('"', '')
19
- end
20
-
21
- def test_should_draw_edge_between_point_and_state
22
- assert_equal(2, @graph.node_count)
23
- assert_equal(1, @graph.edge_count)
24
- end
25
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @machine = StateMachines::Machine.new(Class.new)
7
- @machine.states << @state = StateMachines::State.new(@machine, :parked, value: -> {})
8
-
9
- graph = StateMachines::Graph.new('test')
10
- @state.draw(graph)
11
- @node = graph.get_node('parked')
12
- end
13
-
14
- def test_should_have_a_node
15
- assert(@node)
16
- end
17
-
18
- def test_should_use_description_as_label
19
- assert_equal('parked (*)', @node['label'].to_s.gsub('"', ''))
20
- end
21
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @machine = StateMachines::Machine.new(Class.new)
7
- @machine = StateMachines::Machine.new(Class.new)
8
- @machine.states << @state = StateMachines::State.new(@machine, nil)
9
-
10
- graph = StateMachines::Graph.new('test')
11
- @state.draw(graph)
12
- @node = graph.get_node('nil')
13
- end
14
-
15
- def test_should_have_a_node
16
- assert(@node)
17
- end
18
-
19
- def test_should_use_description_as_label
20
- assert_equal 'nil', @node['label'].to_s.gsub('"', '')
21
- end
22
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @machine = StateMachines::Machine.new(Class.new)
7
- @machine.states << @state = StateMachines::State.new(@machine, :parked)
8
- @machine.event :ignite do
9
- transition parked: :idling
10
- end
11
-
12
- graph = StateMachines::Graph.new('test')
13
- @state.draw(graph)
14
- @node = graph.get_node('parked')
15
- end
16
-
17
- def test_should_use_ellipse_as_shape
18
- assert_equal('ellipse', @node['shape'].to_s.gsub('"', ''))
19
- end
20
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
-
5
- describe StateMachines::Graph do
6
- def setup
7
- @machine = StateMachines::Machine.new(Class.new)
8
- @machine.states << @state = StateMachines::State.new(@machine, :parked, value: 1)
9
- @machine.event :ignite do
10
- transition parked: :idling
11
- end
12
-
13
- graph = StateMachines::Graph.new('test')
14
- @state.draw(graph)
15
- @node = graph.get_node('parked')
16
- end
17
-
18
- def test_should_use_ellipse_shape
19
- assert_equal(@node['shape'].to_s.gsub('"', ''), 'ellipse')
20
- end
21
-
22
- def test_should_set_width_to_one
23
- assert_equal('1', @node['width'].to_s.gsub('"', ''))
24
- end
25
-
26
- def test_should_set_height_to_one
27
- assert_equal('1', @node['height'].to_s.gsub('"', ''))
28
- end
29
-
30
- def test_should_use_description_as_label
31
- assert_equal(@node['label'].to_s.gsub('"', ''), 'parked (1)')
32
- end
33
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'test_helper'
4
- describe StateMachines::Graph do
5
- def setup
6
- @machine = StateMachines::Machine.new(Class.new)
7
- @machine.states << @state = StateMachines::State.new(@machine, :parked, human_name: 'Parked')
8
- @machine.event :ignite do
9
- transition parked: :idling
10
- end
11
-
12
- graph = StateMachines::Graph.new('test')
13
- @state.draw(graph, human_name: true)
14
- @node = graph.get_node('parked')
15
- end
16
-
17
- def test_should_use_description_with_human_name_as_label
18
- assert_equal('Parked', @node['label'].to_s.gsub('"', ''))
19
- end
20
- end
data/test/test_helper.rb DELETED
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'state_machines-graphviz'
4
- require 'minitest/autorun'
5
- require 'minitest/unit'
6
- require 'minitest/pride'