HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 [1 m" N- H: L- U8 R( d* Q* _# @9 w9 s: b: e+ M0 ^9 b$ U
public Object buildActions () {
/ z$ @' ^! h W0 D7 Z# S super.buildActions();
) p" E4 g! ]6 D& }. J2 f ' I6 Z7 P( n; N$ X4 j S& p
// Create the list of simulation actions. We put these in
8 V: ?* \, T/ e, u- j" ~- Z; e3 A // an action group, because we want these actions to be
1 v# Z/ V4 w9 ]6 W: s) X* ^ // executed in a specific order, but these steps should: f* [% H5 d' `$ r# e
// take no (simulated) time. The M(foo) means "The message5 g+ a! L3 [& X! Y( g
// called <foo>". You can send a message To a particular
) N' c' K" ^ |9 c) C) ]) { // object, or ForEach object in a collection.
+ Q3 L* q' Y9 Z + c" M7 S4 P) {' C- M" t5 z, x# f
// Note we update the heatspace in two phases: first run
, Q0 G6 @, s: p- v; U. h) X" H // diffusion, then run "updateWorld" to actually enact the
5 l3 x/ G/ |1 _+ C0 `# s1 \, ]( V // changes the heatbugs have made. The ordering here is
0 J) S4 g& T* v/ u // significant!
# c; n) l0 ]9 w* j3 m" F. _0 Y# E & y: C# G8 d0 J, z% u" f3 [
// Note also, that with the additional
/ p) z! Z1 f$ P, N- Z // `randomizeHeatbugUpdateOrder' Boolean flag we can; E4 K! _: t; J5 t. \% E1 h+ r, j
// randomize the order in which the bugs actually run. ], d* S. E9 \" G- B) D2 ?/ Y5 w+ V3 l
// their step rule. This has the effect of removing any
" B, l! p2 k8 E+ ~/ H // systematic bias in the iteration throught the heatbug' S9 e' ], n! e
// list from timestep to timestep
# G) [4 r+ |+ @! O
0 U0 f# p8 m) e0 e7 E$ j+ Q% f) Q // By default, all `createActionForEach' modelActions have
: }* ^! t P9 L4 ? // a default order of `Sequential', which means that the
' J; ^: a# q+ V) G! q/ J& e% X5 [7 v // order of iteration through the `heatbugList' will be
" Q( y/ ], G& d // identical (assuming the list order is not changed
$ ~, w) U2 j+ k: L+ M: \; R // indirectly by some other process).! j9 k2 X: t4 Z: a4 @
( Y6 C5 H# l }' R5 H* @ modelActions = new ActionGroupImpl (getZone ());
4 s2 r& N7 Y, O1 H$ X5 }% v6 t( I
2 z; J) s: V. N1 s& w try {" O' u9 p# d1 _" }! p' K
modelActions.createActionTo$message
4 |/ I. }5 m2 ]& m3 J (heat, new Selector (heat.getClass (), "stepRule", false));6 M5 C7 A6 {+ h% |* A+ Y1 ^& `* F
} catch (Exception e) {' R! _2 g. V t- u
System.err.println ("Exception stepRule: " + e.getMessage ());
5 ^- w- O; \& F( V& R, L& Z }+ W' d- |- \( P5 g7 D
( l- F7 Z# t. g( U. @
try {: J3 t4 D) q2 N: m% ]! P' v, F
Heatbug proto = (Heatbug) heatbugList.get (0);7 J3 q G; N, n) c& G
Selector sel =
4 R# K+ [- \: @& Y$ X0 q( Y' k# c- X4 c3 b new Selector (proto.getClass (), "heatbugStep", false);5 A6 s( ~# `7 u( K
actionForEach =
! f8 o3 {1 m0 ^5 J& z modelActions.createFActionForEachHomogeneous$call" x5 B, \2 J" N0 O
(heatbugList,* s& {' c9 ^$ X( q5 @; D( C8 f' I
new FCallImpl (this, proto, sel, D( F1 x, s0 M4 W. L2 B+ e
new FArgumentsImpl (this, sel)));1 X) R( x, a5 D( Z
} catch (Exception e) {
, l8 k) e" n' W$ r" m e.printStackTrace (System.err);& v7 Z, C3 h8 M5 V4 r. g
}
/ s* M( ]/ C% O1 p+ A
( ?, j, r. t! ^ X5 ?5 W syncUpdateOrder ();2 _2 }( } U) [8 u" |( c4 k
$ N6 p/ `+ y1 V5 K& W7 O" w: D try {' ^' z) t F, J
modelActions.createActionTo$message 8 y' O: P3 u# u; q8 s v
(heat, new Selector (heat.getClass (), "updateLattice", false));' M" D( I. @( Q5 G; [& k
} catch (Exception e) {
% y! \' E7 @5 B) u* D0 C7 x System.err.println("Exception updateLattice: " + e.getMessage ());3 K. x' { v9 W3 h
}
3 Q( f6 M# h7 L2 v) }0 }1 l
: e2 T# y: x1 ~3 Q# @ // Then we create a schedule that executes the
" W% b6 I8 _6 G+ {4 |$ K, y // modelActions. modelActions is an ActionGroup, by itself it
) F: _ v) r9 t5 L | // has no notion of time. In order to have it executed in
8 J" r6 _$ U" i v8 d2 R# D& p // time, we create a Schedule that says to use the& b$ s7 }3 a. t, A a& a) V" o
// modelActions ActionGroup at particular times. This) V1 a a$ R2 U b
// schedule has a repeat interval of 1, it will loop every* t4 g& Y: U" K; M D0 f: g/ [! }0 S
// time step. The action is executed at time 0 relative to
- ]! U& K( P/ z8 D% B // the beginning of the loop.& f9 h1 R: J3 m4 }1 v
1 i, `7 s. m! w/ W* o
// This is a simple schedule, with only one action that is
- t0 j, w1 _! o: R2 @9 \ // just repeated every time. See jmousetrap for more
! D M& I+ C( r$ L( u // complicated schedules.1 Q7 d7 h1 } b0 _
& O" j2 }* I( e7 C4 \. [+ C4 n& W# a
modelSchedule = new ScheduleImpl (getZone (), 1);) s7 }5 o( O; Z1 e
modelSchedule.at$createAction (0, modelActions);
8 G y# v. y2 I
0 f# D3 T5 r* n return this;
2 K y. p) G% M3 Q0 w3 |8 Q% p } |