Class: PetriNet::Arc

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

Overview

Arc

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

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

Yields:

  • (_self)

Yield Parameters:

  • _self (PetriNet::Arc)

    the object that the method was called on



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

def initialize(options = {}, &block)
    @id = next_object_id
    @name = (options[:name] or "Arc#{@id}")
    @description = (options[:description] or "Arc #{@id}")
    @weight = (options[:weight] or 1)
    self.add_source(options[:source]) unless options[:source].nil?
    self.add_destination(options[:destination]) unless options[:destination].nil?

    yield self unless block == nil
end

Instance Attribute Details

#descriptionObject

Description



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

def description
  @description
end

#destinationObject (readonly)

Source-object



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

def destination
  @destination
end

#idObject (readonly)

Unique ID



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

def id
  @id
end

#nameObject

human readable name



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

def name
  @name
end

#net=(value) ⇒ Object (writeonly)

The net this arc belongs to



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

def net=(value)
  @net = value
end

#sourceObject (readonly)

Source-object



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

def source
  @source
end

#weightObject

Arc weight



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

def weight
  @weight
end

Instance Method Details

#<=>(object) ⇒ Object



120
121
122
123
124
# File 'lib/petri_net/arc.rb', line 120

def <=>(object)
    return false unless object.class.to_s == "PetriNet::Arc"
    return false unless object.source == self.source && object.destination == self.destination
    return object.weight <=> self.weight
end

#add_destination(object) ⇒ Object

Add a destination object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/petri_net/arc.rb', line 47

def add_destination(object)
    if object.class.to_s == "String"
        object = (@net.get_place object or @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) ⇒ Object

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.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/petri_net/arc.rb', line 34

def add_source(object)
    if object.class.to_s == "String"
        object = (@net.get_place object or @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) ⇒ Boolean

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

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
# File 'lib/petri_net/arc.rb', line 103

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?Boolean

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/petri_net/arc.rb', line 61

def ordinary?
    @weight == 1
end

#to_gvObject

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



97
98
99
# File 'lib/petri_net/arc.rb', line 97

def to_gv
    "\t#{@source.gv_id} -> #{@destination.gv_id} [ label = \"#{@name}\", headlabel = \"#{@weight}\" ];\n"
end

#to_sObject

Stringify this arc.



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

def to_s
    "#{@id}: #{@name} (#{@weight}) #{@source.id} -> #{@destination.id}"
end

#update(net) ⇒ Object

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



115
116
117
118
# File 'lib/petri_net/arc.rb', line 115

def update net
    @source.id = net.objects_find_index @source
    @destination.id = net.objects_find_index @destination
end

#validate(net) ⇒ Object

Validate this arc.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/petri_net/arc.rb', line 66

def validate(net)
    return false if @id < 1
    return false if @name.nil? or @name.length <= 0
    return false if @weight < 1
    return false if @source.nil? or @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
    return true
end