HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# D$ E2 F. T, ?. E; E
+ ~% B4 e) k2 q1 }. p/ X8 p public Object buildActions () {! _9 k4 y5 G& u A- y# K1 @
super.buildActions();7 h/ L- s6 q2 p
; C+ A% ^: w! t: [4 C# N // Create the list of simulation actions. We put these in
! R0 N+ J+ P, F7 O* \; |" ^5 V // an action group, because we want these actions to be
. [; E, i( E* M+ c% f1 R, G // executed in a specific order, but these steps should
& u- e& N7 w4 a& `( w0 x! Z // take no (simulated) time. The M(foo) means "The message
4 @& V. x$ m% K' {+ }" B // called <foo>". You can send a message To a particular+ e0 x$ ^% Z, l; [ k8 e
// object, or ForEach object in a collection.
2 ]: ~" f, y$ a5 G `
/ G) |3 X2 f$ M1 a Q j1 J+ ? // Note we update the heatspace in two phases: first run& l) p7 {* c. C1 I8 u3 I
// diffusion, then run "updateWorld" to actually enact the9 J4 c5 |0 g% Q; L, j
// changes the heatbugs have made. The ordering here is
# T$ J& F" V3 `/ ?4 b6 g // significant!
6 [4 v T% j: W' p ( A# l6 M$ `5 _6 j/ c
// Note also, that with the additional
: X5 J) @5 T9 W // `randomizeHeatbugUpdateOrder' Boolean flag we can
) ~# }2 I, [8 o: ?7 N1 ^ // randomize the order in which the bugs actually run
* Y6 p" Y9 ^7 X // their step rule. This has the effect of removing any. y5 U7 ~. p0 z
// systematic bias in the iteration throught the heatbug8 A( l" u9 U! a& V# e O
// list from timestep to timestep1 z5 O: G7 `0 @' f) D
3 ]( C5 b6 J- X7 A' U0 h // By default, all `createActionForEach' modelActions have2 U' H4 _2 g5 C ^
// a default order of `Sequential', which means that the, X7 K. y4 y2 Y; Q8 I$ J6 g
// order of iteration through the `heatbugList' will be a) j+ p8 l1 a
// identical (assuming the list order is not changed4 P+ I! G* ]: T9 M
// indirectly by some other process).- m/ n- h5 c! M0 S" t3 i
! t! D6 O6 F, W. [: ]( m$ \ modelActions = new ActionGroupImpl (getZone ());" o$ n1 E( l& \2 Y* [5 ] F
8 w7 t1 J) B7 ]1 q6 g6 { r% H& }
try {) X$ v: X/ r7 P5 K: w8 b- Y
modelActions.createActionTo$message1 g& ^. G% {* q; F5 v: _
(heat, new Selector (heat.getClass (), "stepRule", false));% ^: e' q$ y- p
} catch (Exception e) {
* R2 N# b6 h8 _- T System.err.println ("Exception stepRule: " + e.getMessage ());
+ Q7 ~7 U# E: E [3 n& { }
4 ]8 L" I# c/ y) S6 y/ T
$ ?0 Q) F" y' p ~ try {
7 D, |- }3 \) ^1 Y Heatbug proto = (Heatbug) heatbugList.get (0);
8 g; [- g$ h) H+ ~: r Selector sel =
! v( o1 \& F: F# P new Selector (proto.getClass (), "heatbugStep", false);& G8 s* p7 I2 {
actionForEach =
8 f$ r- ~( R8 s% r0 i0 f modelActions.createFActionForEachHomogeneous$call
T1 h9 H. n* b* _% k+ C: s6 B* w* Z (heatbugList,+ w0 w) s9 z$ a- m6 y. U+ g
new FCallImpl (this, proto, sel,
5 x! ]+ d+ F! E& _( K6 s new FArgumentsImpl (this, sel)));
5 {. N' j4 |4 e6 p1 Y; x$ ?, X- r } catch (Exception e) {4 V8 D+ {; r' g7 g
e.printStackTrace (System.err);
+ Z( v7 ]! E( b& L1 [( r }! m/ W0 r4 p2 e/ p5 I
, \2 O) `' i3 d( P0 \9 y' \
syncUpdateOrder ();, p& k7 y) l+ G% D
6 x, H6 @& k6 r try {) _' k: G5 C& p5 R8 O" `, l
modelActions.createActionTo$message
" y) ?2 y" f8 C; z+ w, | (heat, new Selector (heat.getClass (), "updateLattice", false));
/ m0 W4 o4 C0 W F6 e8 b+ B6 w } catch (Exception e) {1 Z4 b d' N. j( a$ q% w
System.err.println("Exception updateLattice: " + e.getMessage ());: |/ z7 t9 ]% l+ A
}
+ O8 G+ }8 P6 u$ y2 v* _* ^ / o, e% t* m8 _+ C4 J- ~
// Then we create a schedule that executes the
m0 ~) e5 R0 Z) h X1 C9 t // modelActions. modelActions is an ActionGroup, by itself it
* Q4 P4 T( ]' L( \" |3 G8 z& w' e% ? // has no notion of time. In order to have it executed in& I# x n& b, o+ h; ~* _
// time, we create a Schedule that says to use the
6 J' v0 m% F' ?2 s! _ // modelActions ActionGroup at particular times. This
% k3 n7 R2 Q% R // schedule has a repeat interval of 1, it will loop every
) X5 I# ]6 @7 X, h4 l/ h7 T8 i // time step. The action is executed at time 0 relative to& \% a& l5 Y+ [# r
// the beginning of the loop.$ f+ k+ i/ W5 e8 s/ G+ N
1 M2 l( _* l# s5 A9 X' K0 @# z // This is a simple schedule, with only one action that is4 ]& n0 y7 C% G$ y7 s% W3 |
// just repeated every time. See jmousetrap for more
+ G6 ?7 K1 y1 ~0 y1 |: }# ` // complicated schedules.! b2 q& I( z; a. Z! g
. e' y/ y; H9 c modelSchedule = new ScheduleImpl (getZone (), 1);8 Y0 N+ C! h+ F$ g
modelSchedule.at$createAction (0, modelActions);" @ U9 n. V$ t7 ^! M5 E8 L
$ j- }) K8 H% m( ^: W( [ f) ? return this;
0 A1 T! G* `' t. N: ^# `5 j } |