HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, J$ c! B# U) @
& O& t, M9 G( ]4 { public Object buildActions () {! o$ w0 @( s. Y. \/ t5 R- R
super.buildActions();
m* L$ [9 b/ J0 S4 g; v6 K& M# L
+ u W0 S0 h3 p9 f // Create the list of simulation actions. We put these in
6 }9 o4 g9 U( o/ m5 } Q; \+ \ // an action group, because we want these actions to be0 y! G7 K$ O3 [
// executed in a specific order, but these steps should* I5 Y) n0 n% ~8 j, ]
// take no (simulated) time. The M(foo) means "The message4 \8 { n, {% d
// called <foo>". You can send a message To a particular, r9 t' U5 j; e" Q k) N! H
// object, or ForEach object in a collection.
# ]% V+ Y* i5 P- e4 ~* n$ {
+ x8 N' G6 F; m2 K2 a // Note we update the heatspace in two phases: first run
$ u, t% u: J/ W. b // diffusion, then run "updateWorld" to actually enact the
- Y0 Y+ N* k7 O3 \1 S // changes the heatbugs have made. The ordering here is
( m w" H/ z6 N // significant!- B( {, y6 W" |; E
2 v8 |" [" X- Z T+ T9 N% U; _& _ // Note also, that with the additional
: U, W+ x1 c0 E+ S. m // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ K$ e% X% w! u' o- b // randomize the order in which the bugs actually run- [# ^& A. E o# V" c3 f
// their step rule. This has the effect of removing any
( |$ N1 e' \$ Z8 l* W+ Q6 `3 V7 [- L // systematic bias in the iteration throught the heatbug
' y5 @4 q6 P, T" X% E // list from timestep to timestep
0 @# j6 P( ~+ ~8 L - J) f! O1 z& _1 E% H: L
// By default, all `createActionForEach' modelActions have
( ?/ ]/ S. |! ] J // a default order of `Sequential', which means that the: _% I8 f2 j) s) n9 h* Z! _2 C
// order of iteration through the `heatbugList' will be% E4 w' _3 W+ M% i W
// identical (assuming the list order is not changed3 M1 b. V8 c( F3 ^
// indirectly by some other process).
* S1 ?! Y% S! `$ J- @
4 p+ F2 P3 J. |" m0 t. F modelActions = new ActionGroupImpl (getZone ());
; M9 z+ G4 d/ ], r" H2 V& V* I( S( v1 u3 S) b h
try {; ~4 r- w5 y7 [ t6 d, c
modelActions.createActionTo$message2 U$ Y' t1 }3 v, [
(heat, new Selector (heat.getClass (), "stepRule", false));) Y# E& m' y* ?' N E( n
} catch (Exception e) {0 `+ w/ @1 y f% l' e8 A3 z
System.err.println ("Exception stepRule: " + e.getMessage ());( k& I! W, ]8 C; |- @9 O" p2 L* O
}
}0 {. B0 K1 j3 ]# h3 G! T# [9 n5 w* Y4 t! h
try {$ x3 A& t5 h: i- Y" R1 o4 F0 k
Heatbug proto = (Heatbug) heatbugList.get (0);& L/ z2 M. N' U& ? O$ k5 V6 H
Selector sel = 8 Y+ t. I4 o- F# v* \ [3 Y* c
new Selector (proto.getClass (), "heatbugStep", false);
% O* O, P5 o# F' F actionForEach =
$ r3 v# r7 ~' `! j& C0 D S4 e# U" b modelActions.createFActionForEachHomogeneous$call
' g5 t2 f2 l1 o9 y9 x: m, G (heatbugList,9 ^6 U% [7 O0 A4 ^5 N) r3 y
new FCallImpl (this, proto, sel,
4 N& O2 X Y' I0 e! ^4 V7 D new FArgumentsImpl (this, sel)));% C& Z/ i6 P5 j
} catch (Exception e) {4 c( |) @. ?) y7 s. q( g
e.printStackTrace (System.err);6 c) q4 b, @' f2 n/ x
}, `: V9 p$ b. F
& ?& h2 |0 ?) b syncUpdateOrder ();
6 _ I2 v7 J1 ?6 t
5 p6 H6 E& c4 ]/ J3 b6 c* d try {) F3 h8 A2 W4 \& Q8 s6 o% C
modelActions.createActionTo$message
" V) S R; j1 W; [ (heat, new Selector (heat.getClass (), "updateLattice", false));
# @1 C u! {5 n- }# J( z } catch (Exception e) {2 }! l% P/ S# F- B H
System.err.println("Exception updateLattice: " + e.getMessage ());
3 X) W, \0 s+ v2 q }: e* Z. ^& a2 x, X& g
8 J7 X5 o# P9 Y5 P- E0 ? // Then we create a schedule that executes the
: w5 ^) @1 v+ b2 \. R2 ~ // modelActions. modelActions is an ActionGroup, by itself it
. j9 v0 l& U0 `( B' O // has no notion of time. In order to have it executed in6 k' b/ Y8 O! N6 T7 `6 H
// time, we create a Schedule that says to use the
* K9 e" E ?& h5 @* z% B1 [ // modelActions ActionGroup at particular times. This
$ ]9 O. a( J, V8 I/ B) }* E7 w // schedule has a repeat interval of 1, it will loop every& Z6 D. J6 |; F6 F" I* T2 q
// time step. The action is executed at time 0 relative to& w6 t; j/ u I$ [9 Z6 X
// the beginning of the loop." |8 U) T5 w% q5 { p
?6 {, l; v7 ?& p8 D! e) | // This is a simple schedule, with only one action that is; ^- |/ x5 g7 y# e) I9 T
// just repeated every time. See jmousetrap for more# |. T6 @/ L5 V4 k
// complicated schedules.
8 U, G% x8 d* [( p
3 T: U3 M- `9 u$ G modelSchedule = new ScheduleImpl (getZone (), 1);
7 e0 ~; L5 q) X: c' c$ S2 s4 R modelSchedule.at$createAction (0, modelActions);% K- \+ ~& @, r* G- z
- ~& O, A" t/ f$ |' K t return this;
6 f8 Q# `" R. b) U8 R1 z } |