HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! z K. o' E: U7 E0 O2 J' T
; A& H- N# `- m, { public Object buildActions () {2 b7 f0 D- }* m* [
super.buildActions();- i0 _1 u1 p, u; r+ ]) X+ a
1 Q9 p0 E9 i2 O' K% Q# p
// Create the list of simulation actions. We put these in D- A# e9 Y' d* f7 }+ d% H
// an action group, because we want these actions to be5 o# m) J) K: }/ Y5 G; b
// executed in a specific order, but these steps should& y* y Y: s2 S8 c$ Z3 u
// take no (simulated) time. The M(foo) means "The message
* j8 n' q6 }3 Y6 h( v. k+ k" \ // called <foo>". You can send a message To a particular* l4 M5 X I' R: r' M
// object, or ForEach object in a collection.: _; `: T6 c; N/ h8 s4 f
. U0 ]/ ~: Y2 j. ? // Note we update the heatspace in two phases: first run5 V- [$ O2 D& N) Y3 X- E
// diffusion, then run "updateWorld" to actually enact the/ H6 X- B1 ]! L9 g; s) M3 g
// changes the heatbugs have made. The ordering here is. V' N: D" G1 c; C) t9 ^
// significant!" y% Q, s+ F% P! z
# z8 @# t( r3 c& f: E/ g# s8 {
// Note also, that with the additional
: h) ^6 v. M8 w8 I p* h // `randomizeHeatbugUpdateOrder' Boolean flag we can6 y5 c% @6 ^# ?* p
// randomize the order in which the bugs actually run
# d6 C9 m: D) ^. X" x4 a // their step rule. This has the effect of removing any# f/ D4 O2 e" f
// systematic bias in the iteration throught the heatbug1 D! `. A( v- F, A) T9 u; a' [0 e2 Q
// list from timestep to timestep. a: i# h W7 S! `8 y( q
- L" g* t$ F5 w0 z, u3 M
// By default, all `createActionForEach' modelActions have
% n" e0 z5 g, D8 G+ d: u8 ] // a default order of `Sequential', which means that the5 Z$ w- V ^8 q' Q, x7 q
// order of iteration through the `heatbugList' will be [* `0 G9 }9 m7 K9 _
// identical (assuming the list order is not changed
+ C/ X. H9 s0 m // indirectly by some other process). x3 p/ c3 j. ?$ r: p
2 ?$ B2 Z2 ?$ D0 W) ^& a7 Q2 j F
modelActions = new ActionGroupImpl (getZone ());
2 ?7 I" n r- M" x1 j; d/ t% A$ E% y3 [% Q1 n
try {
; ?$ t1 ?; E( o& ^/ n6 y# X8 n/ W modelActions.createActionTo$message( e/ R* J6 s5 d/ y; s3 X( K; Z
(heat, new Selector (heat.getClass (), "stepRule", false));3 D+ d# M3 D ^, B* b+ L2 Z! C
} catch (Exception e) {
# S. y0 d6 s* o3 _& i- z* C6 H System.err.println ("Exception stepRule: " + e.getMessage ());& I1 p1 T: ?6 c
}' W1 M1 p) p% N) Q8 k& H$ M
+ h* M- c# V. d: T7 E
try {
0 Z9 W8 m5 b" r3 G& B! e$ Y Heatbug proto = (Heatbug) heatbugList.get (0);% z/ E& {0 I; d- y) K
Selector sel =
& v5 y! O$ M) D- u0 r6 N new Selector (proto.getClass (), "heatbugStep", false);
9 e5 g3 N, I! W actionForEach =" W/ J$ Q) g! c1 V5 }& s0 F n2 K
modelActions.createFActionForEachHomogeneous$call
( W2 E6 U6 H8 n- w: n (heatbugList,
$ _# L( o- w' W+ j new FCallImpl (this, proto, sel,# W9 [' c7 o# h; V
new FArgumentsImpl (this, sel)));
8 K' M: Y+ d& J, j+ @* z$ ] } catch (Exception e) {1 D9 H# r# o( k- V5 k. D
e.printStackTrace (System.err);# z5 N( Z) l, q. ]( ?
}& N; L( Y, l; [8 i E
; a1 G1 f% S. E( K5 n1 N2 W/ K! J syncUpdateOrder ();
3 h0 f; v2 u" t) ?6 _
{- S5 |8 s$ Q8 \ try {
6 x7 S! x7 \! K5 ?+ r$ O3 W modelActions.createActionTo$message
: d% M& p1 ^6 U) G6 U (heat, new Selector (heat.getClass (), "updateLattice", false));2 O5 V$ s9 V+ P0 F8 k. b/ _* P
} catch (Exception e) {$ L ^3 H2 t" h# g+ A. R
System.err.println("Exception updateLattice: " + e.getMessage ());
" h: I: y! t3 J" ~) I& B6 ^ }5 _5 p0 [: P2 L. a I9 C
: R' U `/ R" o% G9 j
// Then we create a schedule that executes the
0 @! m q* Y; G // modelActions. modelActions is an ActionGroup, by itself it% z2 @: ~# ]) o- H) e9 m2 i) h4 K
// has no notion of time. In order to have it executed in. O2 O5 Z; |0 W& I& r- T
// time, we create a Schedule that says to use the( o4 a6 ], }5 J- g/ f
// modelActions ActionGroup at particular times. This
* F0 H1 H- A# @5 R! P% P // schedule has a repeat interval of 1, it will loop every* [( |7 g1 {( k$ Z8 K6 X
// time step. The action is executed at time 0 relative to
, ^ S) w* }$ @' Q0 ` S( R5 [# W // the beginning of the loop.
( l) l$ I% k# Y
$ ^. E. r% k- G) n4 w" | // This is a simple schedule, with only one action that is
3 v5 y8 q4 k \; A3 b' z8 H // just repeated every time. See jmousetrap for more$ c4 p8 [3 F& U' Q" A% @
// complicated schedules.7 G n: {" N8 j9 ^1 z; R
y8 T1 [% y) }# j! r$ y4 }
modelSchedule = new ScheduleImpl (getZone (), 1);
$ g. i) F' F+ a. `; [+ R modelSchedule.at$createAction (0, modelActions);
* \3 |' @/ n* C2 S% a- `$ T6 ]
! }0 U" K/ G! s1 k' Z return this;3 ]/ \# ?% K8 q8 ?9 I
} |