HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 K; b! ]7 _6 @) U& D3 ]) n
6 l. v6 a7 c2 Q0 ? J& A g/ W2 g
public Object buildActions () {
+ K+ l1 P2 k3 g2 L super.buildActions();) J3 h! Q+ q+ j1 l0 ^( x, v# c
7 \/ R# _# X3 S( l( Y4 a // Create the list of simulation actions. We put these in
3 F5 D6 W" H6 L) Q i // an action group, because we want these actions to be- s" C. u( `9 P# g6 K5 V
// executed in a specific order, but these steps should
6 V: l8 T3 r. l& g2 H% g // take no (simulated) time. The M(foo) means "The message
( E, I# Z& u& Z' O6 C5 j; { // called <foo>". You can send a message To a particular
4 O4 }, e# k" B3 g // object, or ForEach object in a collection.
" s p! j% u# k3 |( r! W, N, q3 E ( S" b2 H/ W: z) p4 D
// Note we update the heatspace in two phases: first run* U. Q* x e2 h& T
// diffusion, then run "updateWorld" to actually enact the
' x" [9 c( \, j1 Q/ ^. K // changes the heatbugs have made. The ordering here is
( q: ]7 z: y+ v9 O // significant!) t+ \+ _$ f0 p/ r+ s0 Q2 J: e
; q8 x" L% P+ ^) V
// Note also, that with the additional
0 x3 V, f. g, B$ F% x // `randomizeHeatbugUpdateOrder' Boolean flag we can% R I; q [! J7 E! s3 W: _: S
// randomize the order in which the bugs actually run# U0 A: A7 a- r" @, G! a
// their step rule. This has the effect of removing any
" G( J7 W$ Z- x- G/ r5 e) F ` // systematic bias in the iteration throught the heatbug1 F) ^$ N+ {% z2 ?( G
// list from timestep to timestep
* C# I! ~. A2 M( n9 h9 }3 N$ w8 L 0 n# O5 V- I7 C L
// By default, all `createActionForEach' modelActions have+ W( B. i% u6 ^& y( J! O
// a default order of `Sequential', which means that the
; Y/ t/ y8 W) ^/ a; z // order of iteration through the `heatbugList' will be
1 B; a- R! k& _( a$ Y; P // identical (assuming the list order is not changed: W+ e4 V. b5 u* V4 G" X
// indirectly by some other process).
' i9 q3 j8 w; _% G I3 J 2 u& S# O& a: d8 M R4 `0 f y
modelActions = new ActionGroupImpl (getZone ());
; P7 ]+ ^( r' a1 y- f. }9 g
7 U% E: m. J7 i' G& S try {# ?! e; n% A# g( {# O3 y# S3 h7 D/ N
modelActions.createActionTo$message
, l. Z- C% m* L6 I6 f (heat, new Selector (heat.getClass (), "stepRule", false));" s! _9 ?5 s, F" f; ]' n
} catch (Exception e) {! Z q8 V p. {/ X" i" G- M; W
System.err.println ("Exception stepRule: " + e.getMessage ());
% e1 ?+ ~" f) H3 V$ p7 G% a: q3 T }' _% [( _, B0 [
% ~/ k9 h9 A; Z# a: x. P try {# n$ _4 n0 f. p" r
Heatbug proto = (Heatbug) heatbugList.get (0);* t4 T! M P3 v8 U
Selector sel = # l4 L2 B& f* T- `* A
new Selector (proto.getClass (), "heatbugStep", false);1 [' N2 l; g& k1 N
actionForEach =, O6 I" [/ e3 U8 m
modelActions.createFActionForEachHomogeneous$call
* ^7 `+ K& u- y! S( `# H/ p (heatbugList, k+ Q: ~9 M0 R
new FCallImpl (this, proto, sel,
/ |5 X! N- P% z: ]; x8 Y new FArgumentsImpl (this, sel)));
3 R* _, Y$ D& ` D; d. { } catch (Exception e) {; J5 }# l- W5 L% m
e.printStackTrace (System.err);
" r0 i$ A- y r8 ^ }# r0 {9 Q5 y9 w. E6 ]" W9 A6 E
v' b- B; R8 y3 n# d! ?
syncUpdateOrder ();$ y( w8 }4 L) D- m
# ?$ t% E; S/ [0 N5 Z; g* Y+ e try {
& N4 h; n; z8 O/ [8 _ modelActions.createActionTo$message # F+ R; m, ~. T a, F
(heat, new Selector (heat.getClass (), "updateLattice", false));/ p3 D3 M1 c- J
} catch (Exception e) {
- w# C' T1 h1 P4 i$ `+ \) A, n System.err.println("Exception updateLattice: " + e.getMessage ());
, K$ p) x8 v5 A9 D/ i }6 v! x# Y- Q9 J9 G$ l. Z: n
" B1 G z4 h7 w3 }
// Then we create a schedule that executes the6 _5 ^, K0 u0 x3 s) g2 F S- t0 j
// modelActions. modelActions is an ActionGroup, by itself it
/ q4 k" b: Z/ g" ` // has no notion of time. In order to have it executed in& F) ~. V! J/ |- v+ I, b1 s& O0 z s
// time, we create a Schedule that says to use the
& s; e4 r+ ^( [7 Z: l: C // modelActions ActionGroup at particular times. This
$ W% n! z K# C; ~* X; G. ? // schedule has a repeat interval of 1, it will loop every2 ?. p! I3 u% G0 }' e1 \
// time step. The action is executed at time 0 relative to
/ O7 J% O# x7 y ?1 Q // the beginning of the loop.5 w& q; B, z' l8 `( S
& W1 i' f* z5 @$ f1 @2 I. i" D // This is a simple schedule, with only one action that is5 p, h* k8 b9 y3 M; Y! H% p
// just repeated every time. See jmousetrap for more
, }3 F' O6 y! \) n# @9 e/ u // complicated schedules.
# y7 s1 u. x0 w% _. R6 T5 A i5 Y: t6 H5 A( z0 C0 p. e
modelSchedule = new ScheduleImpl (getZone (), 1);1 Y- ]6 K- ~9 a0 |( H
modelSchedule.at$createAction (0, modelActions);
; F( R! Q( w$ w9 S: w
% u0 q/ ]. S2 H& Q, H return this;
b, |8 r m5 e, `/ u$ q7 z } |