HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ P4 A; \5 \& }8 N! d6 @( i
0 N! C! y2 G( V4 p/ M2 O
public Object buildActions () {
/ x) r4 T% B/ W, n4 F super.buildActions();
6 ^* a# Z9 s2 Y U9 O5 L: h
9 n2 n& b- q( l9 e: v // Create the list of simulation actions. We put these in. J: \: n; O2 M$ m& `+ b
// an action group, because we want these actions to be) ~' ?" d: T4 w) C) v
// executed in a specific order, but these steps should6 M1 k) j4 d: B. N2 v, {
// take no (simulated) time. The M(foo) means "The message
" M) s3 J, O2 w' Z* C# c. d // called <foo>". You can send a message To a particular
& m& H- C- ^+ @7 r' g8 b3 [: n4 T5 K // object, or ForEach object in a collection.
8 c ~# Y6 p: E C8 s8 t; _2 I- x l& Z9 ]( C& `
// Note we update the heatspace in two phases: first run+ h, x2 ?1 k; D7 l
// diffusion, then run "updateWorld" to actually enact the3 j4 R2 R8 R# ?8 U$ {( d" z
// changes the heatbugs have made. The ordering here is: @' |: a/ q" ~0 r8 r* k. d
// significant!6 R1 w. A' a1 c/ h3 G0 b" G1 m
) @4 f0 ]+ f% k" S5 g/ r
// Note also, that with the additional
" e3 u1 F. g" }/ e' `8 d0 G // `randomizeHeatbugUpdateOrder' Boolean flag we can: q; }/ }4 e2 h
// randomize the order in which the bugs actually run
; ^/ i$ b+ R( S) v9 P: j" u // their step rule. This has the effect of removing any k# G- ~, |+ b. b/ a$ Z% p: X
// systematic bias in the iteration throught the heatbug$ c5 G& H0 K$ n- \# Z
// list from timestep to timestep
* }9 k+ s* e4 V2 f2 }1 u- |
1 j. |, m3 x; \ // By default, all `createActionForEach' modelActions have# n5 S J" W9 }$ b+ T! g( z# B( S' D
// a default order of `Sequential', which means that the
/ m% Q* p% y! m5 j Y' V3 }, E // order of iteration through the `heatbugList' will be
2 L$ `7 m' R/ B* [$ a9 @* E // identical (assuming the list order is not changed
0 d. c* i4 e( t& W/ E% ? // indirectly by some other process).
I/ I; a6 H5 x1 H, W+ m
% |) l+ } W- w6 v' b% N5 |; U modelActions = new ActionGroupImpl (getZone ());
_: g! M9 y9 _- ? Y" }
# o0 s- z5 W% e3 }- b) \ try {
0 W: U8 J8 P1 O modelActions.createActionTo$message
) J2 B) i h: \( ]8 Y# s (heat, new Selector (heat.getClass (), "stepRule", false));4 q( n" B: A; i2 P$ R; ^) `
} catch (Exception e) {
! g9 M1 Y( l/ [. H# @+ |$ k. e7 `) w System.err.println ("Exception stepRule: " + e.getMessage ());
* t* R4 {/ U& v: y( B7 {* G, K3 {( b }
1 Q0 e5 z5 Y+ L' m: E8 X7 Q
: f: f; ~- I! w; |0 W try {
; ?$ Z. q9 y( y+ I% M Heatbug proto = (Heatbug) heatbugList.get (0);; T! S& L" _: c; o: f4 j
Selector sel =
8 B! P8 x9 H2 X0 ]2 ^ new Selector (proto.getClass (), "heatbugStep", false);% l, J1 i: s" [. F- j
actionForEach =2 R, H! A* p. T
modelActions.createFActionForEachHomogeneous$call" `2 X/ R9 A; n8 k: `
(heatbugList,
1 ~ u9 X+ M; T) n3 E; C new FCallImpl (this, proto, sel,$ N4 _; S; i' l# n
new FArgumentsImpl (this, sel)));+ D e6 h- H5 W% r. O% T1 W# P
} catch (Exception e) {
6 A: O2 N1 b0 R$ |6 Q% O9 A, @ e.printStackTrace (System.err);
* H# D& F* o+ p# P9 M7 S/ Y: c }2 J F0 w5 D3 y
( `% n$ U& I7 Q: k. {; H$ W" ~ syncUpdateOrder ();
{$ ?! _, q- z7 ?& y0 p. y( C) i: K
try { k# L# l: \6 d6 N. b
modelActions.createActionTo$message 0 G, G3 W D7 f+ e" }
(heat, new Selector (heat.getClass (), "updateLattice", false));6 w# a, o! l8 Z$ H# {) {7 t6 x: b
} catch (Exception e) {: O5 P7 n/ j- f) F' s
System.err.println("Exception updateLattice: " + e.getMessage ());
" u! H# I, B* M) `- a6 j1 o5 F }; q0 \* j/ L3 J. S
* \* w) h% v7 T5 |. e. { // Then we create a schedule that executes the
: X- b f% c, x, o1 _9 A7 ]6 w# z // modelActions. modelActions is an ActionGroup, by itself it
2 M8 U! F( h6 O0 {8 ] // has no notion of time. In order to have it executed in
' @+ D# m0 l1 @6 _ // time, we create a Schedule that says to use the i9 l* d, ^! D8 Q- d+ U
// modelActions ActionGroup at particular times. This4 T5 n ^* b% s- S6 Z
// schedule has a repeat interval of 1, it will loop every
. N0 {# S0 \/ D/ E# X) t // time step. The action is executed at time 0 relative to
* _0 }0 O% q" k // the beginning of the loop. @3 z" d/ t, L: z$ Y; D2 ^
. v2 j) h5 d; y& `! W2 v1 j // This is a simple schedule, with only one action that is8 Y8 X# n7 {2 M8 h( r
// just repeated every time. See jmousetrap for more" F4 c9 J `! g( d
// complicated schedules.
6 m: ^5 w' t1 r0 W 0 \% W$ M7 A9 o" y' M6 ]' n
modelSchedule = new ScheduleImpl (getZone (), 1);
- n" V6 W% W/ a2 i1 J modelSchedule.at$createAction (0, modelActions);8 f6 v- d, I7 o
$ c5 i" N9 M4 o/ W" D return this;
3 C0 N+ V& z% Z% j0 N* c' @7 {" a } |