HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- f) ^) Y J* O- B! `% g
' j7 r" Y4 k7 H+ A$ J' n- { public Object buildActions () {" D8 s6 r& v$ p0 y$ H+ R
super.buildActions();# u0 b! b2 ^/ K4 J; O* ]. u- C
( W8 P6 b+ j1 [6 `3 R% l/ | // Create the list of simulation actions. We put these in
4 n3 C" c q' Z // an action group, because we want these actions to be
2 ?# E* k! O1 k9 F // executed in a specific order, but these steps should
6 G8 j9 S* u* w* |6 _: i // take no (simulated) time. The M(foo) means "The message
7 D0 G8 x* g5 Y" \ // called <foo>". You can send a message To a particular
; O. {( p X7 D+ k- r# J // object, or ForEach object in a collection.1 e2 `9 A8 V6 ?4 n, v. p4 ]
" V" @* B, W; j" D! k ?
// Note we update the heatspace in two phases: first run
0 D6 ^; S% m& m9 s) D5 J+ e // diffusion, then run "updateWorld" to actually enact the5 g+ O; X/ m7 H7 _2 g% k+ ]1 a s
// changes the heatbugs have made. The ordering here is
7 s6 A! `& O y# x' C, X // significant!
& M" F: ]6 Z8 `& L . ~: f% u$ l0 l5 ~* w6 z; R
// Note also, that with the additional
) G* V1 P1 A7 @7 z+ k5 A! G // `randomizeHeatbugUpdateOrder' Boolean flag we can
K1 _6 y& [: `) p4 V7 p* m // randomize the order in which the bugs actually run
' m7 Y. N+ y7 _2 z J // their step rule. This has the effect of removing any
/ k5 Z. J% J" f // systematic bias in the iteration throught the heatbug
# k9 M* H. u+ M$ d0 G // list from timestep to timestep& z+ o* b) m- n" |
5 F/ p4 W, H3 z$ j5 ]5 r // By default, all `createActionForEach' modelActions have
0 J! W2 k9 ~) W. w* [ // a default order of `Sequential', which means that the
5 P, ~# T/ f \ // order of iteration through the `heatbugList' will be1 Z( X* e( W2 B! D
// identical (assuming the list order is not changed7 i6 b2 U2 h2 {1 s, t# W9 M' d( ]
// indirectly by some other process).
7 V6 ?2 k8 ~; @4 m t: T4 T. V( H$ r
: Q$ B2 y7 t. \ v! x modelActions = new ActionGroupImpl (getZone ());
# q% M0 U6 ?! U. k( W% ^% a ]
, r0 D- v* i* G. U1 R' k3 S try {
) x' c1 I! k8 X7 r* V3 e4 n" ^# r3 T modelActions.createActionTo$message
+ d1 R, k' z- m* P4 K6 h' | (heat, new Selector (heat.getClass (), "stepRule", false));
/ L' ?& U' _- c9 Y } catch (Exception e) {3 p) Z8 ], }5 S& y; R& P% `
System.err.println ("Exception stepRule: " + e.getMessage ());- ~" p; [; L' V: n
}4 t- m8 D4 u0 m; s6 X \
$ \$ {8 N* z* t
try {
, B; h1 d- L6 U ?- @# m, M Heatbug proto = (Heatbug) heatbugList.get (0);; R! ^. L% u/ h
Selector sel = 2 Y+ N" Z/ K" O/ P
new Selector (proto.getClass (), "heatbugStep", false);
! d/ J+ x a( T( L0 \ R actionForEach =
. d2 A- \1 m: l% t# M" ^ modelActions.createFActionForEachHomogeneous$call1 b* F3 f, E6 P* v% v
(heatbugList,
7 k, c1 ^. }2 i0 P new FCallImpl (this, proto, sel,
9 [: o8 V. n, V( G: J( R new FArgumentsImpl (this, sel)));7 D5 s+ ^( J7 [4 J
} catch (Exception e) {
) D5 V1 f/ K) O0 A e.printStackTrace (System.err);. Y0 W) t& v- i/ F5 R- x
}
2 ?3 P% y7 c; ~4 T ! R# S; V9 l: j
syncUpdateOrder ();
6 w2 Q7 `5 T0 M! Q! W3 M
`# ?- N% Z+ j try {
* {6 F* e* L0 M; J1 ^! L modelActions.createActionTo$message 0 G; T; ~ P8 P$ n1 `+ A. t
(heat, new Selector (heat.getClass (), "updateLattice", false));% h0 `" {) F5 a$ a
} catch (Exception e) {: t5 b' [+ c d3 q6 p4 K$ w1 K1 h, D
System.err.println("Exception updateLattice: " + e.getMessage ());
% F) [. q4 B5 U& A }$ }7 H$ ]: S/ X
! `: S5 b- E- z5 D1 O9 x* ]* D4 f6 d // Then we create a schedule that executes the
# k4 T# a7 h/ ]4 O" D' s; V1 i$ p // modelActions. modelActions is an ActionGroup, by itself it
8 r; B( u+ t$ \9 V3 P // has no notion of time. In order to have it executed in
6 h% k( i$ y ?$ ] // time, we create a Schedule that says to use the
7 `0 M& v# I' W h* K: o1 t- n5 ^ // modelActions ActionGroup at particular times. This. V7 _% m @ d2 R5 u' J: {
// schedule has a repeat interval of 1, it will loop every
! ^3 w/ u: t; _0 y# f7 u5 C* a // time step. The action is executed at time 0 relative to
5 e" e1 o s+ U# ~. g // the beginning of the loop.1 H5 Z. X, N& ~9 P, A, o. y
3 B! U& i. Y1 A3 C4 S; G/ g // This is a simple schedule, with only one action that is
* a& j- h2 A" j5 m1 e; t6 z // just repeated every time. See jmousetrap for more0 R2 u6 _ b) Q8 P {
// complicated schedules.0 N& q& ]% M v
/ \% k( M2 R- c2 s+ f modelSchedule = new ScheduleImpl (getZone (), 1);
" N9 [# u' [/ P6 T! q1 @7 C5 @$ f modelSchedule.at$createAction (0, modelActions);
$ y. X9 C5 R" j5 |
9 U8 [, o& p, c return this;
! @5 }- b: K* A/ U* O7 V } |