HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 x# P+ {& W L7 W4 L
& s% ?8 i0 p9 H; Y+ B" L public Object buildActions () {
8 W1 l6 p" P0 t+ z! {' f super.buildActions();( R8 V1 ^: A1 O/ M
/ V' Y9 Y, h/ o+ D
// Create the list of simulation actions. We put these in
\# n+ P$ R) a! }: D( i // an action group, because we want these actions to be* c6 {" U/ b7 F' o! {( s- ^/ Q
// executed in a specific order, but these steps should
) G1 J" ?- V7 r1 H# i# x* y! i // take no (simulated) time. The M(foo) means "The message
F$ |' ^- I' i' v; p4 {1 y# S" R0 K // called <foo>". You can send a message To a particular2 v: m4 U: U7 c
// object, or ForEach object in a collection." e; h7 N1 i2 c4 q
: e5 T T% A9 r: T
// Note we update the heatspace in two phases: first run' A" B1 Y) N/ H' {& h: R
// diffusion, then run "updateWorld" to actually enact the: A& z t3 O; J& n; ^
// changes the heatbugs have made. The ordering here is3 o# I+ p6 U0 N
// significant!
! M9 U- H$ t/ [
. c i6 k' k" ]' ? // Note also, that with the additional
G% w" R( h+ C }% p% W // `randomizeHeatbugUpdateOrder' Boolean flag we can4 Z6 ]% y3 f1 y+ b* }+ j0 z! @
// randomize the order in which the bugs actually run
( U% Y& ?2 ~6 ^: ~. B+ o$ Y // their step rule. This has the effect of removing any& B* \: H% A8 ]! E
// systematic bias in the iteration throught the heatbug
* S3 ]7 P5 h. }5 d // list from timestep to timestep
4 W7 w& N$ J* o . J1 a4 Z9 o1 ]* R T5 d+ d
// By default, all `createActionForEach' modelActions have
+ A: H' Y S `, V$ C% F // a default order of `Sequential', which means that the
- X$ r- P/ w+ t( N o( _/ @ // order of iteration through the `heatbugList' will be9 Z" J* A6 X) t: w2 t
// identical (assuming the list order is not changed
/ y D- }, a4 r6 @' x" R // indirectly by some other process)." r+ i8 {4 Y6 k- V
! x* q+ l( m- a5 ~, Z
modelActions = new ActionGroupImpl (getZone ());; d4 u1 h- k, ]. g. J& g
- D# ~/ k- R8 Q' `$ } T try {
% i: T- _4 V& s. F9 Q1 T modelActions.createActionTo$message* X0 ^ G( c9 q$ [, ?
(heat, new Selector (heat.getClass (), "stepRule", false));" z Y7 v' [6 H5 x
} catch (Exception e) {
2 Y: C( X6 c( Z8 u System.err.println ("Exception stepRule: " + e.getMessage ());
' p2 s3 V9 H0 S8 U) P% B }
7 v; A$ v; A, Q
* h8 u3 s1 Q p. m try {
. K1 E+ M" T2 T- k( M7 ^ Heatbug proto = (Heatbug) heatbugList.get (0);
2 k n+ q+ _$ J7 x# t Selector sel = 3 R- p+ f1 K \+ H6 Y- o; p
new Selector (proto.getClass (), "heatbugStep", false);2 C1 i0 f3 q4 j7 T$ y+ M
actionForEach =
9 j* X# C" a) `6 p- Y7 h0 _ modelActions.createFActionForEachHomogeneous$call
, x! A7 a. B3 K. h+ v) d3 Y (heatbugList," o. @3 U0 A0 H% Q0 J) r
new FCallImpl (this, proto, sel,1 Q3 ?" e0 D9 ^
new FArgumentsImpl (this, sel)));6 r- `5 h3 r b. ]6 I
} catch (Exception e) {
- i8 y' ]' {. y0 p2 Y2 {5 G* H e.printStackTrace (System.err);" F' S2 ~; e3 _" b8 ?8 q0 `
}4 |; s6 a" g6 `- j9 W
7 Z" b" B$ F7 E8 y: C
syncUpdateOrder ();
+ D( ] o5 {$ n' s6 j2 @) {5 h# e4 I, z! i/ R- D( s. g
try {
2 C8 R" ~: L, J$ s" j8 [2 G5 O modelActions.createActionTo$message
: \: h9 }% b+ f (heat, new Selector (heat.getClass (), "updateLattice", false));
8 z" h+ b7 t+ V! ]8 f0 R* x, a } catch (Exception e) {# m& |& q* S, t4 B" ]& K r% S/ ]3 n
System.err.println("Exception updateLattice: " + e.getMessage ());
" u! _9 r8 H, x5 l& G }6 `7 L" O2 T% a& x+ {" Q
2 b- N( \4 }9 f- R0 k
// Then we create a schedule that executes the& D- _ o% Z+ b" x: Z9 O2 t
// modelActions. modelActions is an ActionGroup, by itself it* Y3 W: v/ E4 A% ]) h; O. m2 g
// has no notion of time. In order to have it executed in
/ ?* q- N3 [+ r9 J& _' V5 }" j // time, we create a Schedule that says to use the- S) L$ O7 x) M# @; k- E
// modelActions ActionGroup at particular times. This q: d1 d5 r' n
// schedule has a repeat interval of 1, it will loop every
+ N! U! `! ^" J9 \9 ~ // time step. The action is executed at time 0 relative to
* s! ?3 S) m: k1 g( Q K$ ?( B- l // the beginning of the loop.
' [- K* C; k" g2 m
! C' i: j" z: P0 Q7 X; o* ? // This is a simple schedule, with only one action that is& Z6 S0 b! L$ C7 Q2 F# k
// just repeated every time. See jmousetrap for more
" F5 X* l; F9 F, \0 P // complicated schedules.( \% a. ~3 @! }+ A% v$ F Z- s
1 s( E$ _) W7 G, b1 R0 H6 v
modelSchedule = new ScheduleImpl (getZone (), 1);
( Q, l X, }" l8 m6 H" y* z' _ modelSchedule.at$createAction (0, modelActions);
& T- G$ F, ~7 `; Z$ U# _2 f; t! P * r2 F+ V, R! H6 }9 U+ ?7 }* f
return this;
9 \; M/ c- b0 [& |9 P# n } |