HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ t' F( Z5 ~$ f
3 F( }, @6 w! p4 P+ O- A public Object buildActions () {
3 O7 [5 @) `5 a" ], j super.buildActions();
- P1 ]9 x5 u: I, K 4 p& r1 f9 v) N1 m
// Create the list of simulation actions. We put these in+ V& t& G. ?8 Y" f/ v
// an action group, because we want these actions to be! }2 }+ ]+ l& h+ v% w
// executed in a specific order, but these steps should
1 n% W4 Y. c' b; P // take no (simulated) time. The M(foo) means "The message) m0 u( q" Z) N$ l" r6 q6 M
// called <foo>". You can send a message To a particular
/ J2 I5 {7 V) W- W) y, J& T! Z // object, or ForEach object in a collection.
6 p6 \. u3 P. u8 g; J* l8 b 3 {4 M# R3 I+ z
// Note we update the heatspace in two phases: first run5 i! Z; O$ [5 r% d
// diffusion, then run "updateWorld" to actually enact the+ I ?0 S* n# L4 |) @
// changes the heatbugs have made. The ordering here is
0 y- M7 M% b6 ~ // significant!7 j$ g3 W, ^6 D! L i
/ K& @/ ]1 w% x) j: E7 a3 q& E // Note also, that with the additional
9 r+ x6 \) p9 }4 Z // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 T1 ]5 T8 j$ c( O$ d5 h // randomize the order in which the bugs actually run% V9 r7 J* s- s9 `8 L8 @: N
// their step rule. This has the effect of removing any
, d) f* @4 B; r. Z$ N // systematic bias in the iteration throught the heatbug
. Y; e5 L7 r4 ^ // list from timestep to timestep6 M' j0 }5 S2 g, v. Z
/ p* u7 g. p C. q
// By default, all `createActionForEach' modelActions have
0 Q' x% U5 v: }( Q5 X& H8 Q$ f! H // a default order of `Sequential', which means that the
5 z; D) h" w8 Q# w // order of iteration through the `heatbugList' will be- v) c; ?" J# O( O0 {1 n( {7 m
// identical (assuming the list order is not changed
+ r) J A8 ~7 @) V# d- q8 K9 W // indirectly by some other process).
. V" _+ b& i9 x) n4 E, _ . L5 k7 }4 _& d' l7 U# Y; x6 J6 Q
modelActions = new ActionGroupImpl (getZone ());8 k3 D0 B' j; _1 ?
3 \0 F% T3 H, { try {' y0 ]' E* B5 h8 E. s: |5 o: ]
modelActions.createActionTo$message J" v" F& k; E( P9 N
(heat, new Selector (heat.getClass (), "stepRule", false));
# {6 F! |" E; K; ^. W) S } catch (Exception e) {6 Q3 T. x/ @; V+ W
System.err.println ("Exception stepRule: " + e.getMessage ());
5 l+ ^3 }9 a* q$ Z I }
k' e0 H) }" J' N8 ^
$ g( j) H* ^! R' m! ~+ x( q try {. C3 {5 p8 c/ D% E/ Y8 c+ D( N
Heatbug proto = (Heatbug) heatbugList.get (0);" X+ ~5 C) H5 H9 w% {# ^
Selector sel = 7 q- S. d* X7 o
new Selector (proto.getClass (), "heatbugStep", false);# [( N+ R8 u( |& b& w" q
actionForEach =
0 B+ A$ S4 E7 J modelActions.createFActionForEachHomogeneous$call
d, X0 k; V4 \4 K6 x0 |# J6 b (heatbugList,6 L# ^) M! |! d5 K" `/ r- K
new FCallImpl (this, proto, sel,
7 v& v! L9 e6 e( x7 G2 L/ r new FArgumentsImpl (this, sel)));5 Y2 Y3 A+ k Z- {
} catch (Exception e) {) ]4 F. `8 P- l; p5 e7 P
e.printStackTrace (System.err);
6 `! H; O6 ^/ C }( T+ I: I7 J8 {" Q: q; K
2 u0 y g* ]$ o# P% _! N
syncUpdateOrder ();
/ a: {0 |4 j9 B* ?+ l1 B- y/ R7 |4 n0 c2 C/ W
try {9 `& a, H* `. K. ?' x$ {+ m
modelActions.createActionTo$message
1 u& P( U( Y% Z8 V0 N (heat, new Selector (heat.getClass (), "updateLattice", false));5 e8 y; p/ `. B9 c" }( _' ~& p
} catch (Exception e) {1 B3 x% E% D3 x9 B3 w7 x9 d; V0 ^8 N' Q
System.err.println("Exception updateLattice: " + e.getMessage ());
+ [0 D( D' z; A4 v. c }3 ~/ R" k; s% e0 R
1 L; ]3 ^, Y P+ e# l4 W9 I // Then we create a schedule that executes the- j+ w: \1 o" ^4 a
// modelActions. modelActions is an ActionGroup, by itself it* \* z+ e+ i. r- @. {! W
// has no notion of time. In order to have it executed in3 l' p5 B2 Y( ^8 |( u. ^
// time, we create a Schedule that says to use the6 R4 a$ y+ C$ U' o' q; t, v
// modelActions ActionGroup at particular times. This
6 W* I0 h7 [) ?) S // schedule has a repeat interval of 1, it will loop every
( Y3 _: K* [8 [6 h8 m$ O: v // time step. The action is executed at time 0 relative to" b" p; k5 ?4 \$ e
// the beginning of the loop.) {* A8 r8 z X* y' U- W8 T* [
( {0 }8 K8 H9 D ^; a. k
// This is a simple schedule, with only one action that is
! @0 I( l j6 R% y3 ^ // just repeated every time. See jmousetrap for more' n3 y# U' a: D# R% h
// complicated schedules.1 O- u" b7 `4 W, [- D& I! [
; ] K; X5 L6 r0 ^- o% A0 p5 c modelSchedule = new ScheduleImpl (getZone (), 1);
& N+ E; d9 q" ?) b modelSchedule.at$createAction (0, modelActions);
$ n. V% s6 O9 @: a( R# r : E- q" Q2 K$ _3 c
return this;; C8 O! `, {( l0 m# L
} |