Class: YPetri::Place::Guard

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

Overview

Marking guard of a place.

Constant Summary collapse

ERRMSG =
-> m, of, assert do
  "Marking #{m}:#{m.class}" +
    if of then " of #{of.name || of rescue of}" else '' end +
    " #{assert}!"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assertion_NL_string, place: nil, &block) ⇒ Guard

Requires a NL guard assertion (used in GuardError messages), and a guard block expressing the same assertion formally, in code. Attention: *Only false result is considered a failure! If the block returns nil, the guard has passed!* When YPetri::Guard is in action (typically via its #validate method), it raises YPetri::GuardError if the guard block returns false. However, the guard block is welcome to raise GuardError on its own, and for this purpose, it is evaluated inside a special “Lab” object, with #fail method redefined so as to accept no arguments, and automatically raise appropriately worded GuardError. See also: YPetri#guard method.



25
26
27
28
29
30
31
# File 'lib/y_petri/place/guard.rb', line 25

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

Instance Attribute Details

#assertionObject (readonly)

Returns the value of attribute assertion.



12
13
14
# File 'lib/y_petri/place/guard.rb', line 12

def assertion
  @assertion
end

#blockObject (readonly)

Returns the value of attribute block.



12
13
14
# File 'lib/y_petri/place/guard.rb', line 12

def block
  @block
end

#placeObject (readonly)

Returns the value of attribute place.



12
13
14
# File 'lib/y_petri/place/guard.rb', line 12

def place
  @place
end

Instance Method Details

#validate(marking) ⇒ Object

Validates a supplied marking value against the guard block. Raises YPetri::GuardError if the guard fails, otherwise returns true.



36
37
38
39
40
# File 'lib/y_petri/place/guard.rb', line 36

def validate( marking )
  λ = __fail__( marking, assertion )
  λ.call if @Lab.new( λ ).instance_exec( marking, &block ) == false
  return true
end