HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 c% e* W" t) m3 t
, |" m8 b# t6 s; S public Object buildActions () {8 a. P! l4 }: d$ N& E
super.buildActions();
1 J# M3 b7 W/ I) ]+ j7 Y2 b ( Y p4 L; Y" u: J6 B" y& G5 C* [
// Create the list of simulation actions. We put these in
+ H/ E" }& f+ | // an action group, because we want these actions to be6 I# j- g, u+ h4 j
// executed in a specific order, but these steps should
; p* r0 v; H* b; N0 D: e; I // take no (simulated) time. The M(foo) means "The message
- X( n$ A; F1 a$ N3 @2 [ // called <foo>". You can send a message To a particular1 q3 e6 w& [/ H+ s* n
// object, or ForEach object in a collection.
/ S. ^0 L& i* N/ O
. X+ J. K0 I, Z* a* Y; H // Note we update the heatspace in two phases: first run* i! @" r" M$ G
// diffusion, then run "updateWorld" to actually enact the: w4 z/ R* W& i
// changes the heatbugs have made. The ordering here is
( J9 W; E/ ]8 Z$ ?0 w& e A& s // significant!1 G! Z; W9 J& B$ {+ W
; e$ O/ }/ h: C5 W) f7 K' E // Note also, that with the additional* E. o$ w1 D9 }9 ?/ J0 Z% d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 @: D, Z! h8 u! E0 A0 E; e // randomize the order in which the bugs actually run
# X; {1 e( y; H( c# Y/ O# \ // their step rule. This has the effect of removing any2 e3 J0 L+ E2 d& \
// systematic bias in the iteration throught the heatbug
9 ]* ]0 ?0 Z7 B9 x; q4 N // list from timestep to timestep, O7 \1 @) `2 n
' F5 y0 J5 m% u9 Z
// By default, all `createActionForEach' modelActions have7 e1 o* ]# r; [8 ?$ ?
// a default order of `Sequential', which means that the
' M% s, T* L. V# t. r3 T: p, G // order of iteration through the `heatbugList' will be7 i, X& D5 O& s9 E4 F. [' n& z
// identical (assuming the list order is not changed
~ d. e1 M( C. @# |; | l% J // indirectly by some other process).+ ], c" t" M' ^9 G5 r; G4 y
9 X2 R; y* V3 b8 N4 R h* V modelActions = new ActionGroupImpl (getZone ());
* z# M1 t: i3 {% C7 \
; i, ?3 K& F1 e! @; [, K try {0 ]( _+ ~& t, S, Z8 D, F( T1 {& X
modelActions.createActionTo$message7 Y8 ]; U. w( Q; I
(heat, new Selector (heat.getClass (), "stepRule", false));
3 {$ h# S* z7 U) U9 w } catch (Exception e) {
& G. I: k: n* J" o7 `$ U% G2 B6 X7 { System.err.println ("Exception stepRule: " + e.getMessage ());
$ V; Q; q: N% z2 M }
E9 u; E! V7 K) v! Y9 C% V% x: W+ U6 y4 a# i$ b/ L* O0 M
try {
2 ~) O; B7 I7 k2 @: O Heatbug proto = (Heatbug) heatbugList.get (0); i* X- y/ K: ^
Selector sel =
) p) R) a( d" \- n new Selector (proto.getClass (), "heatbugStep", false);
5 e ]5 ?2 a' c6 S9 `5 `* | actionForEach =. w/ X- d1 _0 T7 {4 e" y4 Y
modelActions.createFActionForEachHomogeneous$call( _5 t0 ^* }1 M% E+ o
(heatbugList,
& U& I2 Z0 J5 [) T new FCallImpl (this, proto, sel,% p8 g1 Z8 `; G E. g1 L
new FArgumentsImpl (this, sel)));
0 H" \1 L+ k! H } catch (Exception e) {; H, }2 ]; d7 E5 U
e.printStackTrace (System.err);
0 Y2 d' |0 G( _& |. G }
( D! ~" l3 t# }$ s) [
- M- R4 G+ i5 o& _ syncUpdateOrder ();9 u! g4 l/ K d7 T
( B# P% B% Q6 j2 i; O0 a try {8 z* e3 ]8 z: d: C. o* r5 k
modelActions.createActionTo$message 9 s) h% t1 |4 ^
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 s- \+ \+ F9 x0 I& K; j } catch (Exception e) {
' L: t1 N" g0 G/ { System.err.println("Exception updateLattice: " + e.getMessage ());6 R; U6 ]9 Z1 q, |
}4 q- b k, N2 R# ?
: s* S, |$ S- M4 H // Then we create a schedule that executes the% w$ a+ W/ C) Y/ a: x
// modelActions. modelActions is an ActionGroup, by itself it) h* G0 [- x0 a
// has no notion of time. In order to have it executed in' b& Y' G/ j- H" G- u) z
// time, we create a Schedule that says to use the: M& n8 h+ p. [+ a
// modelActions ActionGroup at particular times. This
5 k( B7 A- B7 G) B \ // schedule has a repeat interval of 1, it will loop every
$ ~0 Y0 P" c: F3 O) T. z // time step. The action is executed at time 0 relative to
8 j( g# ~5 u# r* O1 t& P' d8 c5 _& i // the beginning of the loop.2 b* ?/ \2 R* @" N5 u* H0 f0 m9 k
( I. }/ _' {: J7 q* r1 q' K, e // This is a simple schedule, with only one action that is
$ l8 y& ^; `$ z' h W4 ]8 c // just repeated every time. See jmousetrap for more
7 Y; g' s5 X9 v. V; N6 R // complicated schedules.
( G5 j% A: H8 U) L) x& A . a) @& [$ K% X4 B- ?9 M: D4 s& d
modelSchedule = new ScheduleImpl (getZone (), 1);
' H, J7 S" y4 q9 s; A! Z' g modelSchedule.at$createAction (0, modelActions);: {/ ]# W4 {0 c- m0 S
' [ i* p- m& o6 a, _ return this;2 O8 m% Z+ L* \3 x: Y0 v
} |