HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ X- _$ U; C7 k6 _/ o4 Z. @! L
public Object buildActions () {, J* a9 f& F, X
super.buildActions();' y7 b& p/ X8 H+ ~6 C. s
- m- Y0 Q0 G+ G3 H
// Create the list of simulation actions. We put these in
3 [% K- H2 M$ Z/ ~' v7 `: E // an action group, because we want these actions to be
2 e- y( C7 j5 B5 I // executed in a specific order, but these steps should
8 f( b% a/ r' L1 H& V6 T! o // take no (simulated) time. The M(foo) means "The message
. ^! s; r9 @% @7 ~8 ? // called <foo>". You can send a message To a particular: o P4 Y6 O# @
// object, or ForEach object in a collection.: y3 o* w2 f4 m$ s$ S: M K
7 d2 G9 h: S9 T6 ?. y // Note we update the heatspace in two phases: first run
8 x! i. }+ C# ?& V8 b // diffusion, then run "updateWorld" to actually enact the
8 u5 ^/ ]. N0 u+ q7 s3 N5 b p5 { // changes the heatbugs have made. The ordering here is- @" a: l1 G0 z1 N0 Q
// significant!
" |& \3 Y _% a* _3 h
: z0 x( l! J" `6 j% Y; X: e( | // Note also, that with the additional
3 K; s1 ?: t* O! ] // `randomizeHeatbugUpdateOrder' Boolean flag we can) g) c2 X- S4 U) `# }1 @) R
// randomize the order in which the bugs actually run
% r: x8 C3 a6 V3 I1 Z // their step rule. This has the effect of removing any. |8 D" n, e* b2 e# H& Y* ]: m6 ^: b
// systematic bias in the iteration throught the heatbug" M s3 V2 S$ _2 I
// list from timestep to timestep
% x5 w; x; O: @7 S3 {4 m- {" I( V. R 5 ?5 V: R r' j! d' K
// By default, all `createActionForEach' modelActions have
( t- D% k( A3 o& c+ M // a default order of `Sequential', which means that the- q0 X& U% Z/ `7 b
// order of iteration through the `heatbugList' will be
0 L+ K" C5 |: ?/ I // identical (assuming the list order is not changed4 z$ [8 \$ b6 V! S( V( p. }3 f1 X
// indirectly by some other process).- W7 j: J k; m, ?# x
$ z& T! R8 N, V. V s, w: {
modelActions = new ActionGroupImpl (getZone ());
; y5 X @0 J4 S! l% M
3 t* B( {" W. w try {- g' k$ H: ?3 n! Y b1 t5 b0 V
modelActions.createActionTo$message2 _' l( J1 c5 K8 j; Z$ N+ e
(heat, new Selector (heat.getClass (), "stepRule", false));5 S5 ?0 o% `5 q0 o6 `$ k, S: C
} catch (Exception e) {0 `) q. Y2 w9 F; H+ M9 ^
System.err.println ("Exception stepRule: " + e.getMessage ());, o4 d, G+ q) e
}8 J4 v+ g, U) W$ X1 F" f
; `( ~. Z# j! u" H' r/ Z
try {6 e2 u `2 Y1 j1 u
Heatbug proto = (Heatbug) heatbugList.get (0);0 p. K% C) M9 W7 V$ A4 H( c
Selector sel = ! k8 O9 O$ @* K
new Selector (proto.getClass (), "heatbugStep", false);
1 {4 S/ q( B# B( I: l( g actionForEach =
0 U' k% U6 K4 H- B1 R8 D3 w, \ modelActions.createFActionForEachHomogeneous$call
* w2 w0 Z8 a% ^9 e5 Q! O& x+ A (heatbugList,7 Z8 M' X) y( f5 A
new FCallImpl (this, proto, sel,6 `) [* @6 M- [* G4 \; g
new FArgumentsImpl (this, sel))); e3 _- v3 C; l
} catch (Exception e) {
* w+ p, p9 e! V2 g* k e.printStackTrace (System.err);
5 U5 g/ O1 T) N& E }
5 z7 S4 ?9 j, l4 G6 n+ w
z% P7 ?& A5 R, U6 u syncUpdateOrder ();9 P% G* y8 |( Y' w
# g; A# y0 n: a" b* F4 _ K; y try {
& G' x0 q; ? M4 w modelActions.createActionTo$message
8 m: x( [* L* H- f. I$ p7 j4 j (heat, new Selector (heat.getClass (), "updateLattice", false));
. c, p/ d i1 z } catch (Exception e) {
7 T& O* S& Y% {7 E. \ J S System.err.println("Exception updateLattice: " + e.getMessage ());
8 w5 _! Z; s5 p! p& L }
7 n$ q; ~+ n( y$ F; [0 d" x; K
# A; ~8 v8 y4 f% d1 Q2 a _ // Then we create a schedule that executes the1 s/ M& I: {5 O& Q a* _
// modelActions. modelActions is an ActionGroup, by itself it3 V$ w7 J% Y' |
// has no notion of time. In order to have it executed in
( Y. a" f) j3 w& x // time, we create a Schedule that says to use the: B7 _0 _' u( z" G5 B
// modelActions ActionGroup at particular times. This
6 B' E R) I* _5 p* z. n; p9 m$ {6 C8 \/ D // schedule has a repeat interval of 1, it will loop every/ Y4 ~8 r! r0 I5 q4 m9 n0 ^% x
// time step. The action is executed at time 0 relative to
& m, o2 x8 A- j6 a6 m: f0 Q // the beginning of the loop.4 P* R- S5 j) m Q- N
0 t& G6 I% a6 f$ X
// This is a simple schedule, with only one action that is& T% x/ j9 ], C2 _2 N1 L3 _( m+ V
// just repeated every time. See jmousetrap for more) _: H! t0 E+ Z0 d! m! U
// complicated schedules.
; P6 j9 [& M. J: ?8 _* [0 \ 7 H$ V% @8 {. P! x) N, c
modelSchedule = new ScheduleImpl (getZone (), 1); i, c2 b2 I6 t! M a
modelSchedule.at$createAction (0, modelActions);
& u1 {$ l% w3 j f# p7 X
) K! C/ x7 Y" `% x return this;6 Z4 R2 J( c# E
} |