HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# n" P2 @+ I& J% q* h
/ a$ b6 p; x- ?( ]/ [" V1 H public Object buildActions () {: d& @: m* U6 i
super.buildActions();
$ T/ z6 @" K1 m, ?7 m$ w' \
( I2 g# _" i* n. Q+ D2 _ // Create the list of simulation actions. We put these in
1 V2 r3 U V) k, l6 y: ` // an action group, because we want these actions to be. @# F; T# c/ [7 A3 K
// executed in a specific order, but these steps should
/ d" m; H: P7 h0 m2 c. S( \7 { // take no (simulated) time. The M(foo) means "The message
" B+ r8 i$ X& j( K // called <foo>". You can send a message To a particular/ t8 S; g. ~, t# U3 ]- s% T
// object, or ForEach object in a collection.& s' f, t' H! K" O% s) c# F
. k8 |1 K1 b8 j p
// Note we update the heatspace in two phases: first run5 X7 @; N% b$ U& H. [
// diffusion, then run "updateWorld" to actually enact the/ X1 K J9 z) W3 E& \* ^! r% v
// changes the heatbugs have made. The ordering here is
: `) j* e; P1 d! Q8 D, Y5 d! k // significant!: ]/ m$ X* M1 `" t" B1 `* t' _
( F/ y+ ?9 x- {8 i/ F
// Note also, that with the additional8 v1 U$ _2 f1 N* s a" W
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. X; C* _7 G) G- V& `0 d Y // randomize the order in which the bugs actually run8 d6 {1 b& R4 ~0 r
// their step rule. This has the effect of removing any H" f& G2 h4 F0 u/ h6 ?
// systematic bias in the iteration throught the heatbug1 | I6 c& W, p6 S
// list from timestep to timestep
$ S/ Z5 \) d4 Z B1 q. w- O! |/ }
: r: a# B! }9 T // By default, all `createActionForEach' modelActions have
% q5 t- n6 G( _9 \% i // a default order of `Sequential', which means that the- A& n/ R) ]3 P) N/ z5 a
// order of iteration through the `heatbugList' will be
5 Q9 r9 C* ?6 F4 G% d" v // identical (assuming the list order is not changed
6 U7 H" Y4 R- l0 D, [. |- M) Y // indirectly by some other process).6 A8 e/ w- q& e% I7 i/ N" [: @: V
' O5 l: B% e/ A) P# Y
modelActions = new ActionGroupImpl (getZone ());: e$ ]( _: P0 q+ l* p! X" A
! Q F+ \0 @* t5 {2 x' m! `
try {
; ?7 T9 ?% B( o9 g# V modelActions.createActionTo$message6 _5 b- ?, z7 _$ t
(heat, new Selector (heat.getClass (), "stepRule", false));
/ J6 \& Q& V7 q# r2 \/ B } catch (Exception e) {
+ ^9 d4 d9 I* y/ w System.err.println ("Exception stepRule: " + e.getMessage ());
0 k6 o& U: B5 j: v( K) @) i }' z( J# ]6 K0 q, F* j) G
. y% a: k! b0 u+ \& }+ n try {
r2 P _ `) a5 G* m4 [ Heatbug proto = (Heatbug) heatbugList.get (0);
' O2 |' `7 l1 Y Selector sel = / [5 \" {+ v( W1 M
new Selector (proto.getClass (), "heatbugStep", false);' U' l1 l7 w/ }- I/ k' k/ v+ t
actionForEach =$ f! n4 Y3 Y _
modelActions.createFActionForEachHomogeneous$call" Q* y) d8 j/ M; n; }1 g; Z
(heatbugList,' A- c0 S( A9 D" O: i9 Z+ F
new FCallImpl (this, proto, sel," ]8 ?- P0 k; @( C+ r) s+ @
new FArgumentsImpl (this, sel)));
% e+ B9 h' y" l% X6 l- E } catch (Exception e) {
9 f* v$ d/ H( {* w+ _ e.printStackTrace (System.err);; P5 e- {% f' [# I
}
' b4 A; f) Z: |/ x2 P3 O+ f U
) g6 Q. m2 H8 d, p syncUpdateOrder ();2 z) P/ P/ p2 k- L! T7 Z4 G7 ^- K2 F
) \* t v) R+ T+ [; E. N6 D try {$ I( j, N; I0 M' `$ v( ]( U. _& Y4 @ y
modelActions.createActionTo$message ( M* p- _, H( k8 S# X: l0 U
(heat, new Selector (heat.getClass (), "updateLattice", false));
- d/ E" o% @3 o3 t$ X8 g3 C } catch (Exception e) {
5 u$ `+ n4 |8 Q# Y& F0 v$ M System.err.println("Exception updateLattice: " + e.getMessage ());0 o u/ L6 g6 B8 W! w
}
; C- x( n( K, ^) _, h
3 e! b! K |1 N // Then we create a schedule that executes the
& S" Z; F3 B1 Y7 Q8 G // modelActions. modelActions is an ActionGroup, by itself it; O" _9 v1 F7 {* l1 s
// has no notion of time. In order to have it executed in
& d) f- Y1 w v+ j o( u/ \& o // time, we create a Schedule that says to use the
2 W! @6 I0 c4 U! x2 s: J- H // modelActions ActionGroup at particular times. This0 X1 q7 X* F4 c; Z& l
// schedule has a repeat interval of 1, it will loop every6 b, s- p( A; y* t7 M2 K5 ?! _2 B
// time step. The action is executed at time 0 relative to L5 ?6 L- ~% \
// the beginning of the loop.' h; `3 A' Z% t6 s' E. m* k1 R- }
$ H) i* l5 I' |& v3 A
// This is a simple schedule, with only one action that is& @3 ]- y6 Z+ J! E/ d$ V% J
// just repeated every time. See jmousetrap for more+ T0 x0 B7 Q/ ~' m0 o
// complicated schedules.
( J {% V2 S2 g6 i |7 ^ 4 ~0 d& T) S$ A8 k' ^8 R$ V
modelSchedule = new ScheduleImpl (getZone (), 1);. ?+ |: |( _) W
modelSchedule.at$createAction (0, modelActions);% r+ V N3 ^5 X3 v
% o( K: H% a: @# `4 H$ b' d return this;
: B; K: M# U [' W; @ u8 R, a } |