Class: YPetri::Net::State

Inherits:
Array
  • Object
show all
Defined in:
lib/y_petri/net/state.rb

Overview

An array whose elements represent marking of places of a YPetri::Net.

Defined Under Namespace

Classes: Feature, Features

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.Feature(arg = nil, **named_args) ⇒ Object

Returns the feature identified by the argument.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/y_petri/net/state.rb', line 24

def Feature arg=nil, **named_args
  case arg
  when Feature() then arg
  when Feature then arg.class.new( arg )
  when nil then
    key, val = named_args.first
    case key
    when :marking then Feature().Marking( val )
    when :firing then Feature().Firing( val )
    when :flux then Feature().Flux( val )
    when :gradient then Feature().Gradient( *val )
    when :delta then Feature().Delta( *val )
    when :assignment then Feature().Assignment( val )
    else fail ArgumentError, "Unrecognized feature: #{key}!"
    end
  else
    Feature().infer_from_node( arg )
  end
end

.Features(array = nil, **named_args) ⇒ Object

A constructor of a Features instance. Note that the message :Features called without arguments is intercepted by a singleton method and returns the parametrized subclass of State::Features owned by this class.

This method may accept a single array-type argument, constructing a feature set out of it. Alternatively, the method may accept named arguments: :marking, :firing, :gradient, :flux, :delta, and :assignment, specifying the a single (possibly mixed) feature set.



53
54
55
# File 'lib/y_petri/net/state.rb', line 53

def Features array=nil, **named_args
  Features()[ *array, **named_args ]
end

.parametrize(net: ( fail ArgumentError, "No owning net!" )) ⇒ Object

Customization of the parametrize method for the State class: Its dependents Feature and Features (feature set class) are also parametrized.



13
14
15
16
17
18
19
20
# File 'lib/y_petri/net/state.rb', line 13

def parametrize net: ( fail ArgumentError, "No owning net!" )
  Class.new( self ).tap do |ç|
    ç.define_singleton_method :net do net end
    ç.param_class!( { Feature: Feature,
                      Features: Features },
                    with: { State: ç } )
  end
end

Instance Method Details

#marking(place) ⇒ Object

Marking of a single given place in this state.



87
88
89
# File 'lib/y_petri/net/state.rb', line 87

def marking place
  self[ net.places.index net.place( place ) ]
end

#markings(*places) ⇒ Object

Expects an arbitrary number of places or place ids, and returns an array of their markings as per the receiver State instance.



94
95
96
# File 'lib/y_petri/net/state.rb', line 94

def markings *places
  places.map &method( :marking )
end

#to_record(clamped_places) ⇒ Object

Given a set of clamped places, this method outputs a Record instance containing the marking of the free places (complementary to the supplied set of clamped places). I no set of clamped places is supplied, it is considered empty.



76
77
78
79
80
81
82
83
# File 'lib/y_petri/net/state.rb', line 76

def to_record clamped_places
  free_places = case clamped_places
                when Hash then to_record( clamped_places.keys )
                else
                  places - places( clamped_places )
                end
  features( marking: free_places ).Record.load markings( free_places )
end