HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ |! g" q4 |- G$ X# V' o7 s. @4 J; R2 w
public Object buildActions () {
6 I( e5 a% I. T6 X9 m( l* i7 N; C super.buildActions();, b% N3 z8 y6 t# E0 U& a9 V
7 A1 H; ^# c4 y' C8 R // Create the list of simulation actions. We put these in9 T; c+ Q7 x9 l, `; s" z
// an action group, because we want these actions to be. W8 v, T: v2 y- L1 g$ h# m
// executed in a specific order, but these steps should/ p/ Q; F& Y* p G
// take no (simulated) time. The M(foo) means "The message
$ ~& ?8 ^# B4 t% @. ^ // called <foo>". You can send a message To a particular* V9 D8 m2 p( H" C+ A: o
// object, or ForEach object in a collection.9 x* g1 M8 m) Y: c+ B! {7 v
: I4 w7 s# {$ U( B8 I9 V
// Note we update the heatspace in two phases: first run% g: Q2 P" S* {3 p; o
// diffusion, then run "updateWorld" to actually enact the5 C: R v( y7 z- a k: H: b& n) V
// changes the heatbugs have made. The ordering here is" w" N! O( C d( y- Z
// significant!2 q- Y6 i; h R$ I, L
* ~9 [/ v% \4 U& ~, C6 s# N& t
// Note also, that with the additional
- v' u4 u& m- r+ [ |! T // `randomizeHeatbugUpdateOrder' Boolean flag we can
- e- w; m+ J' ?( y9 u // randomize the order in which the bugs actually run% L, n' k: L2 h# z5 X- E
// their step rule. This has the effect of removing any
' s2 j" ~3 m6 s. z. M! w // systematic bias in the iteration throught the heatbug
) \, ^+ n- @4 v1 v$ Q6 Q' B% I // list from timestep to timestep
& R3 ~5 w e1 J0 o5 s z! Z0 J u2 T+ w5 _) R
// By default, all `createActionForEach' modelActions have. M) |. F$ R$ o5 ?
// a default order of `Sequential', which means that the3 `/ \, z9 D/ f) F( e: ~; Y1 e
// order of iteration through the `heatbugList' will be
$ K* F7 C! i& W // identical (assuming the list order is not changed
6 ~5 o9 Q: c3 ] // indirectly by some other process).( s9 H# n( i& f; D; C7 j
/ L) ]! ]2 ~; w modelActions = new ActionGroupImpl (getZone ());2 }+ {4 Y) v+ G* Q0 P
) P! K% U" c2 e7 K6 e# Z* |+ } try {
+ A1 W6 l; @" \+ H5 Y0 ^ modelActions.createActionTo$message
3 F* Q' {; I- a (heat, new Selector (heat.getClass (), "stepRule", false));! Q! b6 v8 S: V
} catch (Exception e) {
/ J/ ?% U+ |" L' \ System.err.println ("Exception stepRule: " + e.getMessage ());
1 n% |2 B! |1 V0 t }! R! Q+ E* y; `: q7 }5 C
7 s/ } V. G9 D* f4 \; @ ^7 U
try {
$ t2 e" i b5 q( J7 | Heatbug proto = (Heatbug) heatbugList.get (0);
! h Y# {; X @) o& E+ U6 u. V Selector sel = % r* I, ]) N4 a
new Selector (proto.getClass (), "heatbugStep", false);8 F+ s& W( D# }# ~, n/ `) l( v( a
actionForEach =
/ q" Z' j; D' I1 i6 Z modelActions.createFActionForEachHomogeneous$call
6 m7 F" \8 H8 A1 V9 l (heatbugList,
. w7 x9 ?1 R+ t& V new FCallImpl (this, proto, sel,
! M3 r- r( e7 f new FArgumentsImpl (this, sel)));
2 W! L M( |' K2 j } catch (Exception e) {
) X: {( F7 c+ u H( J e.printStackTrace (System.err);. y( W. j* W- F, A( w: U) w- c4 P
}; ~9 ~1 ^% P$ m: l
; W; t: I/ P, K
syncUpdateOrder ();9 r0 [. {% Q, d2 ?9 A
2 H3 c4 C: m/ V2 ^. Q+ j' g
try {
: `* E* O% w+ P- T" q6 b6 q: U modelActions.createActionTo$message 7 ?9 ~- P0 C3 f/ Z* m3 R
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 {8 S3 ^7 ^4 O! v/ Y: O } catch (Exception e) {4 H! Q' {! n, p8 v+ i2 O4 E
System.err.println("Exception updateLattice: " + e.getMessage ());8 y/ a" z7 O- ?; F6 M( e
} f& S( m2 }- T' N8 n
- R$ ]2 G3 \7 d( _ // Then we create a schedule that executes the
+ o9 F6 k7 a0 f- {) C; K& { // modelActions. modelActions is an ActionGroup, by itself it, ^2 e& Q4 ?! H4 a7 O1 \% `) H+ V% T
// has no notion of time. In order to have it executed in. O' b, [, V# f) V
// time, we create a Schedule that says to use the( e2 i8 \7 }, \1 @, E% `
// modelActions ActionGroup at particular times. This
! t' v( Q, Z7 @3 {5 I- b, E# @ // schedule has a repeat interval of 1, it will loop every g' m7 r1 F% [% U; K0 O9 _
// time step. The action is executed at time 0 relative to
; }/ t) U% _% w6 i" K( a // the beginning of the loop.
8 D: y. N( {4 Y- f! x
2 Y5 l7 z* s( w // This is a simple schedule, with only one action that is1 b8 N+ }; ?- x3 Y }) w& ?
// just repeated every time. See jmousetrap for more
& u7 J! C# l" U& [4 g. D* i) B2 N$ | // complicated schedules.
0 V3 m# L6 G7 e/ L9 v
' H" D8 V, T. }7 I: f modelSchedule = new ScheduleImpl (getZone (), 1);
* F* Q, A/ \ D' b6 | modelSchedule.at$createAction (0, modelActions);' a. j0 G+ e- B: `" U
# D3 Z+ b7 i9 G% ^( W
return this;
/ Y/ M3 d& j- y2 U/ s+ `/ q } |