HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ Z5 S6 v6 ], L. k
. G. X1 {( i* }" _# x2 F) P, w public Object buildActions () {
& Z+ H: U$ ]; Z super.buildActions();
5 p5 `% I: C! ~* ?# i 8 O- e) [7 _- a3 K" x
// Create the list of simulation actions. We put these in
3 s& ]3 a! E+ M // an action group, because we want these actions to be' Q$ O% u' e4 V9 |
// executed in a specific order, but these steps should% R' c3 G# j3 \; A. D
// take no (simulated) time. The M(foo) means "The message
% I" }) }& o7 J // called <foo>". You can send a message To a particular$ f' m! M: u: X
// object, or ForEach object in a collection.
+ X* K" [& q" z+ V1 R+ l * V6 M- |7 i0 n) Y e* g- @
// Note we update the heatspace in two phases: first run9 Z8 _2 @5 @4 c" B
// diffusion, then run "updateWorld" to actually enact the/ E& G5 b/ V0 M& h% v
// changes the heatbugs have made. The ordering here is
' _7 P; _# S8 v. P! o+ a1 U* B // significant!
, B6 a$ ]* F* z( d" ]5 L6 [ : d+ ?- c3 F; `+ y2 w( M3 b$ D/ H3 ?' |! x
// Note also, that with the additional2 t3 L; J3 }+ X) L
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* J. P; V; E& Z, d) s // randomize the order in which the bugs actually run; H8 P3 i* a F6 @" Z) H0 ?
// their step rule. This has the effect of removing any X9 q" I1 q5 T1 Y" [* S
// systematic bias in the iteration throught the heatbug" r7 ?. S% C8 R5 R
// list from timestep to timestep: [5 n# i0 ?3 b4 |( ^
7 R# h) y4 H) M( p- h // By default, all `createActionForEach' modelActions have. m0 K8 c# n" n) J
// a default order of `Sequential', which means that the$ g; {/ v$ m6 H" Z% L4 N
// order of iteration through the `heatbugList' will be% K- q7 A+ i7 f% b' B; N- P
// identical (assuming the list order is not changed3 O: m# X. e9 X7 D* \; N
// indirectly by some other process).
/ _0 z* F9 w! N& d
. a, ^9 w: c. t" u$ }0 e* N modelActions = new ActionGroupImpl (getZone ());
: D0 X2 }( G( k; T
0 `* p) w* V( Z+ F0 z: \ U6 E- p: I9 V try {
0 @, v) ^( `; Z* r/ `( j modelActions.createActionTo$message
1 U# ~; B( r& Z2 G4 S( n (heat, new Selector (heat.getClass (), "stepRule", false));
4 F* y0 Z& [0 m2 U& y& i5 h } catch (Exception e) {3 A4 w" N8 F% q& i) P
System.err.println ("Exception stepRule: " + e.getMessage ());7 u! _4 k, m5 |8 @0 G9 i
}4 G: O* c1 [9 n
( p0 i% g( z+ T4 f try {3 P0 V! ^8 k/ b! b5 q w$ q0 J
Heatbug proto = (Heatbug) heatbugList.get (0);
. W: j0 C2 S# a6 Y+ v- l( ~ Selector sel = $ v8 h$ O: H9 N' N: D- I ]& L
new Selector (proto.getClass (), "heatbugStep", false);' u4 r/ a8 y* C7 I* k
actionForEach =) Y `7 k% D. X5 }% E( l; W
modelActions.createFActionForEachHomogeneous$call
# ^7 `2 D7 x7 G/ L V (heatbugList,: A1 m" P6 k. g+ u9 i1 k- i8 f0 a
new FCallImpl (this, proto, sel,
* i# O# P! ^# p: Z9 _, D% K new FArgumentsImpl (this, sel)));3 v( o+ d1 \7 I8 A
} catch (Exception e) {
) B/ X; S* H; ]. b/ | e.printStackTrace (System.err);% C: ^* H2 D0 |9 q9 i% }7 _8 `
}2 x: t' `2 n3 v# ~& I6 ~
) Z" B* z+ y0 w5 h6 g6 z+ x9 m7 v syncUpdateOrder ();
% y8 ]7 H! W- |# @
. b* g; _- K- D. ~3 f) i4 { try {7 ]) Q" b, @8 Y
modelActions.createActionTo$message 9 k8 I& X6 K2 @( m; r, k
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 B2 E# [" D; u. n } catch (Exception e) {
8 ~* w" i, n, X4 Y! G System.err.println("Exception updateLattice: " + e.getMessage ());0 Z7 ?, j3 b; Z W
}' O7 j' E, B* Y
' |# q2 N' d5 o5 K4 I // Then we create a schedule that executes the
6 s6 }, l- g" `; ]9 p& n1 T# B // modelActions. modelActions is an ActionGroup, by itself it- j) B) S6 m$ w: y1 S
// has no notion of time. In order to have it executed in H3 i% L, C: _: I+ {
// time, we create a Schedule that says to use the
/ }; \! C# l6 y0 ` // modelActions ActionGroup at particular times. This
9 L- D+ _% E' u // schedule has a repeat interval of 1, it will loop every8 I/ }/ q r5 P1 s5 A0 u( Y
// time step. The action is executed at time 0 relative to
9 r4 U/ p5 k: F; W // the beginning of the loop.. T: a) I6 A# z
- ~7 D! x( K( u
// This is a simple schedule, with only one action that is, E* G. M7 N* E
// just repeated every time. See jmousetrap for more! _6 v+ ^6 P. g
// complicated schedules.
) d7 Q1 q5 {' x- R" O! `: q
7 }$ y. H" w: k' M% p( P: } modelSchedule = new ScheduleImpl (getZone (), 1);
* M0 x& s+ q$ s2 L$ r8 n v$ O) _ modelSchedule.at$createAction (0, modelActions);
6 c/ Z- \( t& [0 m # Y6 P" `0 w0 L% \
return this;
2 R# e) ~) @8 h, C% L+ m# v- v } |