Class: Visualizer
- Inherits:
-
Object
- Object
- Visualizer
- Defined in:
- lib/resyma/program/automaton.rb,
lib/resyma/program/parsetree.rb
Direct Known Subclasses
Instance Method Summary collapse
- #def_node(parsetree) ⇒ Object
- #def_state(state) ⇒ Object
-
#initialize ⇒ Visualizer
constructor
A new instance of Visualizer.
- #label_of(parsetree) ⇒ Object
- #node_of(state) ⇒ Object
- #output(filename) ⇒ Object
- #shorten(str, limit = 10) ⇒ Object
- #viz!(parsetree, node = nil) ⇒ Object
- #viz_matcher(node_matcher) ⇒ Object
Constructor Details
#initialize ⇒ Visualizer
Returns a new instance of Visualizer.
7 8 9 10 11 12 13 14 |
# File 'lib/resyma/program/automaton.rb', line 7 def initialize(automaton) @viz = GraphViz.new(:G, type: :graph) @viz[:rankdir] = "LR" @id_pool = 0 # @type [Resyma::Core::Automaton] @automaton = automaton @node_cache = {} end |
Instance Method Details
#def_node(parsetree) ⇒ Object
30 31 32 33 34 |
# File 'lib/resyma/program/parsetree.rb', line 30 def def_node(parsetree) @id_pool += 1 label = label_of(parsetree) @viz.add_node(@id_pool.to_s, label: label) end |
#def_state(state) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/resyma/program/automaton.rb', line 16 def def_state(state) @id_pool += 1 label = state.id.to_s shape = @automaton.accept?(state) ? "doublecircle" : "circle" @viz.add_node(@id_pool.to_s, label: label, shape: shape) end |
#label_of(parsetree) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/resyma/program/parsetree.rb', line 21 def label_of(parsetree) label = parsetree.symbol if parsetree.leaf? "#{label}(#{shorten(parsetree.children.first)})" else label end end |
#node_of(state) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/resyma/program/automaton.rb', line 32 def node_of(state) node = @node_cache[state] return node if node node = def_state(state) @node_cache[state] = node node end |
#output(filename) ⇒ Object
59 60 61 |
# File 'lib/resyma/program/automaton.rb', line 59 def output(filename) @viz.output(png: filename) end |
#shorten(str, limit = 10) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/resyma/program/automaton.rb', line 23 def shorten(str, limit = 10) str = str.to_s if str.length > limit str[0...limit] + "..." else str end end |
#viz!(parsetree, node = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/resyma/program/parsetree.rb', line 39 def viz! @automaton.transition_table.table.each do |src, value| src_node = node_of src value.each do |can| node_matcher = can.condition dest = can.destination dest_node = node_of(dest) @viz.add_edge(src_node, dest_node, { label: viz_matcher(node_matcher) }) end end end |
#viz_matcher(node_matcher) ⇒ Object
41 42 43 44 45 |
# File 'lib/resyma/program/automaton.rb', line 41 def viz_matcher(node_matcher) rez = node_matcher.type.to_s rez += "(#{shorten(node_matcher.value)})" if node_matcher.value rez end |