HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" F; x/ w/ E$ ^3 W/ e' t
7 A8 ]! B& a$ {+ {$ n public Object buildActions () {0 Q) ^1 }7 M6 J) \+ N; n
super.buildActions();
- V) b H+ d" F
1 m5 |) X# G5 h, P' F& D" T! U0 m9 K // Create the list of simulation actions. We put these in& d g8 |) u+ m
// an action group, because we want these actions to be
& ^- b) H- D" R7 A# ^# O // executed in a specific order, but these steps should! W3 p/ z8 v$ O. ~. Z+ @( p
// take no (simulated) time. The M(foo) means "The message
. _5 d- u3 P. A# M // called <foo>". You can send a message To a particular
" @4 z9 q' f, a C- T$ v- z // object, or ForEach object in a collection.
* V+ G- p; D# n+ c: w. C
& v/ _5 |+ L$ b/ u! c- K // Note we update the heatspace in two phases: first run
4 B9 _2 h4 E* u; c! X) x // diffusion, then run "updateWorld" to actually enact the
) ]+ T2 R- `% H+ |: R0 { // changes the heatbugs have made. The ordering here is
7 N9 v) u$ ^! K1 C // significant! I& M4 x6 e" b7 @1 o, z
/ K, f9 r) s9 _ // Note also, that with the additional0 n# q: f$ A8 i/ y6 i' ]7 K
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ ^0 Y* B5 I8 z- A8 O* ]1 ] // randomize the order in which the bugs actually run5 v* Z* x- d1 h q7 c
// their step rule. This has the effect of removing any
% u& a7 J* n8 e" H. v F" V // systematic bias in the iteration throught the heatbug* i% _. h1 ^! B
// list from timestep to timestep9 n: n* l7 H( o" x
5 d5 o' K7 e0 N, K r // By default, all `createActionForEach' modelActions have
/ [; H- f" G7 C. e8 |8 E // a default order of `Sequential', which means that the$ p5 ]' f5 J. p# Z0 K! }( b/ F5 n% N
// order of iteration through the `heatbugList' will be
& T# @# d% u% \ // identical (assuming the list order is not changed
0 ]- ~; L0 y5 j) O // indirectly by some other process).8 n" k" j+ k: O; S. u; W& Y
; i; ~/ w. ~# {3 u, s$ W modelActions = new ActionGroupImpl (getZone ());
2 ?' J- n ]! W
6 |6 m% D; j- x try {1 {/ O& g5 \+ r# t
modelActions.createActionTo$message
( W( N1 }$ J, m& R6 E# E% t7 L7 P (heat, new Selector (heat.getClass (), "stepRule", false));& V) |+ H8 R4 ^) D' d8 u: l
} catch (Exception e) {7 h' i3 w8 {( K6 F [ z
System.err.println ("Exception stepRule: " + e.getMessage ());
* w; F2 y6 W8 @& o% P; H" I6 l }
2 g; L$ Q z! i* U9 B0 ~
; t/ p" [0 ]) O+ V i9 r5 L try {7 n9 @$ j1 a3 G
Heatbug proto = (Heatbug) heatbugList.get (0);
3 p2 W5 @2 k9 K Selector sel =
; _! A0 W5 B' ~2 m/ H new Selector (proto.getClass (), "heatbugStep", false);
5 R ]) }$ e/ P( X* f actionForEach =
8 H% `2 H! A5 {: e modelActions.createFActionForEachHomogeneous$call( O) a& Q- H( W8 E
(heatbugList,
& D r4 j; P2 o* U9 ~5 y* c l new FCallImpl (this, proto, sel,
! F; [# r, m- t$ a2 x9 a& y new FArgumentsImpl (this, sel)));
0 l7 Z- {! {7 ~6 ^$ u } catch (Exception e) {! X( S/ E S& _0 W) g; z8 B
e.printStackTrace (System.err);
! y1 B4 s5 m! a1 j0 F }" j. A, h; e6 n6 u
, z. e3 T4 i( F7 F7 e1 b+ k
syncUpdateOrder ();# n- D8 @. n% b3 G* x( O+ |& R
' d- {$ _* c' n3 k" v; a
try {7 O2 x8 H& B4 N k! @
modelActions.createActionTo$message
4 p4 y0 l. ^: R2 V/ [& }# G% ]; i (heat, new Selector (heat.getClass (), "updateLattice", false));
( r2 v" o+ N- j/ P w- [' a } catch (Exception e) {/ C+ o7 V' O i# p" E& b8 [: S
System.err.println("Exception updateLattice: " + e.getMessage ());
# H. P. D+ F% W( J$ _ }
6 ^2 Z. _1 Y: q! _0 D' c# } " L, y+ A' I& P: s, H7 }
// Then we create a schedule that executes the; h3 p# x1 T* q3 I9 J8 E
// modelActions. modelActions is an ActionGroup, by itself it
/ Z# q* B8 R( } g3 o& {7 s // has no notion of time. In order to have it executed in
( d6 h9 y) j8 y& I, K* t% W // time, we create a Schedule that says to use the
8 A& H' c1 W; S0 p2 n8 j: f // modelActions ActionGroup at particular times. This
$ t4 R4 ^2 K8 P3 n // schedule has a repeat interval of 1, it will loop every" Q4 p6 f( s! E" X4 A) b& U. C
// time step. The action is executed at time 0 relative to2 i% ^6 Y$ X, E' X8 |5 E
// the beginning of the loop.
( |9 i# ~ B/ z- n
/ T" b: F- ^: f, m // This is a simple schedule, with only one action that is D. e/ R5 Y, s7 }
// just repeated every time. See jmousetrap for more% N- u' a/ O1 a
// complicated schedules.
6 L& H! c0 ^: H1 j" K ) j' u$ O" w9 M h, ~
modelSchedule = new ScheduleImpl (getZone (), 1);
0 m% y$ f7 ]# J7 J. d7 @1 ] modelSchedule.at$createAction (0, modelActions);
! R, T: a' W- T 1 R' o I* ] K( f. Q0 B- t9 `
return this;
& n% z- P1 X3 e. _" K8 g } |