HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' ^$ X8 S8 P) j, q; @- j# T
& W4 ~- o& |% l; u- G public Object buildActions () {/ @& t# ?5 P( p5 G! ~, g0 H
super.buildActions();) _, }5 k: f/ A: v( x
5 {3 _. K$ P; p0 Y // Create the list of simulation actions. We put these in+ M \) l6 \$ O) z: {& l4 U
// an action group, because we want these actions to be
) L3 `# H: ]! s' g // executed in a specific order, but these steps should! y7 @& Z9 G6 @6 K) ?. m# w$ m) H
// take no (simulated) time. The M(foo) means "The message5 V( k2 ]8 i: Y9 @
// called <foo>". You can send a message To a particular1 S$ z0 e/ c5 o* k8 Q O+ w6 i
// object, or ForEach object in a collection.
8 |2 W v2 U, Z1 J6 C4 { - V/ x. w2 q* B, t
// Note we update the heatspace in two phases: first run
- |- t( ~) p& \* v3 | // diffusion, then run "updateWorld" to actually enact the3 ?+ j( Z9 F- n7 m# ~4 ^- D1 i
// changes the heatbugs have made. The ordering here is
0 ]5 Z. w( X6 u // significant! i$ W* \0 K; Q" R+ B+ `) V
2 f9 X* t8 e, P, L& U- G1 t g // Note also, that with the additional
5 W# e k7 {& q6 k# C' q // `randomizeHeatbugUpdateOrder' Boolean flag we can
( ]: m$ r2 U- r: P# b6 M. I v& Z // randomize the order in which the bugs actually run
* Q( ^8 v7 N( H2 ~# O& T // their step rule. This has the effect of removing any1 X, F2 K) ?. ~; V0 a E; Y' ^
// systematic bias in the iteration throught the heatbug
+ Z/ O1 Y# j5 t4 {4 j. t# X // list from timestep to timestep$ d1 ~' F/ {& [) P* U/ _3 i$ w
4 K( f0 b7 R) J: G // By default, all `createActionForEach' modelActions have! L$ j& ~, R; H+ j$ [, o( J
// a default order of `Sequential', which means that the F) e. ~6 ~6 O9 g
// order of iteration through the `heatbugList' will be! X9 N) w6 S: `, k" A' Q
// identical (assuming the list order is not changed. F& _' |) s U* Z; b* H+ j }
// indirectly by some other process).& `" [- w& C% S9 D4 v2 v" @& i2 ?: I* U
) v) o6 i& O! M& p3 p: b
modelActions = new ActionGroupImpl (getZone ());
% @1 ~# u, z! ^4 s6 k; Y" R$ A( M3 j5 ]( t
try {; @* X* r: E; F9 x3 `# F
modelActions.createActionTo$message
/ Y% |( V/ x+ c3 i9 T- @" s O (heat, new Selector (heat.getClass (), "stepRule", false));3 K% c* p" h2 v8 f' C) ]! E
} catch (Exception e) {
9 d0 C% P O/ X; g0 s6 m System.err.println ("Exception stepRule: " + e.getMessage ());
x) y* r$ g6 X4 j }/ @7 v1 A2 o) K# T, m& c0 H6 a& L
% K0 R3 P# u$ p8 l! ^+ ~, [ try {- }) b% o6 p# T- I6 d
Heatbug proto = (Heatbug) heatbugList.get (0);1 m+ F! m9 _: j6 e2 \/ }9 |, O
Selector sel =
$ N/ w7 R( C7 {7 V7 e( ] new Selector (proto.getClass (), "heatbugStep", false);3 u6 u5 e% i9 `* L+ P ?' ~; v& H
actionForEach =- F/ l2 `: W% H* H7 a' n
modelActions.createFActionForEachHomogeneous$call
" F6 @3 e1 b B5 n0 u (heatbugList, h( _+ [# c6 f# V2 g
new FCallImpl (this, proto, sel,
, N, w. F Z- z1 Q new FArgumentsImpl (this, sel)));4 W, F$ S( u( ^. p8 u& {0 W3 |
} catch (Exception e) {
l0 R7 I' D) J. G% R e.printStackTrace (System.err);# F4 \/ q; i% T4 T% n2 s8 ^
}
2 j; o$ s' A! w8 n
# Q6 v% Z+ d# M- u syncUpdateOrder ();2 K6 ?6 p! N7 [) i7 Q
. Q; ~2 _" b% @; i0 Q try {
* y* b+ F7 q7 }& d+ I modelActions.createActionTo$message
" ?6 f: K7 A" ~, R (heat, new Selector (heat.getClass (), "updateLattice", false));9 v5 O9 p: r4 t- I1 z
} catch (Exception e) {0 b9 u2 w( b7 X" O2 O. b
System.err.println("Exception updateLattice: " + e.getMessage ());2 K# u1 G* p) y+ u4 \
}0 v+ n5 @( B9 t0 V- d H
8 |! O7 p0 @4 r // Then we create a schedule that executes the, R" ] B& d& e, c
// modelActions. modelActions is an ActionGroup, by itself it) b3 W8 \6 ?0 ^2 J: D
// has no notion of time. In order to have it executed in! |7 K, ]; B* \7 [& r' P
// time, we create a Schedule that says to use the
) V' F9 M. t6 [6 X% |8 R // modelActions ActionGroup at particular times. This' u" g2 d% T8 z: S% {5 m
// schedule has a repeat interval of 1, it will loop every
) O& k+ o4 m+ y" d0 b // time step. The action is executed at time 0 relative to
: H% |5 S" W4 j, R // the beginning of the loop.: G. _7 T+ M; M4 G/ {
& u0 a2 N ?4 g; Z/ e // This is a simple schedule, with only one action that is
9 k) J7 `6 B# ~ // just repeated every time. See jmousetrap for more
4 e% ?" [1 Y+ \/ V) {. }% [. b // complicated schedules.
C, h# I, F9 v
* n, {/ a' J: x; Y9 D modelSchedule = new ScheduleImpl (getZone (), 1);
( V- _" Z1 Z6 c# k' U2 m0 q modelSchedule.at$createAction (0, modelActions);
1 g5 ]2 N( D8 o, W' J' R0 n7 V 9 i& @. X2 k& H# l* x5 c4 t
return this;% e+ g* T+ U$ h4 q9 |6 h
} |