Class: YPetri::Net::State::Feature::Delta

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

Overview

Change of a Petri net place caused by a certain set of transitions.

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, transitions: net.tt) ⇒ Delta

The constructor of a delta feature takes one ordered argument (place identifier), and one named argument, :transitions, expecting an array of transition identifiers, whose contribution is taken into account in this delta feature.



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

def initialize place, transitions: net.tt
  @place = net.place( place )
  @transitions = net.Transitions( transitions )
end

Class Attribute Details

.instancesObject (readonly)

def parametrize



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

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/delta.rb', line 6

def place
  @place
end

#transitionsObject (readonly) Also known as: tt

Returns the value of attribute transitions.



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

def transitions
  @transitions
end

Class Method Details

.__new__Object



55
# File 'lib/y_petri/net/state/feature/delta.rb', line 55

alias __new__ new

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

Constructor #new is redefined to use instance cache.



89
90
91
92
# File 'lib/y_petri/net/state/feature/delta.rb', line 89

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

.parametrize(*args) ⇒ Object

Customization of the Class#parametrize method.



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/y_petri/net/state/feature/delta.rb', line 12

def parametrize *args
  Class.instance_method( :parametrize ).bind( self ).( *args ).tap do |ç|
    # First, prepare the instance registry.
    hsh = Hash.new do |, id|
      if id.is_a? self then # missing key "id" is a Delta instance
        [ [ id.place, transitions: id.transitions.sort_by( &:object_id ) ] ]
      else
        p = id.fetch( 0 )
        tt = id.fetch( 1 ).fetch( :transitions ) # value of :transitions key
        tt_array = Array( tt )
        if tt == tt_array then
          if p.is_a? ç.net.Place and tt.all? { |t| t.is_a? ç.net.Transition }
            if tt == tt.sort_by( &:object_id ) then
              # Cache the instance.
              [ id ] = if tt.all? &:timed? then
                          ç.timed( *id )
                        elsif tt.all? &:timeless? then
                          fail TypeError, "Net::State::Feature::Delta does " +
                            "not admit A transitions!" if tt.any? &:A?
                          ç.timeless( *id )
                        else
                          fail TypeError, "Net::State::Feature::Delta only " +
                            "admits the transition sets that are either " +
                            "all timed, or all timeless!"
                        end
            else
              [ [ p, transitions: tt.sort_by( &:object_id ) ] ]
            end
          else # convert place and transition ids to places and transitions
            [ [ ç.net.place( p ), transitions: ç.net.Transitions( tt ) ] ]
          end
        else
          [ [ p, transitions: tt_array ] ]
        end
      end
    end
    # Then, assign it to the :@instances variable.
    ç.instance_variable_set :@instances, hsh
  end # tap
end

.timed(place, transitions: net.T_tt) ⇒ Object

Timed delta feature constructor. Takes a place, and an array of timed transition identifiers supplied as +:transitions: parameter.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/y_petri/net/state/feature/delta.rb', line 60

def timed place, transitions: net.T_tt
  tt = begin
         net.T_Transitions( transitions )
       rescue TypeError => err
         msg = "Transitions #{transitions} not recognized as timed " +
           "transitions in #{net}! (%s)"
         raise TypeError, msg % err
       end
  __new__( place, transitions: tt )
    .tap { |inst| inst.instance_variable_set :@timed, true }
end

.timeless(place, transitions: net.t_tt) ⇒ Object

Timeless delta feature constructor. Takes a place, and an array of timeless transition identifiers as +:transitions: parameter.



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

def timeless place, transitions: net.t_tt
  tt = begin
         net.t_Transitions( transitions )
       rescue TypeError => err
         msg = "Transitions #{transitions} not recognized as timed " +
           "transitions in #{net}! (%s)"
         raise TypeError, msg % err
       end
  __new__( place, transitions: net.t_Transitions( transitions ) )
    .tap { |inst| inst.instance_variable_set :@timed, false }
end

Instance Method Details

#==(other) ⇒ Object

Delta features are equal if they are of equal PS and refer to the same place and transition set.



169
170
171
172
# File 'lib/y_petri/net/state/feature/delta.rb', line 169

def == other
  other.is_a? net.State.Feature.Delta and
    place == other.place && transitions == other.transitions
end

#extract_from(arg, **named_args) ⇒ Object

Extracts the value of this feature from the target (eg. a simulation). If the receiver delta feature is timed, this method requires an additional named argument :delta_time, alias :Δt.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/y_petri/net/state/feature/delta.rb', line 110

def extract_from arg, **named_args
  case arg
  when YPetri::Simulation then
    if timed? then
      arg.send( :T_Transitions, transitions )
        .delta( named_args.must_have :delta_time, syn!: :Δt ).fetch( place )
    else
      arg.send( :t_Transitions, transitions ).delta.fetch( place )
    end
  else
    fail TypeError, "Argument type not supported!"
  end
end

#inspectObject

Inspect string of the delta feature.



161
162
163
164
# File 'lib/y_petri/net/state/feature/delta.rb', line 161

def inspect
  "<Feature::Delta Δ:#{place.name || place}:[%s]>" %
    transitions.names( true ).join( ', ' )
end

#labelObject

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



150
151
152
153
154
155
156
157
# File 'lib/y_petri/net/state/feature/delta.rb', line 150

def label
  "Δ:#{place.name}:%s" %
    if transitions.size == 1 then
      transitions.first.name || transitions.first
    else
      "#{transitions.size}tt"
    end
end

#timed?Boolean

Is the delta feature timed?

Returns:

  • (Boolean)


126
127
128
# File 'lib/y_petri/net/state/feature/delta.rb', line 126

def timed?
  @timed
end

#timeless?Boolean

Opposite of #timed?.

Returns:

  • (Boolean)


132
133
134
# File 'lib/y_petri/net/state/feature/delta.rb', line 132

def timeless?
  ! timed?
end

#to_sObject

A string briefly describing this delta feature.



144
145
146
# File 'lib/y_petri/net/state/feature/delta.rb', line 144

def to_s
  label
end

#typeObject

Type of this feature.



138
139
140
# File 'lib/y_petri/net/state/feature/delta.rb', line 138

def type
  :delta
end