HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. p2 L3 \3 Y( ?: o1 H4 f8 R( a
5 j1 |& X% E7 X* Q% A4 x public Object buildActions () {
" w ?! n3 R6 D$ O( v* N# W super.buildActions();
+ E- w! F3 n$ K s9 }
: R$ e+ O ^5 }! [/ M // Create the list of simulation actions. We put these in
( }2 ], n( S6 O7 a // an action group, because we want these actions to be- k, s1 W( T9 ~% \" z) f( j* B4 \7 t
// executed in a specific order, but these steps should
$ c5 c" ?+ T. N3 `( R! v7 f5 F // take no (simulated) time. The M(foo) means "The message
& B; _' `: V, k // called <foo>". You can send a message To a particular T- o. e% M" G% M0 R
// object, or ForEach object in a collection.
1 S: T1 X% y( k! m1 G/ h : {/ ~. }. u ^& ]* V
// Note we update the heatspace in two phases: first run' A& C2 p1 W8 p2 g$ F
// diffusion, then run "updateWorld" to actually enact the1 }! p( O9 N! Y: K4 P F
// changes the heatbugs have made. The ordering here is2 E+ M! W6 X7 }: C' h5 K
// significant!0 F/ _! s7 N) I! q" N1 O$ ^
. V+ a5 _ y' X
// Note also, that with the additional
7 w- h% O. K! q- D: o J" W" l2 U // `randomizeHeatbugUpdateOrder' Boolean flag we can
' @2 F7 Z( o* x# g' Y3 g, f // randomize the order in which the bugs actually run, t3 [' ^. F/ f* T. U! w5 ~
// their step rule. This has the effect of removing any
$ V: P/ b. Y* ^ // systematic bias in the iteration throught the heatbug
@; j0 j* X) }3 C; i1 B( P& g // list from timestep to timestep
$ Y7 q, M+ ^$ Z3 l) f # z6 X6 A0 n! z1 C/ e
// By default, all `createActionForEach' modelActions have
( T# u" c. y: x8 Z% w // a default order of `Sequential', which means that the, N# I. e, n J* a9 W8 e* w
// order of iteration through the `heatbugList' will be
- H1 r4 y* P8 L+ F$ j# K9 k6 f7 L // identical (assuming the list order is not changed
5 V7 C' n4 l8 ~( s // indirectly by some other process).
' J8 |! x; i4 q
8 n3 C6 y' r7 D) j( \ modelActions = new ActionGroupImpl (getZone ());
7 J0 a! C9 d% \9 W/ g% H
9 N& v& A- I- w try {* r( i5 d3 I. c* y
modelActions.createActionTo$message, L! J+ A& W1 R( q$ j( R& v
(heat, new Selector (heat.getClass (), "stepRule", false));- T9 Z- \: P9 a, \
} catch (Exception e) {. u( Q, X; n9 l3 i3 a
System.err.println ("Exception stepRule: " + e.getMessage ());
$ l0 C$ w) o: U; I }
9 Z# w2 ]& k$ [( N5 s; b, b. ?
try {
" F8 e" [ E3 J" e$ Y Heatbug proto = (Heatbug) heatbugList.get (0);$ J- r* S4 O* A- U0 s/ _
Selector sel =
# |. u9 W; w. X0 F5 V5 d7 |# d new Selector (proto.getClass (), "heatbugStep", false);
1 X) S0 J" x$ y" R2 _" E+ t actionForEach =0 j b" m4 Z# N
modelActions.createFActionForEachHomogeneous$call
' t. M1 W0 Q: m (heatbugList,9 A! k* S/ E+ z0 W/ m) I1 k B
new FCallImpl (this, proto, sel,
[9 F5 }- L7 F- u( L new FArgumentsImpl (this, sel)));: c6 a3 L3 Y2 z% i- n9 z
} catch (Exception e) {! A- B+ o( }! g* _( M# x4 Y6 P
e.printStackTrace (System.err);3 E! d2 l& _: q3 {8 n
}- E1 d. f( \. u
/ P5 C# ~ {0 c; S+ k9 w& \8 _$ b
syncUpdateOrder ();
- [& z( ^1 D& E- Y2 q# u6 ^. [1 n/ [# T
try {1 h( r2 r5 M$ x2 |7 l- v% Y h: l
modelActions.createActionTo$message
& _7 P8 L7 [7 v) ^! W, n# K (heat, new Selector (heat.getClass (), "updateLattice", false));! W- z3 F& T" v
} catch (Exception e) {0 c& L; C f4 U$ [; q5 B
System.err.println("Exception updateLattice: " + e.getMessage ());$ a2 C w8 C, ?$ ^+ P
}( s& P d, E [; V- K; ~
+ J* W: e0 A7 q. d% V // Then we create a schedule that executes the
: c/ u4 \0 _) A5 x7 Y _ // modelActions. modelActions is an ActionGroup, by itself it" @" J! m8 h3 k5 h6 x
// has no notion of time. In order to have it executed in) ?0 x. G B- H" b0 e+ r
// time, we create a Schedule that says to use the
/ b7 l$ }3 [0 f8 |7 ^0 x" D4 n // modelActions ActionGroup at particular times. This
9 N' }' b y8 F7 C" v // schedule has a repeat interval of 1, it will loop every
4 e" g+ j/ z) E // time step. The action is executed at time 0 relative to5 c6 S: k) S5 w) r- z( c
// the beginning of the loop.
! C3 i5 A* c2 g6 D ^3 ~7 f% x( `) b/ M% A
// This is a simple schedule, with only one action that is1 o( _# C$ L2 F- u% r4 j
// just repeated every time. See jmousetrap for more% S, C! ?; }3 c2 X6 @5 O
// complicated schedules.
! j# M q+ j$ \. Y8 _( m4 r- L
. Q- @: N- y1 j# [+ V+ u" [ modelSchedule = new ScheduleImpl (getZone (), 1);( y! x7 X) h; q2 @4 O: T. a1 N
modelSchedule.at$createAction (0, modelActions);2 P# D7 @8 F) F% d. \# C
# c1 m# {. A& d' I return this;6 [9 z% Y9 J: g4 c0 ?* t n; U
} |