HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- G n. }% h; g# F# ]
+ ~( A7 K; k5 l0 B% _$ q public Object buildActions () {/ f. t2 u2 [/ \
super.buildActions();
0 L0 \7 i. C/ ^5 K; @ 4 @, ^# O8 b# ]1 v7 l
// Create the list of simulation actions. We put these in
& |4 W% E' s6 b5 h- n4 ?7 [ // an action group, because we want these actions to be
% r, C: l: i/ n5 n! V // executed in a specific order, but these steps should; {" Y) h" ?; S) y
// take no (simulated) time. The M(foo) means "The message, o4 e2 @( M+ ?6 w) v* R. s# x7 _
// called <foo>". You can send a message To a particular
# H. r1 I6 u8 [2 _$ s7 f" k( i9 q // object, or ForEach object in a collection.
4 |: d8 Q' K4 X
6 ?1 y* ^' i9 e' z* y! \* K" N // Note we update the heatspace in two phases: first run
) {7 O: R/ o7 @' @0 Q // diffusion, then run "updateWorld" to actually enact the
& d! N! j% }! ?/ K' E$ b8 | // changes the heatbugs have made. The ordering here is0 [* r% |6 Q; F1 ^& C* o
// significant!
9 X/ q5 o. B( A4 X' q" V [$ {
8 L3 A7 m* n4 H6 c8 t9 { // Note also, that with the additional
3 e$ h, p" S4 w$ [4 w+ s // `randomizeHeatbugUpdateOrder' Boolean flag we can* x6 h7 e# E4 |4 j, J6 n6 o. a
// randomize the order in which the bugs actually run9 Y! f$ c8 H$ d" ~
// their step rule. This has the effect of removing any
i' b" p& d, _6 Q' X% g7 `2 q% M. v U // systematic bias in the iteration throught the heatbug8 i" `6 r, D9 D* E! a, \
// list from timestep to timestep
7 C; A" k2 p7 S6 n$ I
. y, b6 {. [ T# G9 V // By default, all `createActionForEach' modelActions have
; `1 r9 ^: O( i1 a9 X1 @6 | // a default order of `Sequential', which means that the2 Y/ [3 k$ l" ? ^' u
// order of iteration through the `heatbugList' will be
4 B- Z! O& b) v. H4 F# v6 n, b // identical (assuming the list order is not changed; Q: |0 ~- x' r! n" o/ H& K9 _% Q
// indirectly by some other process)., O. u! J* ?; P. N0 j* {( J
, O b3 }" K, v2 a modelActions = new ActionGroupImpl (getZone ());
" m- W0 U) C5 P9 X* l9 s$ b. G* J" s q! B
try {+ ~1 P1 L) E- r( @4 ~
modelActions.createActionTo$message
2 |. |$ w0 K" d% d- \# c* \ (heat, new Selector (heat.getClass (), "stepRule", false));
4 v! G2 F* r% i2 [: ^: s: g } catch (Exception e) {' C- v: a4 D" j. D- K* u$ i
System.err.println ("Exception stepRule: " + e.getMessage ());
( U* \( a6 p+ K& a4 }8 X }% A6 l/ u6 A( U4 Z* X
% x! R$ b3 i) a/ ~0 Z try {
5 e/ A9 e- q. T Heatbug proto = (Heatbug) heatbugList.get (0);- Q) D3 a" m; C: H! n$ x7 Q& M' T
Selector sel = - A/ N7 I% J6 B" J6 A
new Selector (proto.getClass (), "heatbugStep", false);- x9 ^4 X4 W# _7 f# S
actionForEach =3 [" g. G0 G, B9 x* _( r
modelActions.createFActionForEachHomogeneous$call
5 t. ~( k( K6 d0 }" p( R (heatbugList,0 d5 d% p+ Z$ m7 G/ o
new FCallImpl (this, proto, sel,
' p/ R2 A' C! X6 h new FArgumentsImpl (this, sel)));6 A1 I8 n4 }9 ~* |8 B
} catch (Exception e) {
u1 B7 a/ t5 U/ O6 u S: H$ i9 C e.printStackTrace (System.err);0 Z+ c' y& S% J9 S( [
}
3 Z5 |3 `0 C9 \: x2 M4 c 1 h% ~" C% h. C- H6 v
syncUpdateOrder ();
0 [ p& B+ @! X* Y @' A
' Z/ Z2 \1 o9 A! Q& O- @ try {
- L4 W0 D6 A- Q5 w( y9 r modelActions.createActionTo$message ; i( N K* t# |; i% j& t
(heat, new Selector (heat.getClass (), "updateLattice", false));# ?, W4 o l5 ~% F. q- Q* m
} catch (Exception e) {! v; j! J$ T7 G' m& f' }
System.err.println("Exception updateLattice: " + e.getMessage ());0 ], l6 S& G3 L1 f0 Q# b* \
} ]+ t# k% ?9 x7 b8 h, s
. ?' ^5 {. N$ w" a* o1 x
// Then we create a schedule that executes the
6 C8 a$ h% a$ B0 g* b4 { // modelActions. modelActions is an ActionGroup, by itself it
" o7 J- d1 r2 q5 v, K // has no notion of time. In order to have it executed in
, r7 M0 J9 g+ ~$ \7 R8 } // time, we create a Schedule that says to use the
$ N$ l* ]) Q; m7 Z" M8 T! I* D // modelActions ActionGroup at particular times. This
P- }6 j; L j. Y$ @ // schedule has a repeat interval of 1, it will loop every8 E' X7 t' w1 q% m
// time step. The action is executed at time 0 relative to8 b8 o+ ]0 p4 k" |% n
// the beginning of the loop.
! q+ q* `7 z- G" f/ v6 |$ r- ]% G! ?2 q' x) ~
// This is a simple schedule, with only one action that is4 t1 f2 ]0 v6 T, V( \
// just repeated every time. See jmousetrap for more. J1 V3 R, K9 `! A: \
// complicated schedules.* j4 k5 W. I! f9 o) J
( |/ Y3 Z( c+ c. M modelSchedule = new ScheduleImpl (getZone (), 1);
6 b! G6 U) ^' L& x4 a# _ modelSchedule.at$createAction (0, modelActions);
7 ?0 x& p* E" ~* A0 W+ \3 a 4 P- [( n- R, R; @3 [! t6 K! r
return this;
7 w: x5 I* _: P4 l3 N4 \8 q } |