HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 E* I5 A& x* d; V! w; h/ R: ]
6 i! o# O) N% q. x. ~3 F
public Object buildActions () {
3 @2 b+ u) O7 q0 x( s( j/ z super.buildActions();2 F e6 c: Q( T; N) K b! H' ]: x+ _
u* E5 n6 x0 r$ ` // Create the list of simulation actions. We put these in& p: y( [+ M) M( H7 x3 @
// an action group, because we want these actions to be( l- k4 f# a- C& ]4 u8 I" U
// executed in a specific order, but these steps should( z% B4 O6 b0 E1 N- v' d
// take no (simulated) time. The M(foo) means "The message
% D& k. e% C4 c! k: f // called <foo>". You can send a message To a particular& G. R0 x$ y0 L+ e% P7 P
// object, or ForEach object in a collection.) r7 B% C0 h8 f
0 j- n; B/ S* @# X7 q( N$ {/ [( i // Note we update the heatspace in two phases: first run! x3 r# o( [3 d) |# z" b
// diffusion, then run "updateWorld" to actually enact the
0 W: @8 Z+ ^/ L // changes the heatbugs have made. The ordering here is
! B9 F' I/ d* @! F6 U- L, X' q# f // significant!& O+ ]% E `2 |* X7 J3 k( o X
6 ]1 h# V' E: r( e6 j- F0 }4 Y // Note also, that with the additional7 }7 m4 ~% l4 {8 A
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, K: u/ K- G% f! `& E' h7 L0 a: U' R // randomize the order in which the bugs actually run
7 u g' A0 Q4 J // their step rule. This has the effect of removing any
0 K9 p4 o* Y6 O2 u, Y7 e2 p // systematic bias in the iteration throught the heatbug
8 Y; z, X3 F, d! X, q3 F7 c // list from timestep to timestep
; q% L5 W1 m* w( a/ t * t. P2 R7 l$ I' U( Q$ O
// By default, all `createActionForEach' modelActions have
7 y0 j( g5 }$ i( V6 R0 d0 H // a default order of `Sequential', which means that the
# j' \! E: d( K0 Z3 w! N! f7 W // order of iteration through the `heatbugList' will be
. E N* S3 J# f% V5 X% O0 p // identical (assuming the list order is not changed% Z1 E; o3 i+ M+ V. E
// indirectly by some other process).7 s& I/ n. T: o
0 H( S1 o" o/ j2 P+ `! v* g( f modelActions = new ActionGroupImpl (getZone ());* w9 S+ X& l; I) h
5 g c, _( y6 S. U% [1 i
try {
7 ^( d& M* Y; A: J% _- b modelActions.createActionTo$message
: M! c5 {' j! @( v) H (heat, new Selector (heat.getClass (), "stepRule", false));& i4 y3 ?7 `9 N+ N3 z3 U
} catch (Exception e) {
8 U7 T6 ]' M, i6 L) V n3 H System.err.println ("Exception stepRule: " + e.getMessage ());4 U& n' K' q* z+ ], B# r
}" S2 a! m) B; j: U5 J/ k1 j
: z( W7 }% _$ [ try {
. m9 i) b/ K* H1 y' V0 O& y3 | Heatbug proto = (Heatbug) heatbugList.get (0);
) u" U2 a4 ^* p& M Selector sel =
2 o: y3 W1 c6 d2 z/ Y new Selector (proto.getClass (), "heatbugStep", false);
1 g" [8 Q; {3 S% `( e" g) k actionForEach =
4 O. n" ], ^* P5 A' J% a modelActions.createFActionForEachHomogeneous$call$ Q% k+ \6 l0 b! ~0 v
(heatbugList,
+ i" u4 Z, X3 X" i0 R' F- b new FCallImpl (this, proto, sel,1 R- g3 }1 {% Y8 S) [* D
new FArgumentsImpl (this, sel)));2 A/ p6 M# ?6 w% b+ W' w$ ^+ }
} catch (Exception e) {
- I1 w" O. ]5 ~( t7 r; B" N e.printStackTrace (System.err);
! B! t# t2 q+ [ }( X' u0 y+ d: F# d
( R. O+ W0 ~0 W* g; F/ y3 u syncUpdateOrder ();: w! P7 K5 e# Z8 p
+ r' Q& P& G6 a1 _0 s
try {
- L s( j4 Z" \% I modelActions.createActionTo$message
* j. f# j+ f- W& `2 Y5 ?2 U6 f* S5 j (heat, new Selector (heat.getClass (), "updateLattice", false));
5 D3 M1 b: b q) o$ M } catch (Exception e) {. w+ q( R( z$ q d! \
System.err.println("Exception updateLattice: " + e.getMessage ());
+ L/ i2 R9 f) O6 s2 g }, g0 c) w4 n9 L! e" o& w
* E( q- ^; H$ P$ l/ V // Then we create a schedule that executes the. ^, O; ?0 k6 K: T5 @
// modelActions. modelActions is an ActionGroup, by itself it3 j4 J. M" G2 t% F5 p
// has no notion of time. In order to have it executed in
7 ^' L+ s. G, Y- e" n7 p4 X // time, we create a Schedule that says to use the
) [- ^0 s" |2 L5 h- i/ p // modelActions ActionGroup at particular times. This
. C9 A0 @9 ~9 ^6 V) u( [ // schedule has a repeat interval of 1, it will loop every
x4 [5 I, U! e // time step. The action is executed at time 0 relative to
1 v0 x, H Y1 ?( r9 r! D# N) n7 a // the beginning of the loop.
1 X0 I. {, M( @ [, q$ E
* c2 D: x# }& H# y; n# ` // This is a simple schedule, with only one action that is
/ I! A8 P$ b+ m9 A // just repeated every time. See jmousetrap for more
. H5 @3 a I: x; h: l% L: ? // complicated schedules.
" a/ r) C7 w8 |& B
/ z ^- ]# T6 ]1 {/ _8 ^8 _ modelSchedule = new ScheduleImpl (getZone (), 1);
7 u4 N+ B& r+ m& Q; j% Z V modelSchedule.at$createAction (0, modelActions);
$ v: d! B3 M/ u. D) F$ @- @* u! B 5 w* O+ u3 X: _5 |
return this;& w' W3 y" R5 ^ b
} |