state_machines-graphviz 0.0.2 → 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 +5 -5
- data/LICENSE.txt +1 -1
- data/README.md +43 -2
- data/lib/state_machines/graphviz/graph.rb +4 -5
- data/lib/state_machines/graphviz/renderer.rb +140 -0
- data/lib/state_machines/graphviz/version.rb +3 -1
- data/lib/state_machines/graphviz.rb +25 -1
- data/lib/state_machines/tasks/railtie.rb +3 -2
- data/lib/state_machines/tasks/state_machines.rake +2 -0
- data/lib/state_machines/tasks/state_machines.rb +12 -16
- data/lib/state_machines-graphviz.rb +10 -0
- metadata +87 -32
- data/.gitignore +0 -23
- data/.rspec +0 -3
- data/.travis.yml +0 -12
- data/Gemfile +0 -4
- data/Rakefile +0 -13
- data/lib/state_machines/graphviz/monkeypatch.rb +0 -161
- data/spec/files/switch.rb +0 -15
- data/spec/graph_spec.rb +0 -92
- data/spec/machine_drawing_spec.rb +0 -215
- data/spec/machine_spec.rb +0 -19
- data/spec/spec_helper.rb +0 -2
- data/spec/state_spec.rb +0 -142
- data/state_machines-graphviz.gemspec +0 -25
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7e305290570ffa9518655ebfed7a506115fd97235ab030484bf4c955c4ecef70
|
|
4
|
+
data.tar.gz: 570796a514e5ac230626db3841f323130700140d7a0ad046323d1c21c2d6532f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 17be51e4e45e4b1a80119eb98b233ff2c5e088e9f3cf9a028bc8c3e021b4feb3633aa22f8f75e355bbfe80d5c2070329c8b2ed5ef8ee2b14a02683a30f277098
|
|
7
|
+
data.tar.gz: 91e74085ed70e0efd6a0431c1a610356403ce4da205840bda345ce0a4136ac035567f53f4fc7b83c9248982a8788805db8342b2f0e12884f7dc50b9f325975ec
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -19,6 +19,22 @@ 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
|
+
|
|
36
|
+
The default output folder for the images is `doc/state_machines`.
|
|
37
|
+
|
|
22
38
|
#### Examples
|
|
23
39
|
|
|
24
40
|
To generate a graph for a specific file / class:
|
|
@@ -67,16 +83,41 @@ in the class. The generated files will use an output filename of the format
|
|
|
67
83
|
For examples of actual images generated using this task, see those under the
|
|
68
84
|
examples folder.
|
|
69
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
|
+
|
|
101
|
+
#### Example output
|
|
102
|
+
|
|
103
|
+
Generate the sample diagram used in this README:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
bundle exec ruby script/generate_example.rb
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+

