Class: PetriNet::Transition

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

Overview

Transition

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| ... } ⇒ Transition

Create a new transition.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
# File 'lib/petri_net/transition.rb', line 20

def initialize(options = {}, &block)
    @id = next_object_id
    @name = (options[:name] or "Transition#{@id}")
    @description = (options[:description] or "Transition #{@id}")
    @inputs = Array.new
    @outputs = Array.new
    @probability = options[:probability]

    yield self unless block == nil
end

Instance Attribute Details

#descriptionObject

Description



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

def description
  @description
end

#idObject

Unique ID



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

def id
  @id
end

#inputsObject (readonly)

List of input-arcs



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

def inputs
  @inputs
end

#nameObject

Huan readable name



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

def name
  @name
end

#net=(value) ⇒ Object (writeonly)

The net this transition belongs to



17
18
19
# File 'lib/petri_net/transition.rb', line 17

def net=(value)
  @net = value
end

#outputsObject (readonly)

List of output-arcs



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

def outputs
  @outputs
end

#probabilityObject

Probability of firing (this moment)



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

def probability
  @probability
end

Instance Method Details

#==(object) ⇒ Object



63
64
65
# File 'lib/petri_net/transition.rb', line 63

def ==(object)
    name == object.name && description = object.description
end

#activate!Object



90
91
92
93
94
95
96
97
# File 'lib/petri_net/transition.rb', line 90

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?Boolean Also known as: firable?



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

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

#add_input(arc) ⇒ Object

Add an input arc



32
33
34
# File 'lib/petri_net/transition.rb', line 32

def add_input(arc)
    @inputs << arc.id unless arc.nil?
end

#add_output(arc) ⇒ Object

Add an output arc



37
38
39
# File 'lib/petri_net/transition.rb', line 37

def add_output(arc)
    @outputs << arc.id unless arc.nil?
end

#fireObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/petri_net/transition.rb', line 99

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_idObject

GraphViz ID



42
43
44
# File 'lib/petri_net/transition.rb', line 42

def gv_id
    "T#{@id}"
end

#postplacesObject



73
74
75
76
# File 'lib/petri_net/transition.rb', line 73

def postplaces
    raise "Not part of a net" if @net.nil?
    @outputs.map{|o| @net.objects[o].source}
end

#preplacesObject



67
68
69
70
71
# File 'lib/petri_net/transition.rb', line 67

def preplaces
    raise "Not part of a net" if @net.nil?
    places = Array.new
    places << @inputs.map{|i| @net.objects[i].source}
end

#to_gvObject

GraphViz definition



59
60
61
# File 'lib/petri_net/transition.rb', line 59

def to_gv
    "\t#{self.gv_id} [ label = \"#{@name}#{@probability ? ' ' + @probability.to_s : ''}\" ];\n"
end

#to_sObject

Stringify this transition.



54
55
56
# File 'lib/petri_net/transition.rb', line 54

def to_s
    "#{@id}: #{@name}"
end

#validateObject

Validate this transition.



47
48
49
50
51
# File 'lib/petri_net/transition.rb', line 47

def validate
    return false if @id < 1
    return false if @name.nil? or @name.length < 1
    return true
end