HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 m6 L( u8 H/ b$ X" ^" n0 X( M" y, {% O
public Object buildActions () {
- V8 p' L `! ^! V+ x4 u6 \3 B super.buildActions();
* J# h7 \6 I2 ]* L+ J8 M7 `! j 4 m" d8 R+ T0 _4 I8 z
// Create the list of simulation actions. We put these in
" l2 V' H2 b( P // an action group, because we want these actions to be: {1 Z1 ~7 i% Q' ]6 M& J ]: f" f
// executed in a specific order, but these steps should1 J" f: H7 w+ v8 B" `' S- u& `
// take no (simulated) time. The M(foo) means "The message
: {9 {* T/ [! o/ |- W // called <foo>". You can send a message To a particular
1 ]' H9 |0 D$ h3 F$ E. e // object, or ForEach object in a collection.
! t5 G& K* D8 J& J7 s- {
. U( r$ x3 H3 d& B3 a g // Note we update the heatspace in two phases: first run
2 ], r0 _5 X$ Z. r; f p# Z2 f9 m // diffusion, then run "updateWorld" to actually enact the% i4 {, q$ g: t
// changes the heatbugs have made. The ordering here is
5 c) {0 ~& ^& a1 T* l1 c // significant!
2 U1 r$ J/ u8 `) m) f! d8 n $ W; S1 S1 a. W8 K& c2 \
// Note also, that with the additional9 V2 l) H- X: P4 C7 o
// `randomizeHeatbugUpdateOrder' Boolean flag we can" B8 y5 K% i& {: O/ s
// randomize the order in which the bugs actually run; G; x/ w6 Z# d+ u2 W
// their step rule. This has the effect of removing any
, j: Q( }8 h) c; g // systematic bias in the iteration throught the heatbug
& \& X; B( p2 v# J; ~: T: _( b' V0 ~ // list from timestep to timestep2 Q" {1 b o/ e
3 O9 k7 B8 z7 ^( w9 Z% _1 [
// By default, all `createActionForEach' modelActions have
" m' p: x( {1 H8 X // a default order of `Sequential', which means that the w& g- ~* t b5 n
// order of iteration through the `heatbugList' will be B* j) C5 t. }2 p
// identical (assuming the list order is not changed+ L+ o0 r, P. w; R; P
// indirectly by some other process).
) B9 [7 s. M; h8 D4 \( ]0 [
+ ]2 W+ Q0 s! C% S, n+ E3 ~ modelActions = new ActionGroupImpl (getZone ());
4 Y/ y9 w; k. t8 [1 e! d0 P
. D8 n8 _2 H1 S; X try {
. J, w: s! E$ f1 m' x6 D( S modelActions.createActionTo$message
C, S5 P3 p l- i. x) } (heat, new Selector (heat.getClass (), "stepRule", false));
3 G. G' [+ H4 y6 `7 |% W+ J, { } catch (Exception e) {
) k. {. H. Z/ K3 q/ g# E System.err.println ("Exception stepRule: " + e.getMessage ());7 l, Y+ g, K% e; J% m' X# j
}
" |* n2 u3 G6 g" L, ?0 ?- ]5 y; i0 Q) E6 j
try {
0 t! k( k: K& s Heatbug proto = (Heatbug) heatbugList.get (0);' J |* c3 S0 i* V$ `* J* j) O8 m
Selector sel = + M7 s/ F- h7 T# u: }
new Selector (proto.getClass (), "heatbugStep", false);
: {( A( R B& p/ e, H/ B, H& [ actionForEach =
+ W) ]1 H: f2 t modelActions.createFActionForEachHomogeneous$call
b, D- q: R* H: @ (heatbugList,7 u& z% {) o( g1 g( i
new FCallImpl (this, proto, sel,: M0 C& X; ~0 n* U ^! w, c
new FArgumentsImpl (this, sel)));1 R) k9 Y/ E) q1 B) D+ _) z1 [3 q7 V- |
} catch (Exception e) {
$ |, |) Z4 L. y6 I, d e.printStackTrace (System.err);
1 L6 L! W- B" o& R0 q }1 n/ ^3 [: `/ O$ H. ~$ y/ f
. G. e% @7 M) q% A% P syncUpdateOrder ();7 X0 r" E5 S! @2 N9 D* Q$ }4 V
6 U9 o6 W w( a% ]; \7 H( M/ Q try {
" u; E6 E# [6 |3 H6 n( R0 { modelActions.createActionTo$message & i7 [* S+ X/ a" [2 {' b. \
(heat, new Selector (heat.getClass (), "updateLattice", false));7 P; w: C2 H4 V/ \$ s$ Q& f8 W0 G1 T
} catch (Exception e) {
( J: a$ ]8 c& |2 a( X System.err.println("Exception updateLattice: " + e.getMessage ()); s6 `4 Y5 e3 h
}
0 g' W' e. Q' L$ W , t6 F! i/ ^& _9 w( z+ |
// Then we create a schedule that executes the
r5 l5 d5 K5 V$ U' a# W // modelActions. modelActions is an ActionGroup, by itself it
4 g9 H3 P- }& Z ?/ w2 s9 T e+ S1 a // has no notion of time. In order to have it executed in
5 ^$ w9 x4 V9 [2 d0 e C4 n2 | // time, we create a Schedule that says to use the
) o/ n) u# u1 e4 \, F' E // modelActions ActionGroup at particular times. This
; S5 C) R5 R& u6 \3 |5 R" g7 Q // schedule has a repeat interval of 1, it will loop every' O2 C. Y/ j' G% \% K5 U* h7 [% x( y
// time step. The action is executed at time 0 relative to2 F, W, d) w5 F( n
// the beginning of the loop.* O3 `: Z" r7 N
9 N" p% d6 M0 w6 s
// This is a simple schedule, with only one action that is# M8 \7 t8 G5 q7 }$ f/ R( n% i1 R
// just repeated every time. See jmousetrap for more
" p: N& N* M, I4 U6 R n/ ] }& Q6 n // complicated schedules.
9 x* _. T4 c( q $ N! d3 L7 ?! d5 t: i3 C* [$ o
modelSchedule = new ScheduleImpl (getZone (), 1);" F0 z0 J5 L+ ]+ C. g. i. P
modelSchedule.at$createAction (0, modelActions);
$ C- j, o! Z" ^1 Q+ @3 u7 ]2 H6 D
- ]5 f% K0 m. m0 b" D6 W return this;
! r% [* M; h; P3 q } |