class YPetri::Place
Represents a Petri net place.
Attributes
Public Class Methods
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.
# 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
Public Instance Methods
Adds tokens to the place's +@marking+ instance variable.
# File lib/y_petri/place.rb, line 140 def add( amount ) @marking = guard.( @marking + amount ) end
# 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
# File lib/y_petri/place.rb, line 35 def default_marking= marking @has_default_marking = true @default_marking = marking end
Getter of the place's marking attribute under a simulation (current simulation by default).
# File lib/y_petri/place.rb, line 116 def m simulation=world.simulation simulation.net.State.Feature.Marking( self ) % simulation end
Alias for marking=
# File lib/y_petri/place.rb, line 134 def m=( marking ) self.marking = marking end
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.
# 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 setter.
# File lib/y_petri/place.rb, line 122 def marking=( new_marking ) @marking = guard.( new_marking ) end
Resets the place's marking back to its default value (+@default_marking instance variable).
# 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
Subtracts tokens from the place's +@marking+ instance variable.
# File lib/y_petri/place.rb, line 146 def subtract( amount ) @marking = guard.( @marking - amount ) end
Convenience visualizer of the upstream net.
# File lib/y_petri/place.rb, line 161 def uv upstream_net.visualize end
Near-alias for marking
.
# File lib/y_petri/place.rb, line 109 def value marking end
Alias for marking=
# File lib/y_petri/place.rb, line 128 def value=( marking ) self.marking = marking end
Private Instance Methods
# Returns a string representing the place. # def to_s
n, m = name, marking "#{n.nil? ? 'Place' : n}[#{m.nil? ? 'nil' : m}]"
end
# File lib/y_petri/place.rb, line 183 def instance_description_strings m, n, q = marking, name, quantum mς = "marking: #{m.nil? ? 'nil' : m}" nς = "name: #{n.nil? ? '∅' : n}" qς = q == 1 ? nil : "quantum: #{q.nil? ? '∅' : q}" dς = "default_marking: #{has_default_marking ? default_marking : '∅'}" return nς, mς, dς, qς end