|
|
110
|
+
|
|
70
111
|
### Interactive graphs
|
|
71
112
|
|
|
72
|
-
Jean Bovet's [Visual Automata Simulator](
|
|
113
|
+
Jean Bovet's [Visual Automata Simulator](https://github.com/NimaGhaedsharafi/VAS)
|
|
73
114
|
is a great tool for "simulating, visualizing and transforming finite state
|
|
74
115
|
automata and Turing Machines". It can help in the creation of states and events
|
|
75
116
|
for your models. It is cross-platform, written in Java.
|
|
76
117
|
|
|
77
118
|
## Contributing
|
|
78
119
|
|
|
79
|
-
1. Fork it ( https://github.com/
|
|
120
|
+
1. Fork it ( https://github.com/state-machines/state_machines-graphviz/fork )
|
|
80
121
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
81
122
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
82
123
|
4. Push to the branch (`git push origin my-new-feature`)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module StateMachines
|
|
2
4
|
# Provides a set of higher-order features on top of the raw GraphViz graphs
|
|
3
5
|
class Graph < GraphViz
|
|
@@ -23,12 +25,9 @@ module StateMachines
|
|
|
23
25
|
# "landscape"). Default is "portrait".
|
|
24
26
|
def initialize(name, options = {})
|
|
25
27
|
options = { path: 'doc/state_machines', format: 'png', font: 'Arial', orientation: 'portrait' }.merge(options)
|
|
26
|
-
|
|
28
|
+
StateMachines::OptionsValidator.assert_valid_keys!(options, :path, :format, :font, :orientation)
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
unless Dir.exist? options[:path]
|
|
30
|
-
FileUtils.mkpath(options[:path])
|
|
31
|
-
end
|
|
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]
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'state_machines-diagram'
|
|
4
|
+
|
|
5
|
+
module StateMachines
|
|
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.
|
|
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
|
+
|
|
22
|
+
extend self
|
|
23
|
+
|
|
24
|
+
# Draws the whole machine and writes the graph's file
|
|
25
|
+
def draw_machine(machine, **options)
|
|
26
|
+
graph_options = options.dup
|
|
27
|
+
name = graph_options.delete(:name) || "#{machine.owner_class.name}_#{machine.name}"
|
|
28
|
+
human_names = graph_options.delete(:human_names)
|
|
29
|
+
graph = Graph.new(name, graph_options)
|
|
30
|
+
|
|
31
|
+
builder = Diagram::Builder.new(machine, human_names: human_names)
|
|
32
|
+
builder.build
|
|
33
|
+
draw_built(graph, builder)
|
|
34
|
+
graph.output
|
|
35
|
+
graph
|
|
36
|
+
end
|
|
37
|
+
|
|
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
|
|
44
|
+
end
|
|
45
|
+
|
|
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
|
|
53
|
+
|
|
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
|
|
61
|
+
end
|
|
62
|
+
|
|
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
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
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?
|
|
78
|
+
end
|
|
79
|
+
|
|
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)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
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
|
|
105
|
+
end
|
|
106
|
+
|
|
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
|
+
end
|
|
115
|
+
|
|
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
|
+
)
|
|
132
|
+
end
|
|
133
|
+
|
|
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")
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -1,7 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'state_machines'
|
|
2
4
|
require 'graphviz'
|
|
3
|
-
require 'state_machines/graphviz/monkeypatch'
|
|
4
5
|
require 'state_machines/graphviz/graph'
|
|
6
|
+
require 'state_machines/graphviz/renderer'
|
|
5
7
|
require 'state_machines/graphviz/version'
|
|
6
8
|
|
|
9
|
+
module StateMachines
|
|
10
|
+
module Graphviz
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# Draws every state machine of each class in +class_names+ (a
|
|
14
|
+
# comma-separated list) with GraphViz, whichever renderer machines use
|
|
15
|
+
def draw(class_names, options = {})
|
|
16
|
+
names = class_names.to_s.split(',').map(&:strip).reject(&:empty?)
|
|
17
|
+
raise ArgumentError, 'At least one class must be specified' if names.empty?
|
|
18
|
+
|
|
19
|
+
options = options.dup
|
|
20
|
+
options.delete(:file).to_s.split(',').each { |file| require file.strip }
|
|
21
|
+
|
|
22
|
+
names.each do |class_name|
|
|
23
|
+
Object.const_get(class_name).state_machines.each_value do |machine|
|
|
24
|
+
Renderer.draw_machine(machine, **options)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
7
31
|
require 'state_machines/tasks/railtie' if defined?(Rails)
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'state_machines/graphviz'
|
|
2
4
|
require 'rails'
|
|
3
5
|
module StateMachines
|
|
4
|
-
# https://gist.github.com/josevalim/af7e572c2dc973add221
|
|
5
6
|
class Railtie < Rails::Railtie
|
|
6
7
|
rake_tasks do
|
|
7
8
|
load 'state_machines/tasks/state_machines.rake'
|
|
8
9
|
end
|
|
9
10
|
end
|
|
10
|
-
end
|
|
11
|
+
end
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'state_machines/graphviz'
|
|
4
|
+
|
|
1
5
|
# Test with:
|
|
2
|
-
# rake state_machines:draw FILE=./
|
|
6
|
+
# rake state_machines:draw FILE=./test/files/switch.rb CLASS=Switch
|
|
3
7
|
namespace :state_machines do
|
|
4
|
-
desc 'Draws state machines using GraphViz (options: CLASS=User,Vehicle; FILE=user.rb,vehicle.rb
|
|
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)'
|
|
5
11
|
task :draw do
|
|
6
12
|
# Build drawing options
|
|
7
13
|
options = {}
|
|
@@ -11,22 +17,12 @@ namespace :state_machines do
|
|
|
11
17
|
options[:font] = ENV['FONT'] if ENV['FONT']
|
|
12
18
|
options[:orientation] = ENV['ORIENTATION'] if ENV['ORIENTATION']
|
|
13
19
|
options[:human_names] = ENV['HUMAN_NAMES'] == 'true' if ENV['HUMAN_NAMES']
|
|
14
|
-
|
|
20
|
+
|
|
15
21
|
if defined?(Rails)
|
|
16
|
-
puts
|
|
22
|
+
puts 'Files are automatically loaded in Rails; ignoring FILE option' if options.delete(:file)
|
|
17
23
|
Rake::Task['environment'].invoke
|
|
18
|
-
elsif defined?(Merb)
|
|
19
|
-
puts "Files are automatically loaded in Merb; ignoring FILE option" if options.delete(:file)
|
|
20
|
-
Rake::Task['merb_env'].invoke
|
|
21
|
-
|
|
22
|
-
# Fix ruby-graphviz being incompatible with Merb's process title
|
|
23
|
-
$0 = 'rake'
|
|
24
|
-
else
|
|
25
|
-
# Load the library
|
|
26
|
-
$:.unshift(File.dirname(__FILE__) + '/..')
|
|
27
|
-
require 'state_machines'
|
|
28
24
|
end
|
|
29
|
-
|
|
30
|
-
StateMachines::
|
|
25
|
+
|
|
26
|
+
StateMachines::Graphviz.draw(ENV.fetch('CLASS', nil), options)
|
|
31
27
|
end
|
|
32
28
|
end
|
|
@@ -1 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'state_machines/graphviz'
|
|
4
|
+
|
|
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,18 +1,17 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: state_machines-graphviz
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
8
8
|
- Aaron Pfeifer
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
|
-
name:
|
|
14
|
+
name: ruby-graphviz
|
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
|
17
16
|
requirements:
|
|
18
17
|
- - ">="
|
|
@@ -26,19 +25,33 @@ dependencies:
|
|
|
26
25
|
- !ruby/object:Gem::Version
|
|
27
26
|
version: '0'
|
|
28
27
|
- !ruby/object:Gem::Dependency
|
|
29
|
-
name:
|
|
28
|
+
name: state_machines
|
|
30
29
|
requirement: !ruby/object:Gem::Requirement
|
|
31
30
|
requirements:
|
|
32
31
|
- - ">="
|
|
33
32
|
- !ruby/object:Gem::Version
|
|
34
|
-
version:
|
|
33
|
+
version: 0.303.0
|
|
35
34
|
type: :runtime
|
|
36
35
|
prerelease: false
|
|
37
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
38
37
|
requirements:
|
|
39
38
|
- - ">="
|
|
40
39
|
- !ruby/object:Gem::Version
|
|
41
|
-
version:
|
|
40
|
+
version: 0.303.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: state_machines-diagram
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 0.3.0
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.3.0
|
|
42
55
|
- !ruby/object:Gem::Dependency
|
|
43
56
|
name: bundler
|
|
44
57
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -53,6 +66,20 @@ dependencies:
|
|
|
53
66
|
- - ">="
|
|
54
67
|
- !ruby/object:Gem::Version
|
|
55
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'
|
|
56
83
|
- !ruby/object:Gem::Dependency
|
|
57
84
|
name: rake
|
|
58
85
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -68,19 +95,61 @@ dependencies:
|
|
|
68
95
|
- !ruby/object:Gem::Version
|
|
69
96
|
version: '0'
|
|
70
97
|
- !ruby/object:Gem::Dependency
|
|
71
|
-
name:
|
|
98
|
+
name: rubocop
|
|
72
99
|
requirement: !ruby/object:Gem::Requirement
|
|
73
100
|
requirements:
|
|
74
|
-
- - "
|
|
101
|
+
- - "~>"
|
|
75
102
|
- !ruby/object:Gem::Version
|
|
76
|
-
version:
|
|
103
|
+
version: '1.59'
|
|
77
104
|
type: :development
|
|
78
105
|
prerelease: false
|
|
79
106
|
version_requirements: !ruby/object:Gem::Requirement
|
|
80
107
|
requirements:
|
|
81
|
-
- - "
|
|
108
|
+
- - "~>"
|
|
82
109
|
- !ruby/object:Gem::Version
|
|
83
|
-
version:
|
|
110
|
+
version: '1.59'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: rubocop-minitest
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - "~>"
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
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'
|
|
84
153
|
description: Graphviz module for state machines
|
|
85
154
|
email:
|
|
86
155
|
- terminale@gmail.com
|
|
@@ -88,33 +157,21 @@ executables: []
|
|
|
88
157
|
extensions: []
|
|
89
158
|
extra_rdoc_files: []
|
|
90
159
|
files:
|
|
91
|
-
- ".gitignore"
|
|
92
|
-
- ".rspec"
|
|
93
|
-
- ".travis.yml"
|
|
94
|
-
- Gemfile
|
|
95
160
|
- LICENSE.txt
|
|
96
161
|
- README.md
|
|
97
|
-
- Rakefile
|
|
98
162
|
- lib/state_machines-graphviz.rb
|
|
99
163
|
- lib/state_machines/graphviz.rb
|
|
100
164
|
- lib/state_machines/graphviz/graph.rb
|
|
101
|
-
- lib/state_machines/graphviz/
|
|
165
|
+
- lib/state_machines/graphviz/renderer.rb
|
|
102
166
|
- lib/state_machines/graphviz/version.rb
|
|
103
167
|
- lib/state_machines/tasks/railtie.rb
|
|
104
168
|
- lib/state_machines/tasks/state_machines.rake
|
|
105
169
|
- lib/state_machines/tasks/state_machines.rb
|
|
106
|
-
|
|
107
|
-
- spec/graph_spec.rb
|
|
108
|
-
- spec/machine_drawing_spec.rb
|
|
109
|
-
- spec/machine_spec.rb
|
|
110
|
-
- spec/spec_helper.rb
|
|
111
|
-
- spec/state_spec.rb
|
|
112
|
-
- state_machines-graphviz.gemspec
|
|
113
|
-
homepage: https://github.com/seuros/state_machines-graphviz
|
|
170
|
+
homepage: https://github.com/state-machines/state_machines-graphviz
|
|
114
171
|
licenses:
|
|
115
172
|
- MIT
|
|
116
|
-
metadata:
|
|
117
|
-
|
|
173
|
+
metadata:
|
|
174
|
+
rubygems_mfa_required: 'true'
|
|
118
175
|
rdoc_options: []
|
|
119
176
|
require_paths:
|
|
120
177
|
- lib
|
|
@@ -122,16 +179,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
122
179
|
requirements:
|
|
123
180
|
- - ">="
|
|
124
181
|
- !ruby/object:Gem::Version
|
|
125
|
-
version: '0'
|
|
182
|
+
version: '4.0'
|
|
126
183
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
184
|
requirements:
|
|
128
185
|
- - ">="
|
|
129
186
|
- !ruby/object:Gem::Version
|
|
130
187
|
version: '0'
|
|
131
188
|
requirements: []
|
|
132
|
-
|
|
133
|
-
rubygems_version: 2.4.5
|
|
134
|
-
signing_key:
|
|
189
|
+
rubygems_version: 4.0.20
|
|
135
190
|
specification_version: 4
|
|
136
191
|
summary: Drawing module for state machines
|
|
137
192
|
test_files: []
|
data/.gitignore
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
*.gem
|
|
2
|
-
*.rbc
|
|
3
|
-
.bundle
|
|
4
|
-
.config
|
|
5
|
-
.yardoc
|
|
6
|
-
Gemfile.lock
|
|
7
|
-
InstalledFiles
|
|
8
|
-
_yardoc
|
|
9
|
-
coverage
|
|
10
|
-
doc/
|
|
11
|
-
lib/bundler/man
|
|
12
|
-
pkg
|
|
13
|
-
rdoc
|
|
14
|
-
spec/reports
|
|
15
|
-
test/tmp
|
|
16
|
-
test/version_tmp
|
|
17
|
-
tmp
|
|
18
|
-
*.bundle
|
|
19
|
-
*.so
|
|
20
|
-
*.o
|
|
21
|
-
*.a
|
|
22
|
-
mkmf.log
|
|
23
|
-
.idea
|
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
require 'bundler/gem_tasks'
|
|
2
|
-
|
|
3
|
-
require 'rspec/core/rake_task'
|
|
4
|
-
RSpec::Core::RakeTask.new do |t|
|
|
5
|
-
t.pattern = 'spec/**/*_spec.rb'
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
desc 'Default: run all tests.'
|
|
9
|
-
task :default => :spec
|
|
10
|
-
task :test => :spec
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
load File.dirname(__FILE__) + '/lib/state_machines/tasks/state_machines.rake'
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
#TODO register graphviz as render engine
|
|
2
|
-
module StateMachines
|
|
3
|
-
class Machine
|
|
4
|
-
class << self
|
|
5
|
-
# Draws the state machines defined in the given classes using GraphViz.
|
|
6
|
-
# The given classes must be a comma-delimited string of class names.
|
|
7
|
-
#
|
|
8
|
-
# Configuration options:
|
|
9
|
-
# * <tt>:file</tt> - A comma-delimited string of files to load that
|
|
10
|
-
# contain the state machine definitions to draw
|
|
11
|
-
# * <tt>:path</tt> - The path to write the graph file to
|
|
12
|
-
# * <tt>:format</tt> - The image format to generate the graph in
|
|
13
|
-
# * <tt>:font</tt> - The name of the font to draw state names in
|
|
14
|
-
def draw(class_names, options = {})
|
|
15
|
-
raise ArgumentError, 'At least one class must be specified' unless class_names && class_names.split(',').any?
|
|
16
|
-
|
|
17
|
-
# Load any files
|
|
18
|
-
if files = options.delete(:file)
|
|
19
|
-
files.split(',').each { |file| require file }
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
class_names.split(',').each do |class_name|
|
|
23
|
-
# Navigate through the namespace structure to get to the class
|
|
24
|
-
klass = Object
|
|
25
|
-
class_name.split('::').each do |name|
|
|
26
|
-
klass = klass.const_defined?(name) ? klass.const_get(name) : klass.const_missing(name)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# Draw each of the class's state machines
|
|
30
|
-
klass.state_machines.each_value do |machine|
|
|
31
|
-
machine.draw(options)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# Draws a directed graph of the machine for visualizing the various events,
|
|
38
|
-
# states, and their transitions.
|
|
39
|
-
#
|
|
40
|
-
# This requires both the Ruby graphviz gem and the graphviz library be
|
|
41
|
-
# installed on the system.
|
|
42
|
-
#
|
|
43
|
-
# Configuration options:
|
|
44
|
-
# * <tt>:name</tt> - The name of the file to write to (without the file extension).
|
|
45
|
-
# Default is "#{owner_class.name}_#{name}"
|
|
46
|
-
# * <tt>:path</tt> - The path to write the graph file to. Default is the
|
|
47
|
-
# current directory (".").
|
|
48
|
-
# * <tt>:format</tt> - The image format to generate the graph in.
|
|
49
|
-
# Default is "png'.
|
|
50
|
-
# * <tt>:font</tt> - The name of the font to draw state names in.
|
|
51
|
-
# Default is "Arial".
|
|
52
|
-
# * <tt>:orientation</tt> - The direction of the graph ("portrait" or
|
|
53
|
-
# "landscape"). Default is "portrait".
|
|
54
|
-
# * <tt>:human_names</tt> - Whether to use human state / event names for
|
|
55
|
-
# node labels on the graph instead of the internal name. Default is false.
|
|
56
|
-
def draw(graph_options = {})
|
|
57
|
-
name = graph_options.delete(:name) || "#{owner_class.name}_#{self.name}"
|
|
58
|
-
draw_options = {:human_name => false}
|
|
59
|
-
draw_options[:human_name] = graph_options.delete(:human_names) if graph_options.include?(:human_names)
|
|
60
|
-
|
|
61
|
-
graph = Graph.new(name, graph_options)
|
|
62
|
-
|
|
63
|
-
# Add nodes / edges
|
|
64
|
-
states.by_priority.each { |state| state.draw(graph, draw_options) }
|
|
65
|
-
events.each { |event| event.draw(graph, draw_options) }
|
|
66
|
-
|
|
67
|
-
# Output result
|
|
68
|
-
graph.output
|
|
69
|
-
graph
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
class State
|
|
74
|
-
# Draws a representation of this state on the given machine. This will
|
|
75
|
-
# create a new node on the graph with the following properties:
|
|
76
|
-
# * +label+ - The human-friendly description of the state.
|
|
77
|
-
# * +width+ - The width of the node. Always 1.
|
|
78
|
-
# * +height+ - The height of the node. Always 1.
|
|
79
|
-
# * +shape+ - The actual shape of the node. If the state is a final
|
|
80
|
-
# state, then "doublecircle", otherwise "ellipse".
|
|
81
|
-
#
|
|
82
|
-
# Configuration options:
|
|
83
|
-
# * <tt>:human_name</tt> - Whether to use the state's human name for the
|
|
84
|
-
# node's label that gets drawn on the graph
|
|
85
|
-
def draw(graph, options = {})
|
|
86
|
-
node = graph.add_nodes(name ? name.to_s : 'nil',
|
|
87
|
-
:label => description(options),
|
|
88
|
-
:width => '1',
|
|
89
|
-
:height => '1',
|
|
90
|
-
:shape => final? ? 'doublecircle' : 'ellipse'
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
# Add open arrow for initial state
|
|
94
|
-
graph.add_edges(graph.add_nodes('starting_state', :shape => 'point'), node) if initial?
|
|
95
|
-
|
|
96
|
-
true
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
class Event
|
|
101
|
-
# Draws a representation of this event on the given graph. This will
|
|
102
|
-
# create 1 or more edges on the graph for each branch (i.e. transition)
|
|
103
|
-
# configured.
|
|
104
|
-
#
|
|
105
|
-
# Configuration options:
|
|
106
|
-
# * <tt>:human_name</tt> - Whether to use the event's human name for the
|
|
107
|
-
# node's label that gets drawn on the graph
|
|
108
|
-
def draw(graph, options = {})
|
|
109
|
-
valid_states = machine.states.by_priority.map {|state| state.name}
|
|
110
|
-
branches.each do |branch|
|
|
111
|
-
branch.draw(graph, options[:human_name] ? human_name : name, valid_states)
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
true
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
class Branch
|
|
119
|
-
# Draws a representation of this branch on the given graph. This will draw
|
|
120
|
-
# an edge between every state this branch matches *from* to either the
|
|
121
|
-
# configured to state or, if none specified, then a loopback to the from
|
|
122
|
-
# state.
|
|
123
|
-
#
|
|
124
|
-
# For example, if the following from states are configured:
|
|
125
|
-
# * +idling+
|
|
126
|
-
# * +first_gear+
|
|
127
|
-
# * +backing_up+
|
|
128
|
-
#
|
|
129
|
-
# ...and the to state is +parked+, then the following edges will be created:
|
|
130
|
-
# * +idling+ -> +parked+
|
|
131
|
-
# * +first_gear+ -> +parked+
|
|
132
|
-
# * +backing_up+ -> +parked+
|
|
133
|
-
#
|
|
134
|
-
# Each edge will be labeled with the name of the event that would cause the
|
|
135
|
-
# transition.
|
|
136
|
-
def draw(graph, event, valid_states)
|
|
137
|
-
state_requirements.each do |state_requirement|
|
|
138
|
-
# From states determined based on the known valid states
|
|
139
|
-
from_states = state_requirement[:from].filter(valid_states)
|
|
140
|
-
|
|
141
|
-
# If a to state is not specified, then it's a loopback and each from
|
|
142
|
-
# state maps back to itself
|
|
143
|
-
if state_requirement[:to].values.empty?
|
|
144
|
-
loopback = true
|
|
145
|
-
else
|
|
146
|
-
to_state = state_requirement[:to].values.first
|
|
147
|
-
to_state = to_state ? to_state.to_s : 'nil'
|
|
148
|
-
loopback = false
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
# Generate an edge between each from and to state
|
|
152
|
-
from_states.each do |from_state|
|
|
153
|
-
from_state = from_state ? from_state.to_s : 'nil'
|
|
154
|
-
graph.add_edges(from_state, loopback ? from_state : to_state, :label => event.to_s)
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
true
|
|
159
|
-
end
|
|
160
|
-
end
|
|
161
|
-
end
|
data/spec/files/switch.rb
DELETED
data/spec/graph_spec.rb
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
describe StateMachines::Graph do
|
|
3
|
-
context 'GraphDefault' do
|
|
4
|
-
before(:each) do
|
|
5
|
-
@graph = StateMachines::Graph.new('test')
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
it 'should_have_a_default_font' do
|
|
9
|
-
expect('Arial').to eq(@graph.font)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
it 'should_use_current_directory_for_filepath' do
|
|
13
|
-
expect('doc/state_machines/test.png').to eq(@graph.file_path)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'should_have_a_default_file_format' do
|
|
17
|
-
expect('png').to eq(@graph.file_format)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it 'should_have_a_default_orientation' do
|
|
21
|
-
expect('TB').to eq(@graph[:rankdir].source)
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
context 'GraphNodes' do
|
|
26
|
-
before(:each) do
|
|
27
|
-
@graph = StateMachines::Graph.new('test')
|
|
28
|
-
@node = @graph.add_nodes('parked', :shape => 'ellipse')
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'should_return_generated_node' do
|
|
32
|
-
expect(@node).to_not be_nil
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
it 'should_use_specified_name' do
|
|
36
|
-
expect(@node).to eq(@graph.get_node('parked'))
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
it 'should_use_specified_options' do
|
|
40
|
-
expect('ellipse').to eq(@node['shape'].to_s.gsub('"', ''))
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
it 'should_set_default_font' do
|
|
44
|
-
expect('Arial').to eq(@node['fontname'].to_s.gsub('"', ''))
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
context 'GraphEdges' do
|
|
49
|
-
before(:each) do
|
|
50
|
-
@graph = StateMachines::Graph.new('test')
|
|
51
|
-
@graph.add_nodes('parked', :shape => 'ellipse')
|
|
52
|
-
@graph.add_nodes('idling', :shape => 'ellipse')
|
|
53
|
-
@edge = @graph.add_edges('parked', 'idling', :label => 'ignite')
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'should_return_generated_edge' do
|
|
57
|
-
expect(@edge).to_not be_nil
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
it 'should_use_specified_nodes' do
|
|
61
|
-
expect('parked').to eq(@edge.node_one(false))
|
|
62
|
-
expect('idling').to eq(@edge.node_two(false))
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it 'should_use_specified_options' do
|
|
66
|
-
expect('ignite').to eq(@edge['label'].to_s.gsub('"', ''))
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
it 'should_set_default_font' do
|
|
70
|
-
expect('Arial').to eq(@edge['fontname'].to_s.gsub('"', ''))
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
context 'GraphOutput' do
|
|
75
|
-
before(:each) do
|
|
76
|
-
@graph_name = "test_#{rand(1000000)}"
|
|
77
|
-
@graph = StateMachines::Graph.new(@graph_name)
|
|
78
|
-
@graph.add_nodes('parked', :shape => 'ellipse')
|
|
79
|
-
@graph.add_nodes('idling', :shape => 'ellipse')
|
|
80
|
-
@graph.add_edges('parked', 'idling', :label => 'ignite')
|
|
81
|
-
@graph.output
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
it 'should_save_file' do
|
|
85
|
-
expect(File.exist?("doc/state_machines/#{@graph_name}.png")).to be_truthy
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
after(:each) do
|
|
89
|
-
FileUtils.rm Dir["doc/state_machines/#{@graph_name}.png"]
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
describe StateMachines::Graphviz do
|
|
2
|
-
context 'Drawing' do
|
|
3
|
-
let(:klass) do
|
|
4
|
-
Class.new do
|
|
5
|
-
def self.name
|
|
6
|
-
@name ||= "Vehicle_#{rand(1_000_000)}"
|
|
7
|
-
end
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
let(:machine) { StateMachines::Machine.new(klass, initial: :parked) }
|
|
11
|
-
|
|
12
|
-
before(:each) do
|
|
13
|
-
machine.event :ignite do
|
|
14
|
-
transition parked: :idling
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'should_raise_exception_if_invalid_option_specified' do
|
|
19
|
-
expect { machine.draw(invalid: true) }.to raise_error(ArgumentError)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'should_save_file_with_class_name_by_default' do
|
|
23
|
-
machine.draw
|
|
24
|
-
expect(File.exist?("doc/state_machines/#{klass.name}_state.png")).to be_truthy
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it 'should_allow_base_name_to_be_customized' do
|
|
28
|
-
name = "machine_#{rand(1_000_000)}"
|
|
29
|
-
machine.draw(name: name)
|
|
30
|
-
@path = "doc/state_machines/#{name}.png"
|
|
31
|
-
expect(File.exist?(@path)).to be_truthy
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it 'should_allow_format_to_be_customized' do
|
|
35
|
-
machine.draw(format: 'jpg')
|
|
36
|
-
@path = "doc/state_machines/#{klass.name}_state.jpg"
|
|
37
|
-
expect(File.exist?(@path)).to be_truthy
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it 'should_allow_path_to_be_customized' do
|
|
41
|
-
machine.draw(path: "#{File.dirname(__FILE__)}/")
|
|
42
|
-
@path = "#{File.dirname(__FILE__)}/#{klass.name}_state.png"
|
|
43
|
-
expect(File.exist?(@path)).to be_truthy
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
it 'should_allow_orientation_to_be_landscape' do
|
|
47
|
-
graph = machine.draw(orientation: 'landscape')
|
|
48
|
-
expect(graph['rankdir'].to_s.gsub('"', '')).to eq('LR')
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it 'should_allow_orientation_to_be_portrait' do
|
|
52
|
-
graph = machine.draw(orientation: 'portrait')
|
|
53
|
-
expect(graph['rankdir'].to_s.gsub('"', '')).to eq('TB')
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'should_allow_human_names_to_be_displayed' do
|
|
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
|
-
expect(parked_node['label'].to_s.gsub('"', '')).to eq('Parked')
|
|
64
|
-
|
|
65
|
-
idling_node = graph.get_node('idling')
|
|
66
|
-
expect(idling_node['label'].to_s.gsub('"', '')).to eq('Idling')
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
after(:each) do
|
|
70
|
-
FileUtils.rm Dir[@path || "doc/state_machines/#{klass.name}_state.png"]
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
context 'DrawingWithIntegerStates' do
|
|
75
|
-
let(:klass) do
|
|
76
|
-
Class.new do
|
|
77
|
-
def self.name
|
|
78
|
-
@name ||= "Vehicle_#{rand(1_000_000)}"
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
let(:machine) { StateMachines::Machine.new(klass, :state_id, initial: :parked) }
|
|
84
|
-
before(:each) do
|
|
85
|
-
machine.event :ignite do
|
|
86
|
-
transition parked: :idling
|
|
87
|
-
end
|
|
88
|
-
machine.state :parked, value: 1
|
|
89
|
-
machine.state :idling, value: 2
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
let!(:graph) { machine.draw }
|
|
93
|
-
|
|
94
|
-
it 'should_draw_all_states' do
|
|
95
|
-
expect(graph.node_count).to eq(3)
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
it 'should_draw_all_events' do
|
|
99
|
-
expect(graph.edge_count).to eq(2)
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
it 'should_draw_machine' do
|
|
103
|
-
expect(File.exist?("doc/state_machines/#{klass.name}_state_id.png")).to be_truthy
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
after(:each) do
|
|
107
|
-
FileUtils.rm Dir["doc/state_machines/#{klass.name}_state_id.png"]
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
context 'DrawingWithNilStates' do
|
|
112
|
-
let(:klass) do
|
|
113
|
-
Class.new do
|
|
114
|
-
def self.name
|
|
115
|
-
@name ||= "Vehicle_#{rand(1_000_000)}"
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
end
|
|
119
|
-
let(:machine) { StateMachines::Machine.new(klass, initial: :parked) }
|
|
120
|
-
|
|
121
|
-
before(:each) do
|
|
122
|
-
machine.event :ignite do
|
|
123
|
-
transition parked: :idling
|
|
124
|
-
end
|
|
125
|
-
machine.state :parked, value: nil
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
let!(:graph) { machine.draw }
|
|
129
|
-
|
|
130
|
-
it 'should_draw_all_states' do
|
|
131
|
-
expect(graph.node_count).to eq(3)
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
it 'should_draw_all_events' do
|
|
135
|
-
expect(graph.edge_count).to eq(2)
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
it 'should_draw_machine' do
|
|
139
|
-
expect(File.exist?("doc/state_machines/#{klass.name}_state.png")).to be_truthy
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
after(:each) do
|
|
143
|
-
FileUtils.rm Dir["doc/state_machines/#{klass.name}_state.png"]
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
context 'DrawingWithDynamicStates' do
|
|
148
|
-
let(:klass) do
|
|
149
|
-
Class.new do
|
|
150
|
-
def self.name
|
|
151
|
-
@name ||= "Vehicle_#{rand(1_000_000)}"
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
let(:machine) { StateMachines::Machine.new(klass, initial: :parked) }
|
|
157
|
-
|
|
158
|
-
before(:each) do
|
|
159
|
-
machine.event :activate do
|
|
160
|
-
transition parked: :idling
|
|
161
|
-
end
|
|
162
|
-
machine.state :idling, value: lambda { Time.now }
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
let!(:graph) { machine.draw }
|
|
166
|
-
|
|
167
|
-
it 'should_draw_all_states' do
|
|
168
|
-
expect(graph.node_count).to eq(3)
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
it 'should_draw_all_events' do
|
|
172
|
-
expect(graph.edge_count).to eq(2)
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
it 'should_draw_machine' do
|
|
176
|
-
expect(File.exist?("doc/state_machines/#{klass.name}_state.png")).to be_truthy
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
after(:each) do
|
|
180
|
-
FileUtils.rm Dir["doc/state_machines/#{klass.name}_state.png"]
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
context 'ClassDrawing' do
|
|
186
|
-
before(:each) do
|
|
187
|
-
klass = Class.new do
|
|
188
|
-
def self.name
|
|
189
|
-
@name ||= "Vehicle_#{rand(1_000_000)}"
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
machine = StateMachines::Machine.new(klass)
|
|
193
|
-
machine.event :ignite do
|
|
194
|
-
transition parked: :idling
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
it 'should_raise_exception_if_no_class_names_specified' do
|
|
199
|
-
expect { StateMachines::Machine.draw(nil) }.to raise_error(ArgumentError)
|
|
200
|
-
# FixMe
|
|
201
|
-
# assert_equal 'At least one class must be specified', exception.message
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
it 'should_load_files' do
|
|
205
|
-
StateMachines::Machine.draw('Switch', file: File.expand_path("#{File.dirname(__FILE__)}/files/switch.rb"))
|
|
206
|
-
expect(defined?(::Switch)).to be_truthy
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
it 'should_allow_path_and_format_to_be_customized' do
|
|
210
|
-
StateMachines::Machine.draw('Switch', file: File.expand_path("#{File.dirname(__FILE__)}/files/switch.rb"), path: "#{File.dirname(__FILE__)}/", format: 'jpg')
|
|
211
|
-
expect(File.exist?("#{File.dirname(__FILE__)}/#{Switch.name}_state.jpg")).to be_truthy
|
|
212
|
-
FileUtils.rm Dir["{.,#{File.dirname(__FILE__)}}/#{Switch.name}_state.{jpg,png}"]
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
end
|
data/spec/machine_spec.rb
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
describe StateMachines::Machine do
|
|
2
|
-
before(:each) do
|
|
3
|
-
klass = Class.new do
|
|
4
|
-
def self.name
|
|
5
|
-
@name ||= "Vehicle_#{rand(1_000_000)}"
|
|
6
|
-
end
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
@machine = StateMachines::Machine.new(klass, initial: :parked)
|
|
10
|
-
@machine.event :ignite do
|
|
11
|
-
transition parked: :idling
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it 'should not raise exception' do
|
|
16
|
-
expect { @machine.draw }.not_to raise_error
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
end
|
data/spec/spec_helper.rb
DELETED
data/spec/state_spec.rb
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
context 'Drawing' do
|
|
2
|
-
before(:each) do
|
|
3
|
-
@machine = StateMachines::Machine.new(Class.new)
|
|
4
|
-
@machine.states << @state = StateMachines::State.new(@machine, :parked, :value => 1)
|
|
5
|
-
@machine.event :ignite do
|
|
6
|
-
transition :parked => :idling
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
graph = StateMachines::Graph.new('test')
|
|
10
|
-
@state.draw(graph)
|
|
11
|
-
@node = graph.get_node('parked')
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it 'should_use_ellipse_shape' do
|
|
15
|
-
expect(@node['shape'].to_s.gsub('"', '')).to eq('ellipse')
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'should_set_width_to_one' do
|
|
19
|
-
expect('1').to eq(@node['width'].to_s.gsub('"', ''))
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'should_set_height_to_one' do
|
|
23
|
-
expect('1').to eq(@node['height'].to_s.gsub('"', ''))
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it 'should_use_description_as_label' do
|
|
27
|
-
expect('parked (1)').to eq(@node['label'].to_s.gsub('"', ''))
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
context 'DrawingInitial' do
|
|
32
|
-
before(:each) do
|
|
33
|
-
@machine = StateMachines::Machine.new(Class.new)
|
|
34
|
-
@machine.states << @state = StateMachines::State.new(@machine, :parked, :initial => true)
|
|
35
|
-
@machine.event :ignite do
|
|
36
|
-
transition :parked => :idling
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
@graph = StateMachines::Graph.new('test')
|
|
40
|
-
@state.draw(@graph)
|
|
41
|
-
@node = @graph.get_node('parked')
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'should_use_ellipse_as_shape' do
|
|
45
|
-
expect('ellipse').to eq(@node['shape'].to_s.gsub('"', ''))
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
it 'should_draw_edge_between_point_and_state' do
|
|
49
|
-
expect(2).to eq(@graph.node_count)
|
|
50
|
-
expect(1).to eq(@graph.edge_count)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
context 'DrawingNilName' do
|
|
55
|
-
before(:each) do
|
|
56
|
-
@machine = StateMachines::Machine.new(Class.new)
|
|
57
|
-
@machine.states << @state = StateMachines::State.new(@machine, nil)
|
|
58
|
-
|
|
59
|
-
graph = StateMachines::Graph.new('test')
|
|
60
|
-
@state.draw(graph)
|
|
61
|
-
@node = graph.get_node('nil')
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
it 'should_have_a_node' do
|
|
65
|
-
expect(@node).to be_truthy
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it 'should_use_description_as_label' do
|
|
69
|
-
expect('nil').to eq(@node['label'].to_s.gsub('"', ''))
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
context 'DrawingLambdaValue' do
|
|
74
|
-
before(:each) do
|
|
75
|
-
@machine = StateMachines::Machine.new(Class.new)
|
|
76
|
-
@machine.states << @state = StateMachines::State.new(@machine, :parked, :value => lambda {})
|
|
77
|
-
|
|
78
|
-
graph = StateMachines::Graph.new('test')
|
|
79
|
-
@state.draw(graph)
|
|
80
|
-
@node = graph.get_node('parked')
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
it 'should_have_a_node' do
|
|
84
|
-
expect(@node).to be_truthy
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
it 'should_use_description_as_label' do
|
|
88
|
-
expect('parked (*)').to eq(@node['label'].to_s.gsub('"', ''))
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
context 'DrawingNonFinal' do
|
|
93
|
-
before(:each) do
|
|
94
|
-
@machine = StateMachines::Machine.new(Class.new)
|
|
95
|
-
@machine.states << @state = StateMachines::State.new(@machine, :parked)
|
|
96
|
-
@machine.event :ignite do
|
|
97
|
-
transition :parked => :idling
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
graph = StateMachines::Graph.new('test')
|
|
101
|
-
@state.draw(graph)
|
|
102
|
-
@node = graph.get_node('parked')
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
it 'should_use_ellipse_as_shape' do
|
|
106
|
-
expect('ellipse').to eq(@node['shape'].to_s.gsub('"', ''))
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
context 'DrawingFinal' do
|
|
111
|
-
before(:each) do
|
|
112
|
-
@machine = StateMachines::Machine.new(Class.new)
|
|
113
|
-
@machine.states << @state = StateMachines::State.new(@machine, :parked)
|
|
114
|
-
|
|
115
|
-
graph = StateMachines::Graph.new('test')
|
|
116
|
-
@state.draw(graph)
|
|
117
|
-
@node = graph.get_node('parked')
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
it 'should_use_doublecircle_as_shape' do
|
|
121
|
-
expect('doublecircle').to eq(@node['shape'].to_s.gsub('"', ''))
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
context 'DrawingWithHumanName' do
|
|
126
|
-
before(:each) do
|
|
127
|
-
@machine = StateMachines::Machine.new(Class.new)
|
|
128
|
-
@machine.states << @state = StateMachines::State.new(@machine, :parked, :human_name => 'Parked')
|
|
129
|
-
@machine.event :ignite do
|
|
130
|
-
transition :parked => :idling
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
graph = StateMachines::Graph.new('test')
|
|
134
|
-
@state.draw(graph, :human_name => true)
|
|
135
|
-
@node = graph.get_node('parked')
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
it 'should_use_description_with_human_name_as_label' do
|
|
139
|
-
expect('Parked').to eq(@node['label'].to_s.gsub('"', ''))
|
|
140
|
-
end
|
|
141
|
-
end
|
|
142
|
-
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require 'state_machines/graphviz/version'
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |spec|
|
|
7
|
-
spec.name = 'state_machines-graphviz'
|
|
8
|
-
spec.version = StateMachines::Graphviz::VERSION
|
|
9
|
-
spec.authors = ['Abdelkader Boudih', 'Aaron Pfeifer']
|
|
10
|
-
spec.email = ['terminale@gmail.com']
|
|
11
|
-
spec.summary = %q(Drawing module for state machines)
|
|
12
|
-
spec.description = %q(Graphviz module for state machines)
|
|
13
|
-
spec.homepage = 'https://github.com/seuros/state_machines-graphviz'
|
|
14
|
-
spec.license = 'MIT'
|
|
15
|
-
|
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
|
17
|
-
spec.test_files = spec.files.grep(%r{/^spec\//})
|
|
18
|
-
spec.require_paths = ['lib']
|
|
19
|
-
|
|
20
|
-
spec.add_dependency 'state_machines'
|
|
21
|
-
spec.add_dependency 'ruby-graphviz'
|
|
22
|
-
spec.add_development_dependency 'bundler'
|
|
23
|
-
spec.add_development_dependency 'rake'
|
|
24
|
-
spec.add_development_dependency 'rspec' , '>=3.0.0'
|
|
25
|
-
end
|