HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; M7 d( c9 f' Z% I$ P/ K8 e1 E/ R/ V
public Object buildActions () {
5 ~" O8 w6 S2 Z$ r super.buildActions();
: x' L* Z, v5 I. M+ X$ q- S
1 Q7 w: e; w v // Create the list of simulation actions. We put these in3 L0 u! W3 X3 g2 e
// an action group, because we want these actions to be
5 N E+ |4 t. _- n; X+ n% s // executed in a specific order, but these steps should9 N) o6 ~- N$ i: _) O) b# Q& D" ?
// take no (simulated) time. The M(foo) means "The message) ?- U, Z, D \* _
// called <foo>". You can send a message To a particular
; e2 A9 w4 x/ B- j ~ // object, or ForEach object in a collection./ c9 Z; X& [, O* r
1 F/ V0 |6 L; j; J/ Q; w& ^- d // Note we update the heatspace in two phases: first run
: B3 p- M$ X Q, I // diffusion, then run "updateWorld" to actually enact the+ `# {; m! `) h8 Z
// changes the heatbugs have made. The ordering here is
4 U9 V' q4 @. ]4 t$ O" O // significant!2 G" E1 @$ Q t3 e! h$ S
: g6 J5 l$ D" w+ }* O1 T* e // Note also, that with the additional! V0 _/ A9 a; v2 D2 A2 J9 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 a0 Z5 s: A- j `' |' u* L" S
// randomize the order in which the bugs actually run
+ C y# p9 b M. ] h // their step rule. This has the effect of removing any
9 w5 P: r. q, I1 t' T // systematic bias in the iteration throught the heatbug- G9 E- A1 ]4 [) U* G1 g( x$ M. Y) x
// list from timestep to timestep3 S( J, Z. X) O/ Y3 B0 \# U0 v6 w
& G. {; i6 c# n% m // By default, all `createActionForEach' modelActions have1 b" b$ @/ r' T) a% {+ e2 z0 m
// a default order of `Sequential', which means that the% Y: u6 s4 @ d9 J {
// order of iteration through the `heatbugList' will be
8 L% a2 y6 W, A' @& x% a // identical (assuming the list order is not changed
2 G3 C) H8 |; `' h/ I$ P // indirectly by some other process).
6 M) N; q* a$ R9 Z8 x 1 h; o8 F3 h! i; S
modelActions = new ActionGroupImpl (getZone ());
3 J- i0 h8 ~8 `- {
7 p( H# S: `. D' k8 d" E* ]/ T try {( k" X$ A. V, P, [0 A* Q
modelActions.createActionTo$message
0 c8 a$ ~+ V. W (heat, new Selector (heat.getClass (), "stepRule", false));
# M; X+ a9 n+ H2 X7 ~ } catch (Exception e) {5 s1 y* m* q! d: N' {
System.err.println ("Exception stepRule: " + e.getMessage ());/ T0 R% z% _5 @0 ^4 i7 t! O t/ G
}9 P7 w( K& @' W# [9 p
& O# H u" M- F5 i; \; [ try {6 m+ s, |5 D% J$ z+ x: }
Heatbug proto = (Heatbug) heatbugList.get (0);* ?9 G8 l1 A# X) N& p( |5 L, C
Selector sel =
/ A5 v5 }: u7 W% M new Selector (proto.getClass (), "heatbugStep", false);
( L. m8 o; C- T3 r+ M actionForEach =
) E" W# q+ |) k( k' E. E" C modelActions.createFActionForEachHomogeneous$call
3 b; u7 F T, l1 L0 G (heatbugList,
5 ?, h- C# ?- ^5 L8 k new FCallImpl (this, proto, sel,
7 F) N5 R, m. b4 q! }3 Z new FArgumentsImpl (this, sel)));
+ R3 R/ m/ e" N: g } catch (Exception e) {2 c1 f. k- _4 |! f5 ? ~" }9 ]
e.printStackTrace (System.err);0 u1 d0 O* w: R& @
}
/ r5 s+ S" ^! h# t$ a + J( a: y0 X: y- f
syncUpdateOrder ();
9 B' e& N: v' L3 t( Z9 y
: A4 Q3 b0 C3 o try {9 E5 d/ U* e$ H9 \5 O. X
modelActions.createActionTo$message 6 i0 L3 `* `: ? y. s% Q) O- @
(heat, new Selector (heat.getClass (), "updateLattice", false));* l, g: ~: |& k% Q; N/ Q0 }
} catch (Exception e) {1 f1 x/ {1 P$ {
System.err.println("Exception updateLattice: " + e.getMessage ());; P( M9 t* X" u( B4 o: N
}
& g% O1 J- r6 |% Q) H 5 U' E7 s1 v' a- C2 t' |# B* B
// Then we create a schedule that executes the' A4 ^& F: G0 Z- Z
// modelActions. modelActions is an ActionGroup, by itself it
* w) Q/ A- F0 a; t4 q8 \9 H // has no notion of time. In order to have it executed in7 b9 Y+ r0 P q- C: G6 L% d
// time, we create a Schedule that says to use the
% Z+ ^6 T6 T& o- k // modelActions ActionGroup at particular times. This+ D) v; d; o( o0 s3 T! e `; h
// schedule has a repeat interval of 1, it will loop every- t, U: j' k2 } ?
// time step. The action is executed at time 0 relative to
( l8 x2 }7 H- o U( y. ?% [ // the beginning of the loop.# S! F9 a1 m% ^% Q4 Z0 q
2 V" d5 R! }5 S, o4 s- e- u // This is a simple schedule, with only one action that is
' t; N9 z& a8 |/ A2 h // just repeated every time. See jmousetrap for more
# W# w/ j" ^+ A+ R: d) m // complicated schedules.
2 W; R' c* w' H6 M0 Y- D# g0 k( D4 v
# \" z' y& t3 c* R4 L) v modelSchedule = new ScheduleImpl (getZone (), 1);
' L( }0 f; N& w3 @2 y \& z Y( { modelSchedule.at$createAction (0, modelActions);9 |6 ?( r3 A5 r2 P1 p9 i! o* i
) \" y! L/ l; @2 c
return this;
9 N" I- t( W+ J# m% i } |