HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 ]$ ^ e$ G9 H$ A: @. \
% g3 O7 p8 p. x& N public Object buildActions () {& U# w8 s, n @$ m9 b q% S
super.buildActions();$ L, E$ h: {* k3 z4 @" W
" l( Y9 A+ A# A: m& ?& p/ z
// Create the list of simulation actions. We put these in
7 m6 {3 K* }! j# z // an action group, because we want these actions to be
/ u& G1 ~" d8 I1 R // executed in a specific order, but these steps should2 @0 Z# \/ z+ I, R( u, M' C
// take no (simulated) time. The M(foo) means "The message. j) Y; b3 K) z+ a( R# B4 J
// called <foo>". You can send a message To a particular# t$ m9 i6 M& c8 F4 _; Q0 E
// object, or ForEach object in a collection., Z2 r5 e O, x# h& _
/ M3 I; o9 y: N
// Note we update the heatspace in two phases: first run& V% Z. m8 E' i7 C" G6 ?8 _+ x
// diffusion, then run "updateWorld" to actually enact the# l# s0 F( M$ ~
// changes the heatbugs have made. The ordering here is! ?; B+ ?" `. h& r' z
// significant!
& p# }7 c8 k5 y5 _" u' P1 T8 ] ] % v. E e# q% G" w+ t8 h% ^% `
// Note also, that with the additional
- {. O" s5 K. ?" A // `randomizeHeatbugUpdateOrder' Boolean flag we can) b# @: `0 B" V2 ]5 B
// randomize the order in which the bugs actually run
% i; [, s- ^# H4 l# f; x // their step rule. This has the effect of removing any
! z) j! Z) A5 C3 ^ // systematic bias in the iteration throught the heatbug+ z. i' s8 E: Q* `, i: W
// list from timestep to timestep" w$ v% h1 X% R- b$ b/ c
$ w8 y) D8 p2 q1 t+ {
// By default, all `createActionForEach' modelActions have a# d. {1 V+ y( `3 X% c2 C+ |" U
// a default order of `Sequential', which means that the
, _6 w4 G& \! x( G( [% G: y // order of iteration through the `heatbugList' will be
5 r4 r7 i% v: j; A* | E' N ?. K; O // identical (assuming the list order is not changed, H0 \! `7 s* I. y& m
// indirectly by some other process).
L+ Y1 @9 x. q- Q$ |2 Z / u& J+ L- y) b4 b6 \0 x
modelActions = new ActionGroupImpl (getZone ());9 m" f9 G2 u; j5 B) S0 D
4 N/ b7 u6 h2 X" S3 a2 E8 K
try {
8 L9 N- r; l" ?' \+ l+ |) j modelActions.createActionTo$message
8 } t c& _3 k7 y5 U0 w/ M2 \ (heat, new Selector (heat.getClass (), "stepRule", false));5 K& w: t0 L# G7 K8 q; i
} catch (Exception e) {
% e5 l( U- p0 H5 L System.err.println ("Exception stepRule: " + e.getMessage ());
5 [$ W4 h2 m$ R }
* l" B: Z: s: o- Q4 i: ]' T) j3 {
try {
7 D2 h$ F9 i, \. D( k! G Heatbug proto = (Heatbug) heatbugList.get (0);
! T6 p# }/ x0 N5 A Selector sel =
$ I6 Z( J5 {" ]" I9 r/ [, ` new Selector (proto.getClass (), "heatbugStep", false);/ H/ U3 y1 v5 T' E1 H4 q
actionForEach =
' D" ~" G6 G5 P1 Z: d& z5 e. o modelActions.createFActionForEachHomogeneous$call/ X8 G# W9 P' O6 R
(heatbugList,8 O0 |% F# t6 R
new FCallImpl (this, proto, sel,
1 S# f6 e5 k- W% I& y3 W new FArgumentsImpl (this, sel)));' o% ?1 v2 c8 Q9 R0 y, `: M! {# G
} catch (Exception e) {
6 g8 y" h; ]8 g4 n- q) D2 w9 K6 f. V+ ] e.printStackTrace (System.err);
2 u- p# w, }4 N( ?+ h }$ X5 V6 I$ L* @) [6 T
7 v. |! q2 c/ t. H, r0 ` syncUpdateOrder ();
: I2 l9 R+ |" A+ D$ ?9 E. E* E2 C
* E; g0 u1 u0 F- U try {
+ L) t: Y" Q! ]' W$ t9 ?- }, H modelActions.createActionTo$message $ \" {1 i# U; ]5 b% `4 O
(heat, new Selector (heat.getClass (), "updateLattice", false));0 ^; [9 G' I; w- k: G, q. f ]( t
} catch (Exception e) {
+ F8 F" h8 t! V9 }, h( Y System.err.println("Exception updateLattice: " + e.getMessage ());7 l" [ Y% d/ M9 m6 B
}
) [; A1 `" Y% v4 |) v- m$ y
, G% f: U4 N! f I // Then we create a schedule that executes the
- g5 c3 i. t' h& j& f3 \9 u4 D, u; M" P // modelActions. modelActions is an ActionGroup, by itself it& M& X& M. [( z1 Z) i" }
// has no notion of time. In order to have it executed in, A* O! C) w$ g/ t Z5 T* v
// time, we create a Schedule that says to use the( J0 @$ L7 G, N6 y% Y
// modelActions ActionGroup at particular times. This2 E8 [/ l' p9 t% A: G- ?: `, z7 N ?
// schedule has a repeat interval of 1, it will loop every
: c& b. `' d9 H, s* E/ K, j // time step. The action is executed at time 0 relative to% _+ F: y3 A2 E5 S' @
// the beginning of the loop.$ K" s# }5 w6 w
4 B$ f6 W& z3 ~! l H' U' K, u% q
// This is a simple schedule, with only one action that is; s: f& R$ L; I
// just repeated every time. See jmousetrap for more; b* O P0 ^- B3 }; o3 b* ]
// complicated schedules.
8 v/ c1 U$ w1 o
, R @& B* l" d4 P% }0 ]( \ modelSchedule = new ScheduleImpl (getZone (), 1);4 o; J$ e2 j" [+ D. J' J0 c5 J$ P/ i
modelSchedule.at$createAction (0, modelActions);/ {7 ]# x8 ]# i! f* g
) x/ E- z8 W" a# ^) c8 [
return this;+ @9 u( t6 @. E0 ?- n; V+ m1 L
} |