Class: PetriNet::Net

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

Instance Attribute Summary collapse

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#next_object_id, #reset

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Net

Create new Petri Net definition.

options may be

  • name used as a human usable identifier (defaults to ‘petri_net’)

  • filename (defaults to the name)

  • description (defaults to ‘Petri Net’)

Accepts a block and yields itself

Yields:

  • (_self)

Yield Parameters:

  • _self (PetriNet::Net)

    the object that the method was called on



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/petri_net/net.rb', line 30

def initialize(options = {}, &block)
    @name = (options[:name] or 'petri_net')
    @filename = (options[:filename] or @name)
    @description = (options[:description] or 'Petri Net')
    @places = Hash.new
    @arcs = Hash.new
    @transitions = Hash.new
    @markings = Hash.new
    @objects = Array.new
    @up_to_date = false
    @w_up_to_date = false

    yield self unless block == nil
end

Instance Attribute Details

#arcsObject (readonly)

List of arcs



11
12
13
# File 'lib/petri_net/net.rb', line 11

def arcs
  @arcs
end

#descriptionObject

Description



7
8
9
# File 'lib/petri_net/net.rb', line 7

def description
  @description
end

#filenameObject

Storage filename



5
6
7
# File 'lib/petri_net/net.rb', line 5

def filename
  @filename
end

#markingsObject (readonly)

List of markings !depricated!



16
17
18
# File 'lib/petri_net/net.rb', line 16

def markings
  @markings
end

#nameObject

Human readable name



3
4
5
# File 'lib/petri_net/net.rb', line 3

def name
  @name
end

#placesObject (readonly)

List of places



9
10
11
# File 'lib/petri_net/net.rb', line 9

def places
  @places
end

#transitionsObject (readonly)

List of transitions



13
14
15
# File 'lib/petri_net/net.rb', line 13

def transitions
  @transitions
end

Instance Method Details

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

Adds an object to the Petri Net. You can add

  • PetriNet::Place

  • PetriNet::Arc

  • PetriNet::Transition

  • Array of these

The Objects are added by PetriNet::Net#add_place, PetriNet::Net#add_arc and PetriNet::Net#add_transition, refer to these to get more information on how they are added raises an RuntimeError if a wring Type is given

returns itself



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/petri_net/net.rb', line 56

def <<(object)
    return if object.nil?  #TODO WORKAROUND There should never be a nil here, even while merging.
    case object.class.to_s
    when "Array"
        object.each {|o| self << o}
    when "PetriNet::Place" 
        add_place(object)
    when "PetriNet::Arc" 
        add_arc(object)
    when "PetriNet::Transition" 
        add_transition(object)
    else 
        raise "(PetriNet) Unknown object #{object.class}."
    end
    self
end

#add_arc(arc) ⇒ Object

Add an arc to the list of arcs.

see PetriNet::Net#add_place



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/petri_net/net.rb', line 92

def add_arc(arc)
    if (arc.validate self) && !@arcs.include?(arc.name)
        if arc.need_update? self
            arc.update self
        end
        @arcs[arc.name] = arc.id
        @objects[arc.id] = arc
        arc.net = self
        return arc.id
    end
    changed_structure
    return false
end

#add_place(place) ⇒ Object

Adds a place to the list of places. Adds the place only if the place is valid and unique in the objects-list of the net

This Method changes the structure of the PetriNet, you will have to recalculate all cached functions



78
79
80
81
82
83
84
85
86
87
# File 'lib/petri_net/net.rb', line 78

def add_place(place)
    if place.validate && !@places.include?(place.name) 
        @places[place.name] = place.id
        @objects[place.id] = place
        place.net = self
        return place.id
    end
    changed_structure
    return false
end

#add_transition(transition) ⇒ Object

Add a transition to the list of transitions.

see PetriNet::Net#add_place



109
110
111
112
113
114
115
116
117
118
# File 'lib/petri_net/net.rb', line 109

def add_transition(transition)
    if transition.validate && !@transitions.include?(transition.name)
        @transitions[transition.name] = transition.id
        @objects[transition.id] = transition
        transition.net = self
        return transition.id
    end
    changed_structure
    return false
end

#generate_reachability_graph(unlimited = true) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/petri_net/net.rb', line 218

def generate_reachability_graph(unlimited = true)
    raise "Not implemented yet" unless unlimited
    startmarkings = get_markings
    @graph = PetriNet::ReachabilityGraph.new(self)
    @graph.add_node current_node = PetriNet::ReachabilityGraph::Node.new(markings: get_markings)

    reachability_helper startmarkings, current_node

    set_markings startmarkings
    @graph 
end

#generate_weight_functionObject



229
230
231
232
233
234
235
236
237
# File 'lib/petri_net/net.rb', line 229

def generate_weight_function
    @weight = Hash.new
    @arcs.each_value do |id|
        arc = @objects[id]
        @weight[[arc.source.id,arc.destination.id]] = arc.weight
    end
    @w_up_to_date = true
    @weight
end

#get_arc(name) ⇒ Object

