HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 w6 W: z! F" a8 r8 O4 `1 V6 ]# O
6 N) s* L: {2 U! U( z public Object buildActions () {
0 P2 z0 s; {2 J1 w# y super.buildActions();
" T3 Q. b/ b! I9 ?
- m$ o1 u5 y* O+ p$ m1 Y C3 ]0 ]7 Q // Create the list of simulation actions. We put these in' G3 I1 P" V5 @! y; }1 C
// an action group, because we want these actions to be
3 _$ J( a8 i. ` a // executed in a specific order, but these steps should
7 b* h5 b( J; A) R5 m3 U8 R // take no (simulated) time. The M(foo) means "The message
2 H8 o' O$ \: C2 z4 _7 u/ b // called <foo>". You can send a message To a particular
1 d/ O* S0 J9 S& J. ]4 @ // object, or ForEach object in a collection.
( Q$ l% ], T- ~9 V3 T3 S ) Z, v* r% n( ?/ J: i3 J1 S6 F0 S
// Note we update the heatspace in two phases: first run" t3 Q2 K. `: Y4 i6 @* j( f8 ~8 v
// diffusion, then run "updateWorld" to actually enact the
# q7 q! [3 b2 W5 \" y2 A' |* Q8 N // changes the heatbugs have made. The ordering here is
5 }8 X2 T) D5 F9 D9 @' _/ R // significant!
, R' E9 [+ F7 K
" r- F X. r! w2 ]9 t. ~$ m // Note also, that with the additional2 g _+ g9 a3 e8 t1 o$ f$ s
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 B. O+ s, j& h
// randomize the order in which the bugs actually run
/ o, m, E- r- @ // their step rule. This has the effect of removing any5 v% l* j0 g* I
// systematic bias in the iteration throught the heatbug" \: o+ F6 R( A# K& Y
// list from timestep to timestep
* Y0 S, S* t- N, Y1 B! s7 u1 \ ( [- U1 p; }" {" ]
// By default, all `createActionForEach' modelActions have
' v4 ]" s8 c, c1 p // a default order of `Sequential', which means that the( H5 ^3 K: R/ z9 ^+ M' g
// order of iteration through the `heatbugList' will be2 m1 G+ z2 G5 @/ b7 S& r b
// identical (assuming the list order is not changed9 ^6 d* w4 P. P
// indirectly by some other process).0 d8 ^. `9 K3 ~4 B; [& r3 e
" a& X$ s$ i1 _6 r7 ?+ I modelActions = new ActionGroupImpl (getZone ());9 j: e+ o' E9 L, j8 g$ |" \6 B
4 |2 K8 o5 N1 G
try {
/ r3 D- v, W$ ^( d7 x4 D modelActions.createActionTo$message) l7 o* }" j; O g* E
(heat, new Selector (heat.getClass (), "stepRule", false));
4 Q1 V4 d/ i R) Z } catch (Exception e) {
+ h- f. g( X' Q) T4 u# A2 ~ System.err.println ("Exception stepRule: " + e.getMessage ());$ C1 x$ F& j1 U/ S& |) K, \
}
I1 `$ u% M, s$ }/ q0 a( U4 u) Q" ? A3 B1 z4 A
try {5 m, M* E, B- |/ y/ x: ?; R0 h! a; D
Heatbug proto = (Heatbug) heatbugList.get (0);
; g4 C9 w) a1 z$ i- K8 O Selector sel =
) r: Y2 y7 n0 J/ D+ H3 Y/ V new Selector (proto.getClass (), "heatbugStep", false);+ I( F2 \: w3 `( x
actionForEach =6 \, t. F9 {$ I) y3 ?$ R* Q: P$ A
modelActions.createFActionForEachHomogeneous$call
3 P: Z8 q, u; I: Y (heatbugList,
! B9 w* F! z7 z: o5 w$ V# d T0 [ X- @ new FCallImpl (this, proto, sel,
! I( G- q( S5 ?" U3 [8 f4 | new FArgumentsImpl (this, sel)));
$ I. q4 N( L- \# ^ } catch (Exception e) {
- M& ^' G, G5 Z0 g( u2 x e.printStackTrace (System.err);8 n, j2 V1 \7 S% ^
}
' |0 v: ~! b4 A
& H8 g4 T4 A2 V& D syncUpdateOrder ();. S4 Q8 ^1 Z+ `& p9 c, V
& U: X4 ?% d- M7 k, V3 q/ o try {% M- F3 f9 K1 W
modelActions.createActionTo$message " Z. G5 {. b k/ m. I( ?# e
(heat, new Selector (heat.getClass (), "updateLattice", false));! z3 D$ m6 X+ h3 _- [
} catch (Exception e) {) ^9 [6 Z% G5 \* y
System.err.println("Exception updateLattice: " + e.getMessage ());
1 A6 z' s- ?, J. W% f) X: _' q4 j4 u }. Y; j: r5 o0 \% B0 ?; L+ f
u) U( h. S* H; J
// Then we create a schedule that executes the* l" r! L% Y* W/ q+ X' a' |
// modelActions. modelActions is an ActionGroup, by itself it
- [* t- y) a5 P8 \ // has no notion of time. In order to have it executed in
r5 C Y$ F" g: f' W // time, we create a Schedule that says to use the
; }1 [# T2 g& J1 Z3 Y! R // modelActions ActionGroup at particular times. This2 d0 f/ k& Y: R8 s5 a7 `1 B1 F5 D$ u+ M
// schedule has a repeat interval of 1, it will loop every
2 v6 j3 ^, n5 W5 a& F; d* m // time step. The action is executed at time 0 relative to! z* u0 y, M! R
// the beginning of the loop.
) N1 m$ T) x" P, c& a2 k, c: Q- c) X3 B) W- d
// This is a simple schedule, with only one action that is5 {, y! ^" |4 {& e2 L
// just repeated every time. See jmousetrap for more A( ]3 w$ E0 F/ k# z
// complicated schedules.
6 @( k) g: w6 h9 o% n 6 t! V7 r- k. H0 B
modelSchedule = new ScheduleImpl (getZone (), 1);; c; d, ]- E) l8 {
modelSchedule.at$createAction (0, modelActions);
/ k0 q( W3 a0 p& S " v% Z; s6 j" X& h: C8 f
return this;
& q4 c9 d/ K+ C ^/ R0 h' l" m } |