HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) A( a$ T/ [1 |) o
$ t: G0 X; v4 H7 j3 L( @$ M public Object buildActions () {
; Y4 H j! _9 u$ D5 [( q super.buildActions();
' [9 ^- b u, d: c4 |1 _
4 y2 N. R% W! }6 z8 U( o' ~ V" ` // Create the list of simulation actions. We put these in3 q) i1 P% S/ Y% x1 W
// an action group, because we want these actions to be) L1 ]1 p( x, [
// executed in a specific order, but these steps should/ J% ~9 d" R' R
// take no (simulated) time. The M(foo) means "The message8 x: J: {% ^1 V5 I z3 ?
// called <foo>". You can send a message To a particular
+ H2 }3 O5 O- W A; B U) j // object, or ForEach object in a collection.! Q8 T8 S* C; J! r3 T
* u/ ?0 {: M1 e6 L/ W( m
// Note we update the heatspace in two phases: first run
7 J1 j3 N$ M# L' ~* ? // diffusion, then run "updateWorld" to actually enact the* |1 p5 H% L, z% i0 z/ v
// changes the heatbugs have made. The ordering here is
( u) f8 r& Z8 I$ X // significant!
4 Z& t' |; R( ]6 V , a1 j$ a3 E }6 ?
// Note also, that with the additional
( e [0 s) w& C6 R) o- ] } // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ f9 q: A. C) J0 R' B, X // randomize the order in which the bugs actually run
! I, _3 v) A, b // their step rule. This has the effect of removing any
* @5 |" r/ M8 j // systematic bias in the iteration throught the heatbug
+ }2 Z1 k" M( J9 N // list from timestep to timestep
) o5 w0 D5 j+ w& ~
7 a$ O9 A* M. M // By default, all `createActionForEach' modelActions have3 {2 a: [: ?5 L2 s7 l
// a default order of `Sequential', which means that the
, _# p$ V7 v+ H" [1 n! c3 t: A. Z9 k // order of iteration through the `heatbugList' will be
% U9 m9 ]" v! Y6 }6 \- U // identical (assuming the list order is not changed
/ g% V; H1 s7 [% f: r Q4 [& Z // indirectly by some other process).) M- k' S0 ^$ C* K
! D$ q. X% y% v) L$ ]; ~
modelActions = new ActionGroupImpl (getZone ());) S" D& O5 ?# Z* @* ~3 j
; ]% R. A, ~- S3 h( {
try {! n$ n& I7 ?1 W5 m5 V6 I
modelActions.createActionTo$message
9 A2 R% w! x$ O- w! K" } (heat, new Selector (heat.getClass (), "stepRule", false));
+ L) y6 G7 \' K+ c9 }/ i+ t" R+ i } catch (Exception e) {
% u6 ~1 D: X* I4 b) R3 ~% G3 ? System.err.println ("Exception stepRule: " + e.getMessage ());" p; p! V, b9 u2 t- m: M
}) f; M7 a4 f P# o [
; a7 z; ?2 Y/ C! R- v
try {
$ E" A/ x- e: X; W Heatbug proto = (Heatbug) heatbugList.get (0);! L/ C; n: B) s g8 h+ x$ p4 g0 m' `
Selector sel =
( ^; }' ~1 m B new Selector (proto.getClass (), "heatbugStep", false);' b0 e* C* u" D2 f, E0 w# A) _
actionForEach =
8 P$ r" X. J- _' b0 N# X modelActions.createFActionForEachHomogeneous$call
& Y* q. ]: G" J, [; I' k (heatbugList,. ~3 B3 q: n1 A. {+ ~
new FCallImpl (this, proto, sel,
" o6 [ o5 g, G* y# n new FArgumentsImpl (this, sel)));
' S$ q7 ^7 z7 z! f) [3 k# z( o9 s } catch (Exception e) {
' J) C9 J8 N V2 L; u# p e.printStackTrace (System.err);
+ X! [6 N# z0 V) e }
' W [7 T" F4 V J5 p
2 ], w+ a- j; I4 c& Q syncUpdateOrder ();) P$ ~& |/ S4 \2 z
% l" ~' S# Y2 O% f( N try {
8 v+ |; Z8 G4 }7 B* Y modelActions.createActionTo$message
& p4 o- G9 C: ]0 b1 _ (heat, new Selector (heat.getClass (), "updateLattice", false));
* D9 G0 O# f8 S/ E } catch (Exception e) {3 n, ~ ]- x4 \- S* ?, E' i
System.err.println("Exception updateLattice: " + e.getMessage ());
4 z" M% b9 u0 r( X } N; T7 M0 ?2 l) y& v s. [
. D ?) |0 E8 W1 f
// Then we create a schedule that executes the
& _( ~! ?8 g4 K% \7 N- b6 K // modelActions. modelActions is an ActionGroup, by itself it6 b+ y8 w1 R$ d. \; N0 p. Z) b
// has no notion of time. In order to have it executed in
3 ^! V; ~6 p g1 x: s! U // time, we create a Schedule that says to use the
$ r O: x- M; n$ o0 M) z! ]5 ^ // modelActions ActionGroup at particular times. This6 z$ w3 \+ U' d2 {. Y; C
// schedule has a repeat interval of 1, it will loop every
3 [- B7 _0 o4 ?8 t; X" M // time step. The action is executed at time 0 relative to
p4 Y' Z/ C$ e" ~4 {. Q" Z // the beginning of the loop.
/ E" w( R3 _2 \+ [' Z2 b4 S$ Y& P6 }7 `, m$ H$ D
// This is a simple schedule, with only one action that is
G1 P- M2 r1 R C // just repeated every time. See jmousetrap for more
3 R* b8 D0 J8 Y // complicated schedules. w4 X+ U2 w+ T; x1 X V
7 Q2 y% m' z ^/ e8 i
modelSchedule = new ScheduleImpl (getZone (), 1);
U& R+ C$ J7 _9 [* B- z s1 A modelSchedule.at$createAction (0, modelActions);% w: ~& L: ~- ?. V5 l
6 J' F3 d- a2 |* U. @ return this; M" m( Z. i. S! o& K3 F# x
} |