Class: PetriNet::Place

Inherits:
Base
  • Object
show all
Defined in:
lib/petri_net/place.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| ... } ⇒ Place

Initialize a new place. Supports block configuration.

Yields:

  • (_self)

Yield Parameters:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/petri_net/place.rb', line 22

def initialize(options = {}, &block)
    @id = next_object_id
    @name = (options[:name] or "Place#{@id}")
    @description = (options[:description] or "Place #{@id}")
    @capacity = options[:capacity].nil? ? Float::INFINITY : options[:capacity]
    @inputs = Array.new
    @outputs = Array.new
    @markings = Array.new

    yield self unless block == nil
end

Instance Attribute Details

#capacityObject

Token capacity



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

def capacity
  @capacity
end

#descriptionObject

description



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

def description
  @description
end

#idObject

Unique ID



3
4
5
# File 'lib/petri_net/place.rb', line 3

def id
  @id
end

#inputsObject (readonly)

List of input-arcs



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

def inputs
  @inputs
end

#markingsObject (readonly)

Current token



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

def markings
  @markings
end

#nameObject

Human readable name



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

def name
  @name
end

#net=(value) ⇒ Object (writeonly)

The net this place belongs to



19
20
21
# File 'lib/petri_net/place.rb', line 19

def net=(value)
  @net = value
end

#outputsObject (readonly)

List of output-arcs



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

def outputs
  @outputs
end

Instance Method Details

#==(object) ⇒ Object



106
107
108
# File 'lib/petri_net/place.rb', line 106

def ==(object)
    return true if name == object.name && description = object.description
end

#add_input(arc) ⇒ Object

Add an input arc



35
36
37
# File 'lib/petri_net/place.rb', line 35

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

#add_marking(count = 1) ⇒ Object Also known as: +



44
45
46
47
48
49
50
51
52
53
# File 'lib/petri_net/place.rb', line 44

def add_marking(count = 1)
    if count <= @capacity
        count.times do
            @markings << PetriNet::Marking.new
        end
        return true
    else
        raise "Tried to add more markings than possible"
    end
end

#add_output(arc) ⇒ Object

Add an output arc



40
41
42
# File 'lib/petri_net/place.rb', line 40

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

#gv_idObject

GraphViz ID



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

def gv_id
    "P#{@id}"
end

#posttransitionsObject



92
93
94
95
# File 'lib/petri_net/place.rb', line 92

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

#pretransitionsObject



86
87
88
89
90
# File 'lib/petri_net/place.rb', line 86

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

#remove_marking(count = 1) ⇒ Object Also known as: -



62
63
64
65
66
67
68
69
# File 'lib/petri_net/place.rb', line 62

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

#set_marking(count) ⇒ Object



55
56
57
58
# File 'lib/petri_net/place.rb', line 55

def set_marking(count)
    @markings = []
    add_marking count
end

#to_gvObject

GraphViz definition



103
104
105
# File 'lib/petri_net/place.rb', line 103

def to_gv
    "\t#{self.gv_id} [ label = \"#{@name} #{@markings.size}  \" ];\n"
end

#to_sObject

Stringify this place.



98
99
100
# File 'lib/petri_net/place.rb', line 98

def to_s
    "#{@id}: #{@name} (#{@capacity == nil ? -1 : 0}) #{'*' * @markings.length}"
end

#validateObject

Validate the setup of this place.



78
79
80
81
82
83
84
# File 'lib/petri_net/place.rb', line 78

def validate
    return false if @id.nil? or @id < 0
    return false if @name.nil? or @name.strip.length <= 0
    return false if @description.nil? or @description.strip.length <= 0
    return false if @capacity.nil? or @capacity < -1
    return true
end