HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ r# x$ g0 g; N0 A
* i2 o c/ {$ T& A k public Object buildActions () {8 ]. E( i9 m% u- x
super.buildActions();1 X# F8 Y, d9 J" d% n
0 U! ~" Q6 {3 B7 \/ d // Create the list of simulation actions. We put these in* ]" [. \+ p8 E3 g1 p
// an action group, because we want these actions to be
1 H! L/ Y Q3 S) Y) c' q* K* X // executed in a specific order, but these steps should
1 s6 _& b4 P" k // take no (simulated) time. The M(foo) means "The message
7 Q2 `6 W8 M S/ K f // called <foo>". You can send a message To a particular
% s8 d0 O% c/ B* U/ x // object, or ForEach object in a collection.) F; T4 A( i- e, Z4 X# J
- u! j( s& ^$ u7 V4 s // Note we update the heatspace in two phases: first run. [5 X0 n! |, r9 X& A
// diffusion, then run "updateWorld" to actually enact the2 {/ q* X8 u4 x5 M# Y# f. L) u
// changes the heatbugs have made. The ordering here is
2 T9 F/ q; z0 F! K" A8 K3 n // significant!
' }. S8 Z! I2 N; @2 Y6 J 6 t! Q6 o% v# m/ p
// Note also, that with the additional
7 C- O/ K8 y- x3 c0 P: T8 ]5 a // `randomizeHeatbugUpdateOrder' Boolean flag we can
* I! h: O/ Y$ [ // randomize the order in which the bugs actually run1 d9 w/ ^/ k. |9 s, {% a8 i
// their step rule. This has the effect of removing any
g6 ?; I! G. b. Y J, m // systematic bias in the iteration throught the heatbug. H9 c `3 {# e; |2 |2 q
// list from timestep to timestep
: I. y- \0 H$ f0 j. f% x 1 A4 g# Z0 P9 z# d. }
// By default, all `createActionForEach' modelActions have
3 ^. _( H- k2 T$ R# k1 A$ J5 X; K // a default order of `Sequential', which means that the
7 I( C" h$ Z; O- h7 W8 o N4 g // order of iteration through the `heatbugList' will be
5 p. m$ @! P. T // identical (assuming the list order is not changed' ]5 r$ y( d! v5 g3 @* e; S3 h
// indirectly by some other process).
$ o! Y+ Y$ E3 `0 f8 e
5 H# z8 N: Y! D# j; n& z modelActions = new ActionGroupImpl (getZone ());: T( s4 I$ ]$ S% R
5 x1 h6 L2 M# W5 `. X1 _, [. @
try {
- W: B* w _* [, F1 y. ?- r4 Q modelActions.createActionTo$message
1 o/ ~+ Y* h+ T% j4 G# ^ (heat, new Selector (heat.getClass (), "stepRule", false));4 a( D0 ^4 c/ p* h& D+ x
} catch (Exception e) {8 Q* @$ F& @* Y- l. V& F. C/ Y
System.err.println ("Exception stepRule: " + e.getMessage ());9 `* P- n! f$ d- |/ l9 t
}3 ^ m3 d' q7 Y9 Z) W$ l! V
/ T. R$ e* c; t. q1 V. U$ O
try {
' f4 e/ D1 b8 w2 \ Heatbug proto = (Heatbug) heatbugList.get (0);
+ C7 e" y2 r ~6 a# ?* h ~ Selector sel =
5 N9 l! K6 ^+ h6 ? }+ i. g }1 e new Selector (proto.getClass (), "heatbugStep", false);
3 C3 b6 B v1 a* Q4 O5 C7 g actionForEach =: ~; K; `# c" ?7 Z% A
modelActions.createFActionForEachHomogeneous$call
' X2 G. g& L5 _ (heatbugList,
' H' J2 ~: j, `% X new FCallImpl (this, proto, sel,
7 w2 Q5 _) q- E new FArgumentsImpl (this, sel)));
6 O3 F+ U8 M0 c+ h! n } catch (Exception e) {% u/ }* h5 F O( X" y* @% ]
e.printStackTrace (System.err);
7 b& g9 D$ t& O/ a& g0 X: r }
) O' U# F# z2 w" y4 W
3 r/ s3 N4 [2 X9 w% q; ~7 O' ` syncUpdateOrder ();
" K( k) q, [! D
) Y! `9 Q$ U3 Y try {
3 F! U4 x: Z; } m. y* V modelActions.createActionTo$message
9 h- o S7 r: ? (heat, new Selector (heat.getClass (), "updateLattice", false));" q$ j" A+ L* { A* M' q
} catch (Exception e) {' C p. b; b b) S% e
System.err.println("Exception updateLattice: " + e.getMessage ());
6 z$ ?7 [5 i5 Y, N }
5 V9 @4 |; b8 |# M2 H v5 D ' {- \- x$ q6 r/ \ h; X
// Then we create a schedule that executes the
# I8 y/ {9 v* Z! L; x6 A2 Z. e // modelActions. modelActions is an ActionGroup, by itself it8 x1 r' ]( W$ Y# q `
// has no notion of time. In order to have it executed in
/ F4 [( S; m# x1 D // time, we create a Schedule that says to use the
! r: _; s: H9 | Y! W // modelActions ActionGroup at particular times. This
6 q$ T# }2 H" D# x0 c( d // schedule has a repeat interval of 1, it will loop every
* w/ J$ A9 V5 | // time step. The action is executed at time 0 relative to1 e' I( x$ G, d0 p5 ~
// the beginning of the loop.: [: V i: o. Z9 u* k
+ D% Q f8 L% b0 Z
// This is a simple schedule, with only one action that is7 x( |; B" m, R) q
// just repeated every time. See jmousetrap for more/ a, W: l" |! ~8 C' ~
// complicated schedules.
2 b* h' q8 ?4 r ( |1 I+ Z* V, _: x; P; g4 e0 o5 M
modelSchedule = new ScheduleImpl (getZone (), 1);
: T& x1 N6 Z& N; V$ [) u$ n modelSchedule.at$createAction (0, modelActions);
5 [; R1 u- D' C6 `
' J0 @( C( h: w' q8 z6 T8 H return this;- m% j/ k% P. a3 ~) ]& k
} |