HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# y5 o: N+ w" ]- x
0 m6 _' q' }9 X7 B0 h" V
public Object buildActions () {( n5 s) f7 \2 H, x4 v
super.buildActions();! V& H6 l; X1 v8 T9 g1 w! v
: A: d; S4 r! o0 l+ ^
// Create the list of simulation actions. We put these in
6 ]/ r$ n8 ?) {# _& d; b8 k5 Q // an action group, because we want these actions to be8 B5 B4 m* u- }+ {0 ^' e7 `' B
// executed in a specific order, but these steps should( Z' [$ g. X: G4 T) w2 t, f
// take no (simulated) time. The M(foo) means "The message; b* d. S3 u: G& ?9 X
// called <foo>". You can send a message To a particular
/ s R4 N/ t6 m6 x // object, or ForEach object in a collection.
1 f1 a9 N+ e8 ~ 1 G9 k$ r+ F3 D4 Z' t
// Note we update the heatspace in two phases: first run4 t$ b$ X, F/ q5 m5 Q
// diffusion, then run "updateWorld" to actually enact the
7 K5 `1 G2 P! n( B // changes the heatbugs have made. The ordering here is/ X' j' _. G# \$ y
// significant!- V5 |# m* y- \/ g1 [/ U% g
+ T5 O3 j7 {( _6 C
// Note also, that with the additional
$ j& Q2 s( `9 W: k // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 g: ^5 D- ]- x, w // randomize the order in which the bugs actually run2 X2 Q! ^. k* t6 A5 [
// their step rule. This has the effect of removing any6 D) Q2 q9 h# E
// systematic bias in the iteration throught the heatbug. {- ~) K+ t7 R0 I3 `
// list from timestep to timestep
4 L; ~0 Q8 b' {0 |$ U
: N, j- l7 W* ~& W, A! x // By default, all `createActionForEach' modelActions have
/ L2 |" K: ^0 Z, a$ o4 o8 u // a default order of `Sequential', which means that the
, r; X" e( i4 o* u // order of iteration through the `heatbugList' will be
: p/ A4 U! L$ w5 [% V9 b // identical (assuming the list order is not changed
/ H1 `6 o! I: g# N5 M8 q5 Q // indirectly by some other process)., i6 h8 U& E5 M( l9 w
) a8 d3 {/ B' N& l' q modelActions = new ActionGroupImpl (getZone ());
% e p9 U3 H% c5 C3 \5 k% `9 ]6 ?$ x
try {
V. d# W/ }0 N! B0 S7 n$ Z$ ~ modelActions.createActionTo$message
$ q7 @# o6 P+ x: g" s (heat, new Selector (heat.getClass (), "stepRule", false));
7 X Z7 w3 T0 I a5 J+ H7 O6 l4 y- p } catch (Exception e) {( \- _; V& Z0 h: z
System.err.println ("Exception stepRule: " + e.getMessage ());
* `+ N& x& m% l9 M }
$ [% G4 D( d' h1 Y0 `* S% A5 K; c# ?5 {8 w% w6 s: ^
try {% Q) R# [: O1 `# t
Heatbug proto = (Heatbug) heatbugList.get (0);
6 }) }. m: i, i4 y+ [( c Selector sel = 4 X' C4 Q* P3 t3 Z% \
new Selector (proto.getClass (), "heatbugStep", false);, [* Y1 g- l# g( {( T# w1 g. b, L
actionForEach =
) ~. }- h# `$ Q& R modelActions.createFActionForEachHomogeneous$call
8 j8 s) ?) I/ C2 I6 h; a5 D (heatbugList,
$ n; O* Y& O a7 ]& W3 n( I new FCallImpl (this, proto, sel,, `0 |4 _; l$ Y: T; V9 i# g
new FArgumentsImpl (this, sel)));! T+ I% e; k' E& R, ~
} catch (Exception e) {$ \. Z8 m- J x" w8 e& A
e.printStackTrace (System.err);$ r& E0 J% i1 \1 a% r1 T- X; j0 J
}, w f# X. |4 N4 `1 X
$ A2 M: x" ~+ I) v
syncUpdateOrder ();
; I6 E. b {3 T
/ [6 w$ c: D6 A0 g' e0 x" Y try {0 v" V. O4 F- o0 z: n
modelActions.createActionTo$message 3 F9 L# T( R) Z( U
(heat, new Selector (heat.getClass (), "updateLattice", false));
J% m( M$ T k% q( z2 R+ ~ } catch (Exception e) {
1 ?4 _8 ~! t6 |1 v: {" u+ G System.err.println("Exception updateLattice: " + e.getMessage ());, ]9 t0 q$ a4 ^2 J( I
}
5 C( C0 @( J: L. \
% Z+ n) b$ A# w+ f; o // Then we create a schedule that executes the9 y8 i/ ?6 t# F& r
// modelActions. modelActions is an ActionGroup, by itself it
* R6 Z5 t3 @# t# X // has no notion of time. In order to have it executed in
" C) n# F" k# E) b2 |% j // time, we create a Schedule that says to use the2 ]7 T7 n K9 K' ^9 s2 F; n1 R
// modelActions ActionGroup at particular times. This
7 U8 ~, w7 U G7 W) G, G3 C' J // schedule has a repeat interval of 1, it will loop every2 f9 x9 E5 T% u0 q4 i* A
// time step. The action is executed at time 0 relative to
5 H3 J. G% x$ X) p5 t // the beginning of the loop.% E0 ~! }) a" G$ ~
3 b: ^+ p5 {/ v; z6 F // This is a simple schedule, with only one action that is
# A0 ?% E4 A9 S) P! w // just repeated every time. See jmousetrap for more
& m4 l/ ~! P& s1 Y5 C // complicated schedules.# J# U* R9 u" q9 @
0 K/ p8 Q. z* } modelSchedule = new ScheduleImpl (getZone (), 1);; Y" ]. E! D- w% m8 Z, S
modelSchedule.at$createAction (0, modelActions);3 H D# x C/ x: l. M
8 r/ \4 M m, I% ] return this;
' z6 [# y1 g/ m/ G8 h } |