HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) q# `* x& b) J7 ^# w( c9 B$ I
4 G, b* U* ]5 {9 J/ \: ~# ~- R- U public Object buildActions () {' |/ ^/ y a) W. m7 a* I6 E1 u
super.buildActions();
% Q* q$ v7 ^& V9 Z1 F 6 t$ |; I P5 L1 s/ P
// Create the list of simulation actions. We put these in3 S. M+ W$ b- U$ I8 e# n
// an action group, because we want these actions to be9 F! x1 X# a7 q1 ^
// executed in a specific order, but these steps should
* \3 X* m# S% A- J! I // take no (simulated) time. The M(foo) means "The message3 c& c9 Y) j8 w+ G5 M1 @& `
// called <foo>". You can send a message To a particular
& R) L: U% b- z% v9 U1 K& v& E // object, or ForEach object in a collection.
% _ f! j: q' z: G* b) A / U9 Y! B5 G6 L* @
// Note we update the heatspace in two phases: first run
7 w: _5 \* q! k5 ]$ d* F1 x { // diffusion, then run "updateWorld" to actually enact the
& W3 g6 b6 n) S3 `+ |- t* Z, z; | // changes the heatbugs have made. The ordering here is
# B' X( k a! d! ^% b2 d9 [. _4 O // significant!
( R7 }6 j( w4 D7 j, ~# _ 7 s- R0 v% b S- l( J
// Note also, that with the additional% T$ i( t5 s; \2 ? C% \7 A: Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can% ]( B r) |) p/ S& {: @4 ^7 v* s
// randomize the order in which the bugs actually run& ?7 B* C* c2 [
// their step rule. This has the effect of removing any
! C' _% B8 I* E9 ], J O // systematic bias in the iteration throught the heatbug
! q6 H7 K1 H+ O7 w // list from timestep to timestep
! ?* Q) @* q0 k1 P# z: { 1 N2 c4 D: `; Y
// By default, all `createActionForEach' modelActions have+ s7 b2 _! O) K$ G% ]! b3 y: C* }
// a default order of `Sequential', which means that the7 K+ J5 N* ^# v, Q' a3 c
// order of iteration through the `heatbugList' will be( d' I( e I3 a$ R; p6 G
// identical (assuming the list order is not changed
C9 O2 t+ T* @# W- a1 z( [ // indirectly by some other process).% ~: K) T. \$ D
S) B( a& z3 Q
modelActions = new ActionGroupImpl (getZone ());
7 A# q; |# }5 K6 _* u# z4 \! |# J& ]% `
try {- D$ }6 [5 S1 x
modelActions.createActionTo$message
1 G0 H+ c* ?* f9 l6 [% L) u (heat, new Selector (heat.getClass (), "stepRule", false));
5 }4 Q- Q% @$ K9 x2 ~ } catch (Exception e) {1 x. P% [& J( d& ^: X/ @ }
System.err.println ("Exception stepRule: " + e.getMessage ());+ I3 [; v4 g8 h
}
5 K( W6 V# s7 b$ @2 {, L6 T7 I" o
$ B5 @( j2 r2 g! S8 e" | try {! ]7 x) S0 c$ y0 I+ V. F+ i; |
Heatbug proto = (Heatbug) heatbugList.get (0);
( k; z. s1 P5 S% E( Y Selector sel =
* E7 `6 Y+ [9 N5 D8 `: B new Selector (proto.getClass (), "heatbugStep", false);
* g0 A9 G( S* U% a J% v; b actionForEach =
3 C" K5 ?) H* y" d modelActions.createFActionForEachHomogeneous$call5 Y+ z' g1 j3 A' E- `
(heatbugList,) n8 U2 r. k9 p" y* R
new FCallImpl (this, proto, sel," R2 w/ S6 Q( k* `; ^% _% F; R2 h. ~
new FArgumentsImpl (this, sel)));; D! F: x! v/ \ s
} catch (Exception e) {' y l) Z9 e: X. J
e.printStackTrace (System.err);5 i$ t" T* c1 P! A( e8 _) I
}* A' M) X* v" l
' `: e" N1 @0 c4 t syncUpdateOrder ();
7 {3 E1 l$ t. Y5 w9 d0 @; G0 l; B, z( t% C( g
try {9 z6 A. ]" `# N9 C+ B
modelActions.createActionTo$message
. G- d/ a9 I n% G4 {2 { (heat, new Selector (heat.getClass (), "updateLattice", false));1 `6 O+ c% k1 o% ]+ _% M: A
} catch (Exception e) {
. z' @" q+ a+ j K" I2 c d; t System.err.println("Exception updateLattice: " + e.getMessage ());
D; m& N! e5 z2 t }
6 ^( }2 m- a2 }! I5 C & v" P7 \+ c" n; q2 w! E
// Then we create a schedule that executes the0 u! N! T2 v1 }1 U" w- l
// modelActions. modelActions is an ActionGroup, by itself it9 R }' p8 Y6 j
// has no notion of time. In order to have it executed in
; n2 t2 J( H/ F" b2 X // time, we create a Schedule that says to use the, W$ q- T W' N2 k
// modelActions ActionGroup at particular times. This
, X: Q) s, B# t$ f. G8 w. } // schedule has a repeat interval of 1, it will loop every+ [. E# b8 f2 H4 A. O8 p- m M5 m. Q
// time step. The action is executed at time 0 relative to
/ p8 F7 A5 l" I9 F1 [8 i // the beginning of the loop.
' Z; F& q6 U2 |
! }; s5 S+ N* L, E // This is a simple schedule, with only one action that is
& c! Q3 U4 \, ^6 z/ J1 W // just repeated every time. See jmousetrap for more1 P# F& }7 W% I% a7 n
// complicated schedules.
1 B* e2 J& B5 g8 V: e& f3 W4 ] ! f5 P! r. v( k3 k
modelSchedule = new ScheduleImpl (getZone (), 1);+ T$ v! d/ Q# g+ x8 \5 P- G( y& K( n
modelSchedule.at$createAction (0, modelActions);7 P# R4 Y* F) T5 y0 u- _$ I
# [4 w' T: z- Z6 X/ V
return this;' [" G: _9 x1 j3 `. x
} |