Class: YPetri::Place

Inherits:
Object
  • Object
show all
Defined in:
lib/y_petri/place.rb

Overview

Represents a Petri net place.

Defined Under Namespace

Modules: Arcs, Features, Guarded Classes: Guard

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(guard: L!, , **named_args, &block) ⇒ Place

Arguments supplied upon place initialization may include:

  • :marking

  • :default_marking

  • :quantum

  • :guard

Marking is a standard attribute of a Petri net place, default_marking is marking upon calling the reset method. Default marking may also be used as the initial value in the simulations involving the place in question. Quantum attribute is not in use presently. In the future, it might be used in deciding when to switch between continuous and discrete stochastic representation of the marking. Guard is a restriction to the place’s marking. (For example, the place could only admit non-negative numbers, or numbers smaller than 1, or odd numbers etc.) Any number of guards can be specified for a constructed place via Place#guard method. For the cases when a place has only one guard, it is, as a syntactic sugar, possible to introduce exactly one guard already upon place initialization by supplying to this constructor, in addition to other parameters, a string expressing the guard as :guard and a block expressing the same guard in code. If no guard block is supplied to this constructor, default guards are constructed based on the data type of the :marking or :default_marking argument. If it is wished that the place has no guards whatsoever, :guard should be set to false.



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

def initialize guard: L!, **named_args, &block
  @upstream_arcs, @downstream_arcs, @guards = [], [], [] # init to empty
  @quantum = named_args.has?( :quantum ) ? named_args[:quantum] : 1
  named_args.may_have :default_marking, syn!: :m!
  if named_args.has? :default_marking then
    @has_default_marking = true
    @default_marking = named_args[:default_marking]
  else
    @has_default_marking = false
  end
  if named_args.has? :marking then @marking = named_args[:marking] else
    @marking = default_marking if has_default_marking?
  end
  # Check in :guard value and the corresponding &block.
  if guard.ℓ? then # guard NL assertion not given, use block or default guards
    block ? guard( &block ) : add_default_guards!( @marking )
  elsif guard then # guard NL assertion given
    fail ArgumentError, "No guard block given!" unless block
    guard( guard, &block )
  else
    fail ArgumentError, "Block given, but :guard argument is falsey!" if block
  end
end

Instance Attribute Details

#guardsObject (readonly)

Returns the value of attribute guards.



25
26
27
# File 'lib/y_petri/place.rb', line 25

def guards
  @guards
end

#has_default_markingObject (readonly) Also known as: has_default_marking?

Returns the value of attribute has_default_marking.



26
27
28
# File 'lib/y_petri/place.rb', line 26

def has_default_marking
  @has_default_marking
end

#quantumObject (readonly)

Returns the value of attribute quantum.



24
25
26
# File 'lib/y_petri/place.rb', line 24

def quantum
  @quantum
end

Instance Method Details

#add(amount) ⇒ Object

Adds tokens to the place’s @marking instance variable.



140
141
142
# File 'lib/y_petri/place.rb', line 140

def add( amount )
  @marking = guard.( @marking + amount )
end

#default_markingObject



29
30
31
32
33
# File 'lib/y_petri/place.rb', line 29

def default_marking
  fail TypeError, "No default marking was specified for #{self}!" unless
    has_default_marking?
  @default_marking
end

#default_marking=(marking) ⇒ Object



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

def default_marking= marking
  @has_default_marking = true
  @default_marking = marking
end

#m(simulation = world.simulation) ⇒ Object

Getter of the place’s marking attribute under a simulation (current simulation by default).



116
117
118
# File 'lib/y_petri/place.rb', line 116

def m simulation=world.simulation
  simulation.net.State.Feature.Marking( self ) % simulation
end

#m=(marking) ⇒ Object

Alias for #marking=



134
135
136
# File 'lib/y_petri/place.rb', line 134

def m=( marking )
  self.marking = marking
end

#marking(*args, &block) ⇒ Object

Used without arguments, it is a getter of the place’s @marking attribute, just like the Place#m method. However, if a string and a block is supplied to it, it acts as an alias of the Place#guard method. This is because this:

marking "should be a number" do |m| fail unless m.is_a? Numeric end

reads better than this:

guard "should be a number" do |m| fail unless m.is_a? Numeric end

See #guard method for more information.



101
102
103
104
105
# File 'lib/y_petri/place.rb', line 101

def marking *args, &block
  return @marking if args.empty?
  fail ArgumentError, "Too many arguments!" if args.size > 1
  guard args[0], &block
end

#marking=(new_marking) ⇒ Object

Marking setter.



122
123
124
# File 'lib/y_petri/place.rb', line 122

def marking=( new_marking )
  @marking = guard.( new_marking )
end

#reset_markingObject

Resets the place’s marking back to its default value (+@default_marking instance variable).



153
154
155
156
157
# File 'lib/y_petri/place.rb', line 153

def reset_marking
  fail TypeError, "No default marking was specified for #{self}!" unless
    has_default_marking?
  @marking = guard.( @default_marking )
end

#subtract(amount) ⇒ Object

Subtracts tokens from the place’s @marking instance variable.



146
147
148
# File 'lib/y_petri/place.rb', line 146

def subtract( amount )
  @marking = guard.( @marking - amount )
end

#uvObject

Convenience visualizer of the upstream net.



161
162
163
# File 'lib/y_petri/place.rb', line 161

def uv
  upstream_net.visualize
end

#valueObject

Near-alias for #marking.



109
110
111
# File 'lib/y_petri/place.rb', line 109

def value
  marking
end

#value=(marking) ⇒ Object

Alias for #marking=



128
129
130
# File 'lib/y_petri/place.rb', line 128

def value=( marking )
  self.marking = marking
end