HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* [" U/ S9 h& S# b2 W' z
% M+ L! p. v# S$ r public Object buildActions () {
' o% o: _; t. _ super.buildActions();0 g. X% P) R# E/ R" G. Y+ w! |/ @
. Y* E* o# \2 f6 Z
// Create the list of simulation actions. We put these in1 q$ L2 t6 f9 } P- g
// an action group, because we want these actions to be8 I: g& R. x- t1 \: {
// executed in a specific order, but these steps should
: f# P) h# i7 b- }- }$ U* Y2 ~. ~ // take no (simulated) time. The M(foo) means "The message
5 s5 C, r" X d+ f+ z& z) J7 j+ m // called <foo>". You can send a message To a particular
3 S% i1 {$ e# k+ U6 {. D- z( W // object, or ForEach object in a collection.
% X: N& S3 E5 L# V" A' ~ ( D' i) ^% ^, n( s7 ]+ ]$ M8 g
// Note we update the heatspace in two phases: first run
; E2 L, X/ d4 B: k2 R8 Y // diffusion, then run "updateWorld" to actually enact the
! g+ v! V+ ~8 i+ h- |! | // changes the heatbugs have made. The ordering here is3 P. ] i! i1 r/ r0 b8 v
// significant!
, H1 P8 @/ P$ D 8 U, j) h# y2 I/ d/ M
// Note also, that with the additional# |' w/ ?9 }4 z" z- X4 r3 D# W
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( q; j1 c1 k( h% c9 i, ^) x // randomize the order in which the bugs actually run
" @9 \& ?; \9 R( `$ |2 f // their step rule. This has the effect of removing any
+ L* m( D& @. f4 S3 R // systematic bias in the iteration throught the heatbug
4 c' |7 n( D7 `! ?% _, k ~: j, ]/ T // list from timestep to timestep7 \7 [* ?6 [- n
- A9 F: @9 v! M* z! i5 X& [ // By default, all `createActionForEach' modelActions have7 u' n1 X5 n; f/ F
// a default order of `Sequential', which means that the! D9 N, S3 m* t* M$ ?0 B
// order of iteration through the `heatbugList' will be
' T+ _+ a4 n; l+ W1 P* K // identical (assuming the list order is not changed
: g( m6 d" @/ ~9 _' L // indirectly by some other process).5 f( v( B( a1 h. O
* l' J5 A: n7 m+ K
modelActions = new ActionGroupImpl (getZone ());
& j& h J, l# R, w, _
4 L# I2 E. j1 s) D9 V try {
+ z& L1 R: r/ e% Q* b modelActions.createActionTo$message
/ I: K; h% x$ ]- R (heat, new Selector (heat.getClass (), "stepRule", false));
; X/ |* t1 u _3 H& V+ W" Z } catch (Exception e) {4 L+ q8 u1 a% K6 h! o/ R4 O
System.err.println ("Exception stepRule: " + e.getMessage ());
- ], K, ]) W( H7 J) Q' G5 G }
! O5 O* E/ g# W- j, X0 B" e2 ?7 q0 ~- i: X* g8 J' `
try {6 T# j5 f) z0 q8 @7 j% i3 q
Heatbug proto = (Heatbug) heatbugList.get (0);) d6 V5 ~6 C7 Q: z6 y
Selector sel =
/ M8 Y& x2 V% a. `5 H- O$ b new Selector (proto.getClass (), "heatbugStep", false);- \4 r. O- j1 n+ c w
actionForEach =: L6 E) @% c- ]) t% j) P
modelActions.createFActionForEachHomogeneous$call
% z7 d9 N" {1 H7 `8 p9 f (heatbugList,% t: s' E6 q& F7 z) I v/ S( m
new FCallImpl (this, proto, sel,# g( K2 ?8 @* v4 R
new FArgumentsImpl (this, sel)));
; Q3 ]$ ?: q8 C& l, c* x( H5 T( Q) m } catch (Exception e) {7 b! K9 h7 ?$ F9 H
e.printStackTrace (System.err);
2 o8 e& p/ ?2 G7 Q5 ^ }8 C8 R3 k& c& A* A1 V
3 U$ y6 X7 e, \* m* R7 a2 w& v/ R syncUpdateOrder ();" {$ j/ e; U' N& s/ k! ~
! j+ K$ B$ |. K; b- D$ d
try {5 [ k( d) | i7 n
modelActions.createActionTo$message ! v: w8 ?2 \) p* _% ^
(heat, new Selector (heat.getClass (), "updateLattice", false));0 j+ R0 q* r+ p( F
} catch (Exception e) { S* O9 M' ~& }" S' L5 e5 t
System.err.println("Exception updateLattice: " + e.getMessage ());
4 e* N- I1 K* j) A }4 L( E% n& [% H% }" ?
! r' ]+ |7 B3 n4 R& R
// Then we create a schedule that executes the$ i0 b4 _% n5 Z+ H, J: L ~
// modelActions. modelActions is an ActionGroup, by itself it% x0 `+ e5 ~9 l0 M& z$ D
// has no notion of time. In order to have it executed in
: K7 X% V& f" I% [* F- I2 r2 B // time, we create a Schedule that says to use the8 I& U: }8 H: Y, Y: V5 ^
// modelActions ActionGroup at particular times. This
0 r& L2 [& y+ s // schedule has a repeat interval of 1, it will loop every
& r7 k# f9 z# L // time step. The action is executed at time 0 relative to! P; g1 [$ P2 u- T6 @
// the beginning of the loop." ?% ^/ u0 u/ u5 z0 S ^
" A5 c. h* M; S5 K
// This is a simple schedule, with only one action that is# z7 f3 G; |" a" i I9 ^
// just repeated every time. See jmousetrap for more
- M% u* b+ y) ?8 } // complicated schedules.8 ^/ R$ ]7 Y G9 P' X, ?, t; i$ h7 a
& g/ R) `, b9 I+ R1 e2 k modelSchedule = new ScheduleImpl (getZone (), 1);( G; K" Z0 S+ E9 X& H% I
modelSchedule.at$createAction (0, modelActions);
8 S. _& C0 x7 Y# F R% T" o0 M & x* b9 m, U' `; S! q
return this;
: d6 W( e4 X$ K% P8 z- N } |