HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 \ U: x" J z* h
; k+ s9 |' y E public Object buildActions () {
% e% [+ S3 v, V5 ]2 a" ~* s4 r super.buildActions();- v* ]6 R/ e" V6 X$ L( w
9 _, |! j, v( i2 J7 V* |) A
// Create the list of simulation actions. We put these in+ ~" @( t. _6 c
// an action group, because we want these actions to be/ x* K3 Y7 Z8 G9 D+ E
// executed in a specific order, but these steps should
- Y7 \$ E7 m/ ~7 R // take no (simulated) time. The M(foo) means "The message4 d; d* p. |% k1 M% _
// called <foo>". You can send a message To a particular
, K: i1 K |$ P0 ]4 @( \9 V // object, or ForEach object in a collection.! I( O+ k, X5 u) I/ U J
+ r, [3 A" V) D // Note we update the heatspace in two phases: first run
# C' X2 l/ t! p' U // diffusion, then run "updateWorld" to actually enact the; g( }& ] q- Y5 ^7 V$ ?% }7 k
// changes the heatbugs have made. The ordering here is
4 n+ f" l! Y/ F // significant!
6 r" s9 ?8 F0 W
A9 A. n, h# Y* N; k1 y // Note also, that with the additional) `) H7 u( m' P. Q( T) G
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 C! B5 @& h0 l4 d* @. Q8 T
// randomize the order in which the bugs actually run
7 ?% A2 H' {+ C* c& c/ X; i // their step rule. This has the effect of removing any
; g8 U5 E+ S3 Z) U0 m# Y4 e5 | // systematic bias in the iteration throught the heatbug
: `1 e. L# v$ ] // list from timestep to timestep
% l0 d; }1 @0 S2 u! \& u
( T5 E" c1 y i0 @ // By default, all `createActionForEach' modelActions have2 ^3 h9 o, A: _3 S2 P4 T
// a default order of `Sequential', which means that the) _0 c5 [& `! Q! d
// order of iteration through the `heatbugList' will be
# g" B1 ^( R. U0 O/ ~( S0 p // identical (assuming the list order is not changed
' c5 Y x) O& n' o* V3 u // indirectly by some other process)., \0 E8 E) N5 x i( Y
$ H7 k9 @ q2 o6 @; j
modelActions = new ActionGroupImpl (getZone ());8 ]3 a' O1 b7 l& c! W$ d: I, _
$ J7 e7 T/ S! r! v( w
try {
B3 n8 r# ]$ |; V4 D modelActions.createActionTo$message& S7 J5 j! a. |3 M; I, _
(heat, new Selector (heat.getClass (), "stepRule", false));
9 A7 C4 s2 Y" O' @# ~0 h* N } catch (Exception e) {9 v6 W% C/ N7 `3 u& t, T+ p; j
System.err.println ("Exception stepRule: " + e.getMessage ());
8 g+ ~) m+ z5 |" W }
% R7 Q9 q% e9 N' G
! l; L7 c% X) J try {
. \1 u' }& v( w+ { Heatbug proto = (Heatbug) heatbugList.get (0);0 m: n0 U6 ]$ m5 W
Selector sel = & z) w$ T: D5 E5 ~7 G
new Selector (proto.getClass (), "heatbugStep", false);
( J& J' u1 Y, ?" w; _ O actionForEach =
& _9 m2 v8 Q, y0 W modelActions.createFActionForEachHomogeneous$call0 {* v* |0 b9 y/ ~ z7 @
(heatbugList,. L9 L3 p" h4 C
new FCallImpl (this, proto, sel,
$ b7 O( A. B2 Q& p# Q B new FArgumentsImpl (this, sel)));
$ G# U( E( i3 a9 H } catch (Exception e) {1 ^" R7 u, E8 v7 o ?6 o) V
e.printStackTrace (System.err);3 C9 d* d. X" }( _2 Z5 e; {
}# c7 x0 e o9 k% ?# @$ h
g# O2 ?/ ?9 V r5 f syncUpdateOrder ();8 N* H# E5 d8 M/ u
1 T1 {$ W+ h# \8 Z. k( W J6 z try {0 ~" t6 b8 e5 Y$ n$ P8 |/ C
modelActions.createActionTo$message : t0 P) A% c: s# O/ `8 z ?; y
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 J6 T8 d/ `0 W3 A6 i( W } catch (Exception e) {3 d" I' i0 U( `9 y. F* V7 w
System.err.println("Exception updateLattice: " + e.getMessage ());
9 o4 u$ h% o! s: |4 B }- x. v# G Y2 s9 B& Y7 X( O6 q+ @' c
0 b# G! L6 r5 ] // Then we create a schedule that executes the. ?/ d4 p& X5 b; Y* i
// modelActions. modelActions is an ActionGroup, by itself it
" A; y. o4 q/ m. w4 b // has no notion of time. In order to have it executed in
- X8 X3 T. x2 Y8 E) M: } // time, we create a Schedule that says to use the: B8 t5 B, y& ?8 O( f
// modelActions ActionGroup at particular times. This8 q* h" v- Y, B/ l
// schedule has a repeat interval of 1, it will loop every
# U7 s @4 g) v5 G+ `7 l0 ] // time step. The action is executed at time 0 relative to
' y W; p5 \ T6 l( C3 n // the beginning of the loop.5 Z% M# h) M y* D; j
' i. Y- F# l' }1 q( F9 Z- O& Q5 q' s
// This is a simple schedule, with only one action that is
* ]" x2 l+ V; w0 m8 E8 A // just repeated every time. See jmousetrap for more! ?8 H( i: X; Z: j- I" C; \
// complicated schedules.
! X7 [- A- w, W$ Q) f; O2 H& N1 A0 P% Q
4 m# S& {) f# Q! A# R$ Y7 C: Z modelSchedule = new ScheduleImpl (getZone (), 1);
/ ~2 W3 Y. ]5 x9 P: l7 k modelSchedule.at$createAction (0, modelActions);
6 b b: U% ]! \, j+ e- E; c* ?
8 f! w3 u! j( |' s8 b; c9 M) \" H return this;7 u+ u- d9 m1 A( b1 _/ c$ R
} |