HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# S7 v* d4 w% \0 Z7 ?" J" r# K
( f2 E, X" N" ]5 x( H* t public Object buildActions () {9 J. G$ m6 w# k/ g3 @/ p- h' `
super.buildActions();+ r/ X* ^* |; g. r+ y, J( ]: f8 W( j( y
9 A4 K$ Q, S' ^ // Create the list of simulation actions. We put these in* d5 p8 X% _; {
// an action group, because we want these actions to be
& d7 s6 _5 ~/ M7 F$ ^% x9 e // executed in a specific order, but these steps should
1 D3 p4 c; t: w; l/ L // take no (simulated) time. The M(foo) means "The message! W! o ^2 A" b
// called <foo>". You can send a message To a particular" e( B+ a4 L$ d4 `" y) o. u
// object, or ForEach object in a collection.
! t: D7 d. F/ t6 G 8 F. e: y9 I( m0 P$ w4 \% h m9 \$ x
// Note we update the heatspace in two phases: first run( {$ v: U e- i& k. F
// diffusion, then run "updateWorld" to actually enact the
( U- k; b9 L, Z5 e; W7 X$ q // changes the heatbugs have made. The ordering here is8 m% h! p$ c) B% F" z9 x
// significant!
3 H4 v0 R1 l# H! b* O) A @8 l
3 f5 x) ]$ ~1 y- M // Note also, that with the additional9 Q( d( _4 K* o, M0 A( `2 j
// `randomizeHeatbugUpdateOrder' Boolean flag we can: d7 D4 x. \, k
// randomize the order in which the bugs actually run
( E3 q$ @: b, y+ ]8 T& M3 o // their step rule. This has the effect of removing any4 M5 U# c, U) j7 t; u; F
// systematic bias in the iteration throught the heatbug( z, q6 M. {4 e) c0 q
// list from timestep to timestep' N6 t3 q- c1 J
1 l, c. O- r0 W! j
// By default, all `createActionForEach' modelActions have
$ S5 @; K: ~2 _0 J% l- u // a default order of `Sequential', which means that the0 y! N- p2 i1 d- n W- ?
// order of iteration through the `heatbugList' will be' m) ^, C) V( D6 s2 e- E
// identical (assuming the list order is not changed! u% t# B$ Y" G( Q: |
// indirectly by some other process).: b ~6 C! s/ |
" u) M' D: b0 y, o; k: s' A
modelActions = new ActionGroupImpl (getZone ());' {8 v& k" |$ o G. T, I: b
8 o0 I: h+ J, m" Q; l# ^. b try {7 d9 d! S3 q2 J' g$ w
modelActions.createActionTo$message9 x7 J9 T8 n$ r+ |: w0 ~! A# J
(heat, new Selector (heat.getClass (), "stepRule", false));
, K; c$ ~: ^- E/ N } catch (Exception e) {
( p9 L/ w0 u6 ` System.err.println ("Exception stepRule: " + e.getMessage ());
9 |4 a6 M) e7 F" R3 d) s# ] }* f( K& F1 k) M; W7 P0 U
! Z7 g5 H" P4 b3 N/ I8 r5 n! N try {
* @) ]2 n- }; ~1 q8 |" j2 Y Heatbug proto = (Heatbug) heatbugList.get (0);2 v( t3 f6 b0 @1 r2 I* {
Selector sel = " O! J( \6 o0 P+ L7 r2 ~, A
new Selector (proto.getClass (), "heatbugStep", false);7 Q0 Q9 ?8 r* f! H
actionForEach =
k7 Y: v+ t% p9 q0 S* A modelActions.createFActionForEachHomogeneous$call/ o' V5 ]/ x' X( A0 S: v
(heatbugList,
. u. `& x; e! H: F" R8 ? new FCallImpl (this, proto, sel,) Y% \7 E1 I/ H
new FArgumentsImpl (this, sel)));5 G3 i* ]4 E1 D1 ]; R& ? F8 f
} catch (Exception e) {3 N1 o8 p% R: [% N/ {. i" [
e.printStackTrace (System.err);
F# P! V( y$ _8 E. v }
( h* A; ~. S% o " ^8 H) d5 z8 X! _6 y, e
syncUpdateOrder ();
: f* L! b- Q4 ~- }2 j* i& ]; H5 z1 x; F. t) C! P' w) |
try {* d; _+ ?3 Z5 ?+ F- `, h3 b; k
modelActions.createActionTo$message
( c+ U; I) ~( o6 j1 x3 D (heat, new Selector (heat.getClass (), "updateLattice", false));. Z- ?" m; x; G
} catch (Exception e) {4 n! ]$ ]+ |* p2 y! N1 }2 e- v
System.err.println("Exception updateLattice: " + e.getMessage ());# w5 W% T/ F. z& g* t$ N# _" E/ r
}" O/ j4 C+ F4 _
2 ^4 s, t0 H& |+ }8 E
// Then we create a schedule that executes the, R& g+ v& a5 U$ ~
// modelActions. modelActions is an ActionGroup, by itself it2 S1 T/ N) H5 N
// has no notion of time. In order to have it executed in
* K: N4 q, w3 ^+ x // time, we create a Schedule that says to use the
/ n$ X9 b) |) V: z1 R/ L) H! C; P // modelActions ActionGroup at particular times. This; \# r( m* I( d; c9 u- p
// schedule has a repeat interval of 1, it will loop every2 T, L& p0 U5 U7 V% k5 u* x
// time step. The action is executed at time 0 relative to: b1 ]: R+ b5 [9 K- T0 D; M
// the beginning of the loop.
1 [' k- k2 \8 d( i! k1 ~$ H/ {" P/ H$ O1 u* [* `; G+ y
// This is a simple schedule, with only one action that is
; a. r4 I+ J* z" f/ n // just repeated every time. See jmousetrap for more
% x: Z* w9 S7 { // complicated schedules.* B/ K3 ^: W+ U3 e# b% G
, J; L- H6 D. N2 g! L
modelSchedule = new ScheduleImpl (getZone (), 1);
) N: X$ d2 S0 ?" w modelSchedule.at$createAction (0, modelActions);; u) {/ K! O5 b2 a9 T l
1 H* C7 n* e( l! r4 k6 A return this;
; K: V9 E' V# t5 b" N2 A. ^ } |