HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 H& |8 O* O2 G2 F" i
+ o) N" s, t: o: ~
public Object buildActions () {) u% i3 x$ l" r5 J% ]) u% U
super.buildActions();& N; U1 ?7 w9 g* C+ ]
9 E1 g/ S, W3 W' ]' {8 p // Create the list of simulation actions. We put these in4 s& x C& H/ L6 ]$ }, {
// an action group, because we want these actions to be
$ c! n; u+ L- s- D9 m! V // executed in a specific order, but these steps should
; |. J. S- v( e0 t // take no (simulated) time. The M(foo) means "The message l9 ?" X1 A) y2 t5 o
// called <foo>". You can send a message To a particular
6 d# q7 o& s+ l9 B# F // object, or ForEach object in a collection.
4 s6 L: w+ b- [' c ! s6 L+ c2 D9 Y) l0 g8 e
// Note we update the heatspace in two phases: first run
1 o- n. Y- e6 C* k // diffusion, then run "updateWorld" to actually enact the, L9 _3 i, i! J4 T+ w0 p
// changes the heatbugs have made. The ordering here is
% N$ B- Q# d4 F3 z // significant! L6 B9 `# K0 K0 A
9 k. ^7 u9 h4 h4 l
// Note also, that with the additional. f4 A, C _* U# z! a
// `randomizeHeatbugUpdateOrder' Boolean flag we can# G5 T1 }3 R' `. z
// randomize the order in which the bugs actually run/ q' ]" N$ X) @! z, i3 t( W" O
// their step rule. This has the effect of removing any
, e/ h, h; c2 l- N: X8 { // systematic bias in the iteration throught the heatbug
$ W: A2 X; f4 y, B; \ d // list from timestep to timestep
g7 s2 ]0 F2 P5 q D# Y# K n - D. O% w7 K# ?7 g8 ?
// By default, all `createActionForEach' modelActions have
4 p, n) J% I' h* g( C) _0 Z3 z4 m // a default order of `Sequential', which means that the. R6 c$ M6 C1 m* _
// order of iteration through the `heatbugList' will be' a4 M, x8 d# B# L" h3 h* W
// identical (assuming the list order is not changed) f8 x3 t. H% t
// indirectly by some other process).
" {1 Y" k G# m `7 t: y
3 v! l2 A$ @; J8 }! Q modelActions = new ActionGroupImpl (getZone ());
8 R4 k9 b) B; y) o7 X
% K( ?; B h3 }3 \# r6 R/ @ try {
6 v0 H) h+ ~+ B modelActions.createActionTo$message
6 V a! B4 L) x; c (heat, new Selector (heat.getClass (), "stepRule", false));
( F% r7 @( U7 N- _9 W } catch (Exception e) {4 W6 q0 U. @9 y8 [- J6 b/ b5 k
System.err.println ("Exception stepRule: " + e.getMessage ());
/ S ?' K/ J8 f0 y" J }
9 V( h- l2 k" s2 t1 g
2 L: C9 B _5 w) `, ]$ Q try {
* ]/ b# |0 S( e- ]) O Heatbug proto = (Heatbug) heatbugList.get (0);
: ~7 }3 {* V8 A+ c% u6 u: r* D Selector sel = % E% J/ Q% V8 \% o7 ]
new Selector (proto.getClass (), "heatbugStep", false);
! f3 z b* {- y actionForEach =
) L) T+ r. {6 ~8 w6 | modelActions.createFActionForEachHomogeneous$call4 k- R2 `4 f9 U* J+ \
(heatbugList,
& V$ p3 p0 A& {- B. Z new FCallImpl (this, proto, sel,* ^/ Q5 M/ T6 A u" Y. M5 l' S
new FArgumentsImpl (this, sel)));( J- M: u3 [/ U
} catch (Exception e) {) b/ C# J: d0 X7 E! Q$ [
e.printStackTrace (System.err); Q9 T( j+ D% K; n% a- F& Y
}
1 `+ x3 n2 L& K8 n ! F* T$ y2 }( W7 w E4 ` ?
syncUpdateOrder ();% b" N& X1 J) q+ N* v+ C+ h
" B8 x9 W- P7 N2 W" Z7 C& `( d- n
try {& h/ k; z$ O' p9 [) }6 w H Y
modelActions.createActionTo$message 1 Z7 C7 a, O. v) t4 H- [. M3 q S }
(heat, new Selector (heat.getClass (), "updateLattice", false));: J4 U/ x4 }$ S; A! h
} catch (Exception e) {
3 ]3 Z9 x- a) j1 j: f, e System.err.println("Exception updateLattice: " + e.getMessage ());
. L0 w4 U" G, F& R }
% F( b. ?- N& z& B# M. q) R & s+ l. k8 @* r+ A. D& v2 a7 Q( e4 c
// Then we create a schedule that executes the) ^7 @2 d/ O1 ~- ?2 Y# p
// modelActions. modelActions is an ActionGroup, by itself it
) R. G4 a' n. C" k // has no notion of time. In order to have it executed in! q3 R; ]. b! v
// time, we create a Schedule that says to use the% b; Q5 C+ m" o' Y- ^# Q
// modelActions ActionGroup at particular times. This
9 y: y4 E, e! w F/ y // schedule has a repeat interval of 1, it will loop every3 I% |/ M( s8 ]6 S
// time step. The action is executed at time 0 relative to v2 m$ a' S m2 W c# ?* Y
// the beginning of the loop.
1 [8 C' u0 O/ ~3 Q$ q, `7 ?1 q# ]- x. Q/ z0 S( y. U6 j
// This is a simple schedule, with only one action that is) K9 J* s/ E* T& ]
// just repeated every time. See jmousetrap for more
, t( j6 ~) Z" U- k/ }2 T2 ]* T // complicated schedules.
! o0 G& ^; U* h9 `; O* @9 \- q8 l * G2 k6 B8 V/ Y& T) Z L$ k
modelSchedule = new ScheduleImpl (getZone (), 1);' \2 i! ?3 {. l
modelSchedule.at$createAction (0, modelActions);
# ^! y u5 Y2 |4 U9 ] 4 F/ q# t5 q; Y4 O! Y+ n
return this;0 Y# { [3 R" E5 X2 ~. k
} |