HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 |2 c0 z2 v$ ~' t1 p+ y" ]
: O# K* B, M$ ?, Z3 l public Object buildActions () {
. m* a+ [& j" E2 K" c/ J super.buildActions();
2 w' }6 J1 X, E
& Q* r1 V. A8 e; ^3 I // Create the list of simulation actions. We put these in
9 d1 S3 H+ f- m: d p9 i // an action group, because we want these actions to be
" z1 R' c- C! Q2 @0 u" G9 A7 P // executed in a specific order, but these steps should: A; ~+ u# U9 S5 p
// take no (simulated) time. The M(foo) means "The message$ H8 S& b- n2 o9 A$ c8 d
// called <foo>". You can send a message To a particular- `# V" _9 ^$ h0 m
// object, or ForEach object in a collection.
' }+ z/ {% Q' Z5 ]9 L * U8 `4 h1 M9 c5 q9 B
// Note we update the heatspace in two phases: first run
2 u" Z, S. a7 ~, {/ \ // diffusion, then run "updateWorld" to actually enact the
2 l2 [/ p6 @# [) j // changes the heatbugs have made. The ordering here is$ R4 a! K8 v! h# m
// significant!! |2 p* f+ a* W1 D9 z1 H
! C' j1 y: R3 A/ {0 b
// Note also, that with the additional. ^2 _* f! Q; d8 f: ^) K* F
// `randomizeHeatbugUpdateOrder' Boolean flag we can( Z! E5 Z4 f3 C1 S
// randomize the order in which the bugs actually run
& ~' u) Q* ]3 D; p9 q // their step rule. This has the effect of removing any
* L: z! t; g" b8 d1 N6 ^) b2 V q // systematic bias in the iteration throught the heatbug
5 Y& @$ K& ]9 j+ ]# m4 p // list from timestep to timestep
6 c" v8 A3 n1 F
+ o/ ?* h3 R3 @( S6 U+ b2 v' R# t) j // By default, all `createActionForEach' modelActions have
8 J' F4 w$ L3 r) P0 g* I# q // a default order of `Sequential', which means that the/ {+ C9 G$ b3 Y: s2 M
// order of iteration through the `heatbugList' will be, `6 F1 m) R7 I$ X' {' l9 E
// identical (assuming the list order is not changed7 U0 C! L( L0 W$ A
// indirectly by some other process).- ^2 ^& r8 @5 r/ X: x0 C# {2 l
# S H! A9 S6 ?: W# n" z F
modelActions = new ActionGroupImpl (getZone ());) l7 a$ U# |5 P) G& h! v9 w
5 z; W) ^! ]. I5 X; f try {. }- N6 J( G H4 o* {, t
modelActions.createActionTo$message
9 G4 {; E8 O" d& Y+ | (heat, new Selector (heat.getClass (), "stepRule", false));4 ^: H, ^3 w( u" }
} catch (Exception e) {- |$ b; e: Z" G5 h0 O' M
System.err.println ("Exception stepRule: " + e.getMessage ());& ]/ y. H* h9 x1 m( ]( |! a
}$ |, C: ?7 p! X( `: E
+ e, i- R k% ]1 ^2 A4 w, V1 v
try {& W2 q7 O: i( W8 b S+ i
Heatbug proto = (Heatbug) heatbugList.get (0); E8 [+ v8 x7 U* C3 m$ X
Selector sel = " Y& s! R. r; Z6 T0 `
new Selector (proto.getClass (), "heatbugStep", false);
6 z) f5 w2 \: m6 A W actionForEach =
, R# |* X6 y4 I2 k# a8 y" F modelActions.createFActionForEachHomogeneous$call
9 E# B0 ]' ? d* y (heatbugList,
( c; f) v$ S4 M new FCallImpl (this, proto, sel,6 m% B0 l+ X" A8 t
new FArgumentsImpl (this, sel)));
8 Y- k) ~7 z6 `2 O6 d. ] } catch (Exception e) {
$ s- v- F: o8 U8 ^4 F e.printStackTrace (System.err);
. d# k$ U" A/ f7 y6 S9 A: j$ { }( G" w! n6 b, X9 N; F
: F M( W, m3 }2 @% [ syncUpdateOrder ();) T6 G( t9 G. C& P$ G; ?( n
* V. v2 N3 X2 W, @ a% A' m
try {
- x6 k( A3 m: p4 I/ ^* L/ w% ^ modelActions.createActionTo$message
2 ?" q% ]+ ]% {7 M6 c (heat, new Selector (heat.getClass (), "updateLattice", false));
9 U' B) z. c0 `$ [9 _ b } catch (Exception e) {
: F6 c& k4 \; C- U System.err.println("Exception updateLattice: " + e.getMessage ());
2 ^/ Q3 z6 [# c) X }
0 j0 m: A1 N( r4 I
" }5 v0 O2 {9 |( u // Then we create a schedule that executes the
+ M! p; [2 f- S' O' h6 ]% M% j // modelActions. modelActions is an ActionGroup, by itself it+ v, c" m3 @, }8 S& t( Q( D4 O
// has no notion of time. In order to have it executed in3 e5 y& K1 `. ~1 J' G
// time, we create a Schedule that says to use the
7 C F/ n, q! A! j. | // modelActions ActionGroup at particular times. This$ Q+ a; Q( W' X& d
// schedule has a repeat interval of 1, it will loop every
/ E8 P3 b, ]" g* _9 C // time step. The action is executed at time 0 relative to5 b! \7 ~2 h6 _, h; [
// the beginning of the loop./ z" l7 l, I& \' d$ M7 F
2 A/ F, n6 q2 I3 x7 J
// This is a simple schedule, with only one action that is
+ }/ W" r4 e' T- Z // just repeated every time. See jmousetrap for more# ?# ?' h) j/ F6 \. B9 S/ F
// complicated schedules.6 O d- D$ J/ d) W4 C5 m3 ?
/ N" }& J) g4 T: M0 L0 G8 d4 a
modelSchedule = new ScheduleImpl (getZone (), 1);
3 }0 j& s: {+ E1 [$ K# F2 R modelSchedule.at$createAction (0, modelActions);
/ I9 f+ g8 @7 ?2 }- p" q* g & n9 |9 e$ Z z! q# l& |* h
return this;8 E0 d2 V" V) k _
} |