Class: PetriNet::ReachabilityGraph

Inherits:
Base
  • Object
show all
Defined in:
lib/petri_net/reachability_graph.rb,
lib/petri_net/reachability_graph/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, options = Hash.new) ⇒ ReachabilityGraph

Returns a new instance of ReachabilityGraph.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/petri_net/reachability_graph/graph.rb', line 5

def initialize(net, options = Hash.new)
    @objects = Array.new
    @nodes = Hash.new
    @edges = Hash.new
    @name = net.name
    if options['unlimited'].nil? 
        @unlimited = true 
    else 
        @unlimited = options['unlimited']
    end
end

Instance Method Details

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

Add an object to the Petri Net.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/petri_net/reachability_graph/graph.rb', line 60

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



49
50
51
52
53
54
55
56
57
# File 'lib/petri_net/reachability_graph/graph.rb', line 49

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



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
# File 'lib/petri_net/reachability_graph/graph.rb', line 17

def add_node(node)
    double = false
    @nodes.each_value do |n|
        begin
            if node > @objects[n]
                if @unlimited
                    double = n
                    break
                    #return @objects[n].id *-1
                else
                    raise PetriNet::ReachabilityGraph::InfiniteReachabilityGraphError
                end
            end
        rescue ArgumentError
            #just two different markings, completly ok
        end
    end
    return (@objects[double].id * -1) if double
    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

#get_node(id) ⇒ Object



75
76
77
# File 'lib/petri_net/reachability_graph/graph.rb', line 75

def get_node(id)
    return @objects[id]
end

#to_gvObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/petri_net/reachability_graph/graph.rb', line 79

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/petri_net/reachability_graph/graph.rb', line 102

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