Class: StateMachineChecker::CTL::Atom

Inherits:
Formula
  • Object
show all
Defined in:
lib/state_machine_checker/ctl/atom.rb

Overview

An atomic proposition about a single object.

Instance Method Summary collapse

Methods inherited from Formula

#AU, #EU, #and, #implies, #or

Constructor Details

#initialize(method_name_or_fn) ⇒ Atom

Returns a new instance of Atom.

Examples:

Atom.new(:shipped?)
Atom.new(->(x) { x.shipped? })

Parameters:

  • method_name_or_fn (Symbol, Proc)


14
15
16
17
18
19
20
21
# File 'lib/state_machine_checker/ctl/atom.rb', line 14

def initialize(method_name_or_fn)
  @name, @fn = if method_name_or_fn.respond_to?(:call)
    ["atom##{object_id}", method_name_or_fn]
  else
    # Create a function which will send the given method name.
    [method_name_or_fn.to_s, method_name_or_fn.to_proc]
  end
end

Instance Method Details

#apply(instance) ⇒ Object

Evaluate the atom on the given instance.

Examples:

even_atom = Atom.new(:even?)
even_atom.apply(6) #=> true
even_atom.apply(7) #=> false


29
30
31
# File 'lib/state_machine_checker/ctl/atom.rb', line 29

def apply(instance)
  fn.call(instance)
end

#atomsEnumerator<Atom>

Returns an enumerator containing only this object, as it is an atom.

Returns:

  • (Enumerator<Atom>)


36
37
38
# File 'lib/state_machine_checker/ctl/atom.rb', line 36

def atoms
  [self]
end

#check(machine) ⇒ CheckResult

Check which states of the machine are labeled with this atom.

Parameters:

Returns:



44
45
46
47
48
49
50
51
# File 'lib/state_machine_checker/ctl/atom.rb', line 44

def check(machine)
  result = machine.states.each_with_object({}) { |state, h|
    satisfied = machine.labels_for_state(state).include?(self)
    h[state] = StateResult.new(satisfied, [])
  }

  CheckResult.new(result)
end

#to_sObject



53
54
55
# File 'lib/state_machine_checker/ctl/atom.rb', line 53

def to_s
  name
end