HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 U# A6 j# M; m+ O! d! i& Y# q0 y
: [, V& h0 q* {/ Y0 ]( n/ d public Object buildActions () {% n+ n: h/ y2 j7 v, d( Q7 _- M
super.buildActions();
8 `/ U9 [+ |. J' H z$ L3 b8 _ / A9 G4 c2 G# }' b0 n. V
// Create the list of simulation actions. We put these in
$ @) H8 t, i( ]# \# K/ t$ W // an action group, because we want these actions to be% ^2 ~+ r9 ^& C, D; U3 f
// executed in a specific order, but these steps should
& }& ^4 I$ X' K9 p+ ]) i1 f // take no (simulated) time. The M(foo) means "The message( @5 o$ `3 O4 K, |: I
// called <foo>". You can send a message To a particular2 @/ q1 x+ x& h5 e# e
// object, or ForEach object in a collection.
2 ], R' @6 k. `
, H5 M- z6 z" f3 r5 X1 z( A // Note we update the heatspace in two phases: first run
# a) @' ]8 |3 k // diffusion, then run "updateWorld" to actually enact the
+ @# N3 }. h8 t2 q+ l8 [5 ^6 P3 ` // changes the heatbugs have made. The ordering here is% r1 c) b, `6 \# q+ E" y
// significant!+ M! f8 W, }" @
& P; j3 q; m5 b1 ]
// Note also, that with the additional- ^$ z7 T4 B+ v1 w
// `randomizeHeatbugUpdateOrder' Boolean flag we can& Z- k8 G6 @! {$ ^5 ^7 x
// randomize the order in which the bugs actually run% i; {# R, w5 M- m- w( [
// their step rule. This has the effect of removing any) T$ R! ]( B: V' F+ ?; C# k
// systematic bias in the iteration throught the heatbug9 c% }" ~, A# O- K: M
// list from timestep to timestep
0 T- p B2 l. Q9 V" L5 \7 v
3 F/ O; P% }( D# U // By default, all `createActionForEach' modelActions have
" T" ^4 G, q% L1 X$ m: V6 K, L. S2 Q // a default order of `Sequential', which means that the
! N5 R" w4 H. m7 C$ d9 { // order of iteration through the `heatbugList' will be
5 F: c: z$ i& y // identical (assuming the list order is not changed
7 T+ q7 ?9 q2 V: J% V7 R+ y3 u // indirectly by some other process). g1 N; V* }( ?" n/ ~" ^% l
: K4 w* [0 E2 d- T* N' M4 _2 P
modelActions = new ActionGroupImpl (getZone ());* A& T& L& |% j$ q8 }
: D' a( ?: H4 O try {, c$ S! E1 c5 r! h/ z; @$ y2 u6 @
modelActions.createActionTo$message
r8 c# D. }& f- c* A* n (heat, new Selector (heat.getClass (), "stepRule", false));
$ h2 w3 G% ?. z) v. G# E7 m } catch (Exception e) {
" {# t) f4 V" @% U( K1 h s% { System.err.println ("Exception stepRule: " + e.getMessage ());
5 X4 ^: p' f: Q b1 w& ? }5 h" s: ^# ?1 J* Q; T+ R
5 S* d' u4 q( e0 _0 y try {6 q" B9 ^9 L& t. I6 t+ o* ^. T6 B
Heatbug proto = (Heatbug) heatbugList.get (0);" a; Z$ Z1 u5 c( R/ D
Selector sel =
- W& g0 U' ~( v1 ]- H new Selector (proto.getClass (), "heatbugStep", false);0 T1 k6 ]' z7 ^4 @/ G
actionForEach =. O& J$ |* ^ B; O
modelActions.createFActionForEachHomogeneous$call
\' p0 p5 x+ ?/ p5 c (heatbugList," V3 F: R3 m# k" C
new FCallImpl (this, proto, sel,$ Q! J5 z; h- N3 E/ h2 x/ `
new FArgumentsImpl (this, sel)));
R% n9 B% h E0 t } catch (Exception e) {
" S2 {# A# ?3 D5 [) L. W3 a. U ?! x e.printStackTrace (System.err);2 B2 F! Z) R/ w7 w) [* A
}
9 _0 ]) _3 P+ L9 y, ]5 }0 o4 Y$ X
* Z; f# q/ }' x6 J0 B# F2 R7 x3 K( J syncUpdateOrder ();) I, ~4 }0 f0 R* z
0 U, m0 v, I% A7 v$ }
try {2 @" C$ O1 g8 h. V
modelActions.createActionTo$message
5 f2 m8 _) d, l/ F1 _ (heat, new Selector (heat.getClass (), "updateLattice", false));
( T, J+ G; D! R7 o } catch (Exception e) {3 Z$ ]5 n5 ~/ _; M
System.err.println("Exception updateLattice: " + e.getMessage ());; H; I# [, c5 ]
}' g, O3 ~1 T6 Z q4 I0 |
- N D5 O$ d' a' ~" D // Then we create a schedule that executes the6 T+ h/ N6 R7 g$ H' H
// modelActions. modelActions is an ActionGroup, by itself it/ Q0 `8 i9 o' y: b1 N
// has no notion of time. In order to have it executed in
' i* ]* k$ M! S" u // time, we create a Schedule that says to use the
4 {$ b% x9 V) u: X% w" e1 W // modelActions ActionGroup at particular times. This6 E+ ^% v' e* l4 y9 i; u$ u
// schedule has a repeat interval of 1, it will loop every' U# i0 w/ y" U' a7 B- J' F
// time step. The action is executed at time 0 relative to U% q1 D% V4 {3 ~
// the beginning of the loop.; M* {! ~. C* A6 b' Q. F$ A- I5 Y
7 M: e$ r0 K8 r O6 y U B // This is a simple schedule, with only one action that is1 u1 h* e6 ^& G8 O8 h/ }% Z; ]) F
// just repeated every time. See jmousetrap for more
( ~# W9 e2 k% W; |0 L // complicated schedules.5 }2 ^ k8 A" L) p
% {3 j5 @6 a& D0 s
modelSchedule = new ScheduleImpl (getZone (), 1);
8 f+ U* j8 s/ w! A; t9 N. a modelSchedule.at$createAction (0, modelActions);0 d2 j0 E" P5 W) N
, ?" t" F& a" L return this;
+ ]: O% z1 F. ], ` U } |