HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 J0 [6 g0 G$ b8 X
, }6 S1 Y2 `, T) k/ C# G' K public Object buildActions () {
3 o& G% h( v) c& | super.buildActions();
* D2 k. T( e( S& N k
! j# ?, i0 B% D" J // Create the list of simulation actions. We put these in
% z% n @% N' j // an action group, because we want these actions to be6 b* q4 x$ k' n! Q+ n
// executed in a specific order, but these steps should9 m% A9 W' R; M3 v; I7 f/ b2 V2 t
// take no (simulated) time. The M(foo) means "The message
, B& S' P; y/ g7 ^ // called <foo>". You can send a message To a particular9 Z2 R2 l0 l* w
// object, or ForEach object in a collection.
8 H- T& M2 _. ^. _. z. ?+ x, L 2 I2 P% L4 n H$ X u
// Note we update the heatspace in two phases: first run1 v' d* H0 _: H. L; y# g2 c
// diffusion, then run "updateWorld" to actually enact the
- H: V7 p o. D% J9 z$ r // changes the heatbugs have made. The ordering here is* l A% }! L0 |& K# [) e
// significant!
+ S: R' ^( N# H; q5 w 7 n. j7 x6 s# r1 ^' X
// Note also, that with the additional
# v* p% D$ G' U, T# ^( M9 @5 x Y1 o // `randomizeHeatbugUpdateOrder' Boolean flag we can% F$ L: ~, J6 Q
// randomize the order in which the bugs actually run
! i9 w9 Y3 ]3 e7 v // their step rule. This has the effect of removing any
6 j) m9 O' m. e- U7 F+ G$ } // systematic bias in the iteration throught the heatbug
/ S1 [. t, h- k; m // list from timestep to timestep+ x! p) x6 d+ E5 z; J4 o
& R! D' U4 p: d7 l // By default, all `createActionForEach' modelActions have
8 T; I9 _/ A. ^& L // a default order of `Sequential', which means that the
1 p) f8 K5 |3 t% k2 T7 K // order of iteration through the `heatbugList' will be2 j$ q) v8 A$ f* J
// identical (assuming the list order is not changed
8 W5 Y5 @' t4 B, }( i0 O // indirectly by some other process).' V' K) J2 D) u2 C
2 \2 F3 M, X8 L% I& A4 `9 Y% D modelActions = new ActionGroupImpl (getZone ());/ k4 a$ Z, `0 @+ ?! V5 ~: @
/ N8 u% C* D9 M* |: N" a9 V try {
! e( e* m: B. P8 b modelActions.createActionTo$message
. G" [, n6 K F& i, B S, _ (heat, new Selector (heat.getClass (), "stepRule", false));
% t$ N, n; H. {/ R } catch (Exception e) {7 [. w+ l5 T, @: z$ f E: t8 M
System.err.println ("Exception stepRule: " + e.getMessage ());2 H) I! X& ]' \: x2 }
}
) t/ n7 F' y3 f. V, ~" `& G! n; g. a% ^4 g) F8 q& K% [0 H' r" \/ d
try {
. N& ]; k1 [+ f% p, _: Q Heatbug proto = (Heatbug) heatbugList.get (0);' J. m% f9 I& w6 H% i& _
Selector sel = . B( m! C% T" V
new Selector (proto.getClass (), "heatbugStep", false);
: O7 u; M! V+ A6 T V: Y actionForEach =
3 [2 E( q3 o8 k/ s U modelActions.createFActionForEachHomogeneous$call
$ l0 f- J" h' S$ b (heatbugList,3 ?+ t0 k4 U' ?: b6 }, w8 D
new FCallImpl (this, proto, sel,% V& `( V& T: E/ C6 |
new FArgumentsImpl (this, sel)));8 p# M$ {# V4 H) R, P' t
} catch (Exception e) {
- ~; y2 T) `% @" \ e.printStackTrace (System.err);
e; E: T1 [% C. w( G6 Z( `$ ^0 p }+ l& \. C$ h4 p! }% c* a
, e {, x4 M' {2 U1 O( |
syncUpdateOrder ();
/ x3 u; \7 [# l, r. t0 v# W: t( q5 M; S' y% b; n
try {& c, x7 G4 V' M
modelActions.createActionTo$message 7 l( F ~6 @: @5 m( g7 {4 s
(heat, new Selector (heat.getClass (), "updateLattice", false));
' p6 ]; o/ P' d! q } catch (Exception e) {
' L% o2 x' R+ p) j' i% p System.err.println("Exception updateLattice: " + e.getMessage ());0 n2 D O4 j$ X
}
# d% k& d: X" R' d* N6 Y6 h4 R
; T/ J |+ f, g0 b& e/ k // Then we create a schedule that executes the
, ~* I" d! q' h5 {: P; \1 W // modelActions. modelActions is an ActionGroup, by itself it! V% @) i4 Y! R; C8 l" y
// has no notion of time. In order to have it executed in& |- ~! `/ F' M% u8 q) _
// time, we create a Schedule that says to use the n1 V' J1 G- J0 Y& e/ z- L8 e* Y
// modelActions ActionGroup at particular times. This
" }7 s( ~& {2 V6 N+ ~, n* _ // schedule has a repeat interval of 1, it will loop every( v q3 K% o2 t& T' Y
// time step. The action is executed at time 0 relative to/ A: o: G! `" t! | ?( B
// the beginning of the loop.7 E5 w3 f# }7 \* Q
# A' Z. Q5 G. J& c! B. E: P, T# R // This is a simple schedule, with only one action that is, B* L" @% S0 W/ v
// just repeated every time. See jmousetrap for more
& |- K3 g% J" k1 o! r C // complicated schedules.
, m- a- [. c- d3 M$ _ ' |# ~$ {2 D: \$ L: d
modelSchedule = new ScheduleImpl (getZone (), 1);
7 B% \# \) _) k! ~3 i8 |3 {* z- K modelSchedule.at$createAction (0, modelActions);
1 J o+ y2 `3 e/ Q- U0 d* A
( ]5 F# L4 J2 d$ X return this;# H: K; m P( R, q* ^
} |