Class: PetriNet::Graph
- Defined in:
- lib/petri_net/graph.rb,
lib/petri_net/graph/graph.rb
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#net ⇒ Object
readonly
The PetriNet this graph belongs to.
Attributes inherited from Base
Instance Method Summary collapse
-
#<<(object) ⇒ Object
(also: #add_object)
Add an object to the Petri Net.
- #add_edge(edge) ⇒ Object
- #add_node(node) ⇒ Object (also: #add_node!)
- #generate_gv ⇒ Object
- #get_node(id) ⇒ Object
- #get_nodes ⇒ Object
-
#initialize(net, options = Hash.new) ⇒ Graph
constructor
A new instance of Graph.
- #to_gv(output = 'png', filename = '') ⇒ Object
- #to_s ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(net, options = Hash.new) ⇒ Graph
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/petri_net/graph/graph.rb', line 11 def initialize(net, = Hash.new) @net = net @objects = Array.new @nodes = Hash.new @edges = Hash.new @name = net.name @type = "Reachability" if ['unlimited'].nil? @unlimited = true else @unlimited = ['unlimited'] end end |
Instance Attribute Details
#net ⇒ Object (readonly)
The PetriNet this graph belongs to
9 10 11 |
# File 'lib/petri_net/graph/graph.rb', line 9 def net @net end |
Instance Method Details
#<<(object) ⇒ Object Also known as: add_object
Add an object to the Petri Net.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/petri_net/graph/graph.rb', line 47 def <<(object) case object.class.to_s when "Array" object.each {|o| self << o} when "PetriNet::ReachabilityGraph::Edge" add_edge(object) when "PetriNet::ReachabilityGraph::Node" add_node(object) else raise "(PetriNet::ReachabilityGraph) Unknown object #{object.class}." end self end |
#add_edge(edge) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/petri_net/graph/graph.rb', line 36 def add_edge(edge) if (edge.validate && (!@edges.include? edge.name)) @objects[edge.id] = edge @edges[edge.name] = edge.id edge.graph = self return edge.id end return false end |
#add_node(node) ⇒ Object Also known as: add_node!
25 26 27 28 29 30 31 32 33 |
# File 'lib/petri_net/graph/graph.rb', line 25 def add_node(node) if node.validate && (!@objects.include? node) @objects[node.id] = node @nodes[node.name] = node.id node.graph = self return node.id end return false end |
#generate_gv ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/petri_net/graph/graph.rb', line 83 def generate_gv g = GraphViz.new( :G, :type => :digraph ) @nodes.each_value do |node| gv_node = g.add_nodes( @objects[node].markings.to_s ) gv_node.set do |n| n.label = '*' + @objects[node].markings.to_s + '*' if @objects[node].start end end @edges.each_value do |edge| gv_edge = g.add_edges( @objects[edge].source.markings.to_s, @objects[edge].destination.markings.to_s ) gv_edge.set do |e| e.label = @objects[edge].transition end end g end |
#get_node(id) ⇒ Object
62 63 64 |
# File 'lib/petri_net/graph/graph.rb', line 62 def get_node(id) @objects[id] end |
#get_nodes ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/petri_net/graph/graph.rb', line 66 def get_nodes res = Array.new @nodes.each_value do |n| res << @objects[n] end res end |
#to_gv(output = 'png', filename = '') ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/petri_net/graph/graph.rb', line 74 def to_gv(output = 'png', filename = '') g = generate_gv if filename.empty? filename = "#{@name}_graph.png" end g.output( :png => filename ) if output == 'png' g.output end |
#to_s ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/petri_net/graph/graph.rb', line 101 def to_s str = "#{@type} Graph [#{@name}]\n" str += "----------------------------\n" str += "Description: #{@description}\n" str += "Filename: #{@filename}\n" str += "\n" str += "Nodes\n" str += "----------------------------\n" @nodes.each_value {|p| str += @objects[p].to_s + "\n" } str += "\n" str += "Edges\n" str += "----------------------------\n" @edges.each_value {|t| str += @objects[t].to_s + "\n" } str += "\n" return str end |