HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% o' f E& q S* {
1 o. ?, i- V% v7 N
public Object buildActions () {
: S( E+ L$ p6 T& E; h; Z1 m- h super.buildActions();
5 ? Z( m: v! f5 k! q; U6 _8 o 4 V1 P3 r/ V+ B6 D: A& f
// Create the list of simulation actions. We put these in$ V& D J5 c: v" x4 o. G4 |
// an action group, because we want these actions to be2 u7 {' |" k" i9 h0 S
// executed in a specific order, but these steps should% x# w8 C& u2 [3 ]
// take no (simulated) time. The M(foo) means "The message5 ?, y8 k5 `; R2 D
// called <foo>". You can send a message To a particular. {: \, L+ v' J& }
// object, or ForEach object in a collection.
$ q, o3 R4 i4 b8 e: I1 N1 A; w
! k+ Q8 u, j+ y8 H // Note we update the heatspace in two phases: first run' e/ u' g& I- P
// diffusion, then run "updateWorld" to actually enact the
* t) r0 `. X7 J+ f. Y H // changes the heatbugs have made. The ordering here is/ e- }/ d4 }% H+ H
// significant!
% v3 J$ @$ ^6 H2 d. [ 2 p6 J( C7 T9 {+ T# x9 b1 N
// Note also, that with the additional
2 j5 u1 E [" I/ U2 Z. h, R // `randomizeHeatbugUpdateOrder' Boolean flag we can+ L. t. J: Z/ r# z. {
// randomize the order in which the bugs actually run
* V# D" `2 X. e% j8 L // their step rule. This has the effect of removing any
6 B6 l0 |1 z3 K( @$ A+ t. Z+ }) t0 q // systematic bias in the iteration throught the heatbug
! S* q8 o" G- Z // list from timestep to timestep+ |/ g X0 @+ o# E0 B
& E& n/ A+ {$ }- z0 [$ y/ E8 d // By default, all `createActionForEach' modelActions have( H: b% `4 A6 ^/ v- W i5 G+ ~) F
// a default order of `Sequential', which means that the
0 ]% Z2 B) @9 w( A: r // order of iteration through the `heatbugList' will be
7 r, C$ Y P/ \$ v* e5 X K // identical (assuming the list order is not changed
5 a" h6 S) i1 B4 { // indirectly by some other process).8 X4 y: s6 ?7 I E/ N) q; v' C
: G' l- z, T \& j8 C$ X
modelActions = new ActionGroupImpl (getZone ());
9 r+ J3 H4 o2 {% z# C/ G
6 m6 ]9 i) S3 T$ r% k* a try {
; |$ d0 V+ H" x* M6 B1 B* H modelActions.createActionTo$message
9 E4 ]+ G) \0 z1 @ H3 Z, ] `+ q" g (heat, new Selector (heat.getClass (), "stepRule", false));
3 l# d. r6 E6 _* e" n } catch (Exception e) {+ S( Z" i% U1 E$ ~1 N' `
System.err.println ("Exception stepRule: " + e.getMessage ());+ u( }& n7 H1 @ j, \
}
) d5 c' Y% M1 q7 ^0 d& D( r5 O; [; w
try {
/ H; ~6 L4 ^* d) Q) o Heatbug proto = (Heatbug) heatbugList.get (0);
) d ?! W* O4 N' N& A Selector sel =
9 X) ^% A" ^ D& @% a( |# b+ ] new Selector (proto.getClass (), "heatbugStep", false);
: c& A7 o [% \/ t* @ actionForEach =
. j4 @5 e g9 m$ h D, l modelActions.createFActionForEachHomogeneous$call
4 {+ j; L# A! r (heatbugList,1 i5 _6 B% ^ L8 Y7 j( J
new FCallImpl (this, proto, sel," @9 `) S# m Q
new FArgumentsImpl (this, sel)));: H* B+ `. k7 n, n4 Q
} catch (Exception e) {. I N, u* N9 \7 O+ [
e.printStackTrace (System.err);
' q+ q; a( I; p+ p }4 h/ g, J0 r# z
$ Z9 n5 ]) H$ t% Y$ Q3 X syncUpdateOrder ();2 n3 V+ V# s( x5 i7 c. O& [9 w, b
- D0 E D9 I, j% o* d! b3 y; i E: ^
try {- M6 w" u8 y/ R& l
modelActions.createActionTo$message 4 a8 H2 A# V- X; L x p
(heat, new Selector (heat.getClass (), "updateLattice", false));
& }* [3 {. z% E/ ] } catch (Exception e) {/ [. f+ Y& U1 k- _4 J
System.err.println("Exception updateLattice: " + e.getMessage ());
/ j, u- O; K. B* w# D& m; @7 \" t1 s }, r5 E: _: p; ]2 R
4 Z$ C; b# R% g: T# {% d
// Then we create a schedule that executes the% w9 s) F _. J4 z* J$ k, c
// modelActions. modelActions is an ActionGroup, by itself it. _6 H2 r, U+ I) V1 j
// has no notion of time. In order to have it executed in
' m8 ]& ?0 A$ Y+ V // time, we create a Schedule that says to use the
0 Q3 [& y+ B% m. O0 F // modelActions ActionGroup at particular times. This& \% ^+ x$ y, h! R0 r
// schedule has a repeat interval of 1, it will loop every
9 M' @, y+ ^/ v2 k! H9 ]) K0 B% S // time step. The action is executed at time 0 relative to( K x$ N9 O0 k" w; O
// the beginning of the loop.0 k* ]- u3 @7 v6 K+ l8 [+ T' I' e
, V9 b( F/ } Y% Z! E0 y // This is a simple schedule, with only one action that is
8 A3 [0 U7 U& k0 N) X$ N! C' i V // just repeated every time. See jmousetrap for more" d7 @; q) V7 z- a5 V
// complicated schedules.
, F; a" \/ z8 G, x4 O/ T7 ~* S 2 f8 T4 l0 x- O/ w% U
modelSchedule = new ScheduleImpl (getZone (), 1);
, { @4 N4 {+ d& k; f' B, s modelSchedule.at$createAction (0, modelActions);
! l6 `+ h% T& f, O: i3 z7 n* v* ~
9 ~1 X( o+ C( R return this;
4 g1 P S! c$ U' |3 E } |