HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& Q5 a9 j( x" f. j: \
1 V2 I" ~3 _7 ^/ z. a9 x
public Object buildActions () {
: O' ]/ w8 s' l2 T6 I5 g. _$ |0 ^9 t super.buildActions(); c3 l4 W# A2 l
& {' T3 V' \3 q! x$ U
// Create the list of simulation actions. We put these in1 c/ \4 i, m2 v
// an action group, because we want these actions to be" q; Z, M% D* b
// executed in a specific order, but these steps should/ A1 m$ m9 f! N* f
// take no (simulated) time. The M(foo) means "The message# J* @- A3 D2 w# d8 P9 g
// called <foo>". You can send a message To a particular
% U H4 e; |! R/ J) q5 u" G // object, or ForEach object in a collection.& D& W- n, j* v5 H f" p/ @
: v7 E8 h/ s. Y
// Note we update the heatspace in two phases: first run
2 X2 q: Z' ?) {, {* P4 f3 H8 [ // diffusion, then run "updateWorld" to actually enact the
5 A# L$ t* Q( j* M // changes the heatbugs have made. The ordering here is5 V- b( f& E% ?% s/ j& G6 ?; W
// significant!* F$ E, I& w1 d3 Z# P7 L
2 f# e( |8 I6 H) b7 r6 G, T' ^ // Note also, that with the additional2 X8 G- t/ o8 V0 v4 {
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 D5 y" X- m) g7 }4 U* H7 k
// randomize the order in which the bugs actually run' E2 L A* F% ^! @: s
// their step rule. This has the effect of removing any: u3 T' H- @; w/ |' d, o
// systematic bias in the iteration throught the heatbug
. \7 `& W# k8 j. k' |4 A5 z" f' f // list from timestep to timestep8 ~) h5 P4 v/ ^0 I2 g4 `6 n
- i! c8 u1 I3 j8 w! }/ K // By default, all `createActionForEach' modelActions have
: G6 z& h- j: y+ O // a default order of `Sequential', which means that the5 j8 [. M# f2 v+ S3 Z$ N6 H$ H7 f
// order of iteration through the `heatbugList' will be0 H1 Z% ?6 Q$ b/ `! g/ z, M
// identical (assuming the list order is not changed
! ]& n& ~2 |% f // indirectly by some other process).
0 S8 G3 S) j0 B7 V. i, u # g7 v4 H+ M( e$ k9 D0 {/ U
modelActions = new ActionGroupImpl (getZone ());. b; m" G+ S* |# |
2 ]" |; Z/ Y; O* x
try {. ]! Q" h6 q& D7 @4 j% G! G
modelActions.createActionTo$message
. N, R3 b; ^& O4 X- l (heat, new Selector (heat.getClass (), "stepRule", false));' w. ~/ ]8 P5 [, C
} catch (Exception e) {
, S6 B( ^% H+ I/ Z! H System.err.println ("Exception stepRule: " + e.getMessage ());
0 X$ s; ?7 l' x' b1 C( u }
/ B. q% i/ r8 U8 Y" ]( X1 ?8 f* o1 q; c; T% e! P
try {
: z' E W/ K* D9 S a Heatbug proto = (Heatbug) heatbugList.get (0);$ V! K' L, `6 s7 e- U1 p
Selector sel =
9 R3 h$ O- n* g$ @; Q new Selector (proto.getClass (), "heatbugStep", false);
& \4 Y" k2 z/ {2 {9 J actionForEach =4 J% D2 b' ~: D! ~5 d
modelActions.createFActionForEachHomogeneous$call( K# Y9 `6 ?7 q, y: w2 [ E: G
(heatbugList,
, e9 K2 ^' N0 H$ x4 D: o# N new FCallImpl (this, proto, sel,- e5 T, ~3 _. s
new FArgumentsImpl (this, sel)));
6 l7 ?5 G& A1 \" ?/ k1 O/ K } catch (Exception e) {
2 M5 c4 _6 c% a e.printStackTrace (System.err);
1 O9 D( _$ |8 s }9 n& G0 c* \) u! L5 ~1 L
4 Q% V: O' c& g6 K x. j
syncUpdateOrder ();
( s! v: m6 Q' g1 Z$ |0 f# |/ t6 c3 a1 b) a Y$ s/ |
try {
9 m8 i' Y3 _" I H, w modelActions.createActionTo$message . j! J* h, v; o+ }8 M" b2 b! U1 ^4 E- \
(heat, new Selector (heat.getClass (), "updateLattice", false));
& g8 h [8 d* F% _! a6 n* e" N } catch (Exception e) {! s. i* q! z& f1 b9 x* V7 U
System.err.println("Exception updateLattice: " + e.getMessage ());" ]3 ]: _3 O# y% m: d$ a
}
* \* s; ~- u. d( {# r9 Z
) H( c+ g, b5 t& [! \# T5 [ // Then we create a schedule that executes the2 ~$ H+ X0 ], V( Y: M" g
// modelActions. modelActions is an ActionGroup, by itself it0 @! K! T8 X$ G9 O* ~" W1 N; n$ X
// has no notion of time. In order to have it executed in) R2 v) h5 C$ ]7 ]5 e' z
// time, we create a Schedule that says to use the
) R+ E' M0 t; I! o. |2 @; L1 A // modelActions ActionGroup at particular times. This
6 [# l1 l) `" I- I // schedule has a repeat interval of 1, it will loop every
1 A" k4 s; R+ `1 ]8 }6 t, H // time step. The action is executed at time 0 relative to6 m, |+ j' A6 {% z
// the beginning of the loop.
/ R1 ^- L: v% T. g
# Y! j' w1 H `/ V // This is a simple schedule, with only one action that is4 ]: L9 F2 ^1 O
// just repeated every time. See jmousetrap for more' g7 ]* r. ?: D
// complicated schedules.
9 ~3 F6 _' Q: P( U; D! E `- ] * t# } C I/ ]
modelSchedule = new ScheduleImpl (getZone (), 1);8 a u( i8 e7 D# q( ?2 N& h
modelSchedule.at$createAction (0, modelActions);
1 j! @# @& _, E0 ?; p/ j! f; F 0 o; r- a9 x2 ^6 M' G
return this;3 s+ D9 U8 Z( Q) B: U
} |