HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 o9 ?+ L7 X& K6 _4 {; o' U, t+ p( D% ] v' R' L$ [8 R
public Object buildActions () {% ^' F- O& f7 t
super.buildActions();
% s7 o' y4 `5 i8 W/ p }1 g% b( N x" f0 C3 {# I% u% j5 {/ l
// Create the list of simulation actions. We put these in
- `( ^8 b& m# R( y6 A // an action group, because we want these actions to be) e8 z2 }( F! E8 t( b$ s. A
// executed in a specific order, but these steps should
+ {- g- X3 x# R+ h; h // take no (simulated) time. The M(foo) means "The message9 v! N, G: l4 u9 l) j- n' H$ r
// called <foo>". You can send a message To a particular: w7 Y* F, M+ U" ^
// object, or ForEach object in a collection.! z# ^: m7 x' q+ W" G7 ]: x+ n
% ^& `+ v3 @0 h# E
// Note we update the heatspace in two phases: first run5 G8 M% `$ J& b; H5 C
// diffusion, then run "updateWorld" to actually enact the' N% q6 @6 l5 y w! s
// changes the heatbugs have made. The ordering here is
1 r7 i1 t }6 S% s1 b5 J // significant!" ?( [7 c$ ]( G
* U) V& w1 L7 W7 n6 D( I7 G // Note also, that with the additional
, N. i2 t$ U+ R) }" ] // `randomizeHeatbugUpdateOrder' Boolean flag we can% V w( G4 n. C! X, y5 B
// randomize the order in which the bugs actually run% `( r# V; X n1 A; x8 W
// their step rule. This has the effect of removing any6 `: V N9 \# k' U
// systematic bias in the iteration throught the heatbug
& p/ i4 a% h% x# h5 b2 x8 k0 ^ // list from timestep to timestep$ R/ q4 D+ U8 D
/ J$ }3 a9 n# B2 C* K+ Y( \ // By default, all `createActionForEach' modelActions have
$ ]2 X) O e3 ?* w // a default order of `Sequential', which means that the
4 J5 n2 X) m. {1 G // order of iteration through the `heatbugList' will be( K/ G. V6 ?( e. u) Y i4 B( N8 M
// identical (assuming the list order is not changed' A/ Y- l" P' w+ D
// indirectly by some other process).
) s0 P/ w1 {( r/ k3 W1 Z6 W& v
9 @$ H) q. v) m% ^/ j8 w6 @ modelActions = new ActionGroupImpl (getZone ());
$ D& L' q! i* Y! E8 C( e; d
' y8 g$ w2 X( |9 Z try {$ f; K9 N5 w- f. W2 d; H+ U4 w
modelActions.createActionTo$message, P8 t: S9 Z9 O# `% k b _- L
(heat, new Selector (heat.getClass (), "stepRule", false));% z3 Y# W- U2 q
} catch (Exception e) {% ` S4 B/ L9 l2 l. X
System.err.println ("Exception stepRule: " + e.getMessage ());
0 l" }( m) X' G' i }$ X, n4 C6 q! a$ o& T8 ?
; ^! k9 h/ A; h
try {, K7 s& [# w) |0 \6 V- A# R; ^
Heatbug proto = (Heatbug) heatbugList.get (0);, [8 B! z) c( g; | ^
Selector sel = % E: x1 q# b( T& e0 g
new Selector (proto.getClass (), "heatbugStep", false);% [) P: Y2 o! i2 T H. b
actionForEach =
0 H x K' m- ?, _% K5 w6 c modelActions.createFActionForEachHomogeneous$call M% L* X% Q: d# L5 ?0 a/ f
(heatbugList,
9 E& _9 a4 V7 j% n new FCallImpl (this, proto, sel,
# ]/ ?6 t5 X* a6 _* p- R# S new FArgumentsImpl (this, sel)));
+ u& }3 D- }' D+ t5 R5 q( E% W } catch (Exception e) {
" V; L1 I: `) r" b9 W$ y/ X e.printStackTrace (System.err); [6 ?( P! ^& M1 k
}
1 v7 `4 o$ G! m; N: M. s
! y- N% B& P- k# x# M syncUpdateOrder ();0 r; y/ G; E- V c3 Z
t, Q) d- ^. t8 q# x, s2 o7 x try {
3 d# Y! e3 I* C! `1 y% m# k. A modelActions.createActionTo$message
7 j* c1 G9 K* C( ~ (heat, new Selector (heat.getClass (), "updateLattice", false));( ]! m* G4 e- D3 T G/ y
} catch (Exception e) {) r* D; s" q5 y& c0 q: P% l. D0 ~
System.err.println("Exception updateLattice: " + e.getMessage ());0 E) X, S( G% g, G& P
}
. Y4 F5 L7 N: s( T% F( Q" K
7 l4 m8 c ]+ v1 z% x // Then we create a schedule that executes the
$ S Z7 f6 ?* b% w // modelActions. modelActions is an ActionGroup, by itself it
6 R+ H# J4 u4 E0 b // has no notion of time. In order to have it executed in8 ~+ _% e1 \7 o. @& K; P
// time, we create a Schedule that says to use the
9 R o! A! P# x Q // modelActions ActionGroup at particular times. This8 O6 Y2 r3 t) V
// schedule has a repeat interval of 1, it will loop every
9 V9 g7 n( [/ {$ [6 Q) `8 r; b( G // time step. The action is executed at time 0 relative to
/ h8 R: B, u2 D- G i( j+ { // the beginning of the loop.
1 [$ \" v- P$ i# U. g3 R: k; T. u% |. |
// This is a simple schedule, with only one action that is
5 P9 X& u% Z/ p, x // just repeated every time. See jmousetrap for more
5 G( `6 p1 ^+ O0 b: s- _ // complicated schedules.
& G& H( }7 a/ p
6 l3 F5 _8 v3 h, J A1 I modelSchedule = new ScheduleImpl (getZone (), 1);8 E) K+ h2 M4 d; n7 G }2 K
modelSchedule.at$createAction (0, modelActions);2 F4 A* e0 \2 q* o0 ]# X6 E
% i2 |* N, z4 D4 i1 w" W1 r* y7 Q return this;; J1 P) i- j0 O- ?4 U& r' I
} |