HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 h; B9 k7 v, b: w
' ^3 w5 q' h- v! h- f
public Object buildActions () {' Q( B: ^; X; |( O4 I' i( n
super.buildActions();
8 T$ Z! M: b- Y2 t' q& x1 B
; `9 h- u5 f, d& H' N // Create the list of simulation actions. We put these in4 d+ R9 R1 P4 Y5 y, w0 M
// an action group, because we want these actions to be' |( `6 {8 {; o6 e7 n" x$ w
// executed in a specific order, but these steps should
, W& v% b4 ~& b; w: G7 T4 J // take no (simulated) time. The M(foo) means "The message
3 ?& d' @5 S1 x // called <foo>". You can send a message To a particular
) s: t4 b8 \; k( j# K7 g7 w // object, or ForEach object in a collection.6 N- B4 e; J' k+ R& p) o* u
4 z& k) L) z7 A7 h$ ^0 ^+ F/ y // Note we update the heatspace in two phases: first run% O( B9 c4 B% K6 f% g- W
// diffusion, then run "updateWorld" to actually enact the
6 [+ v. T; x0 _6 U, q // changes the heatbugs have made. The ordering here is+ a3 o* l8 }0 b. k( ^/ Y
// significant!
4 s/ u+ M7 x1 Q- g' l; t% ~
4 t3 n2 I" J$ _( W4 t+ @. G/ F- q // Note also, that with the additional" B+ P$ g7 S a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: Y) P. x! ~9 S9 h. ? // randomize the order in which the bugs actually run
6 Z, B5 d' T- B8 O& B/ B+ i // their step rule. This has the effect of removing any
K. ^: h1 _4 t2 G // systematic bias in the iteration throught the heatbug
5 d# m" c3 X. c/ d // list from timestep to timestep8 J, S% J# R& S+ o5 v
5 O7 S0 P1 q/ `/ h) w# M // By default, all `createActionForEach' modelActions have/ N! R" m4 D i: |, X( t. N$ s2 {
// a default order of `Sequential', which means that the
2 A3 y1 _: _# v% B& K // order of iteration through the `heatbugList' will be. j$ o8 [) l% K) x
// identical (assuming the list order is not changed& P* N- y$ f& u8 V: }; t8 O- F% E) f
// indirectly by some other process).
. C$ B" W0 Y- j6 \
% e, g& W7 D _% Y" R: w2 @& z! k modelActions = new ActionGroupImpl (getZone ());; n; t/ K6 P- i1 {( L
7 n4 v3 @9 j5 [5 N5 C try {
$ z$ C3 M5 R9 r2 v modelActions.createActionTo$message! ^ }& W& L! \
(heat, new Selector (heat.getClass (), "stepRule", false));
0 `3 ^2 n# g$ M; y* r6 G } catch (Exception e) {2 b6 U: X, _2 H i2 B
System.err.println ("Exception stepRule: " + e.getMessage ());
- N# Q! n% ~" W" S8 N; N: E }4 F9 J( O5 }+ P5 ^% L0 d- i: T8 R, q
" R; o$ Y M; a) {1 g% U2 Z$ V' s try {
3 _& a' ~# q& U# u$ q: ^) t Heatbug proto = (Heatbug) heatbugList.get (0);
/ E6 E& j* k& I: t \6 e) _+ X4 q Selector sel =
, {. }$ C4 Y& q7 B new Selector (proto.getClass (), "heatbugStep", false);0 @4 E/ y$ e9 h4 L% Z
actionForEach =
5 n1 L6 o- {0 r/ o8 W" r/ n; ` modelActions.createFActionForEachHomogeneous$call
( q9 X' t1 ^0 z5 j6 d5 J+ k9 B (heatbugList,
) O9 ^( @ L+ W* ]1 ^5 n& H new FCallImpl (this, proto, sel,/ P( ^" i2 o- i5 k) Q0 g3 J
new FArgumentsImpl (this, sel)));# M N% b9 C) O( R# Z+ y
} catch (Exception e) {
' R& Q d$ F8 | G7 e( j e.printStackTrace (System.err);
* C+ C! ~! G; e7 g }
) H R( `; q5 z. s 3 }" r8 R; d. d7 M
syncUpdateOrder ();8 n, I1 n) O- z2 Q/ ]2 `; p2 k
6 J" |, d1 X+ X. B/ J try {6 O; X, l9 M3 u7 N6 P2 k; j
modelActions.createActionTo$message ) s N# ?* g! h( z" f0 x
(heat, new Selector (heat.getClass (), "updateLattice", false));
' J% ?% k8 m6 G) q } catch (Exception e) {+ R7 D' r% d$ }; p+ X
System.err.println("Exception updateLattice: " + e.getMessage ());
( \) K0 l* E3 E6 z- G+ h }
, O" p4 k2 T9 M) a& S+ ] f% i
! \3 n% ?" \. i' q$ W# ~ // Then we create a schedule that executes the
6 `+ p+ H' `6 W( s. n6 I // modelActions. modelActions is an ActionGroup, by itself it
4 I" j3 w v/ M G, G, ? B( F // has no notion of time. In order to have it executed in
, V: H8 D8 J6 y# |) B- O, U* r/ r! d // time, we create a Schedule that says to use the; c6 g. x' W+ x, g8 v
// modelActions ActionGroup at particular times. This3 d% c2 k( q, m, ^
// schedule has a repeat interval of 1, it will loop every, o3 P1 N' C, I! K
// time step. The action is executed at time 0 relative to
9 q. m1 {9 K! Z% _" J% E9 j // the beginning of the loop. Q, X' h2 O4 |$ y, I M. e. @
; ~3 U9 m4 k2 G
// This is a simple schedule, with only one action that is
- P0 l6 C5 d7 z0 ?% t u( r: W9 y // just repeated every time. See jmousetrap for more% @5 ?' V1 @5 s6 [2 h
// complicated schedules.
' c1 q. U% v9 B6 t: b& }
: [/ b9 F q$ K z0 @: a7 J modelSchedule = new ScheduleImpl (getZone (), 1);8 O+ Q* F1 ?* E- s; J
modelSchedule.at$createAction (0, modelActions);8 ~& y( f N) a6 R* N8 ~
4 R. H! e9 o+ J. j4 c
return this;
8 {4 b. C' |6 |* u } |