HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 b3 l5 @0 r* _8 \! j
: J+ ^; D) Q3 Z: u4 C/ D public Object buildActions () {
* ^' J' j+ o: I: Z( t super.buildActions();3 ~8 k: U1 V& i
4 {3 Q# }6 @2 k; N3 |* P% a // Create the list of simulation actions. We put these in
" D0 b, V4 L$ K2 B: J+ w! G2 R // an action group, because we want these actions to be; x+ t0 R3 e% u! S8 W) c
// executed in a specific order, but these steps should
: }, ~7 S( [( Q3 Y$ A // take no (simulated) time. The M(foo) means "The message
1 { k! N, A3 ^/ b; r: ~6 a0 i // called <foo>". You can send a message To a particular
. _- k1 P+ b$ l# Z( N // object, or ForEach object in a collection.- \6 t! K' [0 f* F" M- K
u5 p& M& G% S3 o C
// Note we update the heatspace in two phases: first run9 O8 A' ], X- H; ~2 I
// diffusion, then run "updateWorld" to actually enact the9 ~& z) k. W$ _' I
// changes the heatbugs have made. The ordering here is* Q) H7 v8 }. @" k2 |* T
// significant!% ~# ]0 r" i0 r/ b0 j& v, f
; M4 Y6 j' C5 [! p& P
// Note also, that with the additional
2 @$ x1 N0 N! T1 G, s // `randomizeHeatbugUpdateOrder' Boolean flag we can4 A) b5 N; b- d* W1 b1 V
// randomize the order in which the bugs actually run
1 q* x' P9 @: c& j' _6 x& D // their step rule. This has the effect of removing any
' E/ T k" J$ } // systematic bias in the iteration throught the heatbug% {. H! d! m8 H1 A5 I; G+ w9 W
// list from timestep to timestep" W. `( f7 S3 q; R L, \
0 p0 t/ `% N4 i* b; }) C! s. @
// By default, all `createActionForEach' modelActions have
2 O. E/ z$ n. a" H9 L // a default order of `Sequential', which means that the
' B( w* w) d/ T) p // order of iteration through the `heatbugList' will be
) L6 u8 D8 |. Q // identical (assuming the list order is not changed
1 Q# @5 Z$ Y. u4 G // indirectly by some other process).6 n7 H+ f; `, H( L+ k( y. }6 N
& x5 v1 | H+ W, t0 f modelActions = new ActionGroupImpl (getZone ());1 _: e" ? k# [3 E
& K- l$ Q5 U3 Z
try {
- H2 G' w) o! t' }9 G- g' U modelActions.createActionTo$message2 f9 ^ u: M- u+ [; Q
(heat, new Selector (heat.getClass (), "stepRule", false));% ?# B9 m+ A% L
} catch (Exception e) { p4 X# Z) `) ~
System.err.println ("Exception stepRule: " + e.getMessage ());
: a4 J4 l6 w& v! F; y }
- r. b& ?* F. H0 t
) N5 ?: E+ t' H B8 }$ q- |, ~ try {
$ v) ^' M' b( e Q3 M4 ` Heatbug proto = (Heatbug) heatbugList.get (0);
* b5 l8 w4 Z, |* K Selector sel = # h( O h; [( Z+ a* a
new Selector (proto.getClass (), "heatbugStep", false);7 F0 U, [ j; W6 Q. l) h, s& _
actionForEach =# S3 f Y9 p# V5 {
modelActions.createFActionForEachHomogeneous$call @( z3 f. C1 S6 v
(heatbugList,
$ a7 o- \& a) N8 L% w6 d0 p9 s& Z# q new FCallImpl (this, proto, sel,
7 [. @1 D! C( g: E8 B3 G7 O new FArgumentsImpl (this, sel)));# e- s, x$ \5 j# N5 ~
} catch (Exception e) {
. {9 z6 m* |2 q: E e.printStackTrace (System.err);5 M1 r- P3 s d! ^
}4 j; q: C0 b" Q
) F! l% |! r O$ a
syncUpdateOrder ();- D% Z: t. n5 W& Q& S: _
3 z! @" A+ [% c- F; q try {5 \6 L5 A! Q' N: [* x7 r
modelActions.createActionTo$message
$ R, P8 L5 a1 ] (heat, new Selector (heat.getClass (), "updateLattice", false));
. ?3 p* e! k# q' j* y } catch (Exception e) {$ t+ f+ X \9 q5 T7 M$ Q0 I9 r9 b' r
System.err.println("Exception updateLattice: " + e.getMessage ());: i3 J2 e& M! @
}
* i! p8 l3 \) v3 n r& z5 X
& Q" S+ d. ~$ z# Y, i3 p! x$ q // Then we create a schedule that executes the
3 Y: k! h" P1 l/ ^( W/ V // modelActions. modelActions is an ActionGroup, by itself it
9 }) ~0 J4 q4 x: v6 l- q/ i // has no notion of time. In order to have it executed in7 k' I0 b% V3 N4 i8 N3 f. L! @
// time, we create a Schedule that says to use the
9 F8 A6 [9 f! ~0 [) f // modelActions ActionGroup at particular times. This d* t& a$ Q q8 N/ C
// schedule has a repeat interval of 1, it will loop every2 ^6 p# r) B2 ?2 n* m4 K7 s
// time step. The action is executed at time 0 relative to9 m1 j+ {# K! x- \7 _
// the beginning of the loop.7 M. ^3 J2 y, \# G! ?
! n- z5 a$ x" \& n1 | // This is a simple schedule, with only one action that is) E7 W" x: K1 X
// just repeated every time. See jmousetrap for more
, U% O( e& X/ |% e // complicated schedules.
! Y/ T) g- _$ f1 u
# u: E0 {. s1 c& ?4 Y modelSchedule = new ScheduleImpl (getZone (), 1);2 B' { N$ J7 D2 j! e9 |
modelSchedule.at$createAction (0, modelActions);
( ^* I* \; g) G7 d $ ?! [9 D+ w* U1 r2 Q
return this;
' i# N. r: q g# R% k } |