HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* ?! M2 q. Y8 U
% g0 I0 I" _9 D4 r3 ^5 H public Object buildActions () {3 v) u- T9 T! d8 N! T
super.buildActions();& Q& J4 o7 a% L+ R3 l' r, F5 ^
7 H7 E5 K6 x3 G! m. p h
// Create the list of simulation actions. We put these in
: y' d- K0 w" q& c; X) q // an action group, because we want these actions to be' X9 W; S' k* Q/ p3 H3 v6 L' a
// executed in a specific order, but these steps should
+ g0 M$ G. }7 W( ]! x // take no (simulated) time. The M(foo) means "The message
; Y3 o& A' [4 ~* f0 h // called <foo>". You can send a message To a particular
+ }* t0 I$ N) z7 D* ` // object, or ForEach object in a collection.7 l( I& x0 N0 M- i2 c. F
; Z5 U, x' G9 n // Note we update the heatspace in two phases: first run
8 l! D2 E7 k a8 @$ x& |% S5 l // diffusion, then run "updateWorld" to actually enact the
9 s, _+ N9 u4 u# ^* q9 }9 k: K" S ~, | // changes the heatbugs have made. The ordering here is
% t9 U4 P8 j8 ^ // significant!' ?/ P$ }9 \+ D4 q5 D( W( R
$ L+ }" x4 I9 W% F // Note also, that with the additional
( r- b# r( j( k // `randomizeHeatbugUpdateOrder' Boolean flag we can- x5 R# J0 S' S1 L2 T9 v8 X! T
// randomize the order in which the bugs actually run
0 W& m+ `) l) I0 ?; _ // their step rule. This has the effect of removing any2 P2 e( X+ s- X1 B7 s4 d3 M. n
// systematic bias in the iteration throught the heatbug+ d; c; x0 J8 u! L' W- Z
// list from timestep to timestep; I$ ?$ K4 y j' _
+ s8 e* S/ l2 c/ A' F // By default, all `createActionForEach' modelActions have4 B4 R* V* Y" ~6 N5 m( z( w
// a default order of `Sequential', which means that the8 W+ ], `; u3 v* |
// order of iteration through the `heatbugList' will be
$ N m% X. Q3 @- B$ `6 J // identical (assuming the list order is not changed
2 R, s# V3 H1 G1 Z. | // indirectly by some other process).
$ V5 L) v2 a2 n+ u, S h% y
0 O! p' a K w3 y" S modelActions = new ActionGroupImpl (getZone ());6 K& l. T$ z9 Y- m* s6 k
! ]4 \& L& r: j, c! K m4 @ try {: ?% X1 d" q$ e5 U2 F; D8 a: `& v
modelActions.createActionTo$message R- |) N7 _" G6 x
(heat, new Selector (heat.getClass (), "stepRule", false));. v" V% _2 f1 S+ m
} catch (Exception e) {4 \6 U j" v% K; U& j# x6 W* |( c
System.err.println ("Exception stepRule: " + e.getMessage ());
- @ b, G" L9 C$ w3 V }
2 E0 M6 h3 `- G3 W q% n! O7 I5 R/ ]7 M( V1 ?/ C* P5 Q' C
try {5 l+ f! _: ~7 V; \' Z
Heatbug proto = (Heatbug) heatbugList.get (0);+ {' ~6 x7 b- [) c2 g
Selector sel =
0 H8 X: Q- U7 n; Q, U1 U new Selector (proto.getClass (), "heatbugStep", false);# k" ]4 M$ ?0 v' I9 O& t
actionForEach =
) O6 d3 G2 P: [& ^0 F+ c modelActions.createFActionForEachHomogeneous$call
/ K; [- a5 A+ z. z. N (heatbugList,
6 |7 j) ]& V, b, U( C: j' d) F new FCallImpl (this, proto, sel,1 _5 V7 i* x P3 Q9 X- A
new FArgumentsImpl (this, sel)));
9 |7 `% c8 _5 p } catch (Exception e) {( l0 a( \" z7 J. \5 I
e.printStackTrace (System.err);$ G' z5 ^+ G, O& I# f
}! o$ Y$ I$ |. |5 `8 u8 |
4 B' F9 O1 G' b6 A m syncUpdateOrder ();% X5 W2 s) h' k% {" P5 w
1 x, q, y0 ?; P# J) @3 o
try {& {" R- e% \. U
modelActions.createActionTo$message $ @* A4 h. |- f; U; l# J8 U9 A
(heat, new Selector (heat.getClass (), "updateLattice", false));0 u- f0 c" [/ e5 f
} catch (Exception e) {( a- o: L% y/ ]' G8 r
System.err.println("Exception updateLattice: " + e.getMessage ());
, V E# u' R, V% ^2 c2 X }$ M2 P0 I' ?' A. k g
- J7 c3 M6 M5 [2 v // Then we create a schedule that executes the
5 e/ i% r X/ z% z // modelActions. modelActions is an ActionGroup, by itself it/ T3 j! k4 w; L8 B0 l6 z
// has no notion of time. In order to have it executed in
* t3 @9 a/ o" X& ] // time, we create a Schedule that says to use the
! n# }7 g c9 m* F // modelActions ActionGroup at particular times. This; n+ T) _: \; I' Y! w
// schedule has a repeat interval of 1, it will loop every; m% M+ j' g) }5 {0 d5 H7 y
// time step. The action is executed at time 0 relative to
% y; F. J9 @( D) `$ J; N // the beginning of the loop.
! f# V3 Z( j- X
2 x5 S1 i9 C$ M' f" @ // This is a simple schedule, with only one action that is
- l( R# @" {5 p; \) \ // just repeated every time. See jmousetrap for more
6 n0 _/ |( U' G; e! ^& E( Z% i/ k // complicated schedules.
" Y. X- o! h6 R5 b6 Q
' P {8 T, @/ k0 c4 X modelSchedule = new ScheduleImpl (getZone (), 1);
$ ^+ x4 I/ a F: h$ n' \2 A( h modelSchedule.at$createAction (0, modelActions);& e8 d; m% q4 J
4 b! g" j4 O/ c1 h- {" z: t return this;7 x2 ]+ D" E5 r" u$ Z' `8 g
} |