HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) H3 K* A% C0 P: F* j/ R3 ]2 }% B3 [+ G5 \
public Object buildActions () {5 V4 J- z% Z7 t0 j8 z8 o5 n Y/ ?( l9 ?
super.buildActions();
4 h. P0 r( y. s5 S& s' _
, `9 F# G) z. {, ` // Create the list of simulation actions. We put these in
0 ~$ S/ H$ \5 {7 ~; z" U- t7 h' R" \ // an action group, because we want these actions to be
3 y, D% V. m1 @; l2 J/ b3 t8 c& @ // executed in a specific order, but these steps should4 k- H: E9 H k
// take no (simulated) time. The M(foo) means "The message
) e4 a9 W1 U h3 }/ a0 f // called <foo>". You can send a message To a particular+ d9 a. O1 ?9 r# |" Z* j6 ~) Z
// object, or ForEach object in a collection.* E! q5 }- P9 x
. J8 k! @" Y# `7 W( r7 |" f // Note we update the heatspace in two phases: first run# h4 e8 m+ D& d
// diffusion, then run "updateWorld" to actually enact the* T9 a' y" a. S! o5 i
// changes the heatbugs have made. The ordering here is. [' a$ z2 w5 \! f$ N
// significant!, y5 \4 J2 o' ~- {6 D1 p" }
* J E2 z8 E0 i9 C
// Note also, that with the additional$ M. N. y: K$ J
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& `& [ I5 i$ S4 q& A5 o // randomize the order in which the bugs actually run
, d- o7 J! A5 X7 G, ]; t // their step rule. This has the effect of removing any. S6 R& P' u7 O4 u8 W0 u/ s
// systematic bias in the iteration throught the heatbug
/ @8 A( r; d, c( O8 c2 N* z' i9 H // list from timestep to timestep
: F# s( d- ?+ K+ p+ j
- r4 y, `( Z& _ // By default, all `createActionForEach' modelActions have! D4 h0 ^0 f* @0 Y: W
// a default order of `Sequential', which means that the
) z3 r1 Y! H$ r) k0 C // order of iteration through the `heatbugList' will be
1 q! r/ T8 O& i- G/ M) c8 _, H // identical (assuming the list order is not changed
, i. P( [/ |4 ]' z% L // indirectly by some other process).2 T) H k7 i4 q. @
# H- o8 W5 B1 G$ ~
modelActions = new ActionGroupImpl (getZone ());0 I! ?. z# y$ i1 b# G2 d7 I. Z2 k
! m& m q, S& n7 o- }( G1 {8 o
try {5 G- t, g+ u: B8 Q
modelActions.createActionTo$message
[7 }% H: s B! |/ D% i (heat, new Selector (heat.getClass (), "stepRule", false));" R/ d! W! @9 y, V
} catch (Exception e) {
3 T; P$ b: I% D$ B+ d* K) w System.err.println ("Exception stepRule: " + e.getMessage ());$ |. A: h$ x+ X+ l
}
- E1 s6 I+ w( O
2 C$ t5 f% y0 V* a. a! x try {
5 ?, n3 ^0 W% J, X Heatbug proto = (Heatbug) heatbugList.get (0); n# Q7 F8 f/ F* P9 x
Selector sel =
/ C) O# ?8 f; R. ]9 _/ b" d new Selector (proto.getClass (), "heatbugStep", false);5 l7 ]4 F/ d# b0 j
actionForEach =5 H D" V( X Z* ~- z& Z4 J8 o7 H: ^
modelActions.createFActionForEachHomogeneous$call, y+ Z, D+ j# I* H
(heatbugList,
6 C F' T) H2 U* d" e; v5 | new FCallImpl (this, proto, sel,& U, J% m, o9 c, |$ t* T0 \2 d: T
new FArgumentsImpl (this, sel)));
# c+ s2 `6 J# |- f1 i$ K } catch (Exception e) {
# X; W$ `4 F. V e.printStackTrace (System.err);+ `$ b# O3 U# C" V3 \6 C
}- x5 H2 K1 k2 E# T! P# P# r5 p& U
+ ^1 R+ Q% {+ Z( f
syncUpdateOrder ();: |% g# n! K, S! \) X; k# r, N
, \. ^* q/ Y4 o. c try {5 V1 `+ k; S% S# a5 J2 a
modelActions.createActionTo$message
+ G1 w) v j% C7 r* P9 ^& o* r- t (heat, new Selector (heat.getClass (), "updateLattice", false));
$ D& w; a! h( |. z' K4 a8 Y } catch (Exception e) {' j" F7 L0 |# V3 r+ q2 o7 U6 m
System.err.println("Exception updateLattice: " + e.getMessage ());
5 U0 i. Q! o5 Z6 y4 i V; K5 [+ ? }
9 G9 V9 ^" ]' _' l7 b4 L
, `6 [1 K! i5 V5 p/ B/ o // Then we create a schedule that executes the
" m6 A ~0 L$ p; k' E! p0 r5 B* f# O // modelActions. modelActions is an ActionGroup, by itself it. x6 d" d1 D5 s% F. r& B ]0 e
// has no notion of time. In order to have it executed in
' ^! u: O% ^6 i8 P9 K" {' x0 {4 z // time, we create a Schedule that says to use the5 t1 y% v( w, b0 B' m/ }0 u
// modelActions ActionGroup at particular times. This
9 E7 F; d& s1 W4 s3 o2 i H // schedule has a repeat interval of 1, it will loop every% w5 m1 N |; d; Y
// time step. The action is executed at time 0 relative to
/ Q$ G: Y% l; O // the beginning of the loop.
. k: {+ z7 U% B6 _" D$ j" `& [3 E' F% A# g* X$ i( M- x' [
// This is a simple schedule, with only one action that is
9 ]8 `+ b6 W& e1 x/ U) l' t, W // just repeated every time. See jmousetrap for more) g1 `7 }% ?7 t1 C) M
// complicated schedules.1 G, Y# i5 q* Y7 n( P* _
; f& k- f6 E# L* g modelSchedule = new ScheduleImpl (getZone (), 1);6 K3 G+ O( ?* ^9 W+ {3 L
modelSchedule.at$createAction (0, modelActions);5 j9 @+ q% e& d, b
8 ~" b3 \& }% H. `! ?$ m
return this;2 _! O# |. h5 P* J6 i0 L
} |