HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( z" V5 b: C- T% b! d3 @
; Z$ ]# {* z5 }' c- \: m! e public Object buildActions () {) L' y% g6 B k- y! O, w m
super.buildActions();
! c9 w$ R$ B8 i; D# y/ Z `2 { e A3 z: t1 P* g
// Create the list of simulation actions. We put these in
0 h# B) \* c" u* t // an action group, because we want these actions to be
9 j+ ~2 J6 s1 R, q" ^, Q3 v // executed in a specific order, but these steps should+ M- U5 K$ |& X$ h* m
// take no (simulated) time. The M(foo) means "The message
$ ^" P) z" B, H1 h) K // called <foo>". You can send a message To a particular- R; N# L( A1 D3 ?8 j1 n! J
// object, or ForEach object in a collection.- N2 C, Y. X& @3 _3 y+ O
$ B, u8 R; w: f& d: N J
// Note we update the heatspace in two phases: first run
2 R! z, u$ K3 z8 ?/ v // diffusion, then run "updateWorld" to actually enact the
[6 n8 J8 @, ?2 B# d) D( s" i$ m. o // changes the heatbugs have made. The ordering here is
/ V& @3 W9 Q* g+ C8 M // significant!/ t3 ]8 A' h+ d3 Q0 Q
4 P7 Z. }2 U* K7 a7 s // Note also, that with the additional
" T) G, U. z2 A! E // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 `# C" q; |/ P! Q# ?5 z+ Q [4 ~: Z // randomize the order in which the bugs actually run
$ w; ?6 H! r2 V" Z. [' h; y // their step rule. This has the effect of removing any
% J* q# Y' T! H! D! B // systematic bias in the iteration throught the heatbug
: S% r/ s: ?& b // list from timestep to timestep9 o0 s d* _" E; [( b
& e3 u4 S" \5 T0 ` // By default, all `createActionForEach' modelActions have$ _9 J4 B1 ]! H+ D a
// a default order of `Sequential', which means that the1 Z/ {8 m( G, w% i
// order of iteration through the `heatbugList' will be5 [$ x0 t7 t6 R! a h4 N& Z; H
// identical (assuming the list order is not changed
7 o. v7 T) s9 o; l% y // indirectly by some other process).9 d1 @3 c- ~0 I7 W4 j' R3 q
; v: e, k2 W C9 ? modelActions = new ActionGroupImpl (getZone ());8 A, o1 g" n1 k {2 o, \0 }6 B
% Y, e% N2 T* L1 o8 H e6 h+ U5 `# h try {
3 _9 i3 ~, M. W7 d modelActions.createActionTo$message
1 R% ^! f- t# N T: M% p- b2 I& B (heat, new Selector (heat.getClass (), "stepRule", false));2 W" D% a" v' Z; S" U, u
} catch (Exception e) {/ j1 H1 P; S4 q/ W# Y4 l
System.err.println ("Exception stepRule: " + e.getMessage ());
3 A# i3 F! _9 `; R }. C- u- y* t% m. h( s
! U5 o- W7 D$ W; p6 t
try {
# s8 y; X% p8 Y, U; ]& S Heatbug proto = (Heatbug) heatbugList.get (0);% L1 t% T2 z7 _: D8 H
Selector sel = ! {: L6 V2 I' x4 G
new Selector (proto.getClass (), "heatbugStep", false);
. H7 @" a3 M9 i: s& [/ u9 ? actionForEach =/ u8 j: U- K7 S' @% D0 L9 m) v- Q! A
modelActions.createFActionForEachHomogeneous$call
' J4 l1 x9 h; B (heatbugList,
3 u7 X. f- V4 j' t new FCallImpl (this, proto, sel,/ M% H d! p' Z$ U6 G% x: j0 N
new FArgumentsImpl (this, sel)));$ M0 Z9 S) A8 B" p3 v
} catch (Exception e) {% `! ]( K% y9 C! y& v4 y5 V
e.printStackTrace (System.err);; f7 S) S( V0 [% \
}# I6 `0 L9 C2 e8 c1 ]% O" j; q
+ [$ H5 }/ r0 P
syncUpdateOrder ();' v, @# A/ Z2 y) s. O/ |" d0 c& X
! N0 ~# Z# ]2 ~- T! u/ N6 ^7 w
try {1 {' m* n; Q8 e2 J* @
modelActions.createActionTo$message
# V0 p" |, g% S1 W$ _* u N (heat, new Selector (heat.getClass (), "updateLattice", false));
5 c$ {$ C/ I8 A8 a+ o } catch (Exception e) {
- M& l; w& A" r: u% f System.err.println("Exception updateLattice: " + e.getMessage ());
, l* Q1 h5 i) S' o }
( W8 |; q; K" ^& e: a- i t ! D, l+ \1 s3 F' v
// Then we create a schedule that executes the
/ e8 Z' _3 i0 i j // modelActions. modelActions is an ActionGroup, by itself it
8 s; I; O1 A- }! e& a- j0 y- t // has no notion of time. In order to have it executed in; w$ J8 C" n1 y
// time, we create a Schedule that says to use the
" r- D5 o7 V6 c9 Q$ x( B // modelActions ActionGroup at particular times. This- L; E {9 {) x+ M+ Q3 J e4 z
// schedule has a repeat interval of 1, it will loop every! b8 ~9 A6 b: G$ X5 Y! o% R
// time step. The action is executed at time 0 relative to
- n5 X# o8 Q' }0 D // the beginning of the loop.
1 O7 Y1 w6 l+ b) D
; u2 U) E% X H: l // This is a simple schedule, with only one action that is: w# {6 O0 l' x4 I1 m+ W6 _" |
// just repeated every time. See jmousetrap for more
' e6 N- |2 i Z G" a2 p // complicated schedules.+ c! o8 q3 s* w, K$ A
, [6 }; r* B. U2 I& l# i- Q
modelSchedule = new ScheduleImpl (getZone (), 1);, Z9 V4 A- r( s/ Y) {3 ^
modelSchedule.at$createAction (0, modelActions);
8 n) E( {: I2 k1 {$ _. v + U' s, R& e* | x
return this;
! r( v2 F- J0 ^& B \8 c9 _ } |