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 =

Error message template.

-> m, of, assert do
  "Marking #{m.y_inspect}" +
    if of then " of #{of.name || of}" else '' end +
    " #{assert}!"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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!”



35
36
37
38
39
40
41
# 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

Instance Attribute Details

#assertionObject (readonly)

Returns the value of attribute assertion.



14
15
16
# File 'lib/y_petri/place/guard.rb', line 14

def assertion
  @assertion
end

#blockObject (readonly)

Returns the value of attribute block.



14
15
16
# File 'lib/y_petri/place/guard.rb', line 14

def block
  @block
end

#placeObject (readonly)

Returns the value of attribute place.



14
15
16
# File 'lib/y_petri/place/guard.rb', line 14

def place
  @place
end

Instance Method Details

#validate(marking) ⇒ Object

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



45
46
47
48
49
# 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