HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% C, L& ^# ~8 O% R! ]3 A
& o* Y' A5 D- Z% h public Object buildActions () {# l- {7 L/ R% t' m6 Y5 z
super.buildActions();
% n+ s6 t. |* K; Z
c' T+ Z4 l* `2 A% I) L3 j // Create the list of simulation actions. We put these in
* H6 @5 D! f! A4 N9 Q6 E1 r* ]8 w // an action group, because we want these actions to be- N% U3 B1 T! }+ Q. a V
// executed in a specific order, but these steps should5 x+ u8 X& t( j4 a7 n/ W' o# X
// take no (simulated) time. The M(foo) means "The message
8 c- d; n9 ?( y, y" a // called <foo>". You can send a message To a particular
/ W# U J [3 Z, A // object, or ForEach object in a collection.
' T `9 I2 W2 {+ X - _; Z- ^2 u: ] U8 e
// Note we update the heatspace in two phases: first run( [: [, v; {5 g
// diffusion, then run "updateWorld" to actually enact the
3 }0 k L" B' S // changes the heatbugs have made. The ordering here is
, }' l) Z/ P$ n( H5 S, j // significant!
; y4 K6 U) e( v
& a$ ]# c/ x( B- K" X$ T3 g9 l- l% @ // Note also, that with the additional7 r% E! _- K+ Q+ ^* e- x1 `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
|4 H' ]& l& o" x' f% s0 Q // randomize the order in which the bugs actually run
/ Q+ B) C0 }7 F // their step rule. This has the effect of removing any9 H ^6 n& ~! u# c5 {
// systematic bias in the iteration throught the heatbug' m+ j9 O `, P: X/ X
// list from timestep to timestep
6 n3 t& Z5 n0 ~/ b+ b0 p
b8 b. y) Q* m4 ^2 Y, f; S5 k% n // By default, all `createActionForEach' modelActions have
1 z* s/ Q* |$ f" f$ \* W! Y // a default order of `Sequential', which means that the
( j% r# ?6 S. L; J // order of iteration through the `heatbugList' will be
4 l$ @. k/ b- G. _ // identical (assuming the list order is not changed
2 r" Y" r: N& f* N) Q // indirectly by some other process).
1 S3 |. j2 ^; F6 `7 D, v: h7 N ' Z5 l9 ^! q8 b0 l5 \9 ]
modelActions = new ActionGroupImpl (getZone ());& j7 v. G/ w" j: n4 _# h4 K
0 y4 F. _1 R, D9 s' e2 U
try {) {; a2 E, t: [* L9 y- n! R
modelActions.createActionTo$message8 J; B3 j. b4 C! ~: [
(heat, new Selector (heat.getClass (), "stepRule", false));
6 \3 C: C6 b! o6 b% q } catch (Exception e) {
, Y% Y! ]6 _! y# O0 I/ c d; Q8 r System.err.println ("Exception stepRule: " + e.getMessage ());4 W+ `* Q& V2 n( Y2 n; K& s5 g* G
}( a/ c+ P, e: M
( d7 N, g+ d8 B0 Z9 y$ G try {8 T$ d6 i2 P; [" G/ ^- ?6 N
Heatbug proto = (Heatbug) heatbugList.get (0);* @0 B: q/ q7 s/ S3 h; n3 h4 C
Selector sel = 2 `3 G$ H1 Q& }7 L) x- r% e% M
new Selector (proto.getClass (), "heatbugStep", false);6 n! K+ ~/ L5 y9 l
actionForEach =; P& \3 W* s( z4 o) ]4 j
modelActions.createFActionForEachHomogeneous$call
2 h3 f( k1 S7 K, {5 p (heatbugList,+ s3 i& j$ X3 ^1 ?1 L6 f
new FCallImpl (this, proto, sel,
# c/ B2 T* G* g# T. @5 _ new FArgumentsImpl (this, sel)));
5 h7 S! [6 Y2 c7 J } catch (Exception e) {
8 n, f7 Y r/ p8 o& O* J8 w e.printStackTrace (System.err);4 Z7 L$ u0 m! U! e& {3 a7 u, n
}
q5 @$ Y! g e, S# h- i
- J& K- o. c0 a7 r syncUpdateOrder ();
* u: S; o9 j& J' X' q5 e& f. j6 _* n& X) S+ E2 k
try {. G6 z l/ I+ b: k6 W" ]! {/ {; t
modelActions.createActionTo$message
# e$ s' G. _/ P- e) C: N3 N (heat, new Selector (heat.getClass (), "updateLattice", false));+ S# f; M- w& M4 S9 f
} catch (Exception e) {
1 @+ T& H0 u B7 K System.err.println("Exception updateLattice: " + e.getMessage ());
( L% I* y1 x" O8 P5 k2 z7 y }
( j* J( ]; |# ], O) x1 @3 _* y - }2 [1 O/ `) s7 h5 p/ b# ]( N
// Then we create a schedule that executes the
) B( G8 j$ Y: \$ s // modelActions. modelActions is an ActionGroup, by itself it
6 J1 D3 C4 [- J7 W6 x // has no notion of time. In order to have it executed in; d5 ?& p; a! C9 I* r* w
// time, we create a Schedule that says to use the
+ x* U( c! r& h. X5 Z4 _ // modelActions ActionGroup at particular times. This1 ^" y5 a( G5 z
// schedule has a repeat interval of 1, it will loop every
, v% ? c8 b% ~4 l; D% @, _ // time step. The action is executed at time 0 relative to8 }! ^+ B5 j2 o4 L7 a0 Z' R( x5 c
// the beginning of the loop.4 ]! S) a$ |# g2 a* S* h( s
* P7 [7 z4 L1 i8 a) m4 E# |8 l
// This is a simple schedule, with only one action that is, B7 B9 o" I6 |3 W5 y9 `) s! l5 _
// just repeated every time. See jmousetrap for more
, ~% t! L) j5 f // complicated schedules.
- b& u( U1 |. }- h4 x+ v4 p & R( b2 X% ]( x1 D4 Q- D. F1 G% c9 [
modelSchedule = new ScheduleImpl (getZone (), 1);
* D. K% `1 n. P/ ?! S8 p modelSchedule.at$createAction (0, modelActions);
: W+ d5 A; t" W* d- h) K
- R8 M( M3 a1 Y return this;
4 k' w" Q, x. _8 L+ ~% {4 j } |