Class: PetriNet::ReachabilityGraph

Inherits:
Base
  • Object
show all
Defined in:
lib/petri_net/reachability_graph.rb

Defined Under Namespace

Classes: Edge, Node

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#next_object_id, #reset

Constructor Details

#initialize(net) ⇒ ReachabilityGraph



2
3
4
5
6
7
# File 'lib/petri_net/reachability_graph.rb', line 2

def initialize(net)
    @objects = Array.new
    @nodes = Hash.new
    @edges = Hash.new
    @name = net.name
end

Instance Method Details

#<<(object) ⇒ Object Also known as: add_object

Add an object to the Petri Net.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/petri_net/reachability_graph.rb', line 35

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



24
25
26
27
28
29
30
31
32
# File 'lib/petri_net/reachability_graph.rb', line 24

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/petri_net/reachability_graph.rb', line 9

def add_node(node)
    node_index = @objects.index node
    if (!node_index.nil?)
        return @objects[node_index].id * -1
    end

    if (node.validate && (!@nodes.include? node.name))
        @objects[node.id] = node
        @nodes[node.name] = node.id
        node.graph = self
        return node.id
    end
    return false
end

#to_gvObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/petri_net/reachability_graph.rb', line 50

def to_gv
    # General graph options
    str = "digraph #{@name} {\n"
    str += "\t// General graph options\n"
    str += "\trankdir = LR;\n"
    str += "\tsize = \"10.5,7.5\";\n"
    str += "\tnode [ style = filled, fillcolor = white, fontsize = 8.0 ]\n"
    str += "\tedge [ arrowhead = vee, arrowsize = 0.5, fontsize = 8.0 ]\n"
    str += "\n"

    str += "\t// Nodes\n"
    str += "\tnode [ shape = circle ];\n"
    @nodes.each_value {|id| str += @objects[id].to_gv }
    str += "\n"

    str += "\t// Edges\n"
    @edges.each_value {|id| str += @objects[id].to_gv }
    str += "}\n"    # Graph closure

    return str

end

#to_sObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/petri_net/reachability_graph.rb', line 73

def to_s
    str = "Reachability 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