HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# ?9 X. b: {" {6 H* t4 k" {( P$ a- j8 J1 f; s% B
public Object buildActions () {
6 k7 {" u! P+ F1 p super.buildActions();* s( J/ L/ T+ ?: m8 h
2 J2 y$ Y# i8 p! S/ Y
// Create the list of simulation actions. We put these in
1 Z$ A, L' L% ?/ n# B2 l; r // an action group, because we want these actions to be
% X2 ~2 d3 g7 c' z, |' m6 b // executed in a specific order, but these steps should
! r0 f4 ^0 X; c [' D1 } // take no (simulated) time. The M(foo) means "The message
) X) V' e8 _0 w% a5 A // called <foo>". You can send a message To a particular: }. m. c b1 g) U) M! P/ W" [; p
// object, or ForEach object in a collection.$ [4 i8 a3 }0 a7 o6 B3 R6 E
% B; @: l) v3 F- O
// Note we update the heatspace in two phases: first run. e0 Z/ Z P- \* l2 a# Z( Y
// diffusion, then run "updateWorld" to actually enact the
! i9 K) x: v& K, Z, u. Q5 f // changes the heatbugs have made. The ordering here is; s6 g5 ^; o3 D5 w# C( z! N
// significant!; d8 Y# ?/ s8 E6 p
: h9 X/ N* B0 {) d // Note also, that with the additional
; f" |+ z5 G/ U4 C% h( A. ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can' U2 G3 F& z$ W! B
// randomize the order in which the bugs actually run
) U0 L/ v- G$ f. f+ G // their step rule. This has the effect of removing any" m2 e6 c' Q& O2 g
// systematic bias in the iteration throught the heatbug+ j! K) u( q2 c# h' w$ l
// list from timestep to timestep+ \( r8 M/ h9 @' C( l! |
, l8 d$ D- |- K7 K+ ~4 _5 ^ // By default, all `createActionForEach' modelActions have
5 o& G" h9 n2 Y5 k/ T* W0 D // a default order of `Sequential', which means that the
, _8 U1 V1 t' x% }* i. I // order of iteration through the `heatbugList' will be6 K4 G. O( L3 m
// identical (assuming the list order is not changed7 C9 ?9 p4 f& k$ k) {3 W
// indirectly by some other process).( f) R$ T# Z/ j" y9 x; W8 R% [
6 _: p; H) j* O( T& z+ h! R0 N$ u+ O modelActions = new ActionGroupImpl (getZone ());
9 m, c0 j+ x# M* {
8 I8 w6 I# u- T4 N! `" b4 p try {1 Z1 |" L% D4 M& D0 Y
modelActions.createActionTo$message v1 u5 ?/ }+ w1 j* E% s3 q5 R
(heat, new Selector (heat.getClass (), "stepRule", false));% [3 Z4 T% N2 j2 g6 m1 ~& Y' c
} catch (Exception e) {
' F, N( N0 W- {& B) ] System.err.println ("Exception stepRule: " + e.getMessage ());
1 T5 Q4 y' y7 o }
) E3 Y9 a5 [' k% R# P2 z+ k3 ]& p! V( L2 C+ n! E( J$ K U% b
try {
% h2 ]4 R" I% M9 M+ S: s& i, V Heatbug proto = (Heatbug) heatbugList.get (0);
3 A0 I4 N! B3 [1 [ Selector sel = $ v- s. u& p1 n, W M
new Selector (proto.getClass (), "heatbugStep", false);: g# \6 j5 p x) P2 _ l, H d5 I
actionForEach =
4 q9 U. b! [' r6 S. P modelActions.createFActionForEachHomogeneous$call
5 S$ h" h7 e9 C- v: m- ?; U/ r* V9 q (heatbugList,5 o8 I+ ~* f% A5 ?& z) u
new FCallImpl (this, proto, sel,
5 u8 b5 m$ N9 M* d6 y6 w# S new FArgumentsImpl (this, sel)));
& @* @) n7 x" m: e3 W+ p } catch (Exception e) {
, \. y! V/ b: V! H* e- ]+ s3 C e.printStackTrace (System.err);: M$ A# q _, L8 [
}; }5 F8 _; F: I! D2 ~; e
& f k. Q. P. n% Z) T1 M
syncUpdateOrder ();; |1 Q, A6 `2 x( w4 H
8 F7 B( G# \+ r; q& R4 ? H
try {2 t3 p' i) P8 ^6 h4 T5 K$ A. l
modelActions.createActionTo$message
8 L5 R2 \3 n- f: X" o (heat, new Selector (heat.getClass (), "updateLattice", false));# z: y/ F* A* Y* P5 [
} catch (Exception e) {
$ x1 |4 K% B% }) h' e( ?, @ System.err.println("Exception updateLattice: " + e.getMessage ());% C6 Q; i q# Z3 A0 S
}
1 a3 _1 e7 t' D: \
! B3 g6 t0 t2 B& N" T" | d // Then we create a schedule that executes the
9 Z; j9 F/ T A // modelActions. modelActions is an ActionGroup, by itself it
7 o1 ~. k) I7 J) _/ K // has no notion of time. In order to have it executed in' Q. A2 ?8 L5 b/ j
// time, we create a Schedule that says to use the3 u7 _; k v* P4 p% {
// modelActions ActionGroup at particular times. This
/ Z l: z( J) z1 \, U // schedule has a repeat interval of 1, it will loop every
8 P. v' \$ I9 ~. L // time step. The action is executed at time 0 relative to: t, f, }! I8 y
// the beginning of the loop.8 I4 ~6 V6 F/ |. O1 E0 {
3 q, f! ?1 [9 [! `" U' H* ]; A // This is a simple schedule, with only one action that is) N: G O! M V+ `; m4 I0 A% p9 @
// just repeated every time. See jmousetrap for more4 g5 u' H' J0 }1 ?: M
// complicated schedules. @( V3 h/ ?- e3 |) f
, f; Y0 t6 o0 C
modelSchedule = new ScheduleImpl (getZone (), 1);
0 X' Y6 u+ R4 i5 `2 z! z- s l modelSchedule.at$createAction (0, modelActions);
9 l& E; H$ H, w. ` V" t5 J' P
6 G7 E& h8 |( h7 L- b. A; f return this;
4 R) {+ R. l6 l1 v8 W; \ } |