HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
C: O9 w) L# _! v7 z
; I. i: l0 b2 H9 S4 L public Object buildActions () {6 B, j, f& ]- H7 p' c6 B" e
super.buildActions();
( z6 D" C; k+ t1 Z+ p* \! H ! ~1 {4 h: R5 J4 i+ S+ c. \/ q
// Create the list of simulation actions. We put these in
9 x' X& W C/ N // an action group, because we want these actions to be
T# O# ^/ ^; m2 y4 N$ P // executed in a specific order, but these steps should
9 m0 I+ b3 T3 U+ }2 I B // take no (simulated) time. The M(foo) means "The message, q% V' o2 H* Y9 S
// called <foo>". You can send a message To a particular7 Q k1 U7 z& f: X8 P Q* p& ~
// object, or ForEach object in a collection.. y' u/ t. D% u" p) ~$ B e# Y: U
! d9 l9 D; D, O4 K5 M) z# S2 B, h
// Note we update the heatspace in two phases: first run
7 [ k+ K$ p! ]/ D // diffusion, then run "updateWorld" to actually enact the) T6 C8 q* p7 B* V: E7 Z
// changes the heatbugs have made. The ordering here is
$ Y. u" r' i K4 E% N // significant!
5 G' I- ?9 [4 C" m9 O
# l( k7 J8 t+ |- \7 K I1 o // Note also, that with the additional
+ V9 Z1 H0 M+ x" K! e( U // `randomizeHeatbugUpdateOrder' Boolean flag we can" M) d+ A( w" K. S3 V" v$ \
// randomize the order in which the bugs actually run7 w& e! [3 Y4 l/ _" p$ ?" h0 b
// their step rule. This has the effect of removing any3 {9 f3 R. K) ^) U8 y6 a
// systematic bias in the iteration throught the heatbug
* a+ N' s5 F& K // list from timestep to timestep
$ y" k8 E! X8 \# g 7 o2 @8 p5 l7 ~5 c: d6 i7 X
// By default, all `createActionForEach' modelActions have
. y, Y( H, }) h/ Q; v- g+ y5 l9 } // a default order of `Sequential', which means that the
8 D4 `! L1 x+ F, S // order of iteration through the `heatbugList' will be
/ N4 J3 a4 j. ?) E // identical (assuming the list order is not changed
! \5 N0 y- J) L* |6 H' C: S // indirectly by some other process).
8 T4 v1 P1 P4 d
: z6 x: s3 ?9 U b; K( t modelActions = new ActionGroupImpl (getZone ());+ R! P" ~: R. F2 p
3 _& ^: y! M8 h+ i- ]7 G try {0 A) I& c# ?& i) B6 M% P
modelActions.createActionTo$message
2 @+ i: m8 s# K: i+ D: P5 z (heat, new Selector (heat.getClass (), "stepRule", false));
9 K* w+ ^/ R3 m- {8 E w } catch (Exception e) {
5 U+ R4 L0 O4 r7 F$ z System.err.println ("Exception stepRule: " + e.getMessage ());
& N7 h K: K4 i. V% O5 f) T }
1 ^+ q& F* ~& R# j+ `" R1 S3 Q& ~ L9 b5 a2 X0 q# w8 |- Z
try {
6 A7 V' B) |9 U" n+ t; p" D Heatbug proto = (Heatbug) heatbugList.get (0);) y/ f# i$ u) [9 ^# T
Selector sel = 0 `% e T( k# @$ M
new Selector (proto.getClass (), "heatbugStep", false);
7 q9 b( |+ U0 l1 d/ @" | actionForEach =" n( ?' J! @& N4 a( a! w
modelActions.createFActionForEachHomogeneous$call* X Z0 L; t, y/ ?# s9 n
(heatbugList,
4 o' I2 _, O/ v; t! p new FCallImpl (this, proto, sel,$ J( _. }* x" I) u5 {
new FArgumentsImpl (this, sel)));
: c! i q8 {) F7 x) B) E9 [; Z } catch (Exception e) {/ }) x3 H4 n1 W# \1 Z, e0 {
e.printStackTrace (System.err);! j5 u# ~8 z4 |
}
/ X# h; [8 T* H3 B, j% |- E2 s - }; K) s0 ?2 c
syncUpdateOrder ();
$ h. t5 z/ S% k$ G0 Y6 A3 d
- ~3 M& L0 }: J% c, r7 b Q try {
- W! @) u; j! h* ?$ e! U modelActions.createActionTo$message
. y7 X% Y7 z3 I1 K (heat, new Selector (heat.getClass (), "updateLattice", false));
# e! p3 D! h* D" U L& s } catch (Exception e) {
/ J4 h% K7 j# ]! q- S/ n System.err.println("Exception updateLattice: " + e.getMessage ());
7 T7 N' d6 Z) N( P7 ~% c }
" c! D- x6 ]! d 0 V) C ]4 B$ h, O! c4 ~& Z
// Then we create a schedule that executes the
( @0 _- E( N8 H b5 B // modelActions. modelActions is an ActionGroup, by itself it
5 O$ N6 ?' z3 T' N) F7 ? // has no notion of time. In order to have it executed in
$ f* q! O# h& K1 G. q // time, we create a Schedule that says to use the
4 p/ v4 O+ h1 B4 J. x) T // modelActions ActionGroup at particular times. This
0 a. |( D, _( C/ a) s9 w0 P // schedule has a repeat interval of 1, it will loop every, ^: [6 S) U- K7 S: r, t
// time step. The action is executed at time 0 relative to
8 ^+ D# k( K% N. y, c& l" R // the beginning of the loop.- ~# G$ [: C# I
5 d' ~! v9 o# W* ^" O! l% j7 X1 V
// This is a simple schedule, with only one action that is4 n' M& J! ?- [0 S1 e% d
// just repeated every time. See jmousetrap for more8 S& ~( l) d* S# h+ D! ]7 c! g
// complicated schedules.
# e4 V g: @; m/ B) _& x3 I" O. h
& g8 y. @3 I, V0 [ modelSchedule = new ScheduleImpl (getZone (), 1);4 [2 ^; r- N8 |/ w, K
modelSchedule.at$createAction (0, modelActions);; |$ h6 a3 `( k8 D+ u
" y- w8 c- g. y4 X8 e9 Y return this;
& o- y' r4 u! O; p) }0 V5 n/ W# h } |