class YPetri::Place::Guard

Marking guard of a place.

Constants

ERRMSG

Error message template.

Attributes

assertion[R]
block[R]
place[R]

Public Class Methods

new( assertion_NL_string, place: nil, &block ) click to toggle source

Expects a guard assetion in natural language, and a guard block. Guard block is a unary block that validates marking. A validation fails when:

  1. The block returns false.

  2. The block raises YPetri::GuardError.

In all other cases, including when the block returns nil (beware!), the marking is considered valid. Inside the block, #fail keyword is redefined, so that it can (and must) be called without arguments, and it raises an appropriately worded GuardError. (Other exceptions can still be raised using #raise keyword.) Practical example:

YPetri::Place::Guard.new "should be a number" do |m|
  fail unless m.is_a? Numeric
end

Then guard! :foobar raises GuardError with a message “Marking foobar:Symbol should be a number!”

# File lib/y_petri/place/guard.rb, line 35
def initialize( assertion_NL_string, place: nil, &block )
  @place, @assertion, @block = place, assertion_NL_string, block
  @Lab = Class.new BasicObject do
    def initialize λ;  = λ end
    def fail; .call end
  end
end

Public Instance Methods

fail() click to toggle source
# File lib/y_petri/place/guard.rb, line 39
def fail; .call end
validate( marking ) click to toggle source

Validates a marking value. Raises YPetri::GuardError upon failure.

# File lib/y_petri/place/guard.rb, line 45
def validate( marking )
  λ = __fail__( marking, assertion )
  λ.call if @Lab.new( λ ).instance_exec( marking, &block ) == false
  return true
end

Private Instance Methods

__fail__(marking, assertionq) click to toggle source

Constructs the fail closure.

# File lib/y_petri/place/guard.rb, line 55
def __fail__ marking, assertionq
  pl = place
  -> { fail YPetri::GuardError, ERRMSG.( marking, pl, assertion ) }
end