HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 ~8 w; B. S5 ~) l8 e2 a2 }5 k6 a
! X# O7 Z `4 s0 Q5 T
public Object buildActions () {
8 `/ Y$ l8 k b" Z7 f3 h' ? super.buildActions();; p" y9 v g' @
5 f3 _6 @/ _$ w: F4 T // Create the list of simulation actions. We put these in
% ~& a7 v! h( p* j6 _0 ^1 D // an action group, because we want these actions to be
+ V3 O6 i* Q( j3 J5 M- r& m6 f5 k- F // executed in a specific order, but these steps should% S; r2 {. G9 i% h1 L3 `
// take no (simulated) time. The M(foo) means "The message1 V( L, q4 n* @3 u2 S
// called <foo>". You can send a message To a particular, l+ @# I, w5 I9 X! b
// object, or ForEach object in a collection.
* E$ o( Y7 p$ c 0 D+ C9 g' k7 a6 O) j; V
// Note we update the heatspace in two phases: first run
7 r! y$ z, L }5 u' _2 E // diffusion, then run "updateWorld" to actually enact the, \& T; N5 \3 v9 Y
// changes the heatbugs have made. The ordering here is0 E! Y% ?& n o9 E5 D* \
// significant!
2 x3 R m7 e4 e- ^+ x
6 v" E h! ?( \0 T7 d% C6 A7 { // Note also, that with the additional: M! s0 G4 {! D4 }
// `randomizeHeatbugUpdateOrder' Boolean flag we can) b' h4 L3 i5 A, o! l6 y, u6 d& q
// randomize the order in which the bugs actually run
7 ]% t3 t# a" K4 ]4 n4 F // their step rule. This has the effect of removing any2 w5 }: @4 O Z, c
// systematic bias in the iteration throught the heatbug
3 ]* |0 Z$ O7 ?1 @ // list from timestep to timestep
* _* \% T. R; a2 ?! D V
5 n, y- `5 L0 }, y* A3 P // By default, all `createActionForEach' modelActions have8 y% \' \6 m {" F* ^4 |7 Z% ?5 a
// a default order of `Sequential', which means that the
6 W% i* {+ i2 D, Q+ |! `; M // order of iteration through the `heatbugList' will be( ^- O2 V0 h, g2 v$ t7 W
// identical (assuming the list order is not changed
( m1 h5 V- X* J# ]0 G- W0 U // indirectly by some other process).% m2 W9 T( _, f: M+ V) S0 l
% x! I$ K. w0 A5 ]+ y modelActions = new ActionGroupImpl (getZone ());
" i, H9 [4 `% x7 g( i# M7 C0 }' M; f5 J0 D6 e$ o4 E
try {# f5 T9 i, F1 s' [. d2 M
modelActions.createActionTo$message
0 G. B9 E6 H1 g0 w4 Y1 Q* p (heat, new Selector (heat.getClass (), "stepRule", false));
6 `5 B* p2 X" ~' o% T } catch (Exception e) {
4 e& ?6 R9 \; t0 z2 Y System.err.println ("Exception stepRule: " + e.getMessage ());
8 t) w) {/ J) t7 y4 T: U4 T }
! P, |" z4 j- Y3 A2 ~9 ` f2 F% L% G) b% D
try {
2 ~$ A7 z& ~+ D) N" S- ~ Heatbug proto = (Heatbug) heatbugList.get (0);' ^% h) Y3 i+ \2 f* J) |
Selector sel = , D4 g! U9 N7 Z X$ R* y
new Selector (proto.getClass (), "heatbugStep", false);0 f3 T( e3 l2 f6 m" I: M6 z* p) y
actionForEach =' w3 G5 I0 i3 q* h" y
modelActions.createFActionForEachHomogeneous$call
, s: C" E0 ?! \" a2 K+ K/ P5 q (heatbugList,
7 M: D; } |* P$ @7 O. h: Q: ]2 e! V new FCallImpl (this, proto, sel,+ @# o `. l }6 b: \2 U( L
new FArgumentsImpl (this, sel)));
& u( B' n; H; g: @ } catch (Exception e) {/ X3 ~8 Q0 O. Q% o+ `) n
e.printStackTrace (System.err);
) O3 s# L' ]( ^. @& b( [ }
, `& o+ x" G A/ g: l' B! M5 K
- T% l b7 G% Y syncUpdateOrder ();2 F/ T+ y; |: q& D
* i' R- o. ?- }/ A0 R; P# t0 e; J
try {8 r- }: Y. x' E' }
modelActions.createActionTo$message * S4 P$ q |* I3 T( \- a
(heat, new Selector (heat.getClass (), "updateLattice", false));8 E0 V9 K; }) N* H
} catch (Exception e) {0 C9 h8 w" A. T# ]
System.err.println("Exception updateLattice: " + e.getMessage ());
6 I% Y- i0 L& F# e0 O( s }
8 e; x1 X9 y/ P: I7 k # N5 S- P% J+ Q. J/ e! j
// Then we create a schedule that executes the
4 f1 B1 G3 E) Q5 p! b0 Q* q% F // modelActions. modelActions is an ActionGroup, by itself it
2 A. b& }( p: t3 n2 {4 l // has no notion of time. In order to have it executed in7 Z4 R3 @; O4 U
// time, we create a Schedule that says to use the
4 b, r" _% H V4 j( T) w/ N // modelActions ActionGroup at particular times. This5 z/ i3 F6 B( ]) _1 s5 B$ s+ u- l. s0 ?
// schedule has a repeat interval of 1, it will loop every
3 @5 w/ c0 J0 W; Y- D& d // time step. The action is executed at time 0 relative to) |9 z2 ~4 D9 _ G, \) |
// the beginning of the loop.& n7 C( B- g8 r1 X& i
" y" M8 I4 H8 i, `$ d, x
// This is a simple schedule, with only one action that is
; X& K1 x' b# b3 `; K // just repeated every time. See jmousetrap for more
9 m3 [- {; h+ F- M# i% g // complicated schedules.1 _9 E* u8 ~5 ^$ z5 s" @0 b
6 z! |( W( H `2 R' f! n5 g2 H" j
modelSchedule = new ScheduleImpl (getZone (), 1);
4 V* T8 @$ P2 W7 P8 y modelSchedule.at$createAction (0, modelActions);
6 N6 j U; c1 g
6 N( D0 \, f! {& D return this;
; T. a$ p/ E- U5 e' R- M: K" M, r } |