HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 p/ O' j8 O/ P/ ], r1 j% y$ |! {' C& S' t
public Object buildActions () {
. R) n3 I! {& |) P" q( u9 R3 Q( K super.buildActions();% y1 {& X4 L; A1 D5 n5 T; B, W* F7 N2 [
! N- U! r, i: g1 Y
// Create the list of simulation actions. We put these in/ ]1 ]" \& l; V
// an action group, because we want these actions to be1 M o! o* R5 `7 y- V( E/ p
// executed in a specific order, but these steps should' {* q! Q+ w) I0 T
// take no (simulated) time. The M(foo) means "The message
6 ?' E9 v1 g# V* b$ J, M9 B // called <foo>". You can send a message To a particular; G: p: l4 N ]7 v
// object, or ForEach object in a collection.& e5 y* m( ^ ?
7 m |9 T+ ~9 j& G // Note we update the heatspace in two phases: first run' `: ]. A! b2 o% Q' |1 q. U
// diffusion, then run "updateWorld" to actually enact the2 i" e9 b9 ]$ ]) z
// changes the heatbugs have made. The ordering here is
7 t4 S# A* }! v7 M/ @4 }: l% u // significant!2 m8 n3 T1 u- r+ K& {% y
+ g( E7 f' A; Y) T3 ~1 p // Note also, that with the additional# |' N! c% Y- x; |- s# r
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( W8 R; `# p$ N) v // randomize the order in which the bugs actually run; g- E0 k2 S9 z/ k( a
// their step rule. This has the effect of removing any
9 \7 j* K, O0 {7 ?% V+ ^- v6 P // systematic bias in the iteration throught the heatbug: L( ~: m$ t% _! P
// list from timestep to timestep0 B" `% \2 V! U+ k {- p
# [+ ^: y, O4 }
// By default, all `createActionForEach' modelActions have! Z4 i+ B! s- C; U. u
// a default order of `Sequential', which means that the
# y, K5 f/ u2 r. K' n" t // order of iteration through the `heatbugList' will be3 K7 d0 L% G. V9 ~2 ]: v# ?0 P
// identical (assuming the list order is not changed B$ S8 P1 L" x% u
// indirectly by some other process).
; ]5 v' [2 m O5 l. P - \/ d& U8 B% t# V
modelActions = new ActionGroupImpl (getZone ());( M; ~3 x4 U b
) o" c! I! a" d7 Q. \- T& a try {
1 k7 q% u' l0 d9 C6 M0 u8 n$ f modelActions.createActionTo$message7 C& o9 \# o; ^ \# D
(heat, new Selector (heat.getClass (), "stepRule", false));. @9 B: j# I2 J0 Q
} catch (Exception e) {
. Q: G' g: y6 L1 P) R( d- K, | System.err.println ("Exception stepRule: " + e.getMessage ());
( \3 b% K( d" v) Q* T. }, ~) k, _3 [ }
# k6 ~$ h, H9 |; D: \7 S7 Z9 ~9 s( ]+ [7 w# ]# }" C% n
try { _/ A; L; l) J0 a$ p' [
Heatbug proto = (Heatbug) heatbugList.get (0);3 @8 ]- z, {9 a% v7 i S
Selector sel =
1 V- ]. m# O) L7 ]+ Z: t8 ~! k7 ] new Selector (proto.getClass (), "heatbugStep", false);
7 _, |4 o' j: h# Y+ J actionForEach =$ C5 h% Y0 [, ?7 K
modelActions.createFActionForEachHomogeneous$call9 c6 D3 o9 H# u# o( k2 W
(heatbugList,8 I! a4 H6 m! Y
new FCallImpl (this, proto, sel,
( A- c* f5 N) t new FArgumentsImpl (this, sel)));0 n. i, j4 K0 X
} catch (Exception e) {2 V/ g. W: r l; B$ ]
e.printStackTrace (System.err);
% u Z. w4 [" o( ]: H( Z! ? }
/ @, P5 Z( H, Y3 m/ D; q , e& ^4 T% \& @4 C
syncUpdateOrder ();6 j4 n5 w. Y) d. i* F& D0 [% p& ~7 g
) K/ g+ b; j' z
try { K: n* ]* \$ A2 |8 a
modelActions.createActionTo$message
, Q: e& j2 G H: I( }6 P (heat, new Selector (heat.getClass (), "updateLattice", false));
, r' [, w! N0 i5 M3 P2 T6 h } catch (Exception e) {2 Z# F) \. y* ^2 i8 O
System.err.println("Exception updateLattice: " + e.getMessage ()); s9 [: F, ^8 L- V3 A1 Y$ {% m
}
# g5 z" u3 s; k9 |
+ h6 E' |# {2 M: C' k! P, n // Then we create a schedule that executes the# [% C5 l8 }& o6 l8 l5 q
// modelActions. modelActions is an ActionGroup, by itself it
0 J+ u) P5 f8 r; o* r& i" Q // has no notion of time. In order to have it executed in
1 |+ t5 F L9 K( { // time, we create a Schedule that says to use the/ t$ Y( D& i% }: Q$ b+ g' p, h
// modelActions ActionGroup at particular times. This3 P. e3 ?, i; G
// schedule has a repeat interval of 1, it will loop every6 y- o, N0 ^0 S8 K7 c+ f
// time step. The action is executed at time 0 relative to* f9 |* l! f- w5 z% ]
// the beginning of the loop.# _1 O4 K4 u, |5 M! W- P \1 ^
! J& m8 |" a5 S3 d) g" f$ n" [+ X' X
// This is a simple schedule, with only one action that is
5 t9 }& n# P x- K3 J- Z. l+ i$ B- u9 A // just repeated every time. See jmousetrap for more
! V* [/ O$ ?7 p+ N // complicated schedules.. t# G5 v$ t) ]+ g; D, a
# U9 ^2 U& x# [
modelSchedule = new ScheduleImpl (getZone (), 1);
& F7 P7 L* m& v, ] {) A modelSchedule.at$createAction (0, modelActions);/ l( o8 J$ h* E
( N: a1 U! w- z2 }3 P
return this;, {" F0 j: c8 G9 l) R' x- P3 Q3 ~$ N
} |