HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 @! ?& j0 U8 r* Z, L! c6 m
: i0 `5 y' ?0 J# \ public Object buildActions () {
2 u6 p( V% d0 c+ a super.buildActions();: q4 m- {# {% S2 a
8 t2 }9 F4 S" d2 U! h% t! g7 ^
// Create the list of simulation actions. We put these in
4 F* O& h9 y( E& I/ ^3 s4 U$ n# f // an action group, because we want these actions to be
" d0 x0 h; w1 H, W2 W o* P U // executed in a specific order, but these steps should. |" t$ |8 w" b7 F% M2 z1 s: B
// take no (simulated) time. The M(foo) means "The message
% o9 L, q j$ G3 r& O9 e4 S- q // called <foo>". You can send a message To a particular9 L8 h. K; A3 R, O
// object, or ForEach object in a collection.; C; x& f. v2 {: L
4 X2 a; W6 c. W0 ]5 D // Note we update the heatspace in two phases: first run! `- S: P5 u* w+ E
// diffusion, then run "updateWorld" to actually enact the
6 l) E& \3 b2 y% T& F: b1 j // changes the heatbugs have made. The ordering here is
# F" Z" n0 f8 R/ r1 d8 ^0 K1 m // significant!# ?+ c! P$ T4 L; o
' N2 J' u1 ^ Z0 D$ P5 Q& P
// Note also, that with the additional
: l% k# Z( A( [' Q9 D* n8 H! @0 ?% r // `randomizeHeatbugUpdateOrder' Boolean flag we can r4 ?" H C2 D. e4 m6 c
// randomize the order in which the bugs actually run
' r8 i1 \1 U2 n7 S // their step rule. This has the effect of removing any
: X3 U+ h4 d8 D$ W6 v // systematic bias in the iteration throught the heatbug* y, d, y% s5 G) k
// list from timestep to timestep/ @" b4 I6 Q7 ^; b1 Y$ z
( Z, R3 J- b6 f8 e // By default, all `createActionForEach' modelActions have; W; D9 e# v3 ]& P( p
// a default order of `Sequential', which means that the
! W3 @* L1 r* @ P. U' w // order of iteration through the `heatbugList' will be
8 u4 W3 @; M" B1 Z; E5 o1 w // identical (assuming the list order is not changed( f7 ?: v% P, I: Y* a. s
// indirectly by some other process)./ l6 C l5 k! K+ R p& |
0 F6 T7 o8 ?4 }- e# Q
modelActions = new ActionGroupImpl (getZone ());
% m; x# {6 K8 z g3 Z
& R1 h( _. t3 Y X- | try {
8 t+ \; A& v3 C9 f modelActions.createActionTo$message2 s) H+ k4 P+ I
(heat, new Selector (heat.getClass (), "stepRule", false));
$ B' [! q& Z4 x3 r8 O, n } catch (Exception e) {
+ J0 B. k( a6 S# _) Y, j System.err.println ("Exception stepRule: " + e.getMessage ());5 @" O2 f" ~% ^) E5 i
}# i& f" d2 ]' z+ ~5 V3 }
5 b* r$ ~; ~& p) _8 X2 G
try {
9 [4 d1 G1 S& g# U N" G7 y1 H# ] Heatbug proto = (Heatbug) heatbugList.get (0);
. k* ]2 x+ w, D% s4 w, d8 g Selector sel = 5 u' x* U) @. F
new Selector (proto.getClass (), "heatbugStep", false);0 F }* ?( ^: W7 p _0 r
actionForEach =! q9 V& q2 g5 Z4 k8 M
modelActions.createFActionForEachHomogeneous$call
' U& N- Z! p7 }+ c" v' a (heatbugList,
3 U3 h5 q5 O, O) _$ S* s new FCallImpl (this, proto, sel,6 m' O0 @5 ?' t# v+ B( w, G' ?( p
new FArgumentsImpl (this, sel)));2 ~. \; e& X: H) ]$ u/ @; X' K1 @7 i
} catch (Exception e) {, I1 s4 `# Y, ]# ^/ z) ?) A7 G w
e.printStackTrace (System.err);
' h* R/ ]/ C8 b& k# ~) Y$ e( u: h$ X }
1 z7 l- r$ F h0 T o- P( B% L. u
1 J+ g0 \9 T- c- N7 C2 W syncUpdateOrder ();7 x2 L& W3 ~+ S l! p' I
$ Z( ]. V' d+ q) J$ B q try {
, Q( D3 o" `' ` modelActions.createActionTo$message + a9 E& E3 n. @8 l
(heat, new Selector (heat.getClass (), "updateLattice", false));/ |# R g2 }& u9 ^0 r! S1 R: _# t* ?
} catch (Exception e) {
f5 ~* A* l+ O& B+ Q; u$ O System.err.println("Exception updateLattice: " + e.getMessage ());, J# A% F; i) e& M' M, R! O
}
- L+ ~5 m8 E' [ 1 O3 D% \1 K' a3 P! l! c' z: r. a
// Then we create a schedule that executes the
/ D: e5 O) j" e D4 J) R8 R. E // modelActions. modelActions is an ActionGroup, by itself it
# R8 N0 K0 Y% H7 p // has no notion of time. In order to have it executed in
f" i. X% ?1 V; q2 H // time, we create a Schedule that says to use the
8 s* m& w0 O- y8 C+ W // modelActions ActionGroup at particular times. This
; _/ y9 i. h1 `) s* J. S // schedule has a repeat interval of 1, it will loop every9 h# D/ Q7 d" A; v" d
// time step. The action is executed at time 0 relative to' k( A7 u" r6 v! c+ j0 S
// the beginning of the loop.
& E5 T, W% ~6 L5 e4 Z y- u/ I* K7 g9 q5 g; X
// This is a simple schedule, with only one action that is& U/ t" S! l( O- b
// just repeated every time. See jmousetrap for more
6 i- v+ U+ `& K, | // complicated schedules.
" h6 G3 Y6 a2 `3 T" E8 Y
7 j6 y# U/ O; D, H% w' d7 Z8 } modelSchedule = new ScheduleImpl (getZone (), 1);
) P- r- {' ?* \+ R C( H modelSchedule.at$createAction (0, modelActions);
/ z% K* Z9 y7 q- ^" ~ % Q& W4 _ `' l
return this;
# t9 r* }, o9 R* C% c* ]6 Y } |