HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 T6 Y: k& k& Z9 u
( f1 Q% ^' O/ w. p6 K- r6 D public Object buildActions () {
& s/ S/ y3 I4 H% P7 t* F, y super.buildActions();+ E: z3 P& U+ T1 s% A8 C
9 u* b6 W; C) h6 P$ ]( \: K
// Create the list of simulation actions. We put these in
% w, j8 r8 l/ B* P/ \ // an action group, because we want these actions to be
4 l" F4 m+ S3 Q, D) f* Z // executed in a specific order, but these steps should
1 ^/ f! ?: S' @8 C7 E5 L. v // take no (simulated) time. The M(foo) means "The message1 e5 g! H- S! l& |, n) N7 C v
// called <foo>". You can send a message To a particular
2 B4 |0 ^ K& y! g! M // object, or ForEach object in a collection.1 `0 X3 |( A4 x* z! K
4 p7 Q/ d. f& M0 x% ]5 {* \ // Note we update the heatspace in two phases: first run
& k( J0 z+ [% K l+ c // diffusion, then run "updateWorld" to actually enact the" I4 Y/ l" S- ]
// changes the heatbugs have made. The ordering here is
0 \. ~- ?+ N( J1 c9 d/ V // significant!2 M% L8 q# m# L2 E% }, W
, R; x/ o) W! A7 b // Note also, that with the additional
4 A# P7 {* w' ?$ Y$ d% u // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 ~, O; W) x# T/ l // randomize the order in which the bugs actually run
- ]( l6 L( y9 K5 ^( Q8 {' `3 I+ ] // their step rule. This has the effect of removing any
5 z$ r+ O4 M% J // systematic bias in the iteration throught the heatbug
% z+ |4 r$ q: ~2 z/ e. n* P2 C // list from timestep to timestep
w" \! \+ q0 P0 X; U 2 h+ U/ o& j9 }8 X. {8 ]
// By default, all `createActionForEach' modelActions have2 C( [$ ?- d' k* x; J! @, Y0 h! ]
// a default order of `Sequential', which means that the
) g; @/ c5 [( a! K0 S // order of iteration through the `heatbugList' will be# ^, ]9 d& ]7 u
// identical (assuming the list order is not changed# m# _* i% e; J4 I: p. W! q7 W
// indirectly by some other process).8 b2 d/ T4 e+ t: _$ E
: T- C0 O7 w0 \. O5 }# i. n modelActions = new ActionGroupImpl (getZone ());; l) o2 r" I3 V# \6 V) Z
! n, w4 E4 P. _ u( |2 p try {0 S; U2 u5 r3 Z7 P1 C9 O
modelActions.createActionTo$message
' f& c$ P9 B0 H0 S E1 d7 v (heat, new Selector (heat.getClass (), "stepRule", false));' P8 R% g* v1 o4 ~0 ~8 @+ I
} catch (Exception e) {
( B+ M9 o! G$ c System.err.println ("Exception stepRule: " + e.getMessage ());
% v ^8 J8 c3 E! Q1 m }+ j: G, u' }" s1 l$ t7 R% `
2 I" J, p. u$ q/ e% z try {5 N: L* W& g9 K, G3 o
Heatbug proto = (Heatbug) heatbugList.get (0);
/ v: B, \* r z) ]1 K% Q# _ Selector sel =
" j; t$ O3 q1 n3 e. ] new Selector (proto.getClass (), "heatbugStep", false);9 I r! J' `' }: L$ d
actionForEach =* o i3 _0 ?3 `/ T) I7 k% x# b
modelActions.createFActionForEachHomogeneous$call
- I' y: Z" Z8 K: w' b9 Z5 M' J, \ (heatbugList," k4 m& r; S4 n0 a: \/ B U
new FCallImpl (this, proto, sel,
, {/ A. N. q) e) ^# V: o1 g new FArgumentsImpl (this, sel)));
) Z2 c4 z' o- O } catch (Exception e) {9 Q- _6 J! w: m% `% Y2 \' Q
e.printStackTrace (System.err);
. G$ d( l( G5 z D. F4 I% |6 O }" [8 l y' |* R. @# J& e! \7 {
9 ^. `: V: k" J# C: b syncUpdateOrder ();
% V4 x# y* S: {! F& O, j
9 c6 q; h/ X/ n6 _ try {
! C' G* A+ z9 [8 w1 ]( W, @+ t modelActions.createActionTo$message
" c5 V( e9 X8 e( D0 B! i& Y# q; x$ ? (heat, new Selector (heat.getClass (), "updateLattice", false));
1 D) j9 W/ [' H; l# x) g; X8 D } catch (Exception e) {) K6 I0 R0 e/ G' |. z6 y
System.err.println("Exception updateLattice: " + e.getMessage ());! b! ?2 U. x- c/ [
}2 @; L1 F" l2 ~# ^; @7 y
8 s# z# g K# o o+ M // Then we create a schedule that executes the2 B1 b" z1 ?9 h' w2 b0 ^4 w C$ s# u: }
// modelActions. modelActions is an ActionGroup, by itself it
2 ]6 P- v( S$ j! L8 D7 U' u // has no notion of time. In order to have it executed in
+ {% T) J! m% P% E // time, we create a Schedule that says to use the
+ d) V7 g. V0 L( h* v. U) O // modelActions ActionGroup at particular times. This
+ o1 I9 f% z" d% a- I& `% a // schedule has a repeat interval of 1, it will loop every
& E% Q2 k- s k3 E( f! l& O" M3 x // time step. The action is executed at time 0 relative to
: N1 K2 `# G R) L // the beginning of the loop.
( ]' W# A/ \7 _0 F: X" l/ K% `+ i; r$ n( `5 F- o
// This is a simple schedule, with only one action that is
3 h! |& O) b5 l9 P* @( |. Z // just repeated every time. See jmousetrap for more4 g2 s( T7 Z; {" O, ]5 I
// complicated schedules.
/ O% @3 D; }) C; y( _ # S9 @$ h! y* N4 ]) p. H) S5 O
modelSchedule = new ScheduleImpl (getZone (), 1);
. j. T( J' }9 ~; c; S9 q modelSchedule.at$createAction (0, modelActions);0 d: Z2 V3 e% @' {' C+ {8 C3 U, F$ o; j
. W% V# x2 v( G$ s return this;. a7 V' V+ n5 W) O
} |