HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 L1 k- Z) G( ^- ?6 u
# Y7 Q |! E+ H& F8 j1 h9 L
public Object buildActions () {, s# |* m; c# t* G& B" _% o
super.buildActions();
% F0 k3 V5 M/ U; w
6 s. U A% k; D" q1 z // Create the list of simulation actions. We put these in
, K1 A$ o! L0 @6 C // an action group, because we want these actions to be) ?4 C4 U/ D; ]) w8 K
// executed in a specific order, but these steps should
% c: V# ` O8 p2 c5 G, w3 S. } // take no (simulated) time. The M(foo) means "The message+ u0 t5 ^- J9 F: x
// called <foo>". You can send a message To a particular
& W, I- ~) l/ @ // object, or ForEach object in a collection.( S1 \& p& y# `. T% {8 h
8 p! ^% O* H! \( v& m. L- g // Note we update the heatspace in two phases: first run
( H5 U; e+ o2 l; z // diffusion, then run "updateWorld" to actually enact the
/ d5 U; F) @0 }2 y9 m // changes the heatbugs have made. The ordering here is
% p4 M8 g2 s! f+ H8 U // significant!+ c5 [- [+ \+ a8 m' H2 y
( U- k9 ?0 Y- E2 g$ R6 {/ G, A
// Note also, that with the additional
! J+ |: d- W' B, m4 l" W. P5 j2 j // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ d/ q; Z' L: E // randomize the order in which the bugs actually run# f, b% B; B3 Z5 ^
// their step rule. This has the effect of removing any* J8 q/ P' o4 J+ R0 D9 K, L
// systematic bias in the iteration throught the heatbug
$ j% }. C3 M. `$ [/ `7 B' e // list from timestep to timestep+ s& Y+ h( f0 D0 d% \9 |/ ^5 E5 A
! j) n6 f3 }+ I' [# G+ `& X8 o
// By default, all `createActionForEach' modelActions have
- f0 R6 B+ x4 x+ B2 j! _* C // a default order of `Sequential', which means that the. I2 I) i0 C0 {# R+ R7 ~5 L
// order of iteration through the `heatbugList' will be
6 Z. d& E6 F! Y5 E) R; j // identical (assuming the list order is not changed
# m$ o4 C/ O) F // indirectly by some other process)., c9 z. ?/ z3 X" p' n
$ C5 E. A( a0 {9 K1 ~; }; m
modelActions = new ActionGroupImpl (getZone ());
6 n: Y) D+ E, {; |% N5 N/ q9 H# A( \0 @. W! Y' w
try {- B8 A4 S" u( ?' l3 r B
modelActions.createActionTo$message
( _5 W/ T+ D# M3 A" J (heat, new Selector (heat.getClass (), "stepRule", false));1 D1 U$ t- C2 {" L- W
} catch (Exception e) {) Y7 e+ W4 O" { o; o& ]
System.err.println ("Exception stepRule: " + e.getMessage ());
& L. n4 J4 M6 y0 U0 n4 u1 } }
+ @$ R6 g+ y, O
/ p) t/ A0 m, w; ^1 m try {/ f( X& b5 h3 L. C3 P
Heatbug proto = (Heatbug) heatbugList.get (0);
; Q# ]8 V- y4 N& G6 o v3 R Selector sel =
8 h0 q% W, J2 ^7 I& _8 ` new Selector (proto.getClass (), "heatbugStep", false);/ p7 r9 F! C! R9 S( K
actionForEach =
/ N" N- P; m+ r$ x2 O& ^ modelActions.createFActionForEachHomogeneous$call$ x, |9 i! z6 ?: N
(heatbugList,/ I; j. E N& x) v& t5 R) J$ Q
new FCallImpl (this, proto, sel,/ D( {5 M/ S$ F0 R( |, o0 w
new FArgumentsImpl (this, sel)));
0 B5 v, Y- v( }( U! M- | } catch (Exception e) {4 L; A8 ^1 q* f; R& Q! W
e.printStackTrace (System.err);
! m* a ^9 A; Q9 V7 N: V$ z }) n* _. J3 `) U6 x
5 |+ e4 G; B, Z0 y! k: Z syncUpdateOrder ();
/ ?, N7 ]; r9 L! p
- {, k; z5 {0 ^ try {. L4 r& M" V9 @0 L( K
modelActions.createActionTo$message
& I# M! v, c, J1 @( W& s (heat, new Selector (heat.getClass (), "updateLattice", false));, F$ d: d0 b Y+ B+ Y& K! m
} catch (Exception e) {
* Y$ O6 X# t. u/ I! B9 Y System.err.println("Exception updateLattice: " + e.getMessage ());. s. C+ Z9 g% I+ P7 h" o
}
' ^/ ], S* p3 N& I3 }! z7 P& F . |7 Y# R% q2 |6 o1 z5 V9 I3 B1 {
// Then we create a schedule that executes the
9 z6 u m" H5 v5 E" F% B3 [ // modelActions. modelActions is an ActionGroup, by itself it! S) k2 w( ~: l8 Z7 p5 H
// has no notion of time. In order to have it executed in! ~, P7 F- I! E5 Y- |. J# E- ?
// time, we create a Schedule that says to use the6 m* E# J- u3 [( f
// modelActions ActionGroup at particular times. This
1 ^; O- X' n9 W2 p+ M/ T/ g // schedule has a repeat interval of 1, it will loop every2 D$ r8 m! s/ q, |- I$ I4 ^
// time step. The action is executed at time 0 relative to! R4 R" J Y# _* o
// the beginning of the loop.
# Q4 N j- v0 E F7 y- T
0 m4 y/ Z- g3 A5 L" f // This is a simple schedule, with only one action that is
0 s# l6 `, [0 G1 B( v2 M // just repeated every time. See jmousetrap for more
6 I, n# P) p/ W0 d // complicated schedules.
1 ^3 ~* |0 l4 L. m5 M 3 S# }! A6 @) u& d
modelSchedule = new ScheduleImpl (getZone (), 1);
: f! J6 }- t2 ?8 | modelSchedule.at$createAction (0, modelActions);3 _ n W0 x. F# ?7 E
+ X( v/ p- ^( _; R8 u* X
return this;
U& O5 W/ G0 s3 E2 W } |