HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. a0 v# Z" ~% ]2 ^9 g; e% H& ^
9 ]) [8 y0 f9 f5 B# j public Object buildActions () { I9 t. B6 [4 s/ f# Y
super.buildActions();
* i" G5 a0 I# t( a5 J" p2 F
: k+ O2 p$ F4 n M J( V) P // Create the list of simulation actions. We put these in- Y) d2 c; ?& Y2 k0 ^! S
// an action group, because we want these actions to be
7 M. x9 _, F- H. ~" k, {7 A // executed in a specific order, but these steps should& q! i9 L+ Q2 A3 @$ q
// take no (simulated) time. The M(foo) means "The message8 O9 Y \- D9 ^* C
// called <foo>". You can send a message To a particular
# J" a' y( b- }8 A // object, or ForEach object in a collection.& ^8 l0 V0 W& b7 E! R1 }5 `
1 @ j# j1 f0 l( {) d* O
// Note we update the heatspace in two phases: first run& h" M. x, Y( o8 L
// diffusion, then run "updateWorld" to actually enact the8 q0 k$ y- r7 u# \$ |: h5 O# g
// changes the heatbugs have made. The ordering here is+ ]4 w* h, P; o1 z$ N- [" ^
// significant!
1 k& p* k8 f/ A" [ . R- U% }3 L9 ^4 n( W
// Note also, that with the additional: n9 I$ C$ r' B7 z6 k
// `randomizeHeatbugUpdateOrder' Boolean flag we can) g# r4 i+ c5 X
// randomize the order in which the bugs actually run3 o- e+ [2 Y$ i: k2 @, U
// their step rule. This has the effect of removing any( K6 q. b1 j9 Z; p
// systematic bias in the iteration throught the heatbug ^) X' X' x1 X3 d9 j+ h* k5 P' F' N
// list from timestep to timestep! z l8 u1 H0 v& G& L. x
, b' z/ Y% J; S& s8 ^! f // By default, all `createActionForEach' modelActions have/ P$ O& c1 C8 h1 U# x' d
// a default order of `Sequential', which means that the7 _5 y1 q% O" Z* @; x
// order of iteration through the `heatbugList' will be
! L( P; f! r$ H, g5 G // identical (assuming the list order is not changed' R6 b& ]7 O* m5 L; a
// indirectly by some other process).. W* u& u1 H0 a9 Z& X, p
5 K( F1 x9 M0 o( _
modelActions = new ActionGroupImpl (getZone ());" w* W- z% V5 X1 \$ L
2 X. h: v. D a try {' J, P4 y/ q2 j ^+ w1 T
modelActions.createActionTo$message& I8 h/ b+ M/ `% m4 A6 L
(heat, new Selector (heat.getClass (), "stepRule", false));
. K) Y, c( X6 w. H+ D, y" z! a- C } catch (Exception e) {
) |$ ^* f8 x& i; m8 p8 W } System.err.println ("Exception stepRule: " + e.getMessage ()); H3 n1 F! H5 _5 W3 J
} i- M, X- Y2 `8 j9 h' h6 x, V
3 f. @. e/ F9 p2 z( m1 P! K1 Z/ u- U try {
( t3 {( B; L9 n$ K Heatbug proto = (Heatbug) heatbugList.get (0);* v( d2 H" J) e. h% Z. {2 n
Selector sel = / ]5 r$ Z- x" |; F4 O; q/ N
new Selector (proto.getClass (), "heatbugStep", false);7 k! r! I$ q& `6 J
actionForEach =( d o) `- A. }. z: R0 S4 o2 l9 H
modelActions.createFActionForEachHomogeneous$call
% n. C! J7 {; p; U: G- J (heatbugList,
) G* Q! i( J+ t new FCallImpl (this, proto, sel,
( h4 C# j0 ?; p/ N0 d+ t" T$ Q new FArgumentsImpl (this, sel)));8 g/ Y" ?& E' j, y8 d4 i0 k. H
} catch (Exception e) {( g5 |8 t0 Z% L) n( L) ~& a- z
e.printStackTrace (System.err);
: {* T3 A3 k9 }7 u }
! H6 H) ]! T3 C+ {+ ^. ]
! p6 b7 O" b( q4 H* x F syncUpdateOrder ();
: m& [; H. f* R3 ]) t- ]
6 F4 W" ]/ h6 m; Z% Y try {8 }; A! L" O3 D% D! A0 k/ [. P4 c$ ~
modelActions.createActionTo$message 5 ~# l, K9 T3 q" k
(heat, new Selector (heat.getClass (), "updateLattice", false));
& D/ r1 C, H$ P1 J# B- s7 s } catch (Exception e) {4 H7 X5 b% z. t. {. n
System.err.println("Exception updateLattice: " + e.getMessage ());
p8 ~0 D! `6 v! z, ^* U }5 O, y6 L+ V7 k) x4 _; d. Y) K
N- F4 z: c9 }$ \; M; o; {! q" _' F
// Then we create a schedule that executes the+ E% ]! c! r- D/ C
// modelActions. modelActions is an ActionGroup, by itself it
; f) a. [3 k* k5 N/ L' x7 J // has no notion of time. In order to have it executed in) {. {' z; _, _2 k
// time, we create a Schedule that says to use the# f/ D" l; ?: }! Y! E
// modelActions ActionGroup at particular times. This
, V, D# R$ f( \3 y c // schedule has a repeat interval of 1, it will loop every/ ?# m' T6 G( D
// time step. The action is executed at time 0 relative to7 L* A9 ` ?+ l8 D6 X
// the beginning of the loop.( W; l& c$ G$ d8 D7 h8 k O
! t8 a8 J( J9 z // This is a simple schedule, with only one action that is6 Q; ?3 p4 z O, m
// just repeated every time. See jmousetrap for more
- @2 `3 P7 e6 ^ // complicated schedules.
6 d: z$ R3 T( R \
& A$ c& D- w0 n) |5 U modelSchedule = new ScheduleImpl (getZone (), 1);
' d% ]/ b4 M4 t) O& m& c2 [0 x7 i modelSchedule.at$createAction (0, modelActions);
7 z% o& d3 w3 ~3 ~3 R3 \ 9 o9 c: s) a( M7 {
return this;
" S, X# F+ ]1 T i } |