HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 |3 \" {# G% K/ j# J& O
/ ]# f* @' b& ~
public Object buildActions () {
o5 U4 K5 D1 X super.buildActions();3 f( L% _5 `$ v# O$ `2 L
5 M, p* N; r5 c& m // Create the list of simulation actions. We put these in' }- `8 Z* {& Q. ^6 x P5 x
// an action group, because we want these actions to be! \+ x0 Z8 E" L6 l$ }6 L: d3 y
// executed in a specific order, but these steps should
* \! E6 M1 R3 w- ?& V9 @ // take no (simulated) time. The M(foo) means "The message
" T) n/ i4 ?$ Z7 A5 O/ x // called <foo>". You can send a message To a particular A3 a8 N4 X) c5 |
// object, or ForEach object in a collection.3 ^) E& H# u7 L
9 [9 n) R g1 }/ a2 |3 ]5 d' C
// Note we update the heatspace in two phases: first run
# h2 u- i N# L5 z3 o2 l b2 c8 F; q // diffusion, then run "updateWorld" to actually enact the7 O' c( x% X1 }* k6 \& I
// changes the heatbugs have made. The ordering here is' |$ ?7 u, g) H& q
// significant!
7 [$ e+ N4 ^& g+ o" |4 e$ X$ Y4 ~: y
8 u+ {: j: E/ b' e+ o i // Note also, that with the additional
7 q0 [' ?0 E" S; w7 e // `randomizeHeatbugUpdateOrder' Boolean flag we can
( D9 w i) t! B3 p! m // randomize the order in which the bugs actually run
, P# Q( e" J, X0 | A0 ?# k$ {3 \ // their step rule. This has the effect of removing any# \& a( F% P1 j
// systematic bias in the iteration throught the heatbug
! R& I( k+ Z, P: G // list from timestep to timestep$ U0 O* h( N: s( L- o2 j
; b. {' q1 g4 y6 F0 {6 Y // By default, all `createActionForEach' modelActions have
) z0 z2 _# |/ z // a default order of `Sequential', which means that the
- h8 j* x' D+ t/ J. O6 a( k2 k // order of iteration through the `heatbugList' will be
4 _5 X0 z% e g: T+ W* P8 c+ e) l5 p // identical (assuming the list order is not changed+ y; v' r! ]' W- I' E* I5 `
// indirectly by some other process).8 C( C w! s6 I" v9 i
0 ?% X; I5 [; a% Q; T& w) K modelActions = new ActionGroupImpl (getZone ());9 k8 A. I: u& p. L) b* F* O/ K4 H
: h/ k/ b1 f0 I/ d! ?
try {. P# R0 U" q) S* ?" x2 {+ `& ]( W9 @
modelActions.createActionTo$message* \! x c8 J. _' x5 `% `
(heat, new Selector (heat.getClass (), "stepRule", false));
7 k7 x! _ x0 L! h; ~; T } catch (Exception e) {
2 Y0 u1 h1 z: z System.err.println ("Exception stepRule: " + e.getMessage ());
7 t& P2 ^) ^) r' r) g/ U }/ b3 ~& ]! N7 S- L
4 O" F: v8 N. r$ g' S' c& E1 Z! c3 D
try {7 M, w3 z! V8 ?5 W$ o4 Q
Heatbug proto = (Heatbug) heatbugList.get (0);; ~1 h! H; q. ]
Selector sel =
% Q# S' p8 K" X" { new Selector (proto.getClass (), "heatbugStep", false);( `( Y) E3 G a+ Q, C( l- E+ Z
actionForEach =
& j% ~: U; }+ q6 G3 j2 u modelActions.createFActionForEachHomogeneous$call9 ]9 m( E. |$ _6 |2 |$ ` f: i& R
(heatbugList,
4 @ c G- `* b0 g new FCallImpl (this, proto, sel,8 @4 f3 h( L8 B6 b
new FArgumentsImpl (this, sel)));
{: q4 M; b. u9 v* ` } catch (Exception e) {
! Y6 @5 o: a" z1 h* S- H e.printStackTrace (System.err);5 v' V4 l( X5 ?+ H! _2 Z" p
}
7 h; Y) ~6 o2 X" g% d& z2 @6 T
: @8 m7 P0 k- o2 q8 V syncUpdateOrder ();
/ j) B( e) p4 H
8 i; T, r7 a) \+ U) m/ ~ try {
2 f8 ~' L- v5 }; F, C. A5 q" P5 n modelActions.createActionTo$message + n* W( A9 Q7 D9 }$ o' C
(heat, new Selector (heat.getClass (), "updateLattice", false));) m0 L+ g% B( w& g
} catch (Exception e) {
i5 j; ^% {0 L3 V0 |0 P6 Z System.err.println("Exception updateLattice: " + e.getMessage ());$ J/ t9 d3 ^( Q, e. C
}% A! D2 D3 Q: m" b+ z# Y, ]
1 }1 Z5 u& H1 t) m // Then we create a schedule that executes the
4 r; n& @# ~( G/ v( u/ U // modelActions. modelActions is an ActionGroup, by itself it6 H, U9 h8 n4 }. h9 N; `
// has no notion of time. In order to have it executed in, K5 D* D. C1 Q- z
// time, we create a Schedule that says to use the, q: j7 |- @* _% c. b
// modelActions ActionGroup at particular times. This
& q! ~% W7 U e/ `6 g // schedule has a repeat interval of 1, it will loop every
, a7 o M; L8 G& P6 b3 o // time step. The action is executed at time 0 relative to
) A; V( n/ Q5 Q( X // the beginning of the loop.% M" E U6 U* y$ {
6 ~6 y6 c% ]6 ^) c4 y! Y
// This is a simple schedule, with only one action that is
0 m& }: z2 T" [. D3 Z* g // just repeated every time. See jmousetrap for more5 b$ y0 _1 ?$ q4 i
// complicated schedules.
0 J" C" ]; W% p) X9 R8 [4 B 0 Q# n( f0 U9 ` Y7 k
modelSchedule = new ScheduleImpl (getZone (), 1);
4 S, \9 G2 m! O- j& G' C5 ] H& M modelSchedule.at$createAction (0, modelActions); B6 C E3 @. U. N% i0 `: A
+ {2 O& |9 g/ i- t9 J return this;, M6 z& }# X4 G! B$ Q
} |