HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 j# X1 l2 r& f. u( H9 _4 ^9 D* L6 Y, [) i7 m* X
public Object buildActions () {9 ?" i8 x# a. d1 I4 k' B9 U" t! t
super.buildActions();" g; s- p5 a+ w- Q3 Z% J- b4 k
; ]$ _1 X( H) p1 ^+ B0 N7 i- _& g // Create the list of simulation actions. We put these in- M- ~6 C7 U6 A- i
// an action group, because we want these actions to be
' }2 G, @* Q, H0 \$ n5 d // executed in a specific order, but these steps should: A. x6 }+ W2 F3 c
// take no (simulated) time. The M(foo) means "The message+ V$ i+ E# a/ u- H
// called <foo>". You can send a message To a particular8 n2 j- M0 J8 D' U9 u4 v' T
// object, or ForEach object in a collection.
2 W2 r+ U8 C3 L( @& t 7 ]1 o% s& |& j8 r- d3 R! u
// Note we update the heatspace in two phases: first run
6 Q: L* `$ }5 _- q; l4 U // diffusion, then run "updateWorld" to actually enact the
. [3 D! n& H9 `0 J9 L5 g // changes the heatbugs have made. The ordering here is
4 ^9 W; ?, [) n6 b0 N! n i4 f // significant!
' ~, C) |: N. b! L! V& w+ G ' w- v; l5 |( `- B9 f% v% }
// Note also, that with the additional
) V- n1 |8 F6 e' |; D. l$ Y0 p // `randomizeHeatbugUpdateOrder' Boolean flag we can5 _0 j: {# p J3 E% x7 ~
// randomize the order in which the bugs actually run
: r4 O8 Q* b3 z& Q# n. W; A // their step rule. This has the effect of removing any
! q6 F0 K0 F/ D( o$ G2 V! x, R // systematic bias in the iteration throught the heatbug) U8 m, ~. n0 X: s( o0 H$ @. ?
// list from timestep to timestep2 i* l* O% n+ h& t
+ y9 _0 p% |8 Z; `/ r // By default, all `createActionForEach' modelActions have: d( ~6 c8 A# O, u% V- {1 x5 ~
// a default order of `Sequential', which means that the- K u0 l4 ~7 Z M, L- f
// order of iteration through the `heatbugList' will be6 [2 P& ]( y3 R# {& P$ O
// identical (assuming the list order is not changed, S' N6 c" {9 g2 V( ^/ A! Y c
// indirectly by some other process).
& J6 q2 c A+ x- V" B8 \ {
( {1 T7 k1 |: Y/ o modelActions = new ActionGroupImpl (getZone ());6 u, K) S" O- z+ C4 |2 o f
& o0 o6 E1 i# C8 t8 D+ d try {
8 d, k; @% P/ ^! O modelActions.createActionTo$message
! A' t% c( M, N; l* R) l# q (heat, new Selector (heat.getClass (), "stepRule", false));
* t4 C5 ]4 F: P% J# m9 w t. N2 ] } catch (Exception e) {( h) @ H0 G% F# f
System.err.println ("Exception stepRule: " + e.getMessage ()); m- p8 Z: R+ y4 P
}; B' \$ j% U% p
G: [# p4 T1 N) `: W' a( H
try {* p3 J1 a$ }$ u! p- r) d
Heatbug proto = (Heatbug) heatbugList.get (0);
. q- O' T* h( _# g Selector sel = q& {& B' p* O! z2 Q9 _
new Selector (proto.getClass (), "heatbugStep", false);* n& {# {) e5 Y6 Q
actionForEach =
/ h/ k9 d! b; G5 K. W. A5 N modelActions.createFActionForEachHomogeneous$call) m1 R' ~2 y% H6 y7 V" A8 y$ q
(heatbugList,' v1 b7 @1 i3 _$ W1 o6 ~$ h- n! p1 W8 k$ B
new FCallImpl (this, proto, sel,
+ W5 |: _* C8 `* I new FArgumentsImpl (this, sel)));, s4 n9 @( y8 I; J
} catch (Exception e) {& d; A" b; `% d9 H: h. m% Q) B
e.printStackTrace (System.err);) N. h& F' S0 Y1 I* { \+ H$ I" l
}; k$ J2 ]. D n2 l$ D, W% r
, a4 B9 |3 e9 A2 D o- [+ O$ s syncUpdateOrder ();5 ~& n% ?' T2 X) y6 c" p
; n/ M; T f+ u7 h
try {
4 ^! `( M, D3 z. C: c# W modelActions.createActionTo$message 3 T) {$ ~0 ^ g4 t$ U8 \
(heat, new Selector (heat.getClass (), "updateLattice", false));0 |8 |, `$ v; j" U' z$ j
} catch (Exception e) {
6 a5 u- P+ P0 _& T. a3 V& @4 X' l System.err.println("Exception updateLattice: " + e.getMessage ());- K2 P9 f; [5 {% @# S( g5 r
}9 g" A0 ~3 \* R; [0 ~
' v3 m& R# R6 i4 ?
// Then we create a schedule that executes the
+ ^9 k! {6 ~$ F7 B: }8 {) m6 I6 ` // modelActions. modelActions is an ActionGroup, by itself it, P4 V. {+ y1 m) Z' u) A
// has no notion of time. In order to have it executed in6 t2 q3 ]0 ~9 x0 X, m" a
// time, we create a Schedule that says to use the
. @8 j7 \. I8 Y7 Y; J8 ^4 y+ [ // modelActions ActionGroup at particular times. This
; o0 [2 w" W) g& R4 l7 M // schedule has a repeat interval of 1, it will loop every
4 b: G4 g% o5 L q6 l T+ p // time step. The action is executed at time 0 relative to& a8 A6 O" Q2 ?8 n3 I: j2 J! U( x6 @: l
// the beginning of the loop.
\0 p& e8 `! G! ]8 p, }* r3 l: k# V9 ]4 N4 k3 O( q" H! |8 `7 X
// This is a simple schedule, with only one action that is, a! k/ s& O( a, ]/ F- d0 ^
// just repeated every time. See jmousetrap for more
4 d/ m& Q4 l4 v. t7 |) A8 E // complicated schedules.6 v3 t+ N! U) _3 Q2 @6 L
$ _+ B5 {6 a" F5 ~3 ^, ?+ c modelSchedule = new ScheduleImpl (getZone (), 1);4 t2 V- u; K* O( W' y7 _7 Q2 A. k/ d
modelSchedule.at$createAction (0, modelActions);, C2 q( n0 N& y _6 \; x& v
( ]3 L+ P, g. n0 Y% K6 g+ i return this;% N+ \" H- P8 ~ R/ {& c
} |