HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( R, @$ d( H" c/ ^. {5 y7 p% P1 Y( I3 I% d8 W. e) f9 s4 j
public Object buildActions () {
, t( I- Z, N+ u2 B super.buildActions();
$ n% O3 A# j( B # ^9 j* _- P, h9 M# _
// Create the list of simulation actions. We put these in
( |" N/ {$ G/ n& `/ ^+ E) I // an action group, because we want these actions to be
& t5 h/ G" Q9 [ // executed in a specific order, but these steps should
9 n, U, w6 i9 @+ g // take no (simulated) time. The M(foo) means "The message% j: `% ]! d' Y& d
// called <foo>". You can send a message To a particular
' S, i: t2 q% v8 x // object, or ForEach object in a collection.
1 ]3 \7 ~) k( M5 T' F
% }- c/ W& X4 k9 _ // Note we update the heatspace in two phases: first run
$ Z. u1 N' M! e: V: q/ m // diffusion, then run "updateWorld" to actually enact the
0 I1 f/ c& l5 P4 W# S // changes the heatbugs have made. The ordering here is/ n9 J' z. ~2 N \- J
// significant!
$ z* ?* N: \# i# s ' S, x* x3 Z) k8 ^! P" c) \; `
// Note also, that with the additional0 y" c' ~+ g3 }2 v! }% H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
! `2 C6 A; v; r5 q; M // randomize the order in which the bugs actually run
. C8 \$ P7 ^ @9 a! L" t // their step rule. This has the effect of removing any
' S' _& A8 H* }5 p3 ~: ~/ ^ // systematic bias in the iteration throught the heatbug+ H# T! F: a( T! I
// list from timestep to timestep; |0 a. ]! ~) Y6 D, t4 u0 J$ e
8 I) ^( A6 D) C' I# t5 j3 i% U4 g* C // By default, all `createActionForEach' modelActions have
9 V5 Q9 ], {2 Y- c7 p // a default order of `Sequential', which means that the
+ o$ b, T8 L) y6 v3 I1 y& t // order of iteration through the `heatbugList' will be! k9 f$ `3 D! l; f
// identical (assuming the list order is not changed
% J8 Y' e* @6 T6 k // indirectly by some other process).% f: ?$ }8 Z9 [( ]5 v: d% K- Z4 \
- m; N+ a9 d: V2 k modelActions = new ActionGroupImpl (getZone ());8 ~8 T8 m* I5 [( C1 I5 S
6 k* |& q" ^" [* x1 \ try {
! ]/ ]! B, Q3 N3 E5 ^4 H* O! J modelActions.createActionTo$message
+ A8 Y* h2 P* Q6 v4 U (heat, new Selector (heat.getClass (), "stepRule", false));9 \$ F% _+ A2 ]" @8 L
} catch (Exception e) {7 Q2 E: ^5 ^# K' h' I
System.err.println ("Exception stepRule: " + e.getMessage ());
* o8 i* X: g: P* D5 R* Y }
8 @/ y% {" n d0 o) r+ T: i% R4 s( [: Z( h: D2 e: X; V% R9 j" L
try {6 V6 |" P: J$ H2 D4 n
Heatbug proto = (Heatbug) heatbugList.get (0);) I! x3 [, {9 A& j+ L5 e5 i
Selector sel = , r4 B. ]) q7 c' W7 q
new Selector (proto.getClass (), "heatbugStep", false);/ c# Q# z. z7 }. {
actionForEach =
4 p0 G0 L4 q- \2 j modelActions.createFActionForEachHomogeneous$call
# s/ c4 L3 |" g" O2 J/ O! M (heatbugList,
1 X" Y6 E3 }6 |9 ~4 \ new FCallImpl (this, proto, sel,, s; A1 K2 W3 ~, i+ t
new FArgumentsImpl (this, sel)));2 l* F# v" }- L7 w
} catch (Exception e) {
6 d! [7 I7 G7 ?# j; r q4 a e.printStackTrace (System.err);
, {2 A$ i. `3 ]8 d! E }
( y1 Y7 w! N; [# ?" [, r2 [ 4 m4 y5 `: u' o8 ?9 j
syncUpdateOrder ();) s& r0 `+ L/ e4 Q5 A
w, a/ A$ Q2 t. s0 {9 |; z" Q
try {
, X* Q" i; |1 H8 ~, R( a& I5 b! o modelActions.createActionTo$message
* R. ~1 K4 c4 L* _) M (heat, new Selector (heat.getClass (), "updateLattice", false));
# U( W7 O/ d9 _3 N4 \- ? } catch (Exception e) {
) |% t" _' P9 h1 F, G System.err.println("Exception updateLattice: " + e.getMessage ());$ V1 p. ?# b; L- h
}
4 _& V( Z9 O0 n! v+ x # j, s& E0 l+ q! e
// Then we create a schedule that executes the
/ v0 K( g! {+ W5 n( {' m# D // modelActions. modelActions is an ActionGroup, by itself it
! C- r! v* j* ?, R' L" g" z // has no notion of time. In order to have it executed in
6 o5 q0 Q4 S# R- W' A6 j* L( B // time, we create a Schedule that says to use the* S# m( ?" ?+ ~/ l6 X" O
// modelActions ActionGroup at particular times. This2 @, |/ `) }; A3 F: d
// schedule has a repeat interval of 1, it will loop every* X6 y: e8 j: D* x4 s) Y% Q- `
// time step. The action is executed at time 0 relative to
% D4 j4 G9 J3 P9 e& D // the beginning of the loop.
. v( O. T- E4 S3 t2 p2 E9 m6 w& x& x# |3 o+ Z* J) c
// This is a simple schedule, with only one action that is1 C( t4 N: X/ t
// just repeated every time. See jmousetrap for more
7 L2 ]4 K# U1 h // complicated schedules.) f" Y6 _# ~; Z/ J3 F. `4 F
/ p, ]$ [0 w5 Q6 S modelSchedule = new ScheduleImpl (getZone (), 1);8 T; p& @0 R& z( z8 n+ {2 D K% Y
modelSchedule.at$createAction (0, modelActions);
! L8 Y& f8 U0 m! s3 G; W0 n . D" h; r9 p# t" |3 n% c4 D. B
return this;% l! y9 h' X& u# k
} |