HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: [- K. I: o) e4 B' T
& K' q6 B3 H; R+ z& ?, o9 `* m public Object buildActions () {
E5 X" U2 y+ @( ^8 ^; G super.buildActions();( L' L* Y' E1 u* R7 ]$ f
7 R) W/ K( x3 D% Z // Create the list of simulation actions. We put these in" n3 A( I5 V. f( v/ K
// an action group, because we want these actions to be! y- \+ }* d& E- |. L0 W# x
// executed in a specific order, but these steps should
6 x# T4 V6 t+ V `) |( | // take no (simulated) time. The M(foo) means "The message3 f# i& a- V8 V9 b7 k7 U) D
// called <foo>". You can send a message To a particular
1 }7 M2 K+ V1 Q, n3 @ // object, or ForEach object in a collection.
# U; d6 B& ~ `: A* C7 a# Z - I A, K9 f4 k" G3 Z! M0 e4 V: u
// Note we update the heatspace in two phases: first run
4 i- E3 P, N" d5 S# B$ h // diffusion, then run "updateWorld" to actually enact the R n: N" w/ F' X& c# c6 T
// changes the heatbugs have made. The ordering here is7 _$ Q" {- g9 k N+ i9 y
// significant!
3 `& v+ C; ]+ m7 o3 }
9 q8 v0 T2 m' Y9 t0 P% u c // Note also, that with the additional0 M8 B. E) \8 k$ c) ~! W; F
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ m7 g/ C. M! S G$ X: T
// randomize the order in which the bugs actually run
& g, q1 O ^6 L% O8 E8 T8 a // their step rule. This has the effect of removing any
- _. x; _% Z% V: Q# K! y6 ]6 K4 S; c0 S // systematic bias in the iteration throught the heatbug
7 p! n ^5 A" f; T6 J, {6 l // list from timestep to timestep
5 P: V* z3 C7 }( r; {% v! U% X. w5 Y
! F3 i5 g' K) K5 Q9 [3 d4 J! \ // By default, all `createActionForEach' modelActions have
" q/ A2 s3 W# v! p/ A // a default order of `Sequential', which means that the
- V0 W5 M. T+ l( Q8 v // order of iteration through the `heatbugList' will be
. s3 Y! I: h. q6 e% r // identical (assuming the list order is not changed$ K0 S8 B/ j( ?$ d" m0 l
// indirectly by some other process).
. d) s3 a+ G+ M7 I6 b, C& z 5 Z; t+ l7 _/ `. T: j
modelActions = new ActionGroupImpl (getZone ());5 P' J5 F' E0 n
/ r6 ?3 l; g* Z! e g7 r+ P! g9 w
try {# o5 ]& N C; R1 i: x( ^, b: w
modelActions.createActionTo$message
5 y" W7 K0 ]) s5 O- a" p (heat, new Selector (heat.getClass (), "stepRule", false));
8 q4 I; f" d! S& ], Z0 | } catch (Exception e) {
/ \! l6 t* X9 i, w/ O* y9 `8 G System.err.println ("Exception stepRule: " + e.getMessage ());
% v+ B7 ?2 g3 s& l) F$ {# i4 s h }
& o. @: b/ }- t. }) o# z4 D
0 Z4 ^! x. @" O& I" ?: a' b% `4 s try {
9 O" q [0 W4 H Heatbug proto = (Heatbug) heatbugList.get (0);" o9 {6 P# ^' |
Selector sel =
' J" ]3 ?# s7 } new Selector (proto.getClass (), "heatbugStep", false);; k* m3 s# Q) W Z$ u
actionForEach =$ M0 @+ |& [5 @& [0 H
modelActions.createFActionForEachHomogeneous$call' T: B$ c0 K; k3 I! B% N! {
(heatbugList,2 b" ]- S4 c, n4 O# v
new FCallImpl (this, proto, sel,4 D" O+ p( l& o4 G$ K1 C
new FArgumentsImpl (this, sel)));0 P ^! O- V+ n0 A- m
} catch (Exception e) {9 C9 S4 o6 C& d0 R: L- z% ~2 w
e.printStackTrace (System.err);- z2 S% w) S/ o5 T( ?6 M; F% v" v
}
, }9 ^) w- K& Z# y5 ~
! V7 }( m0 b. f+ u9 ? syncUpdateOrder ();
5 u8 W9 W- t& C
# q* L: ]. R) m- k9 a- a& G; P try {& I7 J6 t- ?, w7 f; M. {
modelActions.createActionTo$message " Y9 i7 _' q% d4 q% o5 P/ L
(heat, new Selector (heat.getClass (), "updateLattice", false));! c0 x2 D o" @( r( K( q
} catch (Exception e) {
Z$ H3 @9 r" N9 G9 Y E; @ System.err.println("Exception updateLattice: " + e.getMessage ());
' i$ k" x; @0 Q }- i* g. a6 m. \6 S7 u
9 F: s/ E: B9 p! T- ~
// Then we create a schedule that executes the/ @% H8 F; E1 P+ u$ V# v# m
// modelActions. modelActions is an ActionGroup, by itself it
' l4 ?/ O% H# R2 [0 c: g, X // has no notion of time. In order to have it executed in# @8 u) z6 o/ R( g% }0 [) i, P
// time, we create a Schedule that says to use the1 M7 y/ h- H0 N. U) Y4 U
// modelActions ActionGroup at particular times. This
5 `3 d8 ?4 {; `4 n2 n0 q // schedule has a repeat interval of 1, it will loop every0 {: ]' Y/ G' g* b: F; M! O/ v& g
// time step. The action is executed at time 0 relative to
1 y' o h% x, ^, o // the beginning of the loop.
/ Z4 \6 {# m5 E7 D0 |+ B, t6 Y' w+ k3 o7 y1 K& @
// This is a simple schedule, with only one action that is
9 N' X5 G& x+ [ // just repeated every time. See jmousetrap for more
, f3 X1 Y: l( D3 @! K ~9 x$ Z // complicated schedules.# ?: x. s/ {# {
5 R& P- Y3 W0 i9 i; R modelSchedule = new ScheduleImpl (getZone (), 1);
7 z! J. x3 G" C( u modelSchedule.at$createAction (0, modelActions);
! y/ N k1 S. p, a' h6 }$ p
1 d9 C1 }! B, x8 c return this;
' }. |5 _. B! m" X& }; } y } |