HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 E6 w& c* {. @2 M& R8 g ?( {' K. M( Y6 I3 J$ r9 t8 I
public Object buildActions () {: q0 }0 X- e$ l6 ]+ d: y
super.buildActions();
! D5 R) Z, n7 H1 S# e( t/ t
( g' A& H6 u @7 B! K" b // Create the list of simulation actions. We put these in$ ?9 J* G8 G. x7 t( O
// an action group, because we want these actions to be/ M: ~" d+ p$ U& p
// executed in a specific order, but these steps should
$ h& E3 _- c+ ~7 R9 l# A // take no (simulated) time. The M(foo) means "The message
, g. _1 i! {5 m" ^' H. A% _ // called <foo>". You can send a message To a particular; g3 j4 g: `1 W/ z4 e
// object, or ForEach object in a collection.
' l9 A0 \4 T6 w8 h5 D" u8 n9 d
% N1 V z$ Y8 {, y) N/ T7 Z2 T // Note we update the heatspace in two phases: first run
' K, {# V$ \% _# y6 ?' _3 e# Z // diffusion, then run "updateWorld" to actually enact the% P3 E" p H. B8 o( W
// changes the heatbugs have made. The ordering here is9 i' \( U j( g
// significant!. n8 _& u3 x% [7 S: V' G- o
0 o4 z; h. y6 {2 `" @; z
// Note also, that with the additional" u8 f4 @1 q* ]3 q3 a# { h2 Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 @9 e" o# J: b+ \& ? // randomize the order in which the bugs actually run; G0 d, g) T M+ D
// their step rule. This has the effect of removing any7 D! b# I% L- i. Y: t+ Z5 y2 e D: }+ H
// systematic bias in the iteration throught the heatbug0 Q3 b) `) D( c+ [# ?
// list from timestep to timestep
8 b& H6 K- @% D6 j+ E ) B% W) j6 S- @7 H3 b) Q
// By default, all `createActionForEach' modelActions have
7 z, `# i$ ]9 J( V' h // a default order of `Sequential', which means that the
7 u% r0 J0 t9 r3 g! u // order of iteration through the `heatbugList' will be
- u: }9 z# f4 @* _ // identical (assuming the list order is not changed
j% z% h9 e5 X ~7 |! P3 @& A // indirectly by some other process).% h% A( h, N5 W O6 K% ?6 V
: e* _' ?: X' r9 v* D D modelActions = new ActionGroupImpl (getZone ());$ Q4 p5 F) L# {( m) c1 [) s
x7 X* j/ ]) ^5 M' l
try {' S" p4 a* W/ W; {. K
modelActions.createActionTo$message# t$ q8 j5 m0 q
(heat, new Selector (heat.getClass (), "stepRule", false));
% k4 \9 M, F* f& J: E% v+ G } catch (Exception e) {
$ J9 q4 z& u3 _& q5 u2 Y! ^7 g System.err.println ("Exception stepRule: " + e.getMessage ());
1 _+ o( _% {: z6 C% D' q4 a }
5 F- A1 @& l: [+ q- O+ K9 V- ?4 i9 @( i+ c. X( S
try {
! Y$ u6 ~ ~9 X: ]. G: Z& E Heatbug proto = (Heatbug) heatbugList.get (0);" a, B8 E! F3 m
Selector sel =
" E/ N8 R) |' z, Z new Selector (proto.getClass (), "heatbugStep", false);; u" a4 x) @& W" d' }
actionForEach =
8 T; I' _2 ~3 Z+ q8 o, K. X$ ~ modelActions.createFActionForEachHomogeneous$call9 u: x1 V% P3 |4 [5 J+ E4 P: F, r
(heatbugList,! u" x/ w# }" F' U& v
new FCallImpl (this, proto, sel,+ @3 m7 e% z6 d
new FArgumentsImpl (this, sel)));
! `. P( Q/ T7 |: r, N } catch (Exception e) {
. s) A; i2 a3 G# B e.printStackTrace (System.err);
1 y, p8 L% _- M; m/ ] }# Q# I) d( u: c
% @9 q: g6 S1 e" M
syncUpdateOrder ();
- G& |) R# d- i* Q8 A, {; C$ q. d) i( G
try {$ r* C# Y w7 Z3 J+ Y* q
modelActions.createActionTo$message - J5 s$ C+ c+ W: C% d9 @
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 B: @( p* ~) j1 a! \, l } catch (Exception e) {
3 n! d7 K H8 e& s) o3 X; l( r6 c: z System.err.println("Exception updateLattice: " + e.getMessage ());
/ w2 @. N% E' O: t9 D" ^% W }
7 I, o( S1 ?; h% T, u
( }$ X+ n k$ o* j2 K. t, j // Then we create a schedule that executes the
, r) M$ K* A8 `: ^/ }! Q // modelActions. modelActions is an ActionGroup, by itself it
: \! o* w* M( n! W6 d // has no notion of time. In order to have it executed in, X+ t+ t4 m6 ?- O8 k$ Y$ u
// time, we create a Schedule that says to use the( z5 x% N7 R3 T7 j2 Q( k7 b, e
// modelActions ActionGroup at particular times. This
$ X1 d9 I& L7 O# b, G // schedule has a repeat interval of 1, it will loop every
/ v! r, T+ V5 ]. }& N9 T% F // time step. The action is executed at time 0 relative to: V% i3 }8 Y3 {
// the beginning of the loop.3 c: `# T' z# D6 ~* Q" a
- M; V( I: i, |3 ? // This is a simple schedule, with only one action that is7 a0 C! F& l; U% h" e2 O8 y: i
// just repeated every time. See jmousetrap for more3 ]1 l( n' C5 ?: g, o5 B6 p
// complicated schedules.
+ N' c# H! R' n, a
) W" i4 v- I# Q% R& m modelSchedule = new ScheduleImpl (getZone (), 1);
) a/ z2 g" ^( I. { X* @( l0 z& r modelSchedule.at$createAction (0, modelActions);
+ C8 A) z4 N4 y; Z8 { * y% R/ i% l) f4 |4 I
return this;7 k. ?+ @; k$ Q, y1 C) r. i- P
} |