HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- ]' R8 K$ y. {2 d b/ i( n7 c+ `4 G" T) | x8 W1 Q
public Object buildActions () {9 o8 y) d3 Z3 D
super.buildActions();
: b2 y5 x' w0 S- C
( v+ [4 D) ?6 _* K // Create the list of simulation actions. We put these in
8 T0 A- \( H9 Z: C3 K' Q! }5 _ // an action group, because we want these actions to be! P _# a; `6 G# {3 x
// executed in a specific order, but these steps should
8 E9 T! F$ i- ?& Q8 A) l // take no (simulated) time. The M(foo) means "The message* }% _" I. x( _# s
// called <foo>". You can send a message To a particular
4 M3 g o6 Z+ x: L // object, or ForEach object in a collection.
1 Q1 Y2 H& ~! c0 Q/ f & F3 T) ?' j' ~4 H* ]5 ]2 b) S8 s
// Note we update the heatspace in two phases: first run
1 b7 i `1 f8 g" X; B: F" e // diffusion, then run "updateWorld" to actually enact the
5 V% I& P. c2 [3 t- T5 B# ?$ F9 M // changes the heatbugs have made. The ordering here is
. M# L; F" g+ a) y1 ~7 `) U // significant!
8 U5 H( j! S* I1 S- S% z, v6 J: M 3 {" R6 g' r+ E9 `1 y* v' v* K
// Note also, that with the additional
+ `: C5 O& \7 k& y/ w# H // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 {1 S/ f5 p- f9 p: [; k+ H. e G // randomize the order in which the bugs actually run
) S0 Z* n s: j% T1 e+ F$ x // their step rule. This has the effect of removing any# a4 L5 r$ l5 P$ E
// systematic bias in the iteration throught the heatbug: ?6 a, ^0 v" a- ?3 ]
// list from timestep to timestep
7 V' E C- p# Y# C2 P 6 d' F+ w% q z$ |5 T
// By default, all `createActionForEach' modelActions have+ B2 D. Y' J' Z' `* G
// a default order of `Sequential', which means that the1 S, Y3 n; B4 k9 j1 E
// order of iteration through the `heatbugList' will be, Q7 D" {5 L8 K1 t+ d: K& o
// identical (assuming the list order is not changed
- A1 C3 P& |/ n; k% T0 M2 t- E4 l // indirectly by some other process).
( i( B5 x4 @* I- T
8 U3 E* q0 t* N$ e0 i6 ?* r modelActions = new ActionGroupImpl (getZone ());
0 n3 M+ A6 Q2 |: I( }8 g2 t6 M, `. r& e0 q; F
try {5 b `% l5 u, ~: b4 L
modelActions.createActionTo$message
5 R0 q# Y% @1 H) ]1 [: b (heat, new Selector (heat.getClass (), "stepRule", false));
9 G$ Q5 A" p8 y6 D; B } catch (Exception e) {
0 ?" y( n* T" [' P' s System.err.println ("Exception stepRule: " + e.getMessage ());
! R: O' C% O- j2 }2 H* Y: ^ }
4 m9 h% E' {4 v6 a/ S3 a. C2 v2 d9 h& k. {* {' Z& C
try {
5 m0 Z* S0 m3 M Heatbug proto = (Heatbug) heatbugList.get (0);
; F, q, o o1 T7 ? Selector sel =
i1 M: A: T; t new Selector (proto.getClass (), "heatbugStep", false);
2 }: R; ~- C+ u( O2 H+ Y' T actionForEach =- J: t0 @2 c# c) j& \: y
modelActions.createFActionForEachHomogeneous$call8 m! D3 e6 N8 y
(heatbugList,( p, A* V7 n1 S6 X
new FCallImpl (this, proto, sel,/ G1 p/ i" h- X) u' I( |/ `
new FArgumentsImpl (this, sel)));2 i+ v$ k, }5 n$ ?) m1 v8 \! G3 W
} catch (Exception e) {
) d* K; A; w# C% V- U7 O e.printStackTrace (System.err);
& U' i. y+ N. q9 I }9 \9 h* h6 ~+ R0 m6 q/ @. O( n. o
. c" O: t- Y2 q/ z2 q o syncUpdateOrder ();
! h+ C! B/ Y `9 H) P5 j1 {! K
0 W# e( `& j7 w try {
4 V: q+ z" V6 Z* @ modelActions.createActionTo$message $ _+ Z( ]* E9 V) _
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 [, _5 W. G# j% K1 _ } catch (Exception e) {: r- Z8 C/ G9 x1 O4 @$ `
System.err.println("Exception updateLattice: " + e.getMessage ());
( R4 L4 H2 m6 s6 | }
* X' h6 \" Z# C, ?# `% T6 G }: ^
% Z# K+ z* Q5 j' F7 U // Then we create a schedule that executes the
5 w* t) Z3 w4 f" n$ e" q$ I // modelActions. modelActions is an ActionGroup, by itself it
6 T6 E5 @& _" j8 B5 A // has no notion of time. In order to have it executed in
" R, n. R5 h$ n: A6 ~ // time, we create a Schedule that says to use the# r! A/ E& r) M, u
// modelActions ActionGroup at particular times. This! r& g( n/ K( \7 H/ e
// schedule has a repeat interval of 1, it will loop every
, ?) E' E7 ~% e0 ~7 K // time step. The action is executed at time 0 relative to* M- y3 e5 o, g" e
// the beginning of the loop.8 r& S- _- V" @6 `8 ?
% X0 ^! Y$ P9 [' R! O // This is a simple schedule, with only one action that is5 U+ k! ?) J% z
// just repeated every time. See jmousetrap for more* X# c* i/ s. z* T! _ z
// complicated schedules.7 Q4 ~# i; s- r$ J( |5 e' u z
3 O( k2 S6 H" f
modelSchedule = new ScheduleImpl (getZone (), 1);2 v/ M: Q7 O8 H& L8 j& Q7 h5 T) Y
modelSchedule.at$createAction (0, modelActions);
: F' X6 l4 H+ n, G4 K8 l, Y 0 _* I+ h/ P9 v# D1 V8 [9 _
return this;
$ L/ o4 l; c8 r, L2 V- q9 |7 A } |