class YPetri::Place::Guard
Marking guard of a place.
Constants
- ERRMSG
Error message template.
Attributes
Public Class Methods
Expects a guard assetion in natural language, and a guard block. Guard
block is a unary block that validates marking. A validation fails when:
-
The block returns false.
-
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
# File lib/y_petri/place/guard.rb, line 39 def fail; @λ.call end
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
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