HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% {, y5 w: p X o! l$ k$ t' `3 k- Z v8 }# m& w! ?
public Object buildActions () {: }" S2 g f. `: B+ c
super.buildActions();- J4 z8 ?. h& J/ i. z
: L' Y0 U3 G" v" r
// Create the list of simulation actions. We put these in: E/ \; O+ E' x
// an action group, because we want these actions to be
' o# v' o/ c/ R( z // executed in a specific order, but these steps should2 ?) f' b5 E- x' K7 f' p
// take no (simulated) time. The M(foo) means "The message
8 T. Y& F- F, e // called <foo>". You can send a message To a particular
2 N( R7 o" b3 u2 R! d+ m // object, or ForEach object in a collection.
" H, I X/ e5 t: u" z$ Q g0 r8 c5 k3 R+ _& h; ?
// Note we update the heatspace in two phases: first run
. {5 h! R) |1 s P n. b // diffusion, then run "updateWorld" to actually enact the$ M" e; C( T0 z
// changes the heatbugs have made. The ordering here is" f$ \" U9 I& m0 G+ q2 q, J
// significant!
9 f5 |5 M) ?$ U8 o' a; @, _ J5 t Y
: Q8 X) |3 Z. h# K& ] // Note also, that with the additional
, I3 M2 |5 i* }# m/ f // `randomizeHeatbugUpdateOrder' Boolean flag we can/ ?& D/ Z1 }3 p; Z& h- _0 n
// randomize the order in which the bugs actually run ], Z& P4 s5 J2 a" C
// their step rule. This has the effect of removing any% u, G; s* _1 m
// systematic bias in the iteration throught the heatbug
' H$ {0 V* ~7 H0 o$ r // list from timestep to timestep
) M9 n6 i! _+ O8 | r& k9 z# [# ? ' j/ i- A: f8 o& o# Z
// By default, all `createActionForEach' modelActions have
7 a8 F4 J- i) `! Q' K9 ^* Z2 P // a default order of `Sequential', which means that the
' X7 ^" @; ~0 J0 u. ~! G, _ // order of iteration through the `heatbugList' will be4 R( h U, r# _+ C8 O
// identical (assuming the list order is not changed
) x1 Y) `6 x$ P' p // indirectly by some other process).
5 m8 J8 K( Y/ {
M. `1 I6 `- ~ modelActions = new ActionGroupImpl (getZone ());
' b! i0 X! t2 {+ M* k3 `1 w; X& S' ^; ^8 o& r
try {
, {# O+ S$ v1 }- G5 c2 b( Y/ b modelActions.createActionTo$message
- B; q- ?- x2 \6 U- k (heat, new Selector (heat.getClass (), "stepRule", false));
/ I1 c' N( W! h% r } catch (Exception e) {
; `. [0 n* G* D/ K+ q! m System.err.println ("Exception stepRule: " + e.getMessage ());+ q' ?5 D7 M2 g6 T9 E. i0 I) S
}, ~0 P- r9 x; |: o) H
6 ^. E& D" h3 o7 n: {# e/ [ try {; Q2 g0 ?( A' k- ~
Heatbug proto = (Heatbug) heatbugList.get (0);
# {: q8 H3 S( Z+ }7 f Selector sel = 3 c: p* e) Y) ?! E! F2 O& _
new Selector (proto.getClass (), "heatbugStep", false);6 a! v% R" `) ^* ?( n9 z
actionForEach =$ e. j% C1 | ]
modelActions.createFActionForEachHomogeneous$call* s. N" B+ Q2 B- D ]( L! d! T
(heatbugList,
0 c" C w( m& c/ _2 m6 X" u new FCallImpl (this, proto, sel,& l' N) ~' o& A0 Q
new FArgumentsImpl (this, sel)));
& m; T0 k/ a2 _+ }. P/ w/ j+ t9 t } catch (Exception e) {6 [& d1 N# r8 R% \: ]
e.printStackTrace (System.err);
4 ]6 R P$ S% v }
/ x# s$ `- Z" w8 y! Z
& J9 n& h; x$ a% I7 m! [8 n3 W syncUpdateOrder ();$ T" ^- c( u6 ?" ^
& |! l7 v/ y" ?1 S5 T% M1 P! O try {6 W3 X6 D) {0 e# i
modelActions.createActionTo$message
1 G4 T: ~: \4 r) v& u1 L (heat, new Selector (heat.getClass (), "updateLattice", false));5 t2 ]$ X/ a! u7 { {8 x' u! b
} catch (Exception e) {6 r: P. W' I" b4 p0 N5 q
System.err.println("Exception updateLattice: " + e.getMessage ());- u3 \! C$ G1 W; @) v7 D
}2 f5 l. B9 n2 [) q7 Y
% k0 p3 |9 }! v; ~8 w5 y; Y) R5 d // Then we create a schedule that executes the
7 w# C" T0 K4 g( Y$ B) n( D // modelActions. modelActions is an ActionGroup, by itself it- o3 u n: e9 u( C3 s
// has no notion of time. In order to have it executed in$ C( M0 N5 [$ N$ C- `1 ^7 V
// time, we create a Schedule that says to use the
# r4 I- J8 R' }% e // modelActions ActionGroup at particular times. This- O, b7 R+ ]2 T" j
// schedule has a repeat interval of 1, it will loop every$ G* O `# ]2 G. q6 K! L
// time step. The action is executed at time 0 relative to4 l% U9 w- b6 c) E. u |
// the beginning of the loop.
/ N* @! d' {% e7 ^8 |# f& o, {7 _& f- i3 G$ s0 ?: J
// This is a simple schedule, with only one action that is
$ O) D' \! s; O0 B0 ?% [8 N // just repeated every time. See jmousetrap for more( ?3 D* ~, g. L) W1 \& \/ c
// complicated schedules.
( y: o- @2 ?5 O' s : R/ }# w. a: K
modelSchedule = new ScheduleImpl (getZone (), 1);$ Y1 W' L$ b+ b: l! a7 _
modelSchedule.at$createAction (0, modelActions);
1 d1 e" q% D9 e! @8 y+ ` 5 ?4 u) |' y3 |
return this;
; I" i0 b6 t* X- W- k. k } |