HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 ?+ U2 h9 U) x( a
% _+ `( Q2 f! f$ m& y3 U4 k" O) b6 P
public Object buildActions () {
0 w! v- |" R! i* d _- N2 j super.buildActions();
6 [/ z+ x9 n* d! F
8 h3 o# v: w& K // Create the list of simulation actions. We put these in) B1 e1 B G4 H7 A8 M$ r
// an action group, because we want these actions to be
; q; x9 l4 L' S7 ^ // executed in a specific order, but these steps should
/ ?5 P) p# S; C$ w8 l // take no (simulated) time. The M(foo) means "The message
5 ^7 T9 s( K0 j // called <foo>". You can send a message To a particular F: Y( c0 J, M3 h7 n, y9 ?
// object, or ForEach object in a collection.
$ @9 E& f- r* u
7 z& D c; j. h8 V7 ` // Note we update the heatspace in two phases: first run
4 H) B* J4 {5 Q! w: n% d5 Q3 g3 C // diffusion, then run "updateWorld" to actually enact the: B2 j; |+ z. U' s9 O' h5 A- k
// changes the heatbugs have made. The ordering here is! J5 u: p% u- m& _8 b0 I1 x
// significant!
4 S! f8 q; [; {) w% O
, {& }9 V( z6 y // Note also, that with the additional
+ f; J1 s+ \& D; U( Y5 Q // `randomizeHeatbugUpdateOrder' Boolean flag we can
# n$ j; `: p$ e k, i A n // randomize the order in which the bugs actually run
- U4 q$ f/ F2 i6 R // their step rule. This has the effect of removing any
. F" }6 q: q- ^ // systematic bias in the iteration throught the heatbug
5 w) w _) E* a // list from timestep to timestep
* r. J8 L; k' `+ r6 U a' l' R+ H* @" W; B0 @
// By default, all `createActionForEach' modelActions have
: i$ \. V! m: `, W2 m0 D, E a // a default order of `Sequential', which means that the
7 J. K. R2 E H$ Q M0 U. w8 v // order of iteration through the `heatbugList' will be
. [3 w+ p5 i- P% } // identical (assuming the list order is not changed9 w4 u+ o F$ Q# c8 ]$ w2 _- |
// indirectly by some other process).
/ C* \- ~6 p4 y+ T9 n4 s 9 s! U c& t9 l; n+ N' U6 v
modelActions = new ActionGroupImpl (getZone ());
2 f$ F2 t" ^& L
w7 V S: |' W# x* X1 ~ try {# E( Q* y8 H* k" E
modelActions.createActionTo$message# j; U9 U4 k4 ^
(heat, new Selector (heat.getClass (), "stepRule", false));; Z8 _& a2 f% P( Z# j; L
} catch (Exception e) {
$ @$ D" V! q' B0 I System.err.println ("Exception stepRule: " + e.getMessage ());; v- U. g C* L9 _) J0 t' F) H0 a
}3 e: _' o O/ [2 x$ o& n
- h" ~/ I7 {! \+ h try {
% l- l; B% H3 ?: N4 T% T Heatbug proto = (Heatbug) heatbugList.get (0);* |. H3 ~% @" `1 f0 w
Selector sel =
$ q# I& W I5 Y( [2 e% ^ new Selector (proto.getClass (), "heatbugStep", false);
5 L& H1 x' j! O$ T actionForEach =: X1 R `( Q% h3 b4 [4 V! J$ Q' x
modelActions.createFActionForEachHomogeneous$call7 i$ `- R) U1 u9 g# q
(heatbugList,
/ j* f( e( N2 b2 W new FCallImpl (this, proto, sel,: z6 j4 a7 j2 p; k& S) c
new FArgumentsImpl (this, sel)));
. b! Z* [0 f* \# }$ T1 m } catch (Exception e) {
: p: ?9 _) ]: j" ~' m: H e.printStackTrace (System.err);' G. I7 P5 k3 x1 {: F2 c
}5 @9 p0 u5 @5 E* |% e/ z6 ]; O) Z) i
& S( S8 o/ E' `
syncUpdateOrder ();
. t$ U5 @; E# Z* ~6 Z
, p9 c; k& b4 {* b! ? try {
3 B+ ?% J, l- m3 \ modelActions.createActionTo$message
' C3 f" n! i4 e% K p/ `# q (heat, new Selector (heat.getClass (), "updateLattice", false));7 W; }# w# N* n
} catch (Exception e) {; d( D+ g( B. k( E- m
System.err.println("Exception updateLattice: " + e.getMessage ());9 k" }* T! P: J2 N5 |6 v* H; b
}
3 t5 W: c& G/ \* \6 C8 l
6 o: T# _& s, W# Z) y/ ~ // Then we create a schedule that executes the
( [2 d7 n% @: l, e) j6 U // modelActions. modelActions is an ActionGroup, by itself it! M8 C) M9 v6 Y/ D+ a. U
// has no notion of time. In order to have it executed in
7 X* M, u/ H( q // time, we create a Schedule that says to use the
~* }* f; V' P0 Q. y4 `) H( _ // modelActions ActionGroup at particular times. This
3 b" q7 f9 G; f, X1 ? // schedule has a repeat interval of 1, it will loop every
3 }9 K5 m+ w# z. @4 _' h // time step. The action is executed at time 0 relative to2 e9 z6 D- @9 G6 K) a5 A1 H! C8 w
// the beginning of the loop.
% L; I$ P- l, t& k
, z% F9 y% ]- W" X `& A, P // This is a simple schedule, with only one action that is) l0 H: L% @1 L; y2 q
// just repeated every time. See jmousetrap for more. e* q- g0 O$ j+ Y! ]! Y: P: D
// complicated schedules.
- i- n2 h5 b; S4 H
2 U/ @5 }" d2 V* o0 D modelSchedule = new ScheduleImpl (getZone (), 1);" o2 k4 s% @+ R3 C1 ]! e5 I
modelSchedule.at$createAction (0, modelActions);
" e4 `+ ]3 J1 e* ]. [: K5 [ `4 X/ _0 t' _8 F
return this;
: F, @; ?4 C: f8 C) n/ e, |/ Z } |