lib/y_petri/simulation/transitions/access.rb in y_petri-2.2.4 vs lib/y_petri/simulation/transitions/access.rb in y_petri-2.3.2
- old
+ new
@@ -1,254 +1,281 @@
-#encoding: utf-8
+# encoding: utf-8
# Simulation mixin providing access to transitions.
#
class YPetri::Simulation::Transitions
module Access
# Does a transition belong to the simulation?
#
- def includes_transition?( id )
- true.tap { begin; transition( id )
- rescue NameError, TypeError
+ def includes_transition?( transition )
+ true.tap { begin; transition( transition ); rescue NameError, TypeError
return false
end }
end
alias include_transition? includes_transition?
# Net's transition.
#
- def t( id )
- transitions( id ).source
+ def t( transition )
+ transition( transition ).source
end
- # Net's transitions.
+ # Makes it so that when "transitions" is abbreviated to "tt", transitions
+ # of the underlying net are returned rather than simulation's transition
+ # representations.
#
- def tt( ids=nil )
- transitions( ids ).sources
- end
+ chain Tt: :Transitions,
+ tt: :transitions,
+ ts_Tt: :ts_Transitions,
+ ts_tt: :ts_transitions,
+ tS_Tt: :tS_Transitions,
+ tS_tt: :tS_transitions,
+ Ts_Tt: :Ts_Transitions,
+ Ts_tt: :Ts_transitions,
+ TS_Tt: :TS_Transitions,
+ TS_tt: :TS_transitions,
+ A_Tt: :A_Transitions,
+ A_tt: :A_transitions,
+ S_Tt: :S_Transitions,
+ S_tt: :S_transitions,
+ s_Tt: :S_Transitions,
+ s_tt: :S_transitions,
+ T_Tt: :T_Transitions,
+ T_tt: :T_transitions,
+ t_Tt: :t_Transitions,
+ t_tt: :t_transitions,
+ &:sources
- # Net's *ts* transitions.
+ # Makes it so that +Tn+/+tn+ means "names of transitions", and that when
+ # message "n" + transition_type is sent to the simulation, it returns names
+ # of the trasitions of the specified type.
#
- def ts_tt( ids=nil )
- ts_transitions( ids ).sources
- end
+ chain Tn: :Tt,
+ tn: :tt,
+ nts: :ts_tt,
+ ntS: :tS_tt,
+ nTs: :Ts_tt,
+ nTS: :TS_tt,
+ nA: :A_tt,
+ nS: :S_tt,
+ ns: :s_tt,
+ nT: :T_tt,
+ nt: :t_tt do |r| r.names( true ) end
- # Net's *tS* transitions.
- #
- def tS_tt( ids=nil )
- tS_transitions( ids ).sources
- end
+ protected
-
- # Net's *Ts* transitions.
+ # Transition instance identification.
#
- def Ts_tt( ids=nil )
- Ts_transitions( ids ).sources
+ def transition( transition )
+ begin; Transition().instance( transition ); rescue NameError, TypeError
+ begin
+ transition = net.transition( transition )
+ Transition().instances.find { |t_rep| t_rep.source == transition } ||
+ Transition().instance( transition.name )
+ rescue NameError, TypeError => msg
+ fail TypeError, "Unknown transition instance: #{transition}! (#{msg})"
+ end
+ end
end
- # Net's *TS* transitions.
+ # Constructs an instance of @Transitions parametrized subclass. Expects a
+ # single array of transitions or transition ids and returns an array of
+ # corresponding transition representations in the simulation. Note that the
+ # includer of the +Transitions::Access+ module normally overloads
+ # :Transitions message in such way, that even without an argument, it does
+ # not fil, but returns @Transitions parametrized subclass itself.
#
- def TS_tt( ids=nil )
- TS_transitions( ids ).sources
+ def Transitions( array )
+ Transitions().load array.map &method( :transition )
end
- # Net's *A* transitions.
+ # Without arguments, returns all the transition representations in the
+ # simulation. Otherwise, it accepts an arbitrary number of nodes or node
+ # ids as arguments, and returns an array of the corresponding transition
+ # representations.
#
- def A_tt( ids=nil )
- A_transitions( ids ).sources
+ def transitions( *transitions )
+ return @transitions if transitions.empty?
+ Transitions( transitions )
end
- # Net's *S* transitions.
+ # Simulation's *ts* transitions. Expects a single array of +ts+ transitions
+ # or their ids and returns an array of the corresponding ts transition
+ # representations.
#
- def S_tt( ids=nil )
- S_transitions( ids ).sources
+ def ts_Transitions( array )
+ transitions.ts.subset( array )
end
- # Net's *s* (non-stoichiometric) transitions.
+ # Simulation's *ts* transitions. Without arguments, returns all the ts
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of ts transitions or transition ids as arguments, and returns an array of
+ # the corresponding ts transitions of the simulation.
#
- def s_tt( ids=nil )
- s_transitions( ids ).sources
+ def ts_transitions( *transitions )
+ return transitions().ts if transitions.empty?
+ ts_Transitions( transitions )
end
- # Net's *T* transitions.
+ # Simulation's *tS* transitions. Expects a single array of +tS+ transitions
+ # or their ids and returns an array of the corresponding tS transition
+ # representations.
#
- def T_tt( ids=nil )
- T_transitions( ids ).sources
+ def tS_Transitions( array )
+ transitions.tS.subset( array )
end
- # Net's *t* (timeless) transitions.
+ # Simulation's *tS* transitions. Without arguments, returns all the tS
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of tS transitions or transition ids as arguments, and returns an array of
+ # the corresponding tS transitions of the simulation.
#
- def t_tt ids=nil
- return transitions.t if ids.nil?
- transitions.t.subset( ids )
+ def tS_transitions( *transitions )
+ return transitions().tS if transitions.empty?
+ tS_Transitions( transitions )
end
- # Names of specified transitions.
+ # Simulation's *Ts* transitions. Expects a single array of +Ts+ transitions
+ # or their ids and returns an array of the corresponding Ts transition
+ # representations.
#
- def tn ids=nil
- tt( ids ).names
+ def Ts_Transitions( array )
+ transitions.Ts.subset( array )
end
- # Names of specified *ts* transitions.
+ # Simulation's *Ts* transitions. Without arguments, returns all the Ts
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of Ts transitions or transition ids as arguments, and returns an array of
+ # the corresponding Ts transitions of the simulation.
#
- def nts ids=nil
- ts_tt( ids ).names( true )
+ def Ts_transitions( *transitions )
+ return transitions().Ts if transitions.empty?
+ Ts_Transitions( transitions )
end
- # Names of specified *tS* transitions.
+ # Simulation's *TS* transitions. Expects a single array of +TS+ transitions
+ # or their ids and returns an array of the corresponding TS transition
+ # representations.
#
- def ntS ids=nil
- tS_tt( ids ).names( true )
+ def TS_Transitions( array )
+ transitions.TS.subset( array )
end
- # Names of specified *Ts* transitions.
+ # Simulation's *TS* transitions. Without arguments, returns all the TS
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of TS transitions or transition ids as arguments, and returns an array of
+ # the corresponding TS transitions of the simulation.
#
- def nTs ids=nil
- Ts_tt( ids ).names( true )
+ def TS_transitions( *transitions )
+ return transitions().TS if transitions.empty?
+ TS_Transitions( transitions )
end
- # Names of specified *TS* transitions.
+ # Simulation's *A* transitions. Expects a single array of +A+ transitions
+ # or their ids and returns an array of the corresponding A transition
+ # representations.
#
- def nTS ids=nil
- TS_tt( ids ).names( true )
+ def A_Transitions( array )
+ transitions.A.subset( array )
end
- # Names of specified *A* transitions.
+ # Simulation's *A* transitions. Without arguments, returns all the A
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of A transitions or transition ids as arguments, and returns an array of
+ # the corresponding A transitions of the simulation.
#
- def nA ids=nil
- A_tt( ids ).names( true )
+ def A_transitions( *transitions )
+ return transitions().A if transitions.empty?
+ A_Transitions( transitions )
end
- # Names of specified *S* transitions.
+ # Simulation's *a* transitions. Expects a single array of +a+ transitions
+ # or their ids and returns an array of the corresponding a transition
+ # representations.
#
- def nS ids=nil
- S_tt( ids ).names( true )
+ def a_Transitions( array )
+ transitions.a.subset( array )
end
- # Names of specified *s* transitions.
+ # Simulation's *a* transitions. Without arguments, returns all the a
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of a transitions or transition ids as arguments, and returns an array of
+ # the corresponding a transitions of the simulation.
#
- def ns ids=nil
- s_tt( ids ).names( true )
+ def a_transitions( *transitions )
+ return transitions().a if transitions.empty?
+ a_Transitions( transitions )
end
- # Names of specified *T* transitions.
+ # Simulation's *S* transitions. Expects a single array of +S+ transitions
+ # or their ids and returns an array of the corresponding S transition
+ # representations.
#
- def nT ids=nil
- T_tt( ids ).names( true )
+ def S_Transitions( array )
+ transitions.S.subset( array )
end
- # Names of specified *t* transitions.
+ # Simulation's *S* transitions. Without arguments, returns all the S
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of S transitions or transition ids as arguments, and returns an array of
+ # the corresponding S transitions of the simulation.
#
- def nt ids=nil
- t_tt( ids ).names( true )
+ def S_transitions( *transitions )
+ return transitions().S if transitions.empty?
+ S_Transitions( transitions )
end
- protected
-
- # Transition instance identification.
+ # Simulation's *s* transitions. Expects a single array of +s+ transitions
+ # or their ids and returns an array of the corresponding s transition
+ # representations.
#
- def transition( id )
- begin
- Transition().instance( id )
- rescue NameError, TypeError
- begin
- tr = net.transition( id )
- Transition().instances.find { |t_rep| t_rep.source == tr } ||
- Transition().instance( tr.name )
- rescue NameError, TypeError => msg
- raise TypeError, "The argument #{id} does not identify a " +
- "transition instance! (#{msg})"
- end
- end
+ def s_Transitions( array )
+ transitions.s.subset( array )
end
- # Without arguments, returns all the transitions. If arguments are given,
- # they are converted to transitions before being returned.
+ # Simulation's *s* transitions. Without arguments, returns all the s
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of s transitions or transition ids as arguments, and returns an array of
+ # the corresponding s transitions of the simulation.
#
- def transitions ids=nil
- return @transitions if ids.nil?
- Transitions().load( ids.map { |id| transition id } )
+ def s_transitions( *transitions )
+ return transitions().s if transitions.empty?
+ s_Transitions( transitions )
end
- # Simulation's *ts* transtitions. If arguments are given, they must identify
- # *ts* transitions, and are treated as in +#transitions+ method. Note that *A*
- # transitions are not considered eligible *ts* tranisitions for the purposes
- # of this method.
+ # Simulation's *T* transitions. Expects a single array of +T+ transitions
+ # or their ids and returns an array of the corresponding T transition
+ # representations.
#
- def ts_transitions ids=nil
- return transitions.ts if ids.nil?
- transitions.ts.subset( transitions ids )
+ def T_Transitions( array )
+ transitions.T.subset( array )
end
- # Simulation's *tS* transitions. If arguments are given, they must identify
- # *tS* transitions, and are treated as in +#transitions+ method.
+ # Simulation's *T* transitions. Without arguments, returns all the T
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of T transitions or transition ids as arguments, and returns an array of
+ # the corresponding T transitions of the simulation.
#
- def tS_transitions ids=nil
- return transitions.tS if ids.nil?
- transitions.tS.subset( transitions ids )
+ def T_transitions( *transitions )
+ return transitions().T if transitions.empty?
+ T_Transitions( transitions )
end
- # Simulation's *Ts* transitions. If arguments are given, they must identify
- # *Ts* transitions, and are treated as in +#transitions+ method.
+ # Simulation's *t* transitions. Expects a single array of +t+ transitions
+ # or their ids and returns an array of the corresponding t transition
+ # representations.
#
- def Ts_transitions ids=nil
- return transitions.Ts if ids.nil?
- transitions.Ts.subset( transitions ids )
+ def t_Transitions( array )
+ transitions.t.subset( array )
end
- # Simulation's *TS* transitions. If arguments are given, they must identify
- # *TS* transitions, and are treated as in +#transitions+ method.
+ # Simulation's *t* transitions. Without arguments, returns all the t
+ # transitions of the simulation. Otherwise, it accepts an arbitrary number
+ # of t transitions or transition ids as arguments, and returns an array of
+ # the corresponding t transitions of the simulation.
#
- def TS_transitions ids=nil
- return transitions.TS if ids.nil?
- transitions.TS.subset( transitions ids )
- end
-
- # Simulation's *A* transitions. If arguments are given, they must identify
- # *A* transitions, and are treated as in +#transitions+ method.
- #
- def A_transitions ids=nil
- return transitions.A if ids.nil?
- transitions.A.subset( transitions ids )
- end
-
- # Simulation's *a* transitions. If argument are given, they must identify
- # *a* transitions, and are treated as in +#transitions+ method.
- #
- def a_transitions ids=nil
- return transitions.a if ids.nil?
- transitions.a.subset( transitions ids )
- end
-
- # Simulation's *S* transitions. If arguments are given, they must identify
- # *S* transitions, and are treated as in +#transitions+ method.
- #
- def S_transitions ids=nil
- return transitions.S if ids.nil?
- transitions.S.subset( transitions ids )
- end
-
- # Simulation's *s* transitions. If arguments are given, they must identify
- # *s* transitions, and are treated as in +#transitions+ method.
- #
- def s_transitions ids=nil
- return transitions.s if ids.nil?
- transitions.s.subset( transitions ids )
- end
-
- # Simulation's *T* transitions. If arguments are given, they must identify
- # *T* transitions, and are treated as in +#transitions+ method.
- #
- def T_transitions ids=nil
- return transitions.T if ids.nil?
- transitions.T.subset( transitions ids )
- end
-
- # Simulation's *t* transitions. If arguments are given, they must identify
- # *t* transitions, and are treated as in +#transitions+ method.
- #
- def t_transitions ids=nil
- return transitions.t if ids.nil?
- transitions.t.subset( transitions ids )
+ def t_transitions( *transitions )
+ return transitions().t if transitions.empty?
+ t_Transitions( transitions )
end
end # Access
end # class YPetri::Simulation::Transitions