returns the arc refered by the given name or false if there is no arc with this name



136
137
138
139
# File 'lib/petri_net/net.rb', line 136

def get_arc(name)
    arc = @objects[@arcs[name]]
    arc.nil? ? false : arc
end

#get_markingsObject



259
260
261
# File 'lib/petri_net/net.rb', line 259

def get_markings
    @places.map{|key,pid| @objects[pid].markings.size}
end

#get_object(id) ⇒ Object



280
281
282
# File 'lib/petri_net/net.rb', line 280

def get_object(id)
    @objects[id]
end

#get_objectsObject



284
285
286
# File 'lib/petri_net/net.rb', line 284

def get_objects
    @objects.clone
end

#get_place(name) ⇒ Object

Returns the place refered by the given name or false if there is no place with this name



122
123
124
125
# File 'lib/petri_net/net.rb', line 122

def get_place(name)
    place = @objects[@places[name]]
    place.nil? ? false : place
end

#get_transition(name) ⇒ Object

Returns the transition refered by the given name or false if there is no transition with this name



129
130
131
132
# File 'lib/petri_net/net.rb', line 129

def get_transition(name)
    trans = @objects[@transitions[name]]
    trans.nil? ? false : trans
end

#merge(net) ⇒ Object

Merges two PetriNets Places, transitions and arcs are equal if they have the same name and description, arcs need to have the same source and destination too). With this definition of equality the resultung net will have unique ojects. ATTENTION conflicting capabilities and weights will be lost and the properies of the net you merge to will be used in future #TODO add a parameter to affect this!



211
212
213
214
215
216
# File 'lib/petri_net/net.rb', line 211

def merge(net)
    return self if self.equal? net
    return false if net.class.to_s != "PetriNet::Net"
    self << net.get_objects
    self
end

#objects_find_index(object) ⇒ Object



288
289
290
# File 'lib/petri_net/net.rb', line 288

def objects_find_index(object)
    @objects.find_index object
end

#objects_include?(object) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/petri_net/net.rb', line 276

def objects_include?(object)
    @objects.include?(object)
end

#objects_sizeObject



272
273
274
# File 'lib/petri_net/net.rb', line 272

def objects_size
    @objects.count{|o| !o.nil?}
end

#ordinary?Boolean

Is this Petri Net ordinary? A Petri Net is said to be ordinary if all of its arc weights are 1’s.

Returns:

  • (Boolean)


149
150
151
# File 'lib/petri_net/net.rb', line 149

def ordinary?
    raise "Not implemented yet"
end

#pure?Boolean

Is this Petri Net pure? A Petri Net is said to be pure if it has no self-loops.

Returns:

  • (Boolean)


143
144
145
# File 'lib/petri_net/net.rb', line 143

def pure?
    raise "Not implemented yet"
end

#set_markings(markings) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/petri_net/net.rb', line 263

def set_markings(markings)
    i = 0
    @places.each_value do |pid| 
        @objects[pid].set_marking markings[i]
        i = i+1
    end
    changed_state
end

#to_gvObject

Generate GraphViz dot string.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/petri_net/net.rb', line 180

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// Places\n"
    str += "\tnode [ shape = circle ];\n"
    @places.each_value {|id| str += @objects[id].to_gv }
    str += "\n"

    str += "\t// Transitions\n"
    str += "\tnode [ shape = box, fillcolor = grey90 ];\n"
    @transitions.each_value {|id| str += @objects[id].to_gv }
    str += "\n"

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

    return str
end

#to_sObject

Stringify this Petri Net.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/petri_net/net.rb', line 154

def to_s
    str = "Petri Net [#{@name}]\n"
    str += "----------------------------\n"
    str += "Description: #{@description}\n"
    str += "Filename: #{@filename}\n"
    str += "\n"

    str += "Places\n"
    str += "----------------------------\n"
    @places.each_value {|p| str += @objects[p].to_s + "\n" }
    str += "\n"

    str += "Transitions\n"
    str += "----------------------------\n"
    @transitions.each_value {|t| str += @objects[t].to_s + "\n" }
    str += "\n"

    str += "Arcs\n"
    str += "----------------------------\n"
    @arcs.each_value {|a| str += @objects[a].to_s + "\n"}
    str += "\n"

    return str
end

#updateObject



244
245
246
247
# File 'lib/petri_net/net.rb', line 244

def update
    generate_weight_function
    @up_to_date = true
end

#update?Boolean Also known as: up_to_date

is true if, and only if, the cached elements are calculated AND the net hasn’t changed

Returns:

  • (Boolean)


250
251
252
253
254
255
256
# File 'lib/petri_net/net.rb', line 250

def update?
    if @w_up_to_date && true #all up_to_date-caches!!!
        @up_to_date = true
        return @up_to_date
    end
    false
end

#w0(x, y) ⇒ Object



239
240
241
242
# File 'lib/petri_net/net.rb', line 239

def w0(x,y)
    generate_weight_function unless @w_up_to_date
    return @weight[[x,y]].nil? ? 0 : @weight[[x,y]]
end