HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% v3 A+ Z! ~3 z3 B
3 ^. y9 C \/ d3 \- L% d$ { public Object buildActions () {
; s. s( s! P: I% t ?6 t4 A super.buildActions();% H& Z" z* }5 C, {( L' \3 f
& ]( ^2 U) e% k9 I- n( k- o7 l& n9 @ // Create the list of simulation actions. We put these in
$ c, o3 L& Y1 ^0 D- K4 q // an action group, because we want these actions to be d, m5 U; y' l1 [% \2 B
// executed in a specific order, but these steps should9 g U1 }2 d3 H6 l! T5 z' I# n
// take no (simulated) time. The M(foo) means "The message0 ^, D9 s9 t/ X! _* o/ O
// called <foo>". You can send a message To a particular
2 K F2 c1 J4 I- [% D5 N // object, or ForEach object in a collection.1 J/ T x3 q D) | P- o7 P
) W( W0 V3 i d; r+ \ // Note we update the heatspace in two phases: first run# h: A0 |# |8 l- H: I4 _; u! g
// diffusion, then run "updateWorld" to actually enact the* t' Y; }: a5 n" _+ ]
// changes the heatbugs have made. The ordering here is6 t6 c" b8 p- ]- p
// significant!
6 Z. ?' h0 U! j0 Y1 e+ z$ u
' f5 [' J- o+ Q# j. @* c // Note also, that with the additional( _4 e) g5 H, c8 J7 n
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# M+ l& M% ~$ a& Y4 v7 \ // randomize the order in which the bugs actually run
& `4 j8 f& y1 q C A2 ~ // their step rule. This has the effect of removing any: i3 ] U$ S! S
// systematic bias in the iteration throught the heatbug
- {+ E6 a( ?8 j // list from timestep to timestep& ?& x* \+ i9 t5 F
x3 [* b+ D, R/ P // By default, all `createActionForEach' modelActions have! Y" ]3 ? Y* M( ]
// a default order of `Sequential', which means that the4 H3 t- q# j1 b/ W/ G
// order of iteration through the `heatbugList' will be! c, z9 T0 N. m- N$ P# k! {) n
// identical (assuming the list order is not changed
! X1 l% l* a% q4 O5 V // indirectly by some other process).
" D* u* z" c& q- g
' i, o' R; L( ~ modelActions = new ActionGroupImpl (getZone ());
. a2 N$ i: b4 h6 z3 @1 F+ n' {
6 H3 y5 y6 i# M! l, D try {: G! }) r9 c4 Y' S
modelActions.createActionTo$message
: R. C+ i6 o6 s (heat, new Selector (heat.getClass (), "stepRule", false));
3 j! q: X% g7 G+ d } catch (Exception e) {
* j9 q0 [: Z# m System.err.println ("Exception stepRule: " + e.getMessage ());
( {* ^" O' ]! l" I6 t" f: d9 n }0 w6 Z8 R8 Z3 F* V8 Y
6 O! n; K" P- @/ O/ s+ ] try {
1 r8 P1 K3 @- G2 H Heatbug proto = (Heatbug) heatbugList.get (0);
) J4 |& W) |" ^% m) r" v, p1 I Selector sel = % B9 r1 S& Y" J; i! [0 b
new Selector (proto.getClass (), "heatbugStep", false);- ?, t( e$ E( n: [
actionForEach =$ ~7 C2 ^8 a$ e9 V
modelActions.createFActionForEachHomogeneous$call
8 }3 k6 _ g/ ~4 E7 u (heatbugList,
+ f, s, o" m5 O- _5 j. ] new FCallImpl (this, proto, sel,1 V% W) V1 U+ V% m& @
new FArgumentsImpl (this, sel)));7 V0 ^" y. m% u9 C0 q' ?6 X& e6 F
} catch (Exception e) {( Z. [) R! j. g5 z: ~* @8 I* |
e.printStackTrace (System.err);
* n. U d" v: |& k }$ A8 c/ K* c, t h) s
, J- l; m$ e$ P" q& x9 E syncUpdateOrder ();
3 r/ Z4 L; J+ U# Z% g
: H' \# l6 U2 L: O try {6 T j( x0 `: H/ S
modelActions.createActionTo$message # t6 D W X6 ^* y$ o! R6 S
(heat, new Selector (heat.getClass (), "updateLattice", false));. |: ^- D0 N* o( h5 m' O( ?- {
} catch (Exception e) {4 t5 H8 P' Q5 w b, }& Y
System.err.println("Exception updateLattice: " + e.getMessage ());
+ W+ O4 m' ]) t0 ~. D- K6 k }
' v$ \' r: l0 E; f& _, A 2 i! b; r& L# c9 j
// Then we create a schedule that executes the6 ?6 V, X; y% N/ C
// modelActions. modelActions is an ActionGroup, by itself it% H5 n0 F6 H' f! K
// has no notion of time. In order to have it executed in0 m0 v8 O2 ]/ y) G- I' z% g0 }
// time, we create a Schedule that says to use the" B8 ^/ R- k: z" S: G. W2 U* q3 d( [
// modelActions ActionGroup at particular times. This
0 ?" c; h( ~# q6 Y9 ~9 z // schedule has a repeat interval of 1, it will loop every
; @* q- X" l& ^5 R // time step. The action is executed at time 0 relative to
4 y6 @& ^0 p6 g9 G9 P1 `0 F* P // the beginning of the loop.3 v/ \( J; d8 w3 }3 M
: q& W% B- L& u# \5 K
// This is a simple schedule, with only one action that is; ]! L" ]3 \2 `) X0 w! l. d" i
// just repeated every time. See jmousetrap for more
9 K; z/ Z- |. K // complicated schedules.9 A% ]; U. B( S3 N- w, R- O0 `* O
. Z) Y" @) A. h. E# Q
modelSchedule = new ScheduleImpl (getZone (), 1);! v1 A- o2 G" v
modelSchedule.at$createAction (0, modelActions);
6 i$ _: x7 C/ ]/ L9 M
: A- e$ w1 @, w. Q6 d4 B# J& @6 e9 ] return this;
8 t" B2 }' w J1 E% a% r } |