HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ z9 r7 [) s" W2 Z: f7 a# q
0 B( _9 I' K- p0 ] public Object buildActions () {" j# K# a9 `( F( k, m5 y% r- E$ L
super.buildActions();
( G; {, L1 r+ u5 l / Q, b1 I$ F6 q7 A3 s6 q
// Create the list of simulation actions. We put these in* H; I" M# M4 o' J( Y; ~3 n5 y
// an action group, because we want these actions to be& K e, m/ A- r9 d& P
// executed in a specific order, but these steps should
) I- c9 a- F- {" s: m# P6 u! K // take no (simulated) time. The M(foo) means "The message
* L. \% U" b1 F3 I. z // called <foo>". You can send a message To a particular
& [, B5 t$ `7 _4 _! Q8 ^4 @ // object, or ForEach object in a collection. r6 C1 K9 q! H7 M% V* q7 O
3 l5 ^( L7 C) P" x // Note we update the heatspace in two phases: first run
) L0 z2 ^) R7 a" u& L5 {8 t // diffusion, then run "updateWorld" to actually enact the
# s: Y9 N; P7 a$ T // changes the heatbugs have made. The ordering here is. c7 u. M$ [; ~, l/ J
// significant!
& l# ], f$ r5 @ O" g, G : `6 J* s' y$ K0 I+ i' c5 ~
// Note also, that with the additional
% e) h2 |0 e I7 Y/ Z# z/ S // `randomizeHeatbugUpdateOrder' Boolean flag we can( O8 p7 N6 c: a, S) b7 x3 f
// randomize the order in which the bugs actually run
) j0 n/ A7 k$ R8 P! a // their step rule. This has the effect of removing any. S% W" z; z' i6 ^( ~; s' \* H
// systematic bias in the iteration throught the heatbug+ g8 i( C7 [1 H6 K+ n# R
// list from timestep to timestep& O) q+ j, j0 J) y% L2 G+ g
$ p# C7 b9 [) g! v, j8 k7 ?
// By default, all `createActionForEach' modelActions have
! Z! B3 o- y9 Z, U // a default order of `Sequential', which means that the
8 l: Q; [) [; n% f // order of iteration through the `heatbugList' will be/ W& f8 ^8 C% z# p* y5 k* u# ?
// identical (assuming the list order is not changed4 w/ @$ [8 P1 q3 A/ {
// indirectly by some other process)." P+ @% d2 h; B& t
$ K0 ] o; B F* J# [7 d4 t modelActions = new ActionGroupImpl (getZone ());0 ~2 [3 H: ~( H
" k5 l) Z" P2 R5 A3 g' v/ I# I$ g try {
" j7 H0 E7 k/ y2 \) j; G+ `# p modelActions.createActionTo$message
" z3 t F, `& T: ?7 F- d! y, F (heat, new Selector (heat.getClass (), "stepRule", false));
; C2 {9 _# h3 @: a, _+ M } catch (Exception e) {
3 Q" k" w' Y* T2 O, I1 j System.err.println ("Exception stepRule: " + e.getMessage ());8 s/ ?' D7 ?2 [0 ]7 U& u# f7 I
}
3 L1 b* b+ }$ [# y' ]! W4 {$ d
, ?& J: S1 [8 |. C! G0 U1 e' A try {
1 P ?) S+ M7 d% B j t Heatbug proto = (Heatbug) heatbugList.get (0);
& Z! W+ w" w: S9 K* Y Selector sel =
4 ?7 E) A ]( Z3 {! Z9 g5 e new Selector (proto.getClass (), "heatbugStep", false);
$ T6 I9 D* p4 R6 b actionForEach =: k* K: d. M. L2 H
modelActions.createFActionForEachHomogeneous$call6 E G2 ?& C# B3 L# X
(heatbugList,6 m" d! F& Z4 U0 P4 `
new FCallImpl (this, proto, sel,; V) A2 z' i! L; ]6 s0 C
new FArgumentsImpl (this, sel)));
2 e* ?9 R) U% y4 B) _1 J, G } catch (Exception e) {: Z7 i& x! L1 f0 H" ~9 Z
e.printStackTrace (System.err);
+ T1 i e0 d& M4 i) [: h }
1 [; v' A$ i0 \
# r2 Z3 A* Y# } @6 H syncUpdateOrder ();
; `# z' D9 v; |& E
4 r' }2 p" Y$ h4 B try {
; \1 X0 M f/ N$ H0 P5 E) P modelActions.createActionTo$message $ {+ O' Y5 g4 i1 b; n. L$ u
(heat, new Selector (heat.getClass (), "updateLattice", false));3 `/ m7 {: ]0 `8 t, _
} catch (Exception e) {
; p. i! h5 Y6 b( g( H! b) v5 A- i. y System.err.println("Exception updateLattice: " + e.getMessage ());
! N8 p: }" |8 U4 C, m) |. W( N }
8 h- H. Y1 z2 [# h" p
8 m0 m+ X% f n& t9 d: K; L // Then we create a schedule that executes the
' J0 X' }3 \) p* K; ~6 X // modelActions. modelActions is an ActionGroup, by itself it ?9 ]0 N& _! o# x K# V
// has no notion of time. In order to have it executed in9 c( M$ a* k" V. d
// time, we create a Schedule that says to use the
: {9 m; s, t2 s# O: r) V: E: p // modelActions ActionGroup at particular times. This
5 Y3 x" e9 v, B' f) W1 X2 p, _- W // schedule has a repeat interval of 1, it will loop every
: X: r; Z& a( Z+ ], Z // time step. The action is executed at time 0 relative to
$ e8 C% L4 a; ?( e3 N // the beginning of the loop.$ n6 Z' _/ n% A J( {+ c; q: ^7 h
$ T& E- W9 C1 l# c4 J
// This is a simple schedule, with only one action that is
( i2 N2 `/ m0 h+ l+ s+ M! ^ // just repeated every time. See jmousetrap for more
& o$ ]" F3 n- h. k9 h& n // complicated schedules.. ?$ V4 s2 n1 C+ Y7 _* ^% G
# q, H( i; t" {1 Q1 G5 C8 z+ S1 z modelSchedule = new ScheduleImpl (getZone (), 1);: Z7 }" g; `3 E8 G
modelSchedule.at$createAction (0, modelActions);" G0 U: w$ \$ K, W) e( K2 P g
% e- Q" f% B. O0 [* \( r; L
return this;
# z9 P6 V# h! L6 @ } |