HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* U5 r7 @$ i" @
# e% c/ O7 [9 c public Object buildActions () {
. x8 G+ N; r9 P5 q; o super.buildActions();
$ H3 Q6 Q, Z8 m3 m: E5 y . r# e! Y& R. d
// Create the list of simulation actions. We put these in
% E5 i0 P* o: e4 r0 B- G // an action group, because we want these actions to be9 H4 O* o" p( r! b% q
// executed in a specific order, but these steps should
0 x P3 k$ {& [! g // take no (simulated) time. The M(foo) means "The message
i( Z% r; f9 O8 S' g0 n // called <foo>". You can send a message To a particular8 E5 T! @) h5 b; g
// object, or ForEach object in a collection.
& F4 I, w F; e3 G( {4 V
$ p! M$ d9 o* ^0 J% O // Note we update the heatspace in two phases: first run
+ ^: V9 a Z! B5 G9 t% K3 d- W // diffusion, then run "updateWorld" to actually enact the5 W9 X6 c! U: V0 s$ P
// changes the heatbugs have made. The ordering here is
4 `' Y$ |& j9 w& v+ d1 } // significant!1 I( D2 c# q1 _3 ?/ R
9 P* S# S& Z F0 i t9 R
// Note also, that with the additional9 n' r8 m, L! c& W' i$ t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% ]* \2 f' L+ i8 N' ^4 v // randomize the order in which the bugs actually run
2 {" v3 `- J9 Y/ E // their step rule. This has the effect of removing any4 ], t4 }) l! \. \1 e
// systematic bias in the iteration throught the heatbug6 E3 U- F) L. I& \( D. H0 Y3 ^
// list from timestep to timestep
7 G) k& T% K o2 T( t# m' g& C
' r+ ?+ N' {8 w: z) a+ e/ o0 K& j // By default, all `createActionForEach' modelActions have- b* e9 `8 f% f3 n5 }
// a default order of `Sequential', which means that the) P, Y& A0 N+ R! c8 ^; [! L+ D- v1 w
// order of iteration through the `heatbugList' will be
' {8 D- ^# s3 M) G. q1 b // identical (assuming the list order is not changed- @/ {$ q0 k, m/ v; M# y
// indirectly by some other process).* d, Z3 w8 K* c- L, \5 c& t
" W- z) k3 G* f, t8 o modelActions = new ActionGroupImpl (getZone ());
5 A; L7 a9 Q' T" @0 l' X& t
' K3 `+ g" a( [' O! J try {0 Z9 V7 y. e! Q$ }: B2 B( Z) y9 D
modelActions.createActionTo$message. x5 H( h$ M6 S! E$ L
(heat, new Selector (heat.getClass (), "stepRule", false));
# r! @* V. W; }1 m' v7 Z' y: z+ @ } catch (Exception e) {
2 g4 x8 |( R& X1 M- f System.err.println ("Exception stepRule: " + e.getMessage ());
8 x) I' n: I7 m- T: U }* b) w0 ~( H3 I n' N6 s
0 @" P3 E- _/ ? ` try {
1 e5 i5 x/ \, N Heatbug proto = (Heatbug) heatbugList.get (0);0 o3 L) f9 b& I( D
Selector sel = 3 W, z# y2 M) q# U& A5 o
new Selector (proto.getClass (), "heatbugStep", false);
6 d) }0 ~2 f7 B- W% d' v: I) k actionForEach =
K" i: A& X4 A1 H modelActions.createFActionForEachHomogeneous$call( q6 {; P w' T6 J9 [. s
(heatbugList, H \, _' n n4 ]) p8 F( ^6 |; }# A
new FCallImpl (this, proto, sel,. x8 ~+ _' T4 o i& \/ O8 @
new FArgumentsImpl (this, sel)));; ]0 @ @: {2 a/ \/ ]3 L6 ~
} catch (Exception e) {
2 }2 E& i( r, P0 a8 S8 l e.printStackTrace (System.err);3 g6 ?2 h* `4 F5 G4 S
}, X- [$ j4 T3 {
' v( k% ~( ~: Z+ g syncUpdateOrder ();9 P, u! s0 k0 x9 T9 N
9 s r0 V( N" Y( e
try {& C% w- c5 i8 G
modelActions.createActionTo$message
V q( B5 ?8 ~5 e6 h2 r& n( P2 f (heat, new Selector (heat.getClass (), "updateLattice", false));
$ p+ }6 N) K6 }+ i% s! r } catch (Exception e) {
/ @' e2 |2 N- P- y1 b System.err.println("Exception updateLattice: " + e.getMessage ());' w4 V2 A6 N( ?0 _! c4 ~
}/ ~* L: v! Y- h, X' Z& f) d3 Q
! P8 X, p5 f# J1 w- e
// Then we create a schedule that executes the/ B* V6 r# I$ P! d% I8 O1 {
// modelActions. modelActions is an ActionGroup, by itself it5 y' Q- s/ F' W% G
// has no notion of time. In order to have it executed in. m0 c, \9 w0 F4 H$ A
// time, we create a Schedule that says to use the
) }8 y# b2 ^3 l" q3 c+ v& ]" \ // modelActions ActionGroup at particular times. This
$ j1 ], {0 E( B7 U4 ?" ^9 d // schedule has a repeat interval of 1, it will loop every0 @, n- J* @+ w0 M9 k
// time step. The action is executed at time 0 relative to
& [. s3 E( f! A // the beginning of the loop.
P( j4 K+ e7 O5 K
% c* J; G7 I: G* t' v9 { // This is a simple schedule, with only one action that is) e. a' D" \$ v" l) O
// just repeated every time. See jmousetrap for more0 W$ E( }: `. \# H
// complicated schedules.
; S( n D, u2 `6 H
1 C! M4 |$ u: _! c& y p+ @" U modelSchedule = new ScheduleImpl (getZone (), 1);
6 S" Q( N% m5 @% N modelSchedule.at$createAction (0, modelActions);# |3 h0 N: h! \% m) b$ A1 t
& B* S- z( _& A+ \+ p, e return this;
0 z7 }* R: o; j; a( @" Y } |