HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ ?- n q0 q3 l/ ]
' ?: v- s8 b+ N9 z* T0 B
public Object buildActions () {
6 B/ [: S0 n5 E5 |& W1 j super.buildActions();7 D2 g( P5 b0 Z# j8 c8 M1 F0 G1 D
2 P+ _% }( d G! A5 T3 p$ M% f
// Create the list of simulation actions. We put these in$ P& J6 ]# L) \8 F3 y6 X" ?) A2 L, M/ A
// an action group, because we want these actions to be
3 b( a1 m. F7 O // executed in a specific order, but these steps should1 P: X( t" v+ w9 P* Z
// take no (simulated) time. The M(foo) means "The message0 l7 `) t" k+ z: @+ Q1 k$ f
// called <foo>". You can send a message To a particular
% O) F( _) T/ b/ c% x" F d // object, or ForEach object in a collection.! e6 Z: p* S8 ]9 a$ G( a& S
8 {2 N+ t7 A( K5 B9 h$ @
// Note we update the heatspace in two phases: first run4 |2 Q1 U7 R+ D
// diffusion, then run "updateWorld" to actually enact the
6 N! Q7 B: {+ I, t6 e" p7 o // changes the heatbugs have made. The ordering here is
# L1 j# I1 }1 I' \) H0 Q, c# R( w5 u // significant!0 {+ V8 W6 o i3 c
7 a, Y M }% ]) l
// Note also, that with the additional
6 h$ L3 a: U* ?0 C/ M2 D // `randomizeHeatbugUpdateOrder' Boolean flag we can) w' f8 n7 ?0 p# _8 E4 T
// randomize the order in which the bugs actually run' v+ Y* ]! U. t2 c! a! K& d
// their step rule. This has the effect of removing any
% e9 c& b* t1 X7 [ // systematic bias in the iteration throught the heatbug
' I0 K% y2 e* Q- q9 [3 M // list from timestep to timestep
* e- c* j* A5 i. U+ E! s# ~! C" j
7 }2 v3 w3 D4 E9 L' C1 m // By default, all `createActionForEach' modelActions have
! [: f/ d5 B8 r // a default order of `Sequential', which means that the
' V' \, V9 H h x, O2 Z // order of iteration through the `heatbugList' will be0 A3 a6 t: e$ N: Y* g# ~6 @ I
// identical (assuming the list order is not changed
: D# C( A! e% j0 ^% n2 y // indirectly by some other process).) |3 c& E2 E( H3 q, i% P
8 J/ }" c6 S7 Q3 a
modelActions = new ActionGroupImpl (getZone ());
Z+ r1 ]6 V( x% B ?
) Q* ~1 V7 @3 Y& e try {
5 o( E. m3 r8 P8 g( C2 O% N# l* U1 | modelActions.createActionTo$message
8 H" s2 C: r$ l& J (heat, new Selector (heat.getClass (), "stepRule", false));
# ?& H' o; P% Y } catch (Exception e) {
# t i# H8 m9 f! z6 f6 \ System.err.println ("Exception stepRule: " + e.getMessage ());, O9 t4 E% }$ ^$ E' Z+ W/ I P
}
9 l( L! I' y9 ~0 v
' Z: I% S6 X$ {( |/ W+ E* N try {
* k0 l! C0 J5 i; Z& s3 M1 i$ G/ O) i Heatbug proto = (Heatbug) heatbugList.get (0);; u$ S" S- B6 M- Y& m
Selector sel =
% V8 [/ r, _* |4 Y4 s* g+ | new Selector (proto.getClass (), "heatbugStep", false);
7 p6 d* j! W* d7 E( V actionForEach =
! _& O0 ?# z/ c7 S( h' B modelActions.createFActionForEachHomogeneous$call
V, _+ s, d: ^0 ~& X p' D (heatbugList,' A' S# \6 A F$ S, l
new FCallImpl (this, proto, sel,: c4 [8 r+ |5 A" U p" d
new FArgumentsImpl (this, sel)));9 x$ n: {2 o2 r9 o+ d' \
} catch (Exception e) {& r% {; }; j5 l7 Y+ O6 e9 L8 a
e.printStackTrace (System.err);
: v7 ^( ?8 ~6 Q }
+ A/ p- [+ r9 \5 R6 I$ |2 W; s 1 C& ]; h: v) T8 m4 o
syncUpdateOrder ();
+ @+ e/ B+ ]2 @# o$ I" b. i4 U. {' n! S4 M4 [" s
try {, \) q2 ? M6 c7 N# U( E$ z
modelActions.createActionTo$message 4 h0 r. }# I* F9 I5 D8 l
(heat, new Selector (heat.getClass (), "updateLattice", false));& }) @& D& x9 V0 U
} catch (Exception e) {
5 E; Z4 i. z! J% \+ I System.err.println("Exception updateLattice: " + e.getMessage ()); I3 i0 }4 M! ~( y4 c$ A
}
* \! ^9 X+ ~ Q, ^. z; F7 M* Y 4 h8 S; p0 a+ n5 b! e
// Then we create a schedule that executes the
C" J. X4 Y& X( c8 e // modelActions. modelActions is an ActionGroup, by itself it* b) l3 C5 o. C2 O- t( z, t
// has no notion of time. In order to have it executed in2 B7 a8 k9 g2 d( ]2 s" z9 y
// time, we create a Schedule that says to use the8 Y0 i# E9 r& `. n# }
// modelActions ActionGroup at particular times. This! m) U9 p6 @7 M" w+ q' V% D
// schedule has a repeat interval of 1, it will loop every
2 A; X! v6 b- p/ O // time step. The action is executed at time 0 relative to
2 K0 p' G& I% U$ [* K x V // the beginning of the loop.
% X" m. s! d- C- ^8 T. b2 J+ B2 U( o
// This is a simple schedule, with only one action that is( {3 ~1 p+ y7 I! F0 ?
// just repeated every time. See jmousetrap for more
9 s* [. V+ i" W8 R" m // complicated schedules.
, ]& o; z- J+ P! w% ~! y( j q; o
8 z5 m! T6 W1 O modelSchedule = new ScheduleImpl (getZone (), 1);
5 q! i% O. t; Q4 I# k! `( R9 P modelSchedule.at$createAction (0, modelActions);2 O& f# c x- {
/ A" V5 d( R2 {5 K/ D+ q) _# J return this; o9 l, z8 C+ Z) w- o. b* A- y. l
} |