Class: YPetri::Net::State::Feature::Assignment

Inherits:
YPetri::Net::State::Feature show all
Defined in:
lib/y_petri/net/state/feature/assignment.rb

Overview

Firing of a Petri net A transition.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from YPetri::Net::State::Feature

#%, Assignment, Delta, Firing, Flux, Gradient, Marking, infer_from_node

Constructor Details

#initialize(place, transition: transition()) ⇒ Assignment

The constructor of an assignment feature takes 1 ordered and 1 named (:transition) argument, which must identify the place and the transitions.



68
69
70
71
72
73
74
# File 'lib/y_petri/net/state/feature/assignment.rb', line 68

def initialize place, transition: transition()
  @place = net.place( place )
  @transition = net.transition( transition )
  @place_index_in_codomain = @transition.codomain.index( @place ) or
    fail TypeError, "The place (#@place) must belong to the codomain of " +
      "the supplied A transition (#@transition)!"
end

Class Attribute Details

.instancesObject (readonly)

def parametrize



38
39
40
# File 'lib/y_petri/net/state/feature/assignment.rb', line 38

def instances
  @instances
end

Instance Attribute Details

#placeObject (readonly)

Returns the value of attribute place.



6
7
8
# File 'lib/y_petri/net/state/feature/assignment.rb', line 6

def place
  @place
end

#transitionObject (readonly)

Returns the value of attribute transition.



6
7
8
# File 'lib/y_petri/net/state/feature/assignment.rb', line 6

def transition
  @transition
end

Class Method Details

.__new__Object



40
# File 'lib/y_petri/net/state/feature/assignment.rb', line 40

alias __new__ new

.construct_from_a_place_with_single_upstream_A_transition(place) ⇒ Object

Constructor that enables special syntax of constructing Feature::Assignment instance from a single place, as long as this place has exactly 1 upstream A transition.



46
47
48
49
50
51
52
53
54
# File 'lib/y_petri/net/state/feature/assignment.rb', line 46

def construct_from_a_place_with_single_upstream_A_transition( place )
  pl = net.place( place )
  aa = pl.upstream_arcs.select( &:A? )
  n = aa.size
  fail TypeError, "When constructing Feature::Assignment from a single" +
    "place, its upstream arcs must contain exactly one A transition! " +
    "(place #{pl} has #{n} upstream A transitions)" unless n == 1
  __new__( pl, transition: aa.first )
end

.new(*args) ⇒ Object Also known as: to

Constructor #new is redefined to use instance cache.



58
59
60
61
# File 'lib/y_petri/net/state/feature/assignment.rb', line 58

def new *args
  return instances[ *args ] if args.size == 1
  instances[ args ]
end

.parametrize(*args) ⇒ Object

Customization of the Class#parametrize method.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/y_petri/net/state/feature/assignment.rb', line 11

def parametrize *args
  Class.instance_method( :parametrize ).bind( self ).( *args ).tap do |ç|
    # Prepare the instance registry.
    hsh = Hash.new do |, id|
      if id.is_a? self then # missing key "id" is an Assignment PS instance
        [ [ id.place, transition: id.transition ] ]
      elsif id.is_a? ç.net.Place then # a single place
        ç.construct_from_a_place_with_single_upstream_A_transition( id )
      elsif id.is_a? Array and id.size == 1 then # single place again
        ç.construct_from_a_place_with_single_upstream_A_transition( id.first )
      elsif id.is_a? Array then
        p = id.fetch( 0 )
        t = id.fetch( 1 ).fetch( :transition )
        if p.is_a? ç.net.Place and t.is_a? ç.net.Transition then
          [ id ] = ç.__new__( p, transition: t )
        else
          [ [ ç.net.place( p ), transition: ç.net.transition( t ) ] ]
        end
      else
        ç.construct_from_a_place_with_single_upstream_A_transition( id )
      end
    end # Hash.new do
    # And assign it to @instances:
    ç.instance_variable_set :@instances, hsh
  end # tap
end

Instance Method Details

#==(other) ⇒ Object

Two assignment features are equal if their place and transition is equal.



119
120
121
122
# File 'lib/y_petri/net/state/feature/assignment.rb', line 119

def == other
  other.is_a? net.State.Feature.Assignment and
    place == other.place && transition == other.transition
end

#extract_from(arg, **nn) ⇒ Object

Extracts the receiver marking feature from the argument. This can be typically a simulation instance.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/y_petri/net/state/feature/assignment.rb', line 79

def extract_from arg, **nn
  case arg
  when YPetri::Simulation then
    # First, let's identify the relevant transition representation
    t = arg.send( :A_transitions, transition ).first
    # Then, let's get its assignment closure
    closure = t.assignment_closure
    # And finally, the feature extraction
    Array( closure.call )[ @place_index_in_codomain ]
  else
    fail TypeError, "Argument type not supported!"
  end
end

#inspectObject

Inspect string of the firing feature.



113
114
115
# File 'lib/y_petri/net/state/feature/assignment.rb', line 113

def inspect
  "<Feature::Assignment to #{place.name or place} by #{transition.name or transition}>"
end

#labelObject

Label for the firing feature (to use in the graphics etc.)



107
108
109
# File 'lib/y_petri/net/state/feature/assignment.rb', line 107

def label
  "A:#{place.name}:#{transition.name}"
end

#to_sObject

A string briefly describing the assignment feature.



101
102
103
# File 'lib/y_petri/net/state/feature/assignment.rb', line 101

def to_s
  label
end

#typeObject

Type of this feature.



95
96
97
# File 'lib/y_petri/net/state/feature/assignment.rb', line 95

def type
  :assignment
end