HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ S9 T b1 G! m5 S7 |; g; x
0 e; C; \6 I; K6 N% A- _ public Object buildActions () {* U0 K: i- V0 E4 c
super.buildActions();6 e: Q' Z# p! D, F, U8 Z# E
9 J8 T2 K! J& @! h
// Create the list of simulation actions. We put these in0 C0 @ Y7 j, u: l/ I
// an action group, because we want these actions to be
3 } Y; Y: J9 s7 s% c: c // executed in a specific order, but these steps should
- }1 a) Z5 b! ]3 `1 l, B5 o // take no (simulated) time. The M(foo) means "The message5 Y7 }& W. x3 y9 D ]! ~! `
// called <foo>". You can send a message To a particular
0 I) ~) m, J$ @* c8 @! Q // object, or ForEach object in a collection.
/ V9 S& i, @+ n8 s% \ + S; a' M& i' o$ u; p9 |
// Note we update the heatspace in two phases: first run
7 S3 u+ R5 Z4 \- Z8 e |4 j // diffusion, then run "updateWorld" to actually enact the; I" K1 T' v3 x+ U( m) S
// changes the heatbugs have made. The ordering here is
" u, G3 m( D4 @ // significant!
/ v1 R8 Q: o/ g& m0 y/ Y. F ! W7 m1 }$ j. d
// Note also, that with the additional: b4 k$ k1 H6 J2 [3 F" ^/ r
// `randomizeHeatbugUpdateOrder' Boolean flag we can( {; \' }7 ]/ B, S$ \: S$ h1 {$ q
// randomize the order in which the bugs actually run
3 s& T( E8 L* z4 X4 F // their step rule. This has the effect of removing any4 K! Y- M2 U4 s; f! ~6 W' l
// systematic bias in the iteration throught the heatbug6 k8 V0 u/ e; e% S1 y+ ~
// list from timestep to timestep3 \& {1 H; H, Q% V$ k3 ?
6 t+ E& g, ~ ?1 E n1 Y& Z2 i5 ` Y // By default, all `createActionForEach' modelActions have
, B" u$ @& m: C3 [ g // a default order of `Sequential', which means that the
5 j1 w! J. q' A+ Z // order of iteration through the `heatbugList' will be r; {( u, R1 w8 l# _, Y/ ]- p
// identical (assuming the list order is not changed3 `8 L( G/ t2 y0 n7 h
// indirectly by some other process).* [1 H2 D8 c( l& V
0 z% \. q3 h' a: a0 i' N modelActions = new ActionGroupImpl (getZone ());
3 `, G2 x% }- k7 r8 O* t3 W! L. c8 p4 A" x" B. k) i& Q( d9 ^3 I
try {5 y% w; w* F5 o! I
modelActions.createActionTo$message: X+ L! g6 R) V) d
(heat, new Selector (heat.getClass (), "stepRule", false));
8 p3 v" T1 a5 b4 k5 t/ n/ n } catch (Exception e) {
$ ^& r8 Y. V- F4 i9 C- y System.err.println ("Exception stepRule: " + e.getMessage ());
& w+ E: v+ m, r# R0 K9 E7 ]( ^ }
' p6 p8 j$ V* Z3 g" h
! N! \4 w) {" G+ ^ try {2 a1 U9 ]/ {$ v
Heatbug proto = (Heatbug) heatbugList.get (0);6 n8 y; o+ B2 i7 |: h
Selector sel = . Z$ A. P8 Z/ q# P( T
new Selector (proto.getClass (), "heatbugStep", false);* A* T6 G' |/ K* Z
actionForEach =
8 Y* J) G* `' B8 R modelActions.createFActionForEachHomogeneous$call
5 u8 b& s' j, h4 p% \! i (heatbugList,
' a7 u8 v& l p4 g/ U. |5 a. Q3 t new FCallImpl (this, proto, sel,
8 n* T2 C. ~! c' q, v new FArgumentsImpl (this, sel)));
- w" W& o. }6 ?/ a. t. l+ x } catch (Exception e) {/ V4 V6 Q4 l; \8 Q0 L
e.printStackTrace (System.err);
6 b8 q! I& l" O) x$ U# V+ h }. O' V$ i @( e8 F7 ]
9 I0 L. U& j. A9 G, f) f, n8 t syncUpdateOrder ();
5 I- p( P" d- V0 W# X
/ t" l( X. n; m! ?: h/ k try {
+ S3 t- C2 x1 i' K9 S/ y modelActions.createActionTo$message
9 O2 `' Y! ~- H (heat, new Selector (heat.getClass (), "updateLattice", false));
3 v0 S& d% g( }% r2 B } catch (Exception e) { ~! d2 q/ g. {: U' D* M
System.err.println("Exception updateLattice: " + e.getMessage ());3 u# d& p! W" B4 m
}
0 Y) X6 z) r7 ~0 B9 P: t " V8 p- i! l+ k! Z6 l/ I8 w
// Then we create a schedule that executes the
% u a! O- N+ n: f/ C // modelActions. modelActions is an ActionGroup, by itself it- U4 g* ~& `- I+ @9 B+ L* F: A1 U
// has no notion of time. In order to have it executed in
# N ^2 d& g/ i# C% \ // time, we create a Schedule that says to use the% _2 J$ B: Z; y7 z; u c
// modelActions ActionGroup at particular times. This
o7 H0 V8 P6 A3 m* Q // schedule has a repeat interval of 1, it will loop every t( V" g3 W. @$ j
// time step. The action is executed at time 0 relative to
- N5 }8 ~) K8 `5 }/ a // the beginning of the loop.
/ F( ~# h; W6 ?
$ `2 A) L; V) I X& l( ^ // This is a simple schedule, with only one action that is% C# P0 b9 n" B6 q* S
// just repeated every time. See jmousetrap for more, t/ ]5 y4 S) i0 s( F W% s
// complicated schedules.
7 J- p4 B' q- m. Y6 `) X& F# u
/ i$ Z/ c. \, O0 n: A% C! x1 z modelSchedule = new ScheduleImpl (getZone (), 1);
- F" L- I6 Z! B1 b modelSchedule.at$createAction (0, modelActions);
/ d0 |* [8 `: S
' J4 j; l7 M* k" y! d& b6 y# Z) O return this;+ n& ~) t4 u0 m h) `, G4 i) c
} |