HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& s/ L1 J) J2 @
6 ?4 {. ^9 ^8 [. @7 s( I public Object buildActions () {& _0 o! v! X: P: @* d( Z( p
super.buildActions();
; R4 P2 w4 \ g4 c$ a c4 S( S: L8 n8 v" P+ Y a
// Create the list of simulation actions. We put these in
- p* M# G0 V6 Y0 q$ d, D // an action group, because we want these actions to be9 s h" k2 ^" `
// executed in a specific order, but these steps should! K( |' d' c* ~- W, V8 [, o; Z+ j1 |' U
// take no (simulated) time. The M(foo) means "The message
1 }) Y2 T/ ^2 H // called <foo>". You can send a message To a particular) ]- }% e' {" A) }0 e
// object, or ForEach object in a collection.
. A8 L2 ~5 W% N& y7 O9 z, I" R
+ \- o, c8 s0 e. {7 R: b) G, c // Note we update the heatspace in two phases: first run' c( B6 o0 T( z. f+ s8 o
// diffusion, then run "updateWorld" to actually enact the
% u8 r. V) O4 e* f2 s // changes the heatbugs have made. The ordering here is S' Q1 r/ t( E) h; s3 q
// significant!; j7 l5 Q7 d) r" F* k8 X* U
8 x+ m/ e& u# l/ `7 p
// Note also, that with the additional3 S+ {4 y4 u: R( v
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ f0 m* s7 y. M+ h) k4 ^" |# G
// randomize the order in which the bugs actually run% ~- ^% c; e2 S5 L8 @$ ]
// their step rule. This has the effect of removing any$ v4 i Q8 Z7 Q6 ^9 i- i
// systematic bias in the iteration throught the heatbug* d3 U+ I2 `3 r; X, o' R
// list from timestep to timestep
( S) J5 V! e$ p9 Z
2 c6 i3 W6 D8 t+ n, X3 b/ d+ E // By default, all `createActionForEach' modelActions have
4 c% a( |5 \! k! F/ @. d% u/ I // a default order of `Sequential', which means that the
( l9 j7 _ ]) A // order of iteration through the `heatbugList' will be5 w2 e# V2 f3 E0 @
// identical (assuming the list order is not changed
# t, p3 Z) Y0 Z' p8 |: U3 }7 U // indirectly by some other process).) f" B0 E) s( {7 `3 U1 s; B5 ~8 C3 R
3 B+ R* R# @. K" ~ modelActions = new ActionGroupImpl (getZone ());
9 H0 s. |3 J4 f; v& w" M! f9 ^8 c5 y' w) s$ |3 O) w) x; b8 ~: Z$ Y
try {
7 ~& |4 B1 W+ D4 j& U modelActions.createActionTo$message9 R* }6 V1 r1 s" c1 b% O
(heat, new Selector (heat.getClass (), "stepRule", false));
[* u; P8 r9 o5 x* a+ { } catch (Exception e) { [% i7 `; u. g2 ~* G- ^( H
System.err.println ("Exception stepRule: " + e.getMessage ());' ^ r# t8 L& s% ~
}
5 v$ S+ i/ W1 W6 n- R0 s8 P# ]( g
try {
; _6 w' K9 v$ S$ S Heatbug proto = (Heatbug) heatbugList.get (0);* r( s! Z8 x5 s8 Z4 f$ }
Selector sel =
7 }9 Q8 H, O3 M) v t, U/ D new Selector (proto.getClass (), "heatbugStep", false);
# B& d u9 C1 l7 U actionForEach =0 Y6 E" b4 v M8 `* N' p
modelActions.createFActionForEachHomogeneous$call
8 ` X* `6 E& v- C: { (heatbugList,. b; l g# c5 E$ Y
new FCallImpl (this, proto, sel,
# z' U T R) b! c6 Y% m& @ new FArgumentsImpl (this, sel)));& n0 x$ J6 w* @, z7 \
} catch (Exception e) {
, f# Q: U- X, G& o. y e.printStackTrace (System.err);, d7 R5 O: J, p' K8 b% E
}, y$ M7 l9 I n" R1 \
p, j" E! a! F# s& J) @0 A4 g
syncUpdateOrder ();
s5 V2 v B/ \9 v, ]# N2 R
* |- n5 u4 e: c. _5 R% @( {1 i' o try {
6 d0 d& z( i8 u3 R/ d+ H7 o modelActions.createActionTo$message , y- Q5 J- I# U L
(heat, new Selector (heat.getClass (), "updateLattice", false));. r# ?+ p R6 o w n
} catch (Exception e) {3 b8 J7 {% g/ s* o/ Y; C, J
System.err.println("Exception updateLattice: " + e.getMessage ());/ k" }/ k2 b3 H
}
`% n; f, F/ N6 b: W; w
+ b8 C) j# X5 U3 a$ y( x7 p7 U // Then we create a schedule that executes the
' P% s* R5 y1 V8 T5 ?+ D // modelActions. modelActions is an ActionGroup, by itself it
% C# ~& j9 O$ Z# M. g, b2 O // has no notion of time. In order to have it executed in
5 b, C ~5 P9 {( N9 u: x7 q // time, we create a Schedule that says to use the1 \+ T1 W% V9 F: B* Q/ A
// modelActions ActionGroup at particular times. This
9 _! Y! ~$ [& |1 ^7 K5 Z // schedule has a repeat interval of 1, it will loop every
5 T! |3 j. `6 T/ r) X G6 a6 i // time step. The action is executed at time 0 relative to- t. j7 v' x5 a/ G6 |
// the beginning of the loop.
; A S% q+ i2 a" @
* |! g4 J3 X0 J( l' R // This is a simple schedule, with only one action that is9 e0 q$ g9 ?4 v% G
// just repeated every time. See jmousetrap for more
0 f/ d! s Y0 @% f3 u b1 a, y // complicated schedules.$ E j2 C! D- V e4 T i$ W
+ Y1 C9 _: _" j modelSchedule = new ScheduleImpl (getZone (), 1);
7 o/ L$ x! \# N* s! T modelSchedule.at$createAction (0, modelActions);! r' F+ l6 `* F& W/ A P! U/ C
. u! }0 J" \5 A5 w
return this;: V5 w" @5 I& ~
} |