HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# J, l/ A& }9 E7 s+ ]
- u* @; ^* p* f. F4 C3 B* E- h* t public Object buildActions () {
9 }! w* L1 V( D/ S$ ?8 l; @ super.buildActions();
) M; k* S1 y' g
# b! n. z7 p+ Z$ {- I // Create the list of simulation actions. We put these in5 D# E' O) q2 M1 n0 k
// an action group, because we want these actions to be
4 X. b# \) @$ N9 e6 ]+ y, L // executed in a specific order, but these steps should& I7 h0 v; r; t! u$ U% ~+ g: P$ @
// take no (simulated) time. The M(foo) means "The message7 Q) U0 h( D) g, ]
// called <foo>". You can send a message To a particular
8 I. {7 Q/ ]/ U& u/ K7 e0 f // object, or ForEach object in a collection.
+ c' e+ p0 o0 ~5 K, ] : W; T+ H: a' ^3 R# t: i
// Note we update the heatspace in two phases: first run7 S. y0 n3 F2 `% _
// diffusion, then run "updateWorld" to actually enact the2 z5 d/ m5 L. d
// changes the heatbugs have made. The ordering here is9 @- B _- ?4 ^5 X* ^5 g
// significant!
- a* ]; s' \- }. V# h + ]% g! S8 b+ ^( t
// Note also, that with the additional
8 @1 r" }( Z5 j( W% B // `randomizeHeatbugUpdateOrder' Boolean flag we can) U7 E3 U3 F3 ]9 q# Z3 k! w
// randomize the order in which the bugs actually run9 ?& Y% B( w7 t* N2 s
// their step rule. This has the effect of removing any
5 Z3 d @% i. F // systematic bias in the iteration throught the heatbug
; d _, _$ ?' y8 o( e // list from timestep to timestep
+ J: n4 X; P, I2 \ [' s : j, G @# H1 }( C# y' s
// By default, all `createActionForEach' modelActions have
) I9 Q, g2 e1 ~ // a default order of `Sequential', which means that the( ?% U2 l2 M6 T# ^
// order of iteration through the `heatbugList' will be) i8 m- P; s* b, n" H
// identical (assuming the list order is not changed
1 _1 G; }- F- `" |% w. S, n; V! P9 N6 I // indirectly by some other process).* o% s) ~3 q8 b6 d( A$ O
b/ _/ Y1 Y3 N% v modelActions = new ActionGroupImpl (getZone ());6 d2 A( ~$ ^, d; K0 i
% ~3 G0 B$ c" G2 p3 I4 N; e
try {4 V+ r* r4 [0 K4 S9 }! \. Z
modelActions.createActionTo$message
) Z) {) ~7 r8 w( P, i/ z9 G (heat, new Selector (heat.getClass (), "stepRule", false));6 \4 H% t- s4 A! c( p2 t( P+ B
} catch (Exception e) {
' R# |5 p, A: p6 y; y4 g5 M, \ System.err.println ("Exception stepRule: " + e.getMessage ());
& F8 O$ J$ `$ M8 z8 N }4 |1 P0 ~$ m3 G4 |
& f3 X4 ^9 N% s
try {
/ v3 H/ j" R! Y5 a Heatbug proto = (Heatbug) heatbugList.get (0);! l5 [# W: _- |" a4 B+ E
Selector sel = # }! r7 W- h) \7 t$ }
new Selector (proto.getClass (), "heatbugStep", false);
; M2 Y, h$ J w: H' X actionForEach =# U, b. A& j8 S
modelActions.createFActionForEachHomogeneous$call6 M0 c& W# _. Z/ W
(heatbugList,# D! O, K' H. Y$ N6 o7 J
new FCallImpl (this, proto, sel,0 G1 E! J5 Z' I# p3 D
new FArgumentsImpl (this, sel)));. f6 r9 u& v5 u5 R* ~ ], \
} catch (Exception e) {
8 x% o) l5 a3 P" R% O6 v0 v/ ?! G- _+ M e.printStackTrace (System.err);% R' {) k: a) q/ Y6 I& c8 `
}
7 @( q8 v% k* C1 q9 ~; o) i+ e
& S( S1 _& @: r4 t3 O. }1 w syncUpdateOrder ();
) n- E1 v. ?1 r3 X3 y) y# C/ P
try {
' I7 r0 I) S- d5 L modelActions.createActionTo$message
# X' x, G( F7 R% _; n- m+ \ (heat, new Selector (heat.getClass (), "updateLattice", false));
l5 ?% J3 s$ f0 U2 h } catch (Exception e) {
& E$ \( C0 f' }/ K9 p( C+ U% o& [3 z System.err.println("Exception updateLattice: " + e.getMessage ());8 j) L9 C6 C4 C* B2 h+ M, P2 I
}
2 S9 \5 T0 T0 h1 ?6 V
3 w+ n& U( ?/ E- c! g // Then we create a schedule that executes the
- h, d0 O" _! ` // modelActions. modelActions is an ActionGroup, by itself it+ s5 K8 V% q% S* k
// has no notion of time. In order to have it executed in
" F8 F1 j6 ?2 m // time, we create a Schedule that says to use the
4 N# @' ], v5 ? // modelActions ActionGroup at particular times. This
2 a/ p- w% {7 c // schedule has a repeat interval of 1, it will loop every# a3 E3 \3 t; {; u, a+ O$ _
// time step. The action is executed at time 0 relative to
* S* a H- N. t- }: |6 M& S9 I // the beginning of the loop.
R n$ f8 R3 F
1 q6 H+ K, o: ?7 p' {: H // This is a simple schedule, with only one action that is
! [* U9 l6 M7 [ // just repeated every time. See jmousetrap for more, ~" G" M$ g% i# |. A- A
// complicated schedules.
$ V& A! ~& Z6 X
. l! S: H7 K6 n modelSchedule = new ScheduleImpl (getZone (), 1);
% g4 |( Y' q& h; T- d+ f6 M modelSchedule.at$createAction (0, modelActions);. Y. h- _0 u' l/ {6 o; W2 o7 e4 c4 {
) M, n p m6 w$ A$ D, O return this;! p1 k' r, o. |! H7 k
} |