HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. h4 F+ p- R2 G4 {! o4 k$ k; s4 z
' V& g1 {$ a& l+ Z public Object buildActions () {
4 n# J6 M+ y. X6 s6 |4 n super.buildActions();! b+ m7 W% M# r; C" B" C
$ Z1 H: r N8 p' a7 W [* Z // Create the list of simulation actions. We put these in& V0 D2 H2 b/ ^+ v0 u
// an action group, because we want these actions to be3 ?% x8 U" d: g8 a! B5 a, E
// executed in a specific order, but these steps should
. ~$ |& j8 ^( C // take no (simulated) time. The M(foo) means "The message
8 s& p. i8 c7 {7 y V // called <foo>". You can send a message To a particular
. F1 j9 e/ B: B1 h // object, or ForEach object in a collection.9 R! P2 F% ^6 `" n( o7 z, h7 L
. ~+ w& c6 J; f
// Note we update the heatspace in two phases: first run; g3 {' {% y0 E; ^
// diffusion, then run "updateWorld" to actually enact the
' w3 Y9 Z' j3 S3 ?3 ~ // changes the heatbugs have made. The ordering here is5 m3 o3 R0 z& i I
// significant!) \9 f+ ~' C* X. e
, a& z( b# D) ?0 v1 H9 w$ } // Note also, that with the additional4 O( e- H5 |0 a6 j+ h. S' s
// `randomizeHeatbugUpdateOrder' Boolean flag we can* x; i: R+ i3 G& k* w1 n! R
// randomize the order in which the bugs actually run4 W$ u( X: t# k# u1 [4 {3 g
// their step rule. This has the effect of removing any, h/ n, k6 u7 G- j8 _' @3 I
// systematic bias in the iteration throught the heatbug
1 n7 B/ D7 a+ H // list from timestep to timestep3 U# N9 C/ \, m' ]! I/ ]
5 T3 T7 I4 Y. J' f+ w F0 D
// By default, all `createActionForEach' modelActions have
4 `$ @0 Y8 i$ I* f2 I // a default order of `Sequential', which means that the
+ D3 Y+ [7 G9 c: n2 k M3 D& ~ // order of iteration through the `heatbugList' will be- L6 G( A3 q1 M" b8 I# a- j/ w
// identical (assuming the list order is not changed2 ~& c" K9 s3 j/ |$ M$ _
// indirectly by some other process).
" v { m! t3 E6 {. H. X: B4 T 9 n, j8 U* @# U. [( B6 G9 S1 [+ Y
modelActions = new ActionGroupImpl (getZone ());1 l/ _! s. @6 c# n( S
+ m9 i& `, L# |7 @( i" ^4 ~4 ]4 J/ ^
try {6 U. w7 d% I2 r
modelActions.createActionTo$message
+ X+ F E5 U+ J, ^1 y" V$ | (heat, new Selector (heat.getClass (), "stepRule", false));
/ ]+ S' L: g! V, o) q; e } catch (Exception e) {' I( w5 V" N8 Q+ W& w
System.err.println ("Exception stepRule: " + e.getMessage ());! R8 Q6 \+ q/ e' i! Y3 X
}
3 c/ u" r. M$ [% g) s3 X/ x+ k- \
& j" u" u6 y% ~+ `% J/ D- v8 X" i) N try {
% V# J' N G3 u+ r& I' F Heatbug proto = (Heatbug) heatbugList.get (0);
- Y- P) o9 q% p" Y! O% } Selector sel =
* w4 N# p1 _0 X y& Z new Selector (proto.getClass (), "heatbugStep", false);
/ p3 ~+ f0 u$ h- t" T. D$ N. f0 t actionForEach =& Z& }$ l1 k+ [6 y f% s
modelActions.createFActionForEachHomogeneous$call
" i. e- _ c0 G$ A6 _ (heatbugList,
- |/ ~1 O' I7 H+ K new FCallImpl (this, proto, sel,/ d; L+ o6 x' m3 E
new FArgumentsImpl (this, sel)));4 D" E7 V# Z r. r4 d% r; J, H
} catch (Exception e) {
' O% x/ @. E& s: _+ k6 q& [. x e.printStackTrace (System.err);
5 |2 ]. ?/ s/ z4 d" P9 a9 } }3 W& X! _2 }5 c: {9 J
# G \8 P) E1 G# x; A; `8 F syncUpdateOrder ();6 @( f/ |( E! Z1 v
1 Z3 w# w q. `
try {6 Z/ p) Q5 Z" h3 A
modelActions.createActionTo$message 4 b U2 [3 g; W \
(heat, new Selector (heat.getClass (), "updateLattice", false));# D- m5 s5 L# b& D7 N8 w0 e
} catch (Exception e) {6 p" N$ F; g3 P2 h) q' u
System.err.println("Exception updateLattice: " + e.getMessage ());* |$ A$ g" V9 b% l1 ?4 K! y
}
) i" A5 C- F4 e, s/ _ 1 ?- g+ B# Z; v, d! |: H$ I
// Then we create a schedule that executes the
* @7 s* ]6 l# m6 r // modelActions. modelActions is an ActionGroup, by itself it
s+ E8 D6 W. v4 }7 z! o$ A // has no notion of time. In order to have it executed in0 ?5 W! I; C: C, \6 K4 c
// time, we create a Schedule that says to use the
# q& L7 R. J4 k, W5 w2 j8 O // modelActions ActionGroup at particular times. This4 F" P' d: z. \+ d# q/ ]& @: g w
// schedule has a repeat interval of 1, it will loop every
8 O* J+ x% C7 e1 ~/ w o& t! \ // time step. The action is executed at time 0 relative to2 W6 @! g* \: F- n5 G f6 F: V
// the beginning of the loop.
) h% D0 e; @; W, D1 T
" Y* B9 S3 m* {& d7 _ // This is a simple schedule, with only one action that is! X/ }; _6 l" g' c5 Y
// just repeated every time. See jmousetrap for more# C, i$ ?* }, _! c, C0 `. s
// complicated schedules.' c+ M0 E+ e( Z1 `2 D
3 f0 w: Y) g+ N modelSchedule = new ScheduleImpl (getZone (), 1);
5 D: G0 v o3 ~' P% E# `* l. W6 R modelSchedule.at$createAction (0, modelActions);
! A4 z6 W/ ]: j4 Z 0 B3 \" \! L" F1 P& l8 G
return this;
7 \+ h5 I5 p$ o# e } |