HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) Z+ S2 ?+ f; R7 d+ e& E3 p0 x6 j: h
5 m8 r( ^* p/ l' _
public Object buildActions () {
% m- `! I* ]' Y. m$ O9 k super.buildActions();
$ J5 N! `5 I# n( H( N * X# k! ?9 [4 C& O
// Create the list of simulation actions. We put these in. B5 m: Q; ^; F) j; @
// an action group, because we want these actions to be) b+ l2 D+ p; C, w5 `
// executed in a specific order, but these steps should
& }) H# |7 c& f5 I // take no (simulated) time. The M(foo) means "The message
1 s- Z1 C2 J+ n, B" P2 H // called <foo>". You can send a message To a particular
0 S3 p1 j! K, B // object, or ForEach object in a collection.
: o! H2 `5 t) f6 j . A% P" A& g, k) [: ?- r5 `3 J
// Note we update the heatspace in two phases: first run! z2 _! r2 n' N4 A
// diffusion, then run "updateWorld" to actually enact the; e1 m. O; d; _# n: o( H6 B6 r/ G" v* k
// changes the heatbugs have made. The ordering here is$ y5 @% r; N, T1 r: E$ B
// significant!
) t5 D" o8 O+ U5 V1 W
' L8 m9 }4 K g3 m5 v K1 D // Note also, that with the additional+ J& V0 a$ C+ @ i O ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can' l" u$ [# Z6 Z7 B# g6 H0 C
// randomize the order in which the bugs actually run2 ]5 l$ i4 S& T1 C5 @# D
// their step rule. This has the effect of removing any
" g! h' v4 R( q: z6 s // systematic bias in the iteration throught the heatbug
) A) z; u6 v5 o2 \ U5 D$ u5 E // list from timestep to timestep k, I) b, o8 u' _1 s
9 D6 }/ X; M: P- y // By default, all `createActionForEach' modelActions have
/ E# u, L0 T1 k, l/ x // a default order of `Sequential', which means that the, I- i ?% j( G1 N$ E
// order of iteration through the `heatbugList' will be
) R" ~2 f o0 |9 v! L7 o) s7 q5 q // identical (assuming the list order is not changed7 X0 q4 |' N3 ^ J5 v
// indirectly by some other process).& y+ C ?6 C* T" _
. U4 p( X8 ~+ f2 ]
modelActions = new ActionGroupImpl (getZone ());
; r4 r/ Y9 ?, Y# g2 d
`' Y- D$ I2 C; ` try {' D$ l. S7 J# F( i
modelActions.createActionTo$message
$ \1 |: ~9 _; S% q+ w (heat, new Selector (heat.getClass (), "stepRule", false));
; |! l0 I5 e/ R } catch (Exception e) {
$ z! X. f9 l9 V+ Z2 O System.err.println ("Exception stepRule: " + e.getMessage ());
6 C/ {* I% f" X' O }2 M2 g4 x+ ^3 n" \
}9 x9 R9 \5 }5 v$ T# x6 p try {6 \2 l8 C. E, g
Heatbug proto = (Heatbug) heatbugList.get (0);
$ ^: F l, L d$ C% Q Selector sel = ; `6 L9 I+ T2 a+ |4 u
new Selector (proto.getClass (), "heatbugStep", false);9 l' O% |# d c9 i' ?: v3 A8 z1 I
actionForEach =) Z8 p9 P9 W* [5 j) ~% `9 r( }
modelActions.createFActionForEachHomogeneous$call
0 d3 S6 ~: C" Q; g& i (heatbugList,1 p- q q5 M8 ?+ f& N7 w0 S
new FCallImpl (this, proto, sel,
0 \( T1 e v- W2 b L6 R4 `& \ new FArgumentsImpl (this, sel)));
+ {; x6 k8 }, M, V } catch (Exception e) {
9 J( K/ r+ o/ z e.printStackTrace (System.err);3 U' y6 `" O/ }5 X' o
}, g+ V% X6 r* t0 Y3 M4 ]" ?& |
1 i& y* X6 C7 i syncUpdateOrder ();% L2 B. G& c. L! b
! K+ q- u7 w# `
try {& ~2 B i4 ^9 ^7 b
modelActions.createActionTo$message
9 G- m5 T. L8 i (heat, new Selector (heat.getClass (), "updateLattice", false));$ L+ l0 e" u$ F8 x0 O
} catch (Exception e) {7 E' [" i! P% I( `7 V3 R
System.err.println("Exception updateLattice: " + e.getMessage ());
g/ p7 ^0 D- o1 |4 A0 ]6 \ }
; ?# L/ ]7 I; A! k: j3 g / I- [. }( N5 ~
// Then we create a schedule that executes the- N/ m# t+ ~8 ^# p
// modelActions. modelActions is an ActionGroup, by itself it$ X7 E' j1 H( i. Y; D- x2 J/ i
// has no notion of time. In order to have it executed in% e& f' n# }* i) A- s8 M
// time, we create a Schedule that says to use the) W- F* W" n( L ]% E8 M! s
// modelActions ActionGroup at particular times. This4 Q8 j6 X. V" `; b A K4 ]0 y
// schedule has a repeat interval of 1, it will loop every' k, p9 a+ Q1 M K- F9 ]! C
// time step. The action is executed at time 0 relative to' Z1 a5 X* H8 b) W6 h! [
// the beginning of the loop./ @$ l1 \9 {6 K+ y
% l+ p- B& I' h1 l2 d // This is a simple schedule, with only one action that is' }. ^0 G- {& Y
// just repeated every time. See jmousetrap for more
, u5 Y* x* b4 k, a1 w/ ~( } // complicated schedules.$ V7 s+ S" J: s7 `4 i
% H1 ^6 A7 Y1 a2 q K0 P
modelSchedule = new ScheduleImpl (getZone (), 1);
9 ^0 j0 {. z+ r S# {9 t modelSchedule.at$createAction (0, modelActions);
" c! [+ y2 j* w- V2 c
5 l" j& x3 p- A" M% J# A- j! ^2 E return this;" }6 h7 R% K. T7 g3 h4 c7 C6 O$ _8 I
} |