HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. X+ Q! G& ~3 K' y C2 ^. T
3 X! o" | A3 m( |4 T$ N public Object buildActions () {
- `+ R- E8 d' S super.buildActions(); R9 V* D2 m" o( x' y" S$ ]5 z
2 h$ D3 u0 z9 p, V: |
// Create the list of simulation actions. We put these in! F0 V, B! ?: a+ p2 j4 z h) g7 \
// an action group, because we want these actions to be; Y$ \. d/ X6 W
// executed in a specific order, but these steps should
( l; ?/ }& p- E2 a# o3 B J // take no (simulated) time. The M(foo) means "The message
" k2 q: @ Z& O9 U! B- ~ // called <foo>". You can send a message To a particular
1 V. h- T( h' {, w$ ]: R // object, or ForEach object in a collection.
. c& v+ z) e; w9 }3 D% R
/ D$ J: p) ` N: T/ b // Note we update the heatspace in two phases: first run
5 y! M2 O s$ L% M1 g6 Z // diffusion, then run "updateWorld" to actually enact the
. J8 ^8 ^! P# n- s( h; G5 j // changes the heatbugs have made. The ordering here is! S/ @9 B9 T/ k& M) |( S
// significant!5 X8 P1 g* K n9 ]) V- _
8 G4 n4 p8 C% Z$ M; Y% H
// Note also, that with the additional. S- {1 m* p; ^2 `4 z* _( w
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* W e+ |% F7 {* s: P% G // randomize the order in which the bugs actually run6 i7 z) S, o& p) I# H0 A
// their step rule. This has the effect of removing any& v" e5 S) w6 u! [( u' X: l$ b
// systematic bias in the iteration throught the heatbug
* N4 p5 [1 J' t- H" F+ ~ // list from timestep to timestep* k/ t. a% a7 x2 h
) K3 d+ P' g/ m# K // By default, all `createActionForEach' modelActions have
# p/ K. A; Y5 s2 ? // a default order of `Sequential', which means that the2 v. K" X$ e x. s
// order of iteration through the `heatbugList' will be
5 {$ n7 {: ^$ @8 E3 K g // identical (assuming the list order is not changed
4 P' \: g3 Y4 G // indirectly by some other process).
; M* [0 j: T/ V 4 c/ O8 ] r! ?6 x5 R: g' ^
modelActions = new ActionGroupImpl (getZone ()); F* |/ V( j( y6 L9 t
# ~. C* Z1 Z+ P; ?
try {$ n x, w7 P2 M3 [
modelActions.createActionTo$message
$ s0 p: H& W" b- T (heat, new Selector (heat.getClass (), "stepRule", false));; l q7 p1 U! I# d8 Y, }
} catch (Exception e) {
+ Y5 ?. u+ _ ~* |( J: q) p1 v4 Z System.err.println ("Exception stepRule: " + e.getMessage ());
s, B [9 ^* p+ x7 l. d }! x( a2 _" i" O' t7 l2 y
$ X8 h; n6 C& D6 g try {
% w4 g" r; c. {. l# k* b" L Heatbug proto = (Heatbug) heatbugList.get (0);
+ e, R5 n( ~% X7 I. ?) D Selector sel =
+ \- S6 F9 J P$ N3 b5 T new Selector (proto.getClass (), "heatbugStep", false);5 `: X$ M4 D( `$ d- F7 u
actionForEach =
& Y% i+ K2 Y; h8 b& W9 h modelActions.createFActionForEachHomogeneous$call+ s, z) X1 s. e
(heatbugList,7 W Y$ G; {' Y: u5 X# j& z6 k
new FCallImpl (this, proto, sel,
: h8 y) H* V6 u$ [ new FArgumentsImpl (this, sel)));
: O5 g9 Z4 d, f! v } catch (Exception e) { u3 ]( _7 O8 O* x }
e.printStackTrace (System.err);
6 y/ | P. {3 O0 p4 _4 a }
9 J" X5 k+ r; o9 `+ S
) m8 [; T3 }6 o( E/ | syncUpdateOrder ();
, U$ ~. j+ C- H- ^. R- q
0 I8 `! t! ?7 Y& {. { try {$ t- J6 p* g9 m* U" i( {" y
modelActions.createActionTo$message $ N1 w9 i- c; t7 g
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 V* [, T% ~ S7 T+ x) b4 @" e5 E } catch (Exception e) {, z: b- h _- t# v" z7 H
System.err.println("Exception updateLattice: " + e.getMessage ());. u$ N$ w: d8 p* _/ B
}" _! y6 W* T l. f5 e( W2 Z
$ z! s5 b7 m) P- _$ E A // Then we create a schedule that executes the
2 c# k/ m, v7 N$ W) B- h // modelActions. modelActions is an ActionGroup, by itself it
( e( [" |: u3 y2 b% n$ ` // has no notion of time. In order to have it executed in& I* E# G9 K. R* x8 Y2 V1 F; l
// time, we create a Schedule that says to use the6 [9 w2 ^( j2 J3 e2 i' Z! ?
// modelActions ActionGroup at particular times. This$ i' Z+ N u# \3 C5 r
// schedule has a repeat interval of 1, it will loop every b2 t" p6 H3 `' j2 b' A- f
// time step. The action is executed at time 0 relative to/ F7 m; I4 @* k1 {7 W
// the beginning of the loop., I6 ]6 p/ t/ V2 X
" L# V; o9 h& M9 z5 D! u // This is a simple schedule, with only one action that is, w$ u- ~' W! c
// just repeated every time. See jmousetrap for more
, O) b+ x1 _. [$ s( L // complicated schedules.. {/ M+ @& d! X, Z, w i
4 m ^) u- k0 t8 o& z' \ modelSchedule = new ScheduleImpl (getZone (), 1);
9 |% d b! V) e1 H* C modelSchedule.at$createAction (0, modelActions);; K; y( X, G6 P
- y0 m' R+ C, P) G( m! g- \, h7 V2 @
return this;, a: _9 |* |, `! Y! ]
} |