HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: b# A; R# ]0 L1 v& W9 D, `$ \8 N; R- @& x
public Object buildActions () {
3 e {) X+ `0 Z- H# K2 _/ ^8 n super.buildActions();
" `( S. I7 S) e6 _2 n- z. R, I1 O $ q3 A$ t7 Q( p7 g4 g
// Create the list of simulation actions. We put these in
! t. c2 r# c8 Q" z$ G7 z% E // an action group, because we want these actions to be
0 k( c2 J1 k. m" n- a6 v" s // executed in a specific order, but these steps should8 U; r( q; L5 S
// take no (simulated) time. The M(foo) means "The message" `0 D" O6 `8 ?( W
// called <foo>". You can send a message To a particular0 l2 _9 u) M6 D% g6 v# z
// object, or ForEach object in a collection.
! t/ R1 s$ X0 W7 P9 w! C+ H6 X' U& C 4 P9 q7 W5 Z* R9 g
// Note we update the heatspace in two phases: first run
/ O0 w4 V$ T' }- H' @ // diffusion, then run "updateWorld" to actually enact the
; ~6 S( M* r p( `+ g/ i1 x% Q. d- a // changes the heatbugs have made. The ordering here is+ s. d$ _3 L7 P. o% F
// significant!$ g0 v' \$ Q6 E5 C
& s$ {8 ~! u9 O* n/ i7 A // Note also, that with the additional3 C& o9 i) ^! O: G- g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( P t3 @2 L1 z: I$ G8 q // randomize the order in which the bugs actually run0 l; V2 ~4 i5 `% d4 _6 I
// their step rule. This has the effect of removing any" J7 h+ M! a5 E# f0 b
// systematic bias in the iteration throught the heatbug" R3 b2 l2 _4 H- G+ b" n
// list from timestep to timestep
6 V, m5 k, y c, {& ?# b0 B: D: I
! w. K" E* O6 V8 z: [ // By default, all `createActionForEach' modelActions have/ `/ ]$ A. A1 u: z
// a default order of `Sequential', which means that the
, H5 a8 O; c1 E2 q7 L6 \; p // order of iteration through the `heatbugList' will be# A6 v, r' Q/ `
// identical (assuming the list order is not changed
( x e8 O w$ C: }/ t // indirectly by some other process).3 S( {) }& x, o( B, \- Q
$ L* f, B6 d! N
modelActions = new ActionGroupImpl (getZone ());' L- y. T1 O& I
7 f1 o: o+ _1 c% ?; I
try {2 {! T$ N2 Q0 P0 V+ @
modelActions.createActionTo$message! ?) R: v( e5 ^$ n
(heat, new Selector (heat.getClass (), "stepRule", false));
6 A- f; k6 @1 m2 I* M } catch (Exception e) {0 l5 M( Z8 z; p4 O7 ?( M
System.err.println ("Exception stepRule: " + e.getMessage ());% c: Z: }2 y3 ] l" P& K
}
% _8 o6 q2 o5 v) a9 w" V3 R7 {) Y
3 q3 t9 A* F6 ^+ ~ try {
5 X6 p, j0 h9 u Heatbug proto = (Heatbug) heatbugList.get (0);
7 S% m) r# o3 i" h Selector sel =
% `: v" Z2 X0 E4 ~! `( [4 @ T new Selector (proto.getClass (), "heatbugStep", false);3 T) `5 _: a& `! R% F' q
actionForEach =: E2 P1 _# X/ ~. P0 ]3 _
modelActions.createFActionForEachHomogeneous$call
! Q. j! t4 V) {# P" C (heatbugList,: ]$ d7 o( v. ~8 F
new FCallImpl (this, proto, sel,! T; m1 r( u, B9 k3 G; h
new FArgumentsImpl (this, sel)));
) m: b0 f6 G) Y4 b- h7 l- s } catch (Exception e) {
" Y! D; y( ?: s3 v2 t" V7 ~ e.printStackTrace (System.err);
w% S% x& v; w }
5 I0 i/ q$ j" X: @: R( N
/ |) r: x& v( Z V6 Z, l syncUpdateOrder ();
8 w2 m. C; X2 n& F
5 ?' @% f1 a+ I" t3 D0 V, A try {+ G+ h: F4 o9 [- ^, {" `( F
modelActions.createActionTo$message
, `! g4 ~2 v. E (heat, new Selector (heat.getClass (), "updateLattice", false));
2 s, }% C1 a- B+ f& u& r/ P( X } catch (Exception e) {
' V& m8 v2 g3 S7 y/ X. Q; ]/ a( _ System.err.println("Exception updateLattice: " + e.getMessage ());9 N3 ], ]# G; O4 P
}
, C. {6 g1 l, k
# w+ E: [0 i/ ^0 X( a9 ~6 a // Then we create a schedule that executes the
* v5 h. Y4 A( E; W2 d" u8 D) U // modelActions. modelActions is an ActionGroup, by itself it
9 x" @# L! q$ a7 P // has no notion of time. In order to have it executed in
8 F' A2 Y- e$ P // time, we create a Schedule that says to use the3 o& i- f; C4 q( h; h
// modelActions ActionGroup at particular times. This
# J( R* B% h) Q) | // schedule has a repeat interval of 1, it will loop every
. A7 _( s6 S4 R9 J // time step. The action is executed at time 0 relative to
1 z! D+ U$ K3 C! Z // the beginning of the loop.
# ^3 P) C$ \& `3 Y
9 b3 w0 C# v3 u // This is a simple schedule, with only one action that is
: l8 M, h6 m6 A' k U- p3 S // just repeated every time. See jmousetrap for more* ]5 t/ k* `5 q3 L# k8 ?; _
// complicated schedules.3 D: t$ o b% j) c! i3 `' X5 W
9 G* |9 ?% I2 W2 O0 j4 l9 \" K modelSchedule = new ScheduleImpl (getZone (), 1);
3 i! c$ T$ D( \) ^( R modelSchedule.at$createAction (0, modelActions);% ~8 r( A/ R2 T1 L" j
+ r# L9 @1 m/ W" W1 C: ^6 l ^ return this; b. J5 ^; t1 \# c" Y
} |