HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ Y; B8 w2 M- Z
* l$ R# x* A) z# r
public Object buildActions () {0 m _: s3 s* e4 |" g/ N) r9 B5 |- s! i
super.buildActions();
. y8 }. l5 k6 j3 M
' v& a) ] ~2 K* K) s& T // Create the list of simulation actions. We put these in
0 N: F0 `+ R; Z // an action group, because we want these actions to be. p% g6 F# v( X Y( U9 E. L9 G% U
// executed in a specific order, but these steps should
, O) p0 b! q* R // take no (simulated) time. The M(foo) means "The message
: d7 B/ T; ] R, w2 K9 M" ` // called <foo>". You can send a message To a particular y2 @0 e+ u0 O r! U5 Q4 P
// object, or ForEach object in a collection.1 v. y4 \" }$ C' y. G1 {
/ T5 x) b$ |* V // Note we update the heatspace in two phases: first run
/ L+ ~: X% W5 G! ^/ X6 F // diffusion, then run "updateWorld" to actually enact the
% _! h* \$ c6 }4 e% Y" J( h- @ // changes the heatbugs have made. The ordering here is
! v" X8 z1 h- X& w1 F3 c4 a) N // significant!# Z j3 y' ^6 b/ D6 w C
5 _' _7 F( ^6 ^9 w
// Note also, that with the additional: T# ?! C N: g) U% B
// `randomizeHeatbugUpdateOrder' Boolean flag we can# M% y+ G/ U( l' O
// randomize the order in which the bugs actually run; q: o7 D5 v; ?
// their step rule. This has the effect of removing any
9 ^* e/ k1 U6 C0 X // systematic bias in the iteration throught the heatbug
4 C- S: F7 r4 ?6 D" ` // list from timestep to timestep
$ B* ]9 G) E' {4 _
B$ H: f8 P, [ // By default, all `createActionForEach' modelActions have- ]+ W* Z/ k% E7 P9 k4 ^
// a default order of `Sequential', which means that the2 S$ x2 f0 ?$ i8 _/ `7 i
// order of iteration through the `heatbugList' will be
6 g( J: c F1 t3 x+ \% F // identical (assuming the list order is not changed! u! m4 h$ i- K7 t; V
// indirectly by some other process).
5 S0 G8 W6 G6 a
% Z |1 i) A4 v) p1 X modelActions = new ActionGroupImpl (getZone ());* M4 T' r; x. o. T' @. v6 T/ O
4 G1 B: D- P1 C( Y% g7 Q0 D4 u: j# [ try {
) h: o0 A9 m8 X# j1 _ modelActions.createActionTo$message& W E/ R2 t4 f5 p6 ?7 N
(heat, new Selector (heat.getClass (), "stepRule", false));: G& y% ~, o! c2 z4 Q
} catch (Exception e) {
& u" _3 F: t/ M System.err.println ("Exception stepRule: " + e.getMessage ());
/ G" }2 s* k* ? k; z, l: j6 H; n }
- Y$ h4 t, C$ U+ w& n
5 S6 S$ O. Q6 D& q6 V( C3 g try {. O; G7 S# A2 d4 ^2 T5 O* u) T( _
Heatbug proto = (Heatbug) heatbugList.get (0);
0 R' u0 G% u e- d, q/ k0 C Selector sel =
3 c! s. V* e5 W2 @7 a# M9 x& a0 e new Selector (proto.getClass (), "heatbugStep", false);2 ^+ u( x% W* j" P
actionForEach =4 T! g6 s! b' N# Q+ D8 T% T; ^
modelActions.createFActionForEachHomogeneous$call
9 y* p+ q* B- ^$ n' ] (heatbugList,
& z5 _, S+ U: | c6 z new FCallImpl (this, proto, sel,2 s3 O, t7 S# o& ?
new FArgumentsImpl (this, sel)));! E3 F \6 b0 D, f" x* ]# n" ^
} catch (Exception e) {; b2 R" j1 N& ~5 D& D- G3 i. c
e.printStackTrace (System.err);5 Z3 h7 N: C0 A6 d: l3 _0 F
}
5 O1 S5 c& s8 s j# T7 M
% _9 p2 i7 L/ K syncUpdateOrder ();
. K) [' {+ j K. n5 I; V
+ y2 M; P+ {3 E# { try {
5 _" Z& [% V4 ]( ?3 I4 G" w/ ? modelActions.createActionTo$message
1 z2 |! W* {1 z, L+ i3 ]% r T4 v (heat, new Selector (heat.getClass (), "updateLattice", false));4 w6 F6 q: W" ^2 N3 E
} catch (Exception e) {
' r* c6 Z: [4 B/ \% f System.err.println("Exception updateLattice: " + e.getMessage ());/ B) s1 ~$ E, q, _1 r$ @. C
}% |( T4 {% l% O% q( ^, A
) |/ K+ C0 Q; h0 O
// Then we create a schedule that executes the
! Z R$ _! X& j1 N7 r$ _ // modelActions. modelActions is an ActionGroup, by itself it% x$ m3 J! L8 j" N
// has no notion of time. In order to have it executed in. T" h6 n3 |4 s& @- F& E5 S
// time, we create a Schedule that says to use the7 B+ \: Q+ @" ~' t* E% [5 R
// modelActions ActionGroup at particular times. This; T5 M1 r U0 y$ |' e
// schedule has a repeat interval of 1, it will loop every$ i( b# ~% ~$ k
// time step. The action is executed at time 0 relative to
" a, q$ `+ [& ~+ ^7 i) W6 E6 c' f // the beginning of the loop.# M/ P \9 O# _" F# U1 l0 [" ]' v
S$ w3 N% \; ] g // This is a simple schedule, with only one action that is- g1 j" I/ P$ {. |9 p
// just repeated every time. See jmousetrap for more' N5 Q" G) G) e0 ~
// complicated schedules.3 l. x& P# J3 r( J9 a
- o1 ~/ k& R" ]) O) F; r/ z+ c modelSchedule = new ScheduleImpl (getZone (), 1); t" S# c1 U6 w- w6 t
modelSchedule.at$createAction (0, modelActions);, c$ U5 H. J( d- Q8 f: S" i
4 d. a) R5 I. T
return this;
0 Y% z* o4 s2 w } |