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



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

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



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

def arcs
  @arcs
end

#descriptionObject

Description



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

def description
  @description
end

#filenameObject

Storage filename



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

def filename
  @filename
end

#markingsObject (readonly)

List of markings !depricated!



18
19
20
# File 'lib/petri_net/net.rb', line 18

def markings
  @markings
end

#nameObject

Human readable name



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

def name
  @name
end

#placesObject (readonly)

List of places



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

def places
  @places
end

#transitionsObject (readonly)

List of transitions



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

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



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

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



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

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



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

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



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

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

#fire(transition) ⇒ Object



297
298
299
# File 'lib/petri_net/net.rb', line 297

def fire transition
    get_transition(transition).fire
end

#generate_reachability_graph(unlimited = true) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/petri_net/net.rb', line 215

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



226
227
228
229
230
231
232
233
234
# File 'lib/petri_net/net.rb', line 226

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



138
139
140
141
# File 'lib/petri_net/net.rb', line 138

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

#get_markingsObject



256
257
258
# File 'lib/petri_net/net.rb', line 256

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

#get_object(id) ⇒ Object



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

def get_object(id)
    @objects[id]
end

#get_objectsObject



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

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



124
125
126
127
# File 'lib/petri_net/net.rb', line 124

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



131
132
133
134
# File 'lib/petri_net/net.rb', line 131

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

#load(filename) ⇒ Object



293
294
295
# File 'lib/petri_net/net.rb', line 293

def load filename
    @net = YAML.load(File.read(filename))
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!



208
209
210
211
212
213
# File 'lib/petri_net/net.rb', line 208

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



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

def objects_find_index(object)
    @objects.find_index object
end

#objects_include?(object) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#objects_sizeObject



269
270
271
# File 'lib/petri_net/net.rb', line 269

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)


151
152
153
# File 'lib/petri_net/net.rb', line 151

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)


145
146
147
# File 'lib/petri_net/net.rb', line 145

def pure?
    raise "Not implemented yet"
end

#save(filename) ⇒ Object



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

def save filename
    File.open(filename, 'w') {|f| @net.to_yaml}
end

#set_markings(markings) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/petri_net/net.rb', line 260

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.



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

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.



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

def to_s
    str = 
%{Petri Net [#{@name}]
----------------------------
Description: #{@description}
Filename: #{@filename}

Places
----------------------------
#{str = ''; @places.each_value {|p| str += @objects[p].to_s + "\n"}; str }
Transitions
----------------------------
#{str = ''; @transitions.each_value {|t| str += @objects[t].to_s + "\n" }; str }
Arcs
----------------------------
#{str = ''; @arcs.each_value {|a| str += @objects[a].to_s + "\n" }; str}
}
    return str
end

#updateObject



241
242
243
244
# File 'lib/petri_net/net.rb', line 241

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)


247
248
249
250
251
252
253
# File 'lib/petri_net/net.rb', line 247

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



236
237
238
239
# File 'lib/petri_net/net.rb', line 236

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