HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 o! r m+ ?: G' @1 @( S7 Y' [" b- a
- ~6 H! V, N+ O9 r public Object buildActions () {
( R$ P3 I0 u8 ^+ m! Z: `- H# n super.buildActions();2 [, {0 n. Q! u: j! s' w
4 S' ]9 l: Q# v2 B3 l" a5 Y3 ] // Create the list of simulation actions. We put these in
+ g5 C; y# v6 m2 l" R, A4 w" @ // an action group, because we want these actions to be2 ]- A6 K7 r: N5 e
// executed in a specific order, but these steps should
+ A( V$ C( D1 Q; { // take no (simulated) time. The M(foo) means "The message
! x3 {2 i6 Z; g4 F5 G1 Q" G2 i // called <foo>". You can send a message To a particular+ H+ X& M( q! |: n/ J! \; `/ l
// object, or ForEach object in a collection.
' e) j" S) r) z9 c) u
7 H! X- ~- p/ b: s0 c // Note we update the heatspace in two phases: first run/ k: I- O0 t) s
// diffusion, then run "updateWorld" to actually enact the6 ?* ?' R3 |, Z: y( e6 w( ^2 Y
// changes the heatbugs have made. The ordering here is
6 A. o$ M2 B6 ^ W // significant!! _7 [2 X1 ~; }* f9 Q/ W7 M
, s( `; y0 F6 ]6 v- l6 m // Note also, that with the additional
3 z l* r( {0 ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 T: h5 B) V8 G' v // randomize the order in which the bugs actually run( V6 Z6 x: g1 I# N
// their step rule. This has the effect of removing any( C6 R6 o7 ?- G
// systematic bias in the iteration throught the heatbug
) M7 \ t! V" j0 H: u // list from timestep to timestep
8 w9 J+ K# q- ?% i* Z
/ S, }9 M( k- [$ d- a // By default, all `createActionForEach' modelActions have% h. Z" ]; X" y) u7 D% g5 q
// a default order of `Sequential', which means that the/ m% A; ?* w" M
// order of iteration through the `heatbugList' will be7 g5 L/ K1 Q% f2 d
// identical (assuming the list order is not changed% c7 E* t" y/ H, W, t1 a$ A' W$ Y9 i
// indirectly by some other process).4 m! \) I6 V% o0 S8 \
7 q/ A. ~! \( Y4 \- j. a' m
modelActions = new ActionGroupImpl (getZone ());) |6 t$ k% c6 n( z! }3 |, X
# s( Q4 R0 t Q8 ]; d
try {9 I* s7 J1 O, m3 R1 [
modelActions.createActionTo$message- I& J# S% J: O/ }) t; D5 x
(heat, new Selector (heat.getClass (), "stepRule", false));
" w# G9 k# Z+ ` } catch (Exception e) {3 U$ _- q& ]3 r+ e2 e2 P
System.err.println ("Exception stepRule: " + e.getMessage ());; \/ `* w# k& ~8 q" v
}
) I$ |& |9 ?; V% d g2 p9 y7 @0 R. P4 J$ h
try {
0 v! o* E, [) k; g& x1 V7 } Heatbug proto = (Heatbug) heatbugList.get (0);
8 n P/ D% I3 ^ Selector sel = % G9 q: }+ ~9 `- ?9 }
new Selector (proto.getClass (), "heatbugStep", false);
" u* w' o5 l# e( O actionForEach =
; {6 D$ G6 H: h, s( a modelActions.createFActionForEachHomogeneous$call% n4 V/ h. i) {6 l/ z6 X* k
(heatbugList,
; X% ~' \) q$ G+ L% Z* A- \& l: x; g new FCallImpl (this, proto, sel,
2 K1 Z* t& P0 _4 l& U/ }; u new FArgumentsImpl (this, sel)));( n/ s- |2 ~' R: g7 w) e
} catch (Exception e) {
& l4 y% q) t9 ]5 A e.printStackTrace (System.err);
5 i% w2 ]) I- l, H. M }
8 d5 T8 e" z' j9 W! {, c" `( ]
9 \3 J' a7 m" k syncUpdateOrder ();
- a S4 [+ w( |$ b( ^/ b( ?$ v+ B Y. b/ K5 @
try {
! X% Y+ g& \" F- b; C modelActions.createActionTo$message
, h1 X3 n8 o8 N. U (heat, new Selector (heat.getClass (), "updateLattice", false));3 L: z1 G% }! d# v
} catch (Exception e) {
' t1 K* \( Z; X# w0 n System.err.println("Exception updateLattice: " + e.getMessage ());
" ~% ~6 Q8 X x. M/ l }* v- w |7 H6 m* A
, \! p! ]" `9 s" g" y; |# n2 t
// Then we create a schedule that executes the
. s8 Q9 G* X, P // modelActions. modelActions is an ActionGroup, by itself it
4 H: ~2 g; C* f: @9 M // has no notion of time. In order to have it executed in
n2 q2 W' N+ z // time, we create a Schedule that says to use the
5 u, U7 B- w0 T) ]! [ L& o9 W" ` // modelActions ActionGroup at particular times. This
& z. N% t, _2 p% i5 ]9 I // schedule has a repeat interval of 1, it will loop every2 m6 f+ |7 `$ g: Y/ w/ O8 c" R( U
// time step. The action is executed at time 0 relative to7 |" U8 l, j; J
// the beginning of the loop.6 ]; M/ w, L. S$ N+ O7 e
5 q/ H3 Z, q) r; [
// This is a simple schedule, with only one action that is4 ?& ^! U S8 B! v3 f: R. ]1 f
// just repeated every time. See jmousetrap for more
9 \, G' l2 e0 _: N3 V // complicated schedules.2 @! [# h" V* s/ e! m3 E$ w
* e h4 V+ c# A2 ?# ?9 B
modelSchedule = new ScheduleImpl (getZone (), 1);2 A+ V1 W% [2 q
modelSchedule.at$createAction (0, modelActions);) ]. d- b' ?4 B' B' C: ^
2 Q. ]+ ^; y& M$ G7 Z return this;7 G1 D% ?3 |8 _
} |