HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& E& T/ x' `/ @% D* I4 F0 |; X& y: S& i
public Object buildActions () {4 M* b) S) B/ U: z' C! @
super.buildActions();
9 t, g |- C- f2 { ! c5 R5 H1 Y" B
// Create the list of simulation actions. We put these in
5 a$ O# x( V) b; c- ?( j! ? // an action group, because we want these actions to be
( e* Q- c& L1 \9 V3 D* | // executed in a specific order, but these steps should
1 ^2 A$ o* D6 r: f( k6 q- E // take no (simulated) time. The M(foo) means "The message
9 N O3 B w1 @! J8 k5 k1 ]9 } // called <foo>". You can send a message To a particular
3 `- G& r/ K. m* m& O9 u // object, or ForEach object in a collection.
t" g4 d$ k6 V0 G" ]/ a # t. l0 S0 u* p# _
// Note we update the heatspace in two phases: first run6 M6 |0 k" t2 g
// diffusion, then run "updateWorld" to actually enact the# }' z X8 U" S* C0 e z5 X( C
// changes the heatbugs have made. The ordering here is) J w( n/ Y5 [5 ~; j, q
// significant!4 B' a6 V3 {" ~! q6 h2 F
& o s" i- P; J4 ~, O // Note also, that with the additional3 h- \. y# u3 M+ M. f ^- Y1 N) { f% c3 F
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 o& f/ X( |! m u
// randomize the order in which the bugs actually run0 K# I2 C' N2 a% r3 C' j
// their step rule. This has the effect of removing any0 l x" D: X! V; u
// systematic bias in the iteration throught the heatbug6 X) C: e2 Y9 F) T* v! y
// list from timestep to timestep
" b* d, _ x; l0 N$ a- @
1 l+ c$ z+ h6 {$ R // By default, all `createActionForEach' modelActions have/ B: G: t# X5 v L
// a default order of `Sequential', which means that the
& }- K$ z4 H. b& |1 `- X // order of iteration through the `heatbugList' will be2 l& ]" a5 N8 B x0 n4 L5 r/ L6 {! b
// identical (assuming the list order is not changed
4 f7 A' ^9 R% g( R' s* w // indirectly by some other process).
! L; \' f3 q" N
1 O" n( m' N8 |" K modelActions = new ActionGroupImpl (getZone ());% @1 ?7 f& S9 s& Q7 X: q6 o
3 S: a! e9 J G | E
try {1 e* F4 c% O5 f
modelActions.createActionTo$message% X5 V+ @/ d8 j3 c. ~
(heat, new Selector (heat.getClass (), "stepRule", false));0 g- h' i L# H" H3 z6 r
} catch (Exception e) {7 h1 c2 m/ r5 ?
System.err.println ("Exception stepRule: " + e.getMessage ());
+ a4 z9 }& F/ S+ h) F7 x }2 g6 [! i( D1 v/ ] J4 g4 N& L9 ]
- C( w$ Z/ X6 z' q6 q( `+ ^/ Z( h" c+ e try {7 I' x( j4 g& A* T( C3 t
Heatbug proto = (Heatbug) heatbugList.get (0);2 v9 |5 E- u# O1 I
Selector sel =
$ Q% }8 ^7 }: s0 F7 q) `" f new Selector (proto.getClass (), "heatbugStep", false);
! h' J$ w4 q" p5 c. v, E actionForEach =
" h4 Z P4 S3 O& ]2 x" _ modelActions.createFActionForEachHomogeneous$call
5 ]7 p/ ~7 Q. @7 k* h (heatbugList,
+ {" w1 c- I k new FCallImpl (this, proto, sel,$ e& Q* p! P+ g3 L6 z
new FArgumentsImpl (this, sel)));' }- ]4 g) T/ R# U, N: z4 {
} catch (Exception e) {5 N' |/ ?/ T1 L& Z3 ?
e.printStackTrace (System.err);. E( P/ E' z7 D! X& `
}6 {+ P9 X: c) `7 t( C0 W* @( [! h
' { h- T+ \! k4 I! |5 v0 F1 ~
syncUpdateOrder ();0 U5 i( W% N5 ~7 l5 T! d6 d7 ^+ {9 M
0 i$ }% c4 {9 Q Z) H1 y" G* j) p
try {7 \# Y. S7 x. A
modelActions.createActionTo$message
, X0 B7 p5 w. ]3 L6 p7 U8 _ (heat, new Selector (heat.getClass (), "updateLattice", false));+ U; o; ~. Q1 e m, b
} catch (Exception e) {. T" Z o9 j# L6 n2 Q9 q
System.err.println("Exception updateLattice: " + e.getMessage ());
% E2 ~! X K. q }7 @! ^ j3 N! _: v' S0 z: @ q
4 Z+ r# U1 C, a- w; Q6 x* R( Q // Then we create a schedule that executes the$ u' v' J! e9 Y6 j. M: f7 a; N
// modelActions. modelActions is an ActionGroup, by itself it- D* S+ B. H: _' X: m- M1 ~* y
// has no notion of time. In order to have it executed in$ ?' I9 F$ y6 F, P5 T
// time, we create a Schedule that says to use the
( l! B! _8 K7 `5 c0 I // modelActions ActionGroup at particular times. This
$ G6 O# g" A7 {9 G* W- }- T // schedule has a repeat interval of 1, it will loop every; m. d; ? D x/ O W
// time step. The action is executed at time 0 relative to
" c- g7 ]/ c+ @/ T // the beginning of the loop.
0 c$ |/ r- ?9 u+ f: ?% n" S4 N* x5 D0 E3 H# A" t$ ~% P& p0 R
// This is a simple schedule, with only one action that is0 m' M) z* ]" {# {: {) P
// just repeated every time. See jmousetrap for more- D. i" Z& N( @( s9 i
// complicated schedules.
$ u$ R% G' B4 n# I" N5 Z 1 s5 }* G- p& T/ i, [
modelSchedule = new ScheduleImpl (getZone (), 1);8 o" W/ E3 D& x& \4 h8 e
modelSchedule.at$createAction (0, modelActions);
# d% O- [, B. B8 t" X" l, i, p
0 `+ I, V6 x3 ]: u7 C return this;
, l; R. p+ f& C8 A3 P } |