class PetriNet::Transition

Transition

Attributes

description[RW]

Description

id[RW]

Unique ID

inputs[R]

List of input-arcs

name[RW]

Huan readable name

net[W]

The net this transition belongs to

outputs[R]

List of output-arcs

probability[RW]

Probability of firing (this moment)

Public Class Methods

new(options = {}) { |self| ... } click to toggle source

Create a new transition.

# File lib/petri_net/transition.rb, line 22
def initialize(options = {}, &block)
  @id = next_object_id
  @name = (options[:name] || "Transition#{@id}")
  @description = (options[:description] || "Transition #{@id}")
  @inputs = []
  @outputs = []
  @probability = options[:probability]

  yield self unless block.nil?
end

Public Instance Methods

==(object) click to toggle source
# File lib/petri_net/transition.rb, line 66
def ==(object)
  name == object.name && description = object.description
end
activate!() click to toggle source
# File lib/petri_net/transition.rb, line 96
def activate!
  @inputs.each do |i|
    source = @net.get_object(i).source
    source.add_marking(@net.get_object(i).weight - source.markings.size)
  end

  # what to do with outputs, if they have a capacity
end
activated?() click to toggle source
# File lib/petri_net/transition.rb, line 83
def activated?
  raise 'Not part of a net' if @net.nil?

  @inputs.each do |i|
    return false if @net.get_object(i).source.markings.size < @net.get_object(i).weight
  end

  @outputs.each do |o|
    return false if @net.get_object(o).destination.markings.size + @net.get_object(o).weight > @net.get_object(o).destination.capacity
  end
end
Also aliased as: firable?
add_input(arc) click to toggle source

Add an input arc

# File lib/petri_net/transition.rb, line 34
def add_input(arc)
  @inputs << arc.id unless arc.nil? || !validate_input(arc)
end
add_output(arc) click to toggle source

Add an output arc

# File lib/petri_net/transition.rb, line 39
def add_output(arc)
  @outputs << arc.id unless arc.nil? || !validate_output(arc)
end
firable?()
Alias for: activated?
fire() click to toggle source
# File lib/petri_net/transition.rb, line 105
def fire
  raise 'Not part of a net' if @net.nil?
  return false unless activated?

  @inputs.each do |i|
    @net.get_object(i).source.remove_marking @net.get_object(i).weight
  end

  @outputs.each do |o|
    @net.get_object(o).destination.add_marking @net.get_object(o).weight
  end
  true
end
gv_id() click to toggle source

GraphViz ID

# File lib/petri_net/transition.rb, line 44
def gv_id
  "T#{@id}"
end
postplaces() click to toggle source
# File lib/petri_net/transition.rb, line 77
def postplaces
  raise 'Not part of a net' if @net.nil?

  @outputs.map { |o| @net.objects[o].source }
end
preplaces() click to toggle source
# File lib/petri_net/transition.rb, line 70
def preplaces
  raise 'Not part of a net' if @net.nil?

  places = []
  places << @inputs.map { |i| @net.objects[i].source }
end
to_gv() click to toggle source

GraphViz definition

# File lib/petri_net/transition.rb, line 62
def to_gv
  "\t#{gv_id} [ label = \"#{@name}#{@probability ? ' ' + @probability.to_s : ''}\" ];\n"
end
to_s() click to toggle source

Stringify this transition.

# File lib/petri_net/transition.rb, line 57
def to_s
  "#{@id}: #{@name}"
end
validate() click to toggle source

Validate this transition.

# File lib/petri_net/transition.rb, line 49
def validate
  return false if @id < 1
  return false if @name.nil? || @name.empty?

  true
end

Private Instance Methods

validate_input(arc) click to toggle source
# File lib/petri_net/transition.rb, line 121
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/transition.rb, line 128
def validate_output(arc)
  outputs.each do |a|
    return false if (@net.get_objects[a] <=> arc) == 0
  end
  true
end