Module: YPetri::Net::Visualization

Defined in:
lib/y_petri/net/visualization.rb

Overview

Own visualization capabilities of a Petri net.

Instance Method Summary collapse

Instance Method Details

#visualizeObject

Visualizes the net with Graphviz.


8
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/y_petri/net/visualization.rb', line 8

def visualize
  require 'graphviz'
  γ = GraphViz.new :G  # Add places and transitions.

  place_nodes = places.map.with_object Hash.new do |pl, |
    [pl] = γ.add_nodes pl.name.to_s,
                        fillcolor: 'lightgrey',
                        color: 'grey',
                        style: 'filled'
  end
  transition_nodes = transitions.map.with_object Hash.new do |tr, |
    [tr] = γ.add_nodes tr.name.to_s,
                        shape: 'box',
                        fillcolor: ( if tr.assignment? then 'yellow'
                                     elsif tr.type == :TS then 'lightcyan'
                                     else 'ghostwhite' end ),
                        color: ( if tr.assignment? then 'goldenrod'
                                 elsif tr.type == :TS then 'cyan'
                                 else 'grey' end ),
                        style: 'filled'
  end  # Add Petri net arcs.

  transition_nodes.each { |tr, tr_node|
    if tr.assignment? then
      tr.codomain.each { |pl|
        γ.add_edges tr_node, place_nodes[pl], color: 'goldenrod'
      }
      ( tr.domain - tr.codomain ).each { |pl|
        γ.add_edges tr_node, place_nodes[pl], color: 'grey', arrowhead: 'none'
      }
    elsif tr.type == :TS then
      tr.codomain.each { |pl|
        if tr.stoichio[pl] > 0 then # producing arc
          γ.add_edges tr_node, place_nodes[pl], color: 'cyan'
        elsif tr.stoichio[pl] < 0 then # consuming arc
          γ.add_edges place_nodes[ pl ], tr_node, color: 'cyan'
        else # zero stoichiometry => test arc
          γ.add_edges place_nodes[ pl ], tr_node, color: 'grey', arrowhead: 'none'
        end
      }
      ( tr.domain - tr.codomain ).each { |pl| # remaining test arcs
        γ.add_edges tr_node, place_nodes[pl], color: 'grey', arrowhead: 'none'
      }
    elsif tr.S?
      tr.codomain.each { |pl|
        if tr.stoichio[pl] > 0 then # producing arc
          γ.add_edges tr_node, place_nodes[pl], color: 'cyan'
        elsif tr.stoichio[pl] < 0 then # consuming arc
          γ.add_edges place_nodes[ pl ], tr_node, color: 'cyan'
        else # zero stoichiometry => test arc
          γ.add_edges place_nodes[ pl ], tr_node, color: 'grey', arrowhead: 'none'
        end
      }
      ( tr.domain - tr.codomain ).each { |pl| # remaining test arcs
        γ.add_edges tr_node, place_nodes[pl], color: 'grey', arrowhead: 'none'
      }
    else
      tr.codomain.each { |pl|
        γ.add_edges tr_node, place_nodes[pl], color: 'cyan'
      }
      ( tr.domain - tr.codomain ).each { |pl|
        γ.add_edges tr_node, place_nodes[pl], color: 'grey', arrowhead: 'none'
      }
    end
  }  # Generate output image.


  # Note: The following code is good on Linux systems only
  # puts File.expand_path "~/y_petri_graph.png"
  # γ.output png: File.expand_path( "~/y_petri_graph.png" )

  γ.output png: 'y_petri_graph.png'

  # The last line only works on KDE systems.
  show_file_with_kioclient File.expand_path( "~/y_petri_graph.png" )
end