HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" N0 Y: i/ ^2 M) s7 e/ W! `8 n/ X" e$ J% b
public Object buildActions () {6 b4 }: X4 q1 m0 w$ Y( J( R
super.buildActions();# f2 ?! Z. q, J$ C2 \* k- ~' G
, |8 _+ S* t: X) N* Y& ^
// Create the list of simulation actions. We put these in8 w1 n" M& a' E+ V1 `7 s3 @
// an action group, because we want these actions to be" J8 a0 Z9 [: Y% H
// executed in a specific order, but these steps should/ H( {* N/ {* m: G
// take no (simulated) time. The M(foo) means "The message
5 J3 ` F0 N7 Z" o( g- n // called <foo>". You can send a message To a particular& f, z, x+ N; B! [0 M+ |' {
// object, or ForEach object in a collection.9 F5 I1 M+ F9 b8 I8 V' y
+ H& ?) h1 h& K // Note we update the heatspace in two phases: first run% [/ W7 n, ^2 x& B4 A* t: U
// diffusion, then run "updateWorld" to actually enact the' ~+ K1 Y; A% O& a0 y) u V
// changes the heatbugs have made. The ordering here is
! @3 A" U' W$ R* Q // significant!
$ s9 {1 q6 p( I! I" |6 a, J' }5 V + R5 `9 W3 R/ o) e) d
// Note also, that with the additional2 n- T. \$ Y1 u0 |
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 L- N4 o, N$ J } // randomize the order in which the bugs actually run
) H" v7 X! ?( I // their step rule. This has the effect of removing any
. o# h/ [9 T, ] // systematic bias in the iteration throught the heatbug, D9 O$ w2 p" n( o. i
// list from timestep to timestep8 C D: u$ }. x$ u8 |
7 i' Z7 i6 A @: \$ e' X3 C9 {8 G; g // By default, all `createActionForEach' modelActions have H, P$ C8 \8 X0 ~, x' w6 v
// a default order of `Sequential', which means that the: {& u `( K8 f$ o; M d0 L9 u5 a/ P
// order of iteration through the `heatbugList' will be" t% ^+ D* ?+ b8 }/ j
// identical (assuming the list order is not changed* a% h; j0 G. A6 E$ E* B# |
// indirectly by some other process).
8 Q/ j7 A: ~8 Y8 g" d4 o( S
. E1 _" i# M2 y modelActions = new ActionGroupImpl (getZone ());0 c5 W9 b, K$ [" Q( P: s
9 _4 W; V. m4 [ try {( L n, h$ h6 K3 U; ]% M
modelActions.createActionTo$message
$ w# j! a h$ s (heat, new Selector (heat.getClass (), "stepRule", false));7 j# F( C2 o, u
} catch (Exception e) {
" [( T" M3 O! v+ t4 A+ C& E System.err.println ("Exception stepRule: " + e.getMessage ());
+ Z S' P0 Q( P: |/ V }% m8 N- T8 t& E7 e a; I
# E" t7 K! o0 _/ x) o( i8 I( {; | try {
: S6 n' Q8 D' w5 J1 P3 Q) y Heatbug proto = (Heatbug) heatbugList.get (0);
0 X3 J) [2 u( X. ?2 ] Selector sel =
5 s# U8 H( w7 c& ?9 T% [7 b new Selector (proto.getClass (), "heatbugStep", false);2 {9 }8 T- L" M5 G6 ?
actionForEach =" s) W+ p( ?! h6 M# C0 d, P6 j c
modelActions.createFActionForEachHomogeneous$call- V' @# S3 S. u( a1 X
(heatbugList,& [* H) m* H7 A$ C1 C% r0 k
new FCallImpl (this, proto, sel,& {/ M: ~' Q9 W+ L- V {9 e
new FArgumentsImpl (this, sel)));. z3 W( J; b9 q) J
} catch (Exception e) {
# m( T- g0 f1 p: x' X' ` e.printStackTrace (System.err);
6 A- X* e8 R0 q- [( E! i# p& Y }; [7 i/ f; `8 e* y
# a5 ?5 R' ], }, G syncUpdateOrder ();0 G% h) ?8 K8 g! r$ x# x, T' X) m+ o
9 d5 D/ A3 g7 T- I- o
try {
0 v" Q) ]( i7 S9 F: S/ l modelActions.createActionTo$message $ `: S! ]6 l4 \3 I0 L7 c' V
(heat, new Selector (heat.getClass (), "updateLattice", false));& I. a6 J: t2 t6 p& {
} catch (Exception e) {/ H/ t% R$ S; l: P/ L/ M/ _4 Y
System.err.println("Exception updateLattice: " + e.getMessage ());; H# _. D1 s) R9 R! h( g$ |5 t( F
}
2 a4 r" t, A* d- f6 ~4 B
) n" N- j" D+ ~% Y // Then we create a schedule that executes the
1 a, y0 n% {/ B // modelActions. modelActions is an ActionGroup, by itself it
7 B/ l2 w; o0 ]0 K/ f) j( a' e* w7 G // has no notion of time. In order to have it executed in
5 r! J* i% H1 G // time, we create a Schedule that says to use the
B4 g: y8 c u/ @$ k; g // modelActions ActionGroup at particular times. This
/ b* V" w \2 y) F8 }$ L // schedule has a repeat interval of 1, it will loop every! c; H& U: S$ n4 X$ n
// time step. The action is executed at time 0 relative to( N7 x& G7 c+ `
// the beginning of the loop." T% [( c% o9 R
\) W- n* n9 ~' g/ [" j0 t& P // This is a simple schedule, with only one action that is
+ u7 @" B! q. z8 N2 k8 z' B // just repeated every time. See jmousetrap for more+ j, I- ?5 M$ \9 m4 y6 d4 r4 Q
// complicated schedules.
7 D2 b6 Z8 C. T' M3 \' i, C 8 ~& S) c. T* @9 {" V0 s
modelSchedule = new ScheduleImpl (getZone (), 1);
& z( W3 L- O3 z1 Z" G modelSchedule.at$createAction (0, modelActions);. L7 D1 ?( [$ f' O( C0 s
% C5 L3 |' H: @, [% p5 x' M5 m7 _3 ` return this;
4 H1 L. i! c( Q. Z" P } |