class PetriNet::Arc

Arc

Attributes

description[RW]

Description

destination[R]

Source-object

id[R]

Unique ID

name[RW]

human readable name

net[W]

The net this arc belongs to

source[R]

Source-object

weight[RW]

Arc weight

Public Class Methods

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

Creates an arc. An arc is an directed edge between a place and a transition (or visa versa) and can have a weight which indicates how many token it comsumes or produces from/to the place

# File lib/petri_net/arc.rb, line 23
def initialize(options = {}, &block)
  @id = next_object_id
  @name = (options[:name] || "Arc#{@id}")
  @description = (options[:description] || "Arc #{@id}")
  @weight = (options[:weight] || 1)
  add_source(options[:source]) unless options[:source].nil?
  add_destination(options[:destination]) unless options[:destination].nil?

  yield self unless block.nil?
end

Public Instance Methods

<=>(object) click to toggle source
# File lib/petri_net/arc.rb, line 123
def <=>(object)
  return false unless object.class.to_s == 'PetriNet::Arc'
  return false unless object.source == source && object.destination == destination

  object.weight <=> weight
end
add_destination(object) click to toggle source

Add a destination object

# File lib/petri_net/arc.rb, line 49
def add_destination(object)
  if object.class.to_s == 'String'
    object = (@net.get_place(object) || @net.get_transition(object))
  end
  if validate_source_destination(object)
    @destination = object
    object.add_input(self)
  else
    raise "Invalid arc destination object: #{object.class}"
  end
end
add_source(object) click to toggle source

Add a source object to this arc. Validation of the source object will be performed before the object is added to the arc and an exception will be raised.

# File lib/petri_net/arc.rb, line 36
def add_source(object)
  if object.class.to_s == 'String'
    object = (@net.get_place(object) || @net.get_transition(object))
  end
  if validate_source_destination(object)
    @source = object
    object.add_output(self)
  else
    raise "Invalid arc source object: #{object.class}"
  end
end
need_update?(net) click to toggle source

Checks if the information in this arc are still correct. The information can get wrong if you merge two nets together.

# File lib/petri_net/arc.rb, line 106
def need_update?(net)
  if net.get_object(@source.id).nil? || (@source.name != net.get_object(@source.id).name)
    return true
  end
  if net.get_object(@destination.id).nil? || (@destination.name != net.get_object(@destination.id).name)
    return true
  end
end
ordinary?() click to toggle source

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

# File lib/petri_net/arc.rb, line 63
def ordinary?
  @weight == 1
end
to_gv() click to toggle source

Gives the GraphViz-representation of this arc as string of a GV-Edge

# File lib/petri_net/arc.rb, line 100
def to_gv
  "\t#{@source.gv_id} -> #{@destination.gv_id} [ label = \"#{@name}\", headlabel = \"#{@weight}\" ];\n"
end
to_s() click to toggle source

Stringify this arc.

# File lib/petri_net/arc.rb, line 95
def to_s
  "#{@id}: #{@name} (#{@weight}) #{@source.id} -> #{@destination.id}"
end
update(net) click to toggle source

Updates the information in this arc Should only be necessary if PetriNet::Arc#need_update? is true affects source and destination

# File lib/petri_net/arc.rb, line 118
def update(net)
  @source.id = net.objects_find_index @source
  @destination.id = net.objects_find_index @destination
end
validate(net) click to toggle source

Validate this arc.

# File lib/petri_net/arc.rb, line 68
def validate(net)
  return false if @id < 1
  return false if @name.nil? || (@name.length <= 0)
  return false if @weight < 1
  return false if @source.nil? || @destination.nil?
  return false if @source == @destination
  return false if @source.class == @destination.class

  if @source.class.to_s == 'PetriNet::Place'
    return net.objects_include? @source
  elsif @source.class.to_s == 'PetriNet::Transition'
    return net.objects_include? @source
  else
    return false
  end
  if @destination.class.to_s == 'PetriNet::Place'
    return net.objects.include? @destination
  elsif @destination.class.to_s == 'PetriNet::Transition'
    return net.objects.include? @destination
  else
    return false
  end

  true
end

Private Instance Methods

validate_source_destination(object) click to toggle source

Validate source or destination object

# File lib/petri_net/arc.rb, line 133
def validate_source_destination(object)
  return false if object.nil?

  return object.class.to_s == 'PetriNet::Place' || object.class.to_s == 'PetriNet::Transition'

  # return if @source.nil? or @source.class.to_s == object.class.to_s
  # return if @destination.nil? or @destination.class.to_s == object.class.to_s
  true
end