HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 h; a7 {% F1 @" t3 r y3 h% d4 X0 }
public Object buildActions () {
" f9 X( R9 ^' p& g super.buildActions();' H8 W2 F u) L" P' M$ E! ~
. Q# ~8 E% x5 z9 U" e2 I8 `" w
// Create the list of simulation actions. We put these in- Z6 l+ A! Y6 Z
// an action group, because we want these actions to be
3 i( P1 Q5 W& }) T* m // executed in a specific order, but these steps should- ?7 p2 @' W) o0 t$ [ ~" ? r( h
// take no (simulated) time. The M(foo) means "The message5 F: x$ Y |. p X2 x
// called <foo>". You can send a message To a particular
' k Q A2 e# } }4 V$ `/ n5 x% r& G // object, or ForEach object in a collection.
" j/ q7 W. W! _6 O/ G 8 M; X, K% h2 A; X. k z
// Note we update the heatspace in two phases: first run
7 k0 K) _! y) |$ j* K // diffusion, then run "updateWorld" to actually enact the: f' v$ n# R$ g3 ~
// changes the heatbugs have made. The ordering here is2 ^) G3 M6 [" d1 U9 ^
// significant!
9 ~1 \. A/ C: C. z+ W+ ?* q
+ N+ I# X/ B0 M0 D/ O0 i0 X9 c$ e5 O // Note also, that with the additional
4 `, k- z, j v5 {0 p // `randomizeHeatbugUpdateOrder' Boolean flag we can2 o: L) j( y3 }% v/ u
// randomize the order in which the bugs actually run
/ L! _* _1 d% e5 i // their step rule. This has the effect of removing any, @/ ^, R( {0 G3 e0 A, E
// systematic bias in the iteration throught the heatbug
. z+ P, b' j5 e. W; v# | // list from timestep to timestep
( r# y I! }5 R$ P+ j! [! o
/ u9 F& L# q8 `5 ?; u! F // By default, all `createActionForEach' modelActions have
; q+ S4 ~( {: R- E8 N // a default order of `Sequential', which means that the# b$ p% g0 F: w; E+ n' y) }0 T2 i
// order of iteration through the `heatbugList' will be8 ^ m5 n2 |& t _0 U# g. m
// identical (assuming the list order is not changed7 i4 `, q) m0 x! v
// indirectly by some other process)./ [: I5 Y& {6 q! ~* ~8 c
, L# Q8 H- K1 ?$ m' i modelActions = new ActionGroupImpl (getZone ());
) ?) N1 l g G; s4 A$ u" L1 x) X8 i+ s" S; a
try {
+ R& ]& P" P3 |! P3 h$ o/ G7 ] modelActions.createActionTo$message
% U1 h% `4 Y* m( I/ b (heat, new Selector (heat.getClass (), "stepRule", false));
. ]# N# `; X, @8 h7 ~# f+ Z& I } catch (Exception e) {
* q) u5 C8 u% ^1 J, K' g' C System.err.println ("Exception stepRule: " + e.getMessage ());( e8 r% k$ T+ E ] |! `& d
}( Y- R( f2 e9 T7 _- I1 m
- } L6 o3 T8 e1 s& U9 C X
try {# |% ` q# \: q1 h3 w0 p* K
Heatbug proto = (Heatbug) heatbugList.get (0);2 |6 V1 c& C5 w( G( g2 \
Selector sel = ! H$ \0 H/ y4 l+ J0 q7 d/ h) X
new Selector (proto.getClass (), "heatbugStep", false);; k! q& ]1 V9 U
actionForEach =- T8 N0 q# h1 V$ g. x+ ] a2 F% K
modelActions.createFActionForEachHomogeneous$call7 A1 p1 H7 n1 G9 I
(heatbugList,7 w- O! n. V6 u% B( d6 L" E o$ A0 C
new FCallImpl (this, proto, sel,
" ~4 R( Q+ @! ~8 X0 s new FArgumentsImpl (this, sel)));
# J; \# [7 c2 b+ N) y } catch (Exception e) {
& U9 p& i8 w x2 } e.printStackTrace (System.err);1 y9 I" [/ X7 Y4 b8 I
}
J# b" m( L! K! J6 c& e 0 o; g d, V. n! Z6 @$ h
syncUpdateOrder ();5 u/ T$ U) ?, i- G2 Q+ l
3 F5 o7 r2 d/ N9 X
try {. o: e$ H4 b) L8 P" A
modelActions.createActionTo$message
( t# a+ w1 i* U6 v* y (heat, new Selector (heat.getClass (), "updateLattice", false));
I5 V8 A; a, @( I" @$ D/ W } catch (Exception e) {
. K( I9 P) }. R- A' E System.err.println("Exception updateLattice: " + e.getMessage ());
2 L v4 O. ?$ `6 b" O) Q }) k( o- O/ h6 W. x K
, \1 y- [( _) a) G& \
// Then we create a schedule that executes the
' Z5 x" ~. X* w0 @, _4 m+ e6 [# q( Y" g // modelActions. modelActions is an ActionGroup, by itself it( w6 { y1 k$ E: q
// has no notion of time. In order to have it executed in
: Y$ j( \/ D% N // time, we create a Schedule that says to use the; ~ S2 m; s W
// modelActions ActionGroup at particular times. This! ? \5 G, V7 ] ^. t% Q
// schedule has a repeat interval of 1, it will loop every
. w+ B+ M' a8 g' g3 z9 G& j0 n // time step. The action is executed at time 0 relative to" _, h* S6 | D, l2 K; i
// the beginning of the loop.
, G4 D# |" f9 V7 t _" N: W
, u$ p# h1 U+ m% V // This is a simple schedule, with only one action that is7 S0 b% r9 T" v6 _
// just repeated every time. See jmousetrap for more
/ S4 k! [# P! y4 P! g // complicated schedules.
) X/ W1 v# U0 D5 a6 ] H) G 6 f0 E+ d6 D' v" ^5 N
modelSchedule = new ScheduleImpl (getZone (), 1);. i( h# d5 x* W$ U* D. c% V G
modelSchedule.at$createAction (0, modelActions);
6 C ]* k9 Q) N$ c % S V- J; x) ~1 t) Y
return this;
- k$ f& c/ Y! H+ `# I } |