HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# b' h8 k: @& r! u2 a2 |% ^6 N3 K
public Object buildActions () {
+ h. E& c2 X, J2 ]! w' i" m super.buildActions();( W3 a& B4 f$ h2 m/ [+ r/ Z
( q/ Q( l+ }% ^* P9 A' k // Create the list of simulation actions. We put these in) q- c+ [* n; Q# X- P5 h, W
// an action group, because we want these actions to be
5 l+ f- o( ~" o1 a B. F% W+ W // executed in a specific order, but these steps should
7 T ]3 K c4 N4 ] // take no (simulated) time. The M(foo) means "The message& h6 Q4 ^9 t9 R
// called <foo>". You can send a message To a particular
0 M0 H i1 D0 g' h // object, or ForEach object in a collection.
' R/ q" L! z; n e- D- d& | 6 |9 U! Y* {: y4 h
// Note we update the heatspace in two phases: first run* v* ]! u8 e/ R4 ^% \! V% U
// diffusion, then run "updateWorld" to actually enact the Q% F4 f2 P0 H8 l
// changes the heatbugs have made. The ordering here is2 w! E. D- Q$ e
// significant!
: R* k9 z; m' z 6 p& N: M* |- w5 f
// Note also, that with the additional
% h1 v2 D4 ^( w' O/ D' F // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ @8 _7 g# f. c. i( q4 t // randomize the order in which the bugs actually run
) i4 S6 j8 R p6 n& n3 l // their step rule. This has the effect of removing any
# w6 U6 U/ r* o+ [- m // systematic bias in the iteration throught the heatbug: ?! h( e2 p) r( ^3 r/ E2 d2 o
// list from timestep to timestep
5 f! n' s2 w" }& c8 }3 R, d; X" q
) [& ^. ~7 h" z: Y // By default, all `createActionForEach' modelActions have
2 Y2 [" _( I) c // a default order of `Sequential', which means that the
" w( E1 } V" R2 `" } // order of iteration through the `heatbugList' will be
) w+ f7 M. X# p7 X( e // identical (assuming the list order is not changed
6 |7 u( H, |6 c9 { // indirectly by some other process).
, o' i" I# b* e4 j/ n / o2 Q3 }' `5 W* i* Q! ~
modelActions = new ActionGroupImpl (getZone ());% Z, v) i1 D2 T) j' d4 _! [; R
/ e4 r& \, l+ I" Y) y try {
0 I& w( K) Y5 t0 D; _ s modelActions.createActionTo$message
* M' y( P1 f$ c4 o+ S$ x (heat, new Selector (heat.getClass (), "stepRule", false));
) k2 Z1 n9 s, j } catch (Exception e) {6 ?4 h& C2 F. l; Q+ l
System.err.println ("Exception stepRule: " + e.getMessage ());
! C/ v% L0 \0 ~9 v* J }
5 i1 _: [, Q4 W6 E
( e: J# K! [ O* ^* j3 z" `% ? try {4 d/ }9 y1 X/ K3 s* R& i
Heatbug proto = (Heatbug) heatbugList.get (0);- P' `! z4 d% ~( J/ y
Selector sel = * ^: u+ _) q. S0 Z" Z: m
new Selector (proto.getClass (), "heatbugStep", false);8 x+ S: K/ |5 p, X& e. i" e
actionForEach =# G; A Z4 e; z% q" K7 L
modelActions.createFActionForEachHomogeneous$call# c# d" y, G3 W
(heatbugList,
, @. }/ M6 D. z4 v. V+ W8 ?0 \; E) y new FCallImpl (this, proto, sel,
* v% a% h3 }/ Y! W new FArgumentsImpl (this, sel)));
3 K) E9 i* E" \: L% J } catch (Exception e) {
) P% L3 ~/ O; b6 N) t e.printStackTrace (System.err);
8 N0 J$ A2 T! Y+ R. H }
8 ^1 k) ^" B7 M( W* T
- r" g& P. L7 Y4 @# t5 _3 T% I h syncUpdateOrder ();7 ~8 w1 K4 Q: V8 h
: @+ T' |) r6 d7 K2 ^ try {
! h3 G# U9 Y# N* ^' L4 u8 {+ z modelActions.createActionTo$message
% d3 E/ V* R0 a( U! Q (heat, new Selector (heat.getClass (), "updateLattice", false));
/ e" A4 h* [( @" F$ u0 ^* r; W } catch (Exception e) {
6 q- c) H, r0 ~/ M% F0 ^ System.err.println("Exception updateLattice: " + e.getMessage ());
, v! F! F( }; g# c; a }
) q9 p: O$ w0 t% a
; }" d" O. i6 U7 Q7 b. X% T // Then we create a schedule that executes the
2 L b; y: `& D5 _ // modelActions. modelActions is an ActionGroup, by itself it/ F0 s6 R5 d( a; [ l) \ c: f
// has no notion of time. In order to have it executed in
+ q( j( j* B X // time, we create a Schedule that says to use the1 [. _6 g' B* G) o8 a2 |
// modelActions ActionGroup at particular times. This- N% S2 l3 q' |1 \8 U( t# p& s7 w
// schedule has a repeat interval of 1, it will loop every
) s, I: y5 ]* G+ a+ e! K0 {9 @ // time step. The action is executed at time 0 relative to
1 M8 S8 ?! k) B/ I1 V5 c" } // the beginning of the loop.2 ^% K$ d d( A3 q& F2 E% j
$ z4 I1 b7 d! K. r7 `) d4 i // This is a simple schedule, with only one action that is
3 W _! j! f; K; s3 k9 \: J0 x // just repeated every time. See jmousetrap for more
4 Y2 I; p' B F+ ?! T" W: j // complicated schedules.
0 P% Y2 A5 h) a& G8 U
3 y7 x/ B7 z/ q$ U modelSchedule = new ScheduleImpl (getZone (), 1);
. G% Z, K% y5 h, I modelSchedule.at$createAction (0, modelActions);
$ O/ P4 R' e+ }8 `2 f " F' a8 E* c3 t0 k5 Z- a8 N% l1 H
return this;- p7 K7 [5 [, T' x: x( R- j! X
} |