HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 l- b" Z+ W5 c0 Q% z. C; b* e" ^$ e. J4 l8 p" B! C6 w& g
public Object buildActions () {% J8 Z$ W d0 P# i' b+ Y5 C# x
super.buildActions();
# p g' p6 Q/ f+ e3 g( e4 d 3 T r8 q: S& C8 j2 i5 S% y
// Create the list of simulation actions. We put these in
) b/ `& D) W& n$ b, s# O4 t // an action group, because we want these actions to be
( S! b% ]0 _( O X. D // executed in a specific order, but these steps should* Z$ w3 a" g6 L' `$ \
// take no (simulated) time. The M(foo) means "The message
5 F* ?2 f0 k- h4 E, _ // called <foo>". You can send a message To a particular
. U& @0 V( c; ?$ F& v# t // object, or ForEach object in a collection.' K9 ]8 W c o) f% X2 S" O7 q
0 Y+ S5 N/ U- P/ {& s // Note we update the heatspace in two phases: first run; V" a: s8 ~4 K4 I3 j/ [
// diffusion, then run "updateWorld" to actually enact the7 @' J: ~7 n& \
// changes the heatbugs have made. The ordering here is& k% d2 D3 x' e8 b$ a4 ?
// significant!5 f5 J Q4 X+ i5 ? _* a
& M: R% z8 h% W4 T y4 W* X
// Note also, that with the additional2 T8 Y/ T$ J: f, d% o( X
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ Q+ c* p$ b8 q1 _ // randomize the order in which the bugs actually run
5 P+ ?+ X. ] {) O // their step rule. This has the effect of removing any
3 T- i: W" t! W& a+ | // systematic bias in the iteration throught the heatbug3 D2 N. p. j, `$ w' d7 B. U
// list from timestep to timestep/ l) V* ]( P) [9 j5 p
$ G; G, T1 O! z% s- l( `3 y // By default, all `createActionForEach' modelActions have+ Z8 G- U6 \/ c/ P% X- d
// a default order of `Sequential', which means that the
3 d9 w( F- j; t3 u // order of iteration through the `heatbugList' will be
7 w- Z) x* L' ` // identical (assuming the list order is not changed; v1 f) I/ o" @' t. O7 E# y0 j: y
// indirectly by some other process).
1 {8 i6 w; |; f3 o4 Y0 E/ P ! G; A# ^* o& }& Y( b3 B$ m
modelActions = new ActionGroupImpl (getZone ());% v% P- u, Z2 P3 ?, R0 y, x
/ i* [0 ]( h c: p# y
try {; z; b5 `6 m; a0 X& g
modelActions.createActionTo$message
/ m3 @; ^7 o& S4 b; |( y5 d (heat, new Selector (heat.getClass (), "stepRule", false));: d6 o r7 Q. A' r1 Z: i7 e
} catch (Exception e) {
4 n$ ]8 {5 D0 |4 L: `4 h System.err.println ("Exception stepRule: " + e.getMessage ());
8 ]& _) A# v" O3 [6 d$ x2 ` }% L# {+ J) {0 h5 w( S3 C
) U4 f- i, L7 A try {
, q/ E4 x0 T8 r- u+ M8 @! K9 f Heatbug proto = (Heatbug) heatbugList.get (0);
: R/ ]' _' d. @7 d& C Selector sel = ( Y: R+ }" X- m6 @
new Selector (proto.getClass (), "heatbugStep", false);6 l4 p6 |, o2 ^9 B/ p3 c* l% J
actionForEach =
4 k& b$ B a0 K6 Q modelActions.createFActionForEachHomogeneous$call, m6 X# M3 f: p: _8 P: \
(heatbugList,
5 z2 r7 O) @6 Y, h new FCallImpl (this, proto, sel,. Y8 p) s: l2 `6 _0 t/ c; [
new FArgumentsImpl (this, sel))); N. ^* v* H, X) c9 O
} catch (Exception e) {2 {2 L+ o8 \- ^; L
e.printStackTrace (System.err);; X2 ]/ J) Q# f5 E4 ]! ?& d( {
}; p D2 `7 w) O; y9 K$ [$ C! W
: e7 U3 o/ ~7 C3 ?1 { syncUpdateOrder ();
3 F' z) P0 d' [- I: h @
# a1 H: |6 ]# L7 t$ j# T try {0 ~. n1 c, D# w0 J
modelActions.createActionTo$message 7 v' I+ N& s( K, @
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 q* F( ?% O+ @; J( T0 B3 p- G } catch (Exception e) {3 J* y7 H; d: K+ S; R
System.err.println("Exception updateLattice: " + e.getMessage ());
% c0 O% _% F7 m0 q* p7 P }
- r' E1 b+ s( G0 s5 j ) J& w6 n( Z3 l1 b. v% Q- o
// Then we create a schedule that executes the* Y- j/ \$ J; U+ E0 o" _" @' o
// modelActions. modelActions is an ActionGroup, by itself it
" ]6 H/ i3 L5 r( O) w+ f5 y // has no notion of time. In order to have it executed in% n3 ~( e( S: f- T9 M3 Y1 |
// time, we create a Schedule that says to use the* G+ K/ E7 v5 @: ]4 ~; l" }' R
// modelActions ActionGroup at particular times. This
+ ^# Y, Y0 }9 D1 \ // schedule has a repeat interval of 1, it will loop every+ P2 s3 d7 _( H! \8 R5 \
// time step. The action is executed at time 0 relative to
9 v: i! O) M( X7 q# [0 A // the beginning of the loop.
% Z* p8 _3 z: [: ^: L4 [4 V: }( z" U! p; d3 B( A
// This is a simple schedule, with only one action that is k2 F8 G: b o' i
// just repeated every time. See jmousetrap for more
# F4 W2 o# I% n c // complicated schedules.
7 z2 H" }, h6 N 9 O. w, @8 U& p3 O
modelSchedule = new ScheduleImpl (getZone (), 1);
' M% D4 ~# l& Z* w/ J4 _4 V d modelSchedule.at$createAction (0, modelActions);
3 V# D" R5 Z" S$ g C0 \ 5 P; _- K( _( H9 e2 a
return this;4 \6 G2 m+ U" a" q- ^! \ E
} |