HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& E; A" u* X1 O" L; J A8 H
9 L. O8 S# d9 f4 N; w1 S# |. }) [! x public Object buildActions () {: j& e% d6 a8 v+ u4 P" R
super.buildActions(); X0 O* K* n: y
: p' M8 W4 r$ }6 _ ` // Create the list of simulation actions. We put these in; M. h# C( w( f
// an action group, because we want these actions to be$ E/ B' } n2 U5 o) o
// executed in a specific order, but these steps should
M9 z* d$ b- e4 P I- m1 r- I // take no (simulated) time. The M(foo) means "The message
5 q1 o0 l% F' y9 a: g% g/ z0 W // called <foo>". You can send a message To a particular4 @- q# n+ k. u
// object, or ForEach object in a collection.
6 L* k4 p( j/ Q" g6 h
, s: t: u; U$ Z$ h$ w; w; Z // Note we update the heatspace in two phases: first run
9 s. |' D9 N/ {( U) k2 E // diffusion, then run "updateWorld" to actually enact the6 x$ _6 _) @9 f; i: H1 v6 N3 `+ m
// changes the heatbugs have made. The ordering here is! J2 R# f" ]/ b" C" \
// significant!
1 Q+ P6 E. f! ~. ^4 u6 H0 l5 b& c + S! o6 a+ }# x& K/ @5 f/ b }6 A
// Note also, that with the additional
R; s. t p% k' p" s0 V. m+ w // `randomizeHeatbugUpdateOrder' Boolean flag we can/ U4 R! ^# L0 V) t: s& j1 j
// randomize the order in which the bugs actually run
! c& k' y; O0 f' `) y // their step rule. This has the effect of removing any
$ o" [( x0 i; v // systematic bias in the iteration throught the heatbug1 M6 d6 M+ h" `
// list from timestep to timestep
3 B* C+ ]8 b" Y ) {9 j( i3 U0 e- F' ~1 W
// By default, all `createActionForEach' modelActions have
5 | `4 F4 k r7 {. u$ k // a default order of `Sequential', which means that the. [% J. W/ @6 _1 P$ }9 j2 w
// order of iteration through the `heatbugList' will be1 W1 J8 S4 E5 _6 B0 y6 _
// identical (assuming the list order is not changed
' W* T) d) i5 o# Z // indirectly by some other process).
k- x4 F+ U3 U& D( f ( I0 w6 y- x2 U2 S* |! D# D/ t
modelActions = new ActionGroupImpl (getZone ());
& V/ h# ]$ L& Q) A
5 z: {7 h- g- X5 J' Z. |3 i- Y try {$ o# U4 `9 R6 v: u
modelActions.createActionTo$message
' l5 e6 Q/ n! d( m/ k- R2 i6 {3 H. j (heat, new Selector (heat.getClass (), "stepRule", false));
: e4 T7 p. e2 ]0 J. |- Q } catch (Exception e) {+ b. j; @2 `* ]4 N# P
System.err.println ("Exception stepRule: " + e.getMessage ());* _) J1 V+ j% F7 q) i( N
}) N! X: T+ W# }3 i$ f* w6 T ], }
/ r/ e7 T7 T+ O+ q7 X3 M
try {9 X0 G/ d, G6 Z/ c
Heatbug proto = (Heatbug) heatbugList.get (0);
4 H' L4 z- E1 _; \4 J Selector sel =
% e: c' b, ^- I% J new Selector (proto.getClass (), "heatbugStep", false);
2 z+ B l: u6 ~% i actionForEach =
! j( t% W0 }3 ~4 z2 x4 o8 i modelActions.createFActionForEachHomogeneous$call8 |- f8 H& N) m( u. G
(heatbugList,' F& K& Z1 X; ^# n( B' N
new FCallImpl (this, proto, sel,# Q! @; c4 H6 W3 K
new FArgumentsImpl (this, sel)));8 Q" |! ^+ ^9 a9 v
} catch (Exception e) {
. Q0 q9 l" t1 U e.printStackTrace (System.err);
' A. b1 H9 n# |- ^5 I; ^ }
4 e, G8 \9 {9 f$ ~0 |) H f! D3 A9 O+ {' J
syncUpdateOrder ();
3 D7 [: p- {' A+ d
+ M% p) i7 J. c3 n, G0 l# @ try {
6 s8 e5 `5 _: f7 L( R, E modelActions.createActionTo$message
' l; c4 n( ]( c! E- {7 g- R (heat, new Selector (heat.getClass (), "updateLattice", false));
, v6 J4 ^9 q( K+ c$ Z4 X- N } catch (Exception e) {
2 n/ R. x% O3 \$ _ System.err.println("Exception updateLattice: " + e.getMessage ());9 L) L: P4 ]' Q* z
}
) {9 b- |6 z+ B2 _6 V
( k/ p z, A! U) E3 y+ X- u* `5 [ // Then we create a schedule that executes the
! Q8 h! G7 A9 c' z, l- W" W // modelActions. modelActions is an ActionGroup, by itself it" s% R( o+ K Z$ T1 o! n- D4 S
// has no notion of time. In order to have it executed in
r$ ]/ ~# p0 v4 t4 H1 F // time, we create a Schedule that says to use the' h- [5 H3 T5 H
// modelActions ActionGroup at particular times. This
* ? s' I% D/ \ // schedule has a repeat interval of 1, it will loop every
g, D5 `* m- D. F7 ]. n7 o d // time step. The action is executed at time 0 relative to
1 v/ G' }1 J% G, D) T5 W1 S6 q // the beginning of the loop.7 d) [) ?. Z7 v
) \' O1 a: O# g. H( c6 R ` // This is a simple schedule, with only one action that is/ v" F- k+ z' I% F
// just repeated every time. See jmousetrap for more$ |2 p! G3 d$ E) r
// complicated schedules.
+ s* ^" @; o1 a. |( J8 t* t ^. S3 p! a8 _( e7 P
modelSchedule = new ScheduleImpl (getZone (), 1);
8 I7 ?$ Y7 o( L% I: a modelSchedule.at$createAction (0, modelActions);! {% W5 C" C1 g7 b1 Q- F& A
0 @/ i. C) Y% L: @# S- C6 R return this;
( K9 g/ J1 w' N# I" i4 s } |