class PetriNet::Place
Attributes
capacity[RW]
Token capacity
description[RW]
description
id[RW]
Unique ID
inputs[R]
List of input-arcs
markings[R]
Current token
name[RW]
Human readable name
net[W]
The net this place belongs to
outputs[R]
List of output-arcs
Public Class Methods
new(options = {}) { |self| ... }
click to toggle source
Initialize a new place. Supports block configuration.
# File lib/petri_net/place.rb, line 24 def initialize(options = {}, &block) @id = next_object_id @name = (options[:name] || "Place#{@id}") @description = (options[:description] || "Place #{@id}") @capacity = options[:capacity].nil? ? Float::INFINITY : options[:capacity] @inputs = [] @outputs = [] @markings = [] yield self unless block.nil? end
Public Instance Methods
==(object)
click to toggle source
# File lib/petri_net/place.rb, line 112 def ==(object) return true if name == object.name && description = object.description end
add_input(arc)
click to toggle source
Add an input arc
# File lib/petri_net/place.rb, line 37 def add_input(arc) @inputs << arc.id unless arc.nil? || !validate_input(arc) end
add_marking(count = 1)
click to toggle source
# File lib/petri_net/place.rb, line 46 def add_marking(count = 1) if count <= @capacity count.times do @markings << PetriNet::Marking.new end true else raise 'Tried to add more markings than possible' end end
Also aliased as: +
add_output(arc)
click to toggle source
Add an output arc
# File lib/petri_net/place.rb, line 42 def add_output(arc) @outputs << arc.id unless arc.nil? || !validate_input(arc) end
gv_id()
click to toggle source
GraphViz ID
# File lib/petri_net/place.rb, line 75 def gv_id "P#{@id}" end
posttransitions()
click to toggle source
# File lib/petri_net/place.rb, line 96 def posttransitions raise 'Not part of a net' if @net.nil? outputs.map { |o| @net.objects[o].source } end
pretransitions()
click to toggle source
# File lib/petri_net/place.rb, line 89 def pretransitions raise 'Not part of a net' if @net.nil? transitions = [] places << inputs.map { |i| @net.objects[i].source } end
remove_marking(count = 1)
click to toggle source
# File lib/petri_net/place.rb, line 64 def remove_marking(count = 1) if @markings.size >= count ret = @markings.pop(count) return ret unless ret.nil? else raise 'Tried to remove more markings that possible' end end
Also aliased as: -
set_marking(count)
click to toggle source
# File lib/petri_net/place.rb, line 57 def set_marking(count) @markings = [] add_marking count end
to_gv()
click to toggle source
GraphViz definition
# File lib/petri_net/place.rb, line 108 def to_gv "\t#{gv_id} [ label = \"#{@name} #{@markings.size} \" ];\n" end
to_s()
click to toggle source
Stringify this place.
# File lib/petri_net/place.rb, line 103 def to_s "#{@id}: #{@name} (#{@capacity.nil? ? -1 : 0}) #{'*' * @markings.length}" end
validate()
click to toggle source
Validate the setup of this place.
# File lib/petri_net/place.rb, line 80 def validate return false if @id.nil? || (@id < 0) return false if @name.nil? || (@name.strip.length <= 0) return false if @description.nil? || (@description.strip.length <= 0) return false if @capacity.nil? || (@capacity < -1) true end
Private Instance Methods
validate_input(arc)
click to toggle source
# File lib/petri_net/place.rb, line 118 def validate_input(arc) inputs.each do |a| return false if (@net.get_objects[a] <=> arc) == 0 end true end
validate_output(arc)
click to toggle source
# File lib/petri_net/place.rb, line 125 def validate_output(arc) outputs.each do |a| return false if (@net.get_objects[a] <=> arc) == 0 end true end