HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# p; b0 j0 m5 p& K; G: }
# h) W" O# p0 Y( k) |1 F public Object buildActions () {# i/ r, ~. S% H+ p) s9 u
super.buildActions();+ U3 @ S( S. M' r/ i
0 ^) c5 n5 P7 J/ k$ d
// Create the list of simulation actions. We put these in! B ^3 i# G E# T% Q; N% T: [
// an action group, because we want these actions to be
A- [' x9 n P# d/ ]# x, N& f6 o" j // executed in a specific order, but these steps should* Y" N- T- H; h2 a
// take no (simulated) time. The M(foo) means "The message
6 H, l7 P/ S: i4 N! s // called <foo>". You can send a message To a particular( o O" A# I9 v F% S3 j& p; n) h
// object, or ForEach object in a collection.
$ d8 H. s; \- K6 J5 a/ T
, r; \% A/ a0 Q8 B // Note we update the heatspace in two phases: first run
# e& i+ O, [ _* a // diffusion, then run "updateWorld" to actually enact the
) m* J! x3 v. J$ Y // changes the heatbugs have made. The ordering here is
7 H7 y# J* G1 e: K, d // significant!( v: _, X5 |6 }* D( D4 H+ ~
6 S! W: R7 r, D# a4 T$ @8 m // Note also, that with the additional
( x9 c/ L% g* `6 a; G& y4 K2 D2 ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can9 G1 \# Y; P+ x: \
// randomize the order in which the bugs actually run6 a7 s% L/ T. z
// their step rule. This has the effect of removing any
; ~- E1 G3 B- ^ // systematic bias in the iteration throught the heatbug0 M8 D: q3 J2 `* y) v( f0 H
// list from timestep to timestep
# O7 x. v. Q% ]8 w, y e2 {; ~ 6 Y, l/ e) l6 @ O0 q
// By default, all `createActionForEach' modelActions have P, I7 w' f8 i$ n# M
// a default order of `Sequential', which means that the$ @' Y# e, W% z, u4 H" i1 j2 J# F
// order of iteration through the `heatbugList' will be* V8 L/ W' A F( f9 [/ n; ^
// identical (assuming the list order is not changed2 c6 v; I$ g4 p; G
// indirectly by some other process).+ H9 C T" s9 i5 N
6 y2 ~- y1 e5 x( k modelActions = new ActionGroupImpl (getZone ());: _% U' H' F9 c7 [& C
& Q9 t' |9 G, O1 T. Y+ U
try {" D7 D, c3 `( Y& W l
modelActions.createActionTo$message
0 q( G- c2 g. S: @) I. u& _5 Y0 b9 N (heat, new Selector (heat.getClass (), "stepRule", false));
/ z0 p1 R8 e4 H( T1 V } catch (Exception e) {5 |% D+ m$ p4 R! t2 `4 P% K
System.err.println ("Exception stepRule: " + e.getMessage ());
* g1 Z. V/ S; l$ P% v4 Y% ]8 v }9 ~0 t u# u) C/ [
( E( D! V( U. v/ q; Z+ E; i try {
2 [5 u. y6 L9 r/ Y4 b Heatbug proto = (Heatbug) heatbugList.get (0);" i! f' |$ }8 Q, A! I2 f8 u
Selector sel =
: {; Z6 G$ j& V3 i new Selector (proto.getClass (), "heatbugStep", false);& q2 j+ a9 d8 `1 W/ y. v
actionForEach =
; Z/ [- B3 a- Y$ w% l modelActions.createFActionForEachHomogeneous$call
9 o, Y* |0 ?$ K$ \$ f1 M (heatbugList,5 z; ?& ?: A' Y( B1 k, {) G
new FCallImpl (this, proto, sel,
h9 n ]9 i4 P new FArgumentsImpl (this, sel)));
' a8 v$ j! r( e* k6 h c3 B" q$ X } catch (Exception e) {
+ x' l! c: @" i/ J* @3 |! i/ z e.printStackTrace (System.err);
3 ?1 ^/ ~( J) `$ v }- f w" j5 I& V8 \
. E) f2 E! [, @6 G; u
syncUpdateOrder ();, C/ o/ F6 @$ J6 g
2 z' S- v/ {5 f6 X try {' ~% s2 I6 Z& r- E4 c( ?, H0 ?
modelActions.createActionTo$message $ ~) ~. p h0 e9 W# h; n) N
(heat, new Selector (heat.getClass (), "updateLattice", false));: {4 s; D f- ] r. p
} catch (Exception e) {
( v* Q6 ]. `6 I System.err.println("Exception updateLattice: " + e.getMessage ());6 T5 s5 v1 p& D0 M# u7 B( M
}
& o2 b) o! d) v* p4 e3 G# I
7 N: J4 T3 R! v. X) D, S5 S2 p // Then we create a schedule that executes the1 y, ?% u. l! j8 l, E7 |! {
// modelActions. modelActions is an ActionGroup, by itself it2 E: w+ h$ u7 z; N9 X
// has no notion of time. In order to have it executed in4 B# n4 M/ B3 R( H* i( d( o3 E# {% D( y
// time, we create a Schedule that says to use the
+ S7 H6 t: O- x+ w2 Q // modelActions ActionGroup at particular times. This
, s# @9 m6 ]3 r5 }% z9 G // schedule has a repeat interval of 1, it will loop every
+ t! B- z/ i5 h5 h; \/ S; [ // time step. The action is executed at time 0 relative to
! I- R6 i- ?4 k- z4 \2 W" m7 _2 X // the beginning of the loop." O5 d7 W ?& m+ {
& `% m3 G7 w# u4 h // This is a simple schedule, with only one action that is1 {# b, A5 ?6 k. s, j
// just repeated every time. See jmousetrap for more
3 t2 {& s. { ^& C0 k // complicated schedules.: K; Q0 d9 { e$ i# N' d4 G2 s
8 ]1 o! e8 e9 b+ v modelSchedule = new ScheduleImpl (getZone (), 1);4 G: U7 f8 b4 G9 l8 o6 t+ I1 `
modelSchedule.at$createAction (0, modelActions);; c$ C1 o, P0 u( n p2 @
" d* L% E: z! R4 F- N7 n
return this;
0 p$ N/ L5 z$ B k7 E6 ]# _8 r } |