HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 ]$ c' ?7 B( v6 s" m& E
/ G- d$ R% _8 w$ ]4 m( n
public Object buildActions () {
" G8 H2 W1 m) b' z9 `0 p& i super.buildActions();! U6 L( c3 A7 |0 v5 r
0 F0 J7 m: m% `, [3 z! M; Z% D
// Create the list of simulation actions. We put these in
- y7 |5 k4 D+ r5 ~& ` // an action group, because we want these actions to be9 w" S4 p3 y' L5 p q
// executed in a specific order, but these steps should
7 @7 F! e! C, N1 t# n. h# N) [ // take no (simulated) time. The M(foo) means "The message" C- v' d0 `& D
// called <foo>". You can send a message To a particular
& j# y D! D% H. L5 Z // object, or ForEach object in a collection.; q* _& s5 _: k, C, u
, _. [6 a+ D5 F) W4 I7 E
// Note we update the heatspace in two phases: first run
1 Q4 ?0 O: f0 B$ w7 Y# y* S // diffusion, then run "updateWorld" to actually enact the9 z( Z i* l' I3 G9 w
// changes the heatbugs have made. The ordering here is5 n3 M7 L2 {8 j3 m
// significant!
. l+ {7 O' x4 V: L 5 V$ p" Z4 q. z5 P, G
// Note also, that with the additional
9 o( v! {) R1 L$ [9 T1 f4 |2 V$ b5 O // `randomizeHeatbugUpdateOrder' Boolean flag we can% l: Q7 P4 T6 _, ~0 z
// randomize the order in which the bugs actually run
* q' }% l0 W! C: \* @% y // their step rule. This has the effect of removing any
7 b6 w/ c4 _: J e // systematic bias in the iteration throught the heatbug
8 z A4 l* d, _# Z1 q" a // list from timestep to timestep }' I0 g. l% n- B3 b$ k4 }
$ P x2 R6 z/ P4 @7 D9 M
// By default, all `createActionForEach' modelActions have
" _( { e' ^9 q) \ // a default order of `Sequential', which means that the
* E( O: z6 M1 q3 _) u8 g7 T* j // order of iteration through the `heatbugList' will be
$ U5 k8 I- R0 L // identical (assuming the list order is not changed
9 X" G" n! u# F; Q$ i // indirectly by some other process).
; [1 U- a6 g/ L; { 3 U: m- J" V( E- @+ z8 t7 D% ^
modelActions = new ActionGroupImpl (getZone ());, F( c `# ?$ B e+ i" n g
4 j9 ?; c5 Q- `- u3 _/ ~ try {
4 W, h' G. D* [: L3 v. z | modelActions.createActionTo$message
7 s6 u* I( b4 R( _8 J* e& X2 R7 n (heat, new Selector (heat.getClass (), "stepRule", false));
1 D. ?/ R) ^4 N: |, x9 E- k } catch (Exception e) {2 d5 e) X; t$ _) Z
System.err.println ("Exception stepRule: " + e.getMessage ());
# p4 R0 ~4 @7 d2 n' x }& v$ Q8 s, Y2 h0 \
& o) z9 v, M) z: P- N0 V
try {7 r; P9 N! O3 R/ `. E9 v8 K
Heatbug proto = (Heatbug) heatbugList.get (0);
: g& V8 \0 t% ` Selector sel =
% t: Z: i" y+ @1 B% e+ N$ ? new Selector (proto.getClass (), "heatbugStep", false);/ r; G4 K' t6 T4 p; k
actionForEach =
, Z; I7 ~9 O4 I9 y6 u! P8 f modelActions.createFActionForEachHomogeneous$call
9 H1 ^" m* }& X4 l) t; S2 t% ` (heatbugList,7 \7 {! J% J/ c
new FCallImpl (this, proto, sel,
, q, s5 q+ B, S; ~+ i new FArgumentsImpl (this, sel)));
8 d. ]; s0 t( A# ~+ m( \( w2 p } catch (Exception e) {$ |, J$ A$ i9 z; {1 X
e.printStackTrace (System.err);
& S# k0 E% b, N, S) J ~$ u }
V2 C5 w, R5 g/ m4 s5 w9 S
) i" l c3 _6 x) M/ o syncUpdateOrder ();, v3 x& B1 |9 p" I# W. P% a
% D$ R( \$ W6 F* O* Y0 Z try {
" B& L; {3 {( P/ d modelActions.createActionTo$message , N; o6 f9 V. d+ I3 ~' `! M
(heat, new Selector (heat.getClass (), "updateLattice", false));% r& _0 B+ a+ S V2 M$ C% V
} catch (Exception e) {
; H# n$ Y8 w4 n5 X J0 q6 Z. { System.err.println("Exception updateLattice: " + e.getMessage ());
! Y+ W( R$ G4 a1 ~ }( O) n, V3 V2 N* E( F
4 L4 ?0 K( o3 G% h" D. e
// Then we create a schedule that executes the
+ G; Z6 x7 V" g( B" o! P% f // modelActions. modelActions is an ActionGroup, by itself it
4 E; P% _ i) v0 M // has no notion of time. In order to have it executed in
* |; S& C* M2 T3 H) M! y // time, we create a Schedule that says to use the5 x* B% G& {4 f' D [$ A9 e6 d
// modelActions ActionGroup at particular times. This
9 i E: X! Y6 i! {* W1 S // schedule has a repeat interval of 1, it will loop every5 T5 [* j0 F$ L! T0 ~6 i1 P
// time step. The action is executed at time 0 relative to9 d! s s8 e' H; m# [' Y( X' u
// the beginning of the loop.' }) T- I9 t6 a1 u B! u+ X; Q
* J0 l7 v7 L5 k4 d; O
// This is a simple schedule, with only one action that is; S6 j9 K" K j1 l/ B
// just repeated every time. See jmousetrap for more
3 r9 h, |% h( W6 J2 K% P6 B' m // complicated schedules. d. g/ E9 y7 F& Y/ U! S" D
. @$ W) L3 W A p, [ {" Y5 U modelSchedule = new ScheduleImpl (getZone (), 1);& a+ a' N9 ~' d- l6 V. J- R
modelSchedule.at$createAction (0, modelActions);$ f* B4 W( a6 h* T& r
5 M" k% c6 b9 V: O
return this;
/ `# h4 G' K1 F" Z7 ?9 \3 _$ c. y } |