HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& O. B' j- f: T4 e
* F2 G& a% K: |* w$ s: ` public Object buildActions () {
6 { n/ q, }, | super.buildActions();
: Q) U4 K( V" g( R! T. K2 t- L' { / i6 i% g7 z" [ K
// Create the list of simulation actions. We put these in7 Y. w |9 \) {* H
// an action group, because we want these actions to be
8 a0 |" j3 B( |- h8 H // executed in a specific order, but these steps should k( j B; }! h" s! y$ l
// take no (simulated) time. The M(foo) means "The message9 [( l4 f& r- G
// called <foo>". You can send a message To a particular. ^8 y. M: i* a: \: X5 O3 v
// object, or ForEach object in a collection.
" U0 X0 e4 o0 g$ F+ J- R " N" h- _: t1 [. j3 V6 \) j
// Note we update the heatspace in two phases: first run
. q9 Z7 I1 R, e4 }* |" R, D9 M t // diffusion, then run "updateWorld" to actually enact the0 c. E' W! \7 C5 M, M
// changes the heatbugs have made. The ordering here is
; l( L, O9 L% Y( ^ // significant!
6 S. q% c( E z9 u& z' Z; w! @
" O5 x9 c. U! R3 [8 E @ // Note also, that with the additional
+ D9 ~; e; ~* _: U! C // `randomizeHeatbugUpdateOrder' Boolean flag we can% f+ Q! T0 o2 G. K; z4 c
// randomize the order in which the bugs actually run
5 L0 p. A8 ]+ u- _ // their step rule. This has the effect of removing any
" z9 ~, l( _1 c/ I // systematic bias in the iteration throught the heatbug! m- Q1 |$ D: q8 ^0 v. \) u7 X
// list from timestep to timestep
6 d0 B; ?! [8 S
) F) u+ u5 I6 r3 X4 d // By default, all `createActionForEach' modelActions have9 \4 A* w7 U2 ]& S# T5 d
// a default order of `Sequential', which means that the: X3 D0 Y0 @( E$ t% b |$ e
// order of iteration through the `heatbugList' will be% Y1 K+ O" w: M' w" _
// identical (assuming the list order is not changed! B/ a8 S4 v: z; n+ _
// indirectly by some other process).
& X+ l$ {5 X& ]1 C# g
3 O8 F8 Q3 L; ^! M; N- p: A modelActions = new ActionGroupImpl (getZone ());4 q7 O: j, U9 d; w# Z5 L
: o& }' K& j1 g) }) e
try {- J5 E2 d/ q9 t' r$ R
modelActions.createActionTo$message& M8 w; [6 K% K6 s
(heat, new Selector (heat.getClass (), "stepRule", false));
7 ~; z+ e2 ?9 Z8 n } catch (Exception e) {' z8 }4 C- L% L! x" \: e
System.err.println ("Exception stepRule: " + e.getMessage ());% H8 X8 w! Q! i$ q1 B
}. ], J; Y0 c9 _# K5 @5 h$ Y h: Y" L/ i
. Y) c n; V9 S, y0 y" m; o try {
! ~ W( w0 C6 k6 ^* O* Z1 { Heatbug proto = (Heatbug) heatbugList.get (0);% \: c% @: I0 \( y
Selector sel = - x0 c0 l+ G" ?+ K: o9 C- c
new Selector (proto.getClass (), "heatbugStep", false);6 D7 c. s9 [8 H( g& U
actionForEach =
/ `' e6 K+ ^9 _' t& M+ l. ` modelActions.createFActionForEachHomogeneous$call
1 S/ A, _5 M0 }1 A; B (heatbugList,
4 w% o# \% G G% G" A& a new FCallImpl (this, proto, sel,% Q! ~, A, |0 J$ n% t
new FArgumentsImpl (this, sel)));
2 ?5 E1 |: ?5 r8 P } catch (Exception e) {& K6 i# J( U+ M" @; f! S
e.printStackTrace (System.err);" Z5 K6 C4 Y6 r4 M
}
2 k5 r$ l l/ p8 _2 S i! c9 N- d+ C5 ^+ h2 r
syncUpdateOrder ();
0 y& O0 D I1 }+ c/ R n/ }$ ^* I7 r/ i
try {1 @% [+ s4 N9 z" M
modelActions.createActionTo$message 3 V$ h/ J" d% {- U& e6 g' |
(heat, new Selector (heat.getClass (), "updateLattice", false)); l, A3 H2 f. [) g9 U
} catch (Exception e) {
) U( A0 A, l8 [; I System.err.println("Exception updateLattice: " + e.getMessage ());; a, V& ?% Z3 K# l
}
# {, d# i: M" n* L+ z, a - e+ J. _% N& @7 d) S
// Then we create a schedule that executes the9 i$ X: ?) m9 D& N* ]8 I, a! x
// modelActions. modelActions is an ActionGroup, by itself it
7 t+ c. I4 r: \& @! K7 Q // has no notion of time. In order to have it executed in
1 p2 d& }0 [5 [$ C // time, we create a Schedule that says to use the* E+ j( }2 F- B, x7 _+ v% _& M
// modelActions ActionGroup at particular times. This* [: w# b+ z, N2 @& l
// schedule has a repeat interval of 1, it will loop every. S# e6 \" o5 |4 S* i5 L- C# @
// time step. The action is executed at time 0 relative to" F0 d- S# G* E# X* a! c
// the beginning of the loop.3 B0 v) A: N% I' ~0 f
; l, H) x5 s6 k
// This is a simple schedule, with only one action that is+ y* U) _2 u; j' f' K1 x
// just repeated every time. See jmousetrap for more
1 p) R# ]& E; Y& ] // complicated schedules.2 U/ `; V! B. K$ M
3 z' k U+ ~! L2 M# W% k modelSchedule = new ScheduleImpl (getZone (), 1);
+ J/ Q3 G q9 q2 N5 i/ o: R modelSchedule.at$createAction (0, modelActions);/ I( L1 J. o% u# L+ k, z' y
3 ~% U T7 k, v3 X2 I return this;5 G5 j& t: R2 `
} |