HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# b% d! c B. f) X# h. _, z% g& V
5 m$ D: o2 u. t3 {7 w
public Object buildActions () {
1 V: D& c: m+ D! |- K super.buildActions();
8 A6 @" [% o- Z4 o8 F6 L
( k# f u9 J5 ?( @# h+ @ // Create the list of simulation actions. We put these in
7 o( w) q+ h, V9 L+ [' j // an action group, because we want these actions to be
! s l4 p; m! n( b" d // executed in a specific order, but these steps should5 W4 B2 d2 k- x1 i% `" J+ M$ F
// take no (simulated) time. The M(foo) means "The message
, |, V9 Y! C5 f X T2 h // called <foo>". You can send a message To a particular
$ E8 d5 I/ A; [, |: p* d3 a // object, or ForEach object in a collection.& ~4 _* ]9 w, \* j; a" d4 w
; r6 p% j6 d7 ~ s" b5 h
// Note we update the heatspace in two phases: first run: M' H, y6 y) h7 Z0 p' K ~
// diffusion, then run "updateWorld" to actually enact the6 D. F* w3 y# K$ ~
// changes the heatbugs have made. The ordering here is8 [: F: a" N- b) `1 m5 @1 J
// significant!5 w) q, E% i7 d) s- c2 s
7 a2 z _4 ~# ^. _) e w // Note also, that with the additional# I# N9 N; z0 O# f$ u# m
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" r' `8 K7 @, |3 ?) l // randomize the order in which the bugs actually run: g* x: @6 H/ n8 y
// their step rule. This has the effect of removing any
' I9 m7 D2 [7 d7 Z8 T: n& j // systematic bias in the iteration throught the heatbug9 z2 L" ]& p- G8 R
// list from timestep to timestep# K% m0 ]& D- P. N' K- d
8 M7 h2 X$ a6 ^+ z6 O3 @. Z) V
// By default, all `createActionForEach' modelActions have7 r% S, H0 Q% k; D
// a default order of `Sequential', which means that the
6 `9 X1 O0 I/ H2 B7 k // order of iteration through the `heatbugList' will be @" X' Y& T% X x8 N0 i
// identical (assuming the list order is not changed
8 F; Z. P2 C! z: Z# j // indirectly by some other process).7 x$ L# V4 b& {
" L @5 b1 z/ [ modelActions = new ActionGroupImpl (getZone ());
( n- Q6 d: I! Z2 V6 P4 x- F# W$ M$ g2 M) y2 i) @
try {, v0 T; H4 m' w. d1 {
modelActions.createActionTo$message7 w) L( C1 f- V' r: O& ]. G7 a
(heat, new Selector (heat.getClass (), "stepRule", false));: ]' K) e* A! Z* m
} catch (Exception e) {; q0 A7 k0 s h& d
System.err.println ("Exception stepRule: " + e.getMessage ());8 o0 V. \7 D# D r9 C
}# E& a8 v8 V: ~$ B1 d
5 E& e7 w+ ?( B! g" c try {
g# x' C: A% [. @! G+ U J2 @ Heatbug proto = (Heatbug) heatbugList.get (0);2 H( x3 p2 }' Y4 Q5 m& E9 {9 U
Selector sel = ) e4 _4 R$ o$ r+ |/ { [1 d8 {
new Selector (proto.getClass (), "heatbugStep", false);
; N4 D. D7 [& f actionForEach =
, C1 b" e- Q5 z! I( r' ]; v9 @ modelActions.createFActionForEachHomogeneous$call
. a: l3 l; ]+ ], A (heatbugList,
2 }7 k. n# a5 I0 U: N new FCallImpl (this, proto, sel,% _2 w ]5 }9 X8 Z
new FArgumentsImpl (this, sel)));
& a& }1 z0 y! g- P4 K; G) N } catch (Exception e) {1 J' Z5 Z2 l3 d9 [
e.printStackTrace (System.err);
3 i0 S7 Z# ` p- A6 x: j, i0 F }. D) w& B8 E- h. i; D5 J( E
$ L. h2 Y9 ^, r G+ z syncUpdateOrder ();0 J+ C9 {2 k' h
5 T$ B8 k' R, h try {3 f3 E. e7 g: r' L
modelActions.createActionTo$message
& c8 n6 ], n$ B" K+ V (heat, new Selector (heat.getClass (), "updateLattice", false));
, C* q2 G& d8 g } catch (Exception e) {+ u7 _( _8 t8 g& Y" ]' c6 A
System.err.println("Exception updateLattice: " + e.getMessage ());
9 `# K: O6 s4 |- D7 D6 b }
% D0 `* u) z4 K* O4 ~ * n+ i& c3 N' R4 L! e
// Then we create a schedule that executes the
& L5 l" U+ m# Q. Y // modelActions. modelActions is an ActionGroup, by itself it0 w/ Y! s8 G/ j% d) Q' g: a' R
// has no notion of time. In order to have it executed in
: j7 x) H1 V! _0 m // time, we create a Schedule that says to use the
. ]# U4 ^- m' m6 h. l9 h/ v& @" t // modelActions ActionGroup at particular times. This2 V0 x$ K3 c% [5 d. O* s3 A
// schedule has a repeat interval of 1, it will loop every. M4 Y: x1 V* M! a: g
// time step. The action is executed at time 0 relative to7 N/ V8 [, A: K6 l" N9 ?
// the beginning of the loop.
% Z; |- t X" W5 Z; A$ j& K, L1 u1 i0 S+ \ P9 d0 s7 D9 E
// This is a simple schedule, with only one action that is
" }" W$ u* `' q2 U% ~) x7 a( H // just repeated every time. See jmousetrap for more
+ z" ?0 p" o7 c. a3 _( \ // complicated schedules.
" T0 x( B0 `) K' c7 H
8 R& n8 Y0 V. M5 n modelSchedule = new ScheduleImpl (getZone (), 1);
) {. l5 ^# i0 \4 P modelSchedule.at$createAction (0, modelActions);
6 b; h& @' a7 r; W6 l( k - L* O4 q- B! H$ ~4 A, U% S
return this;
# `" ^2 O; ~) X4 L! h1 j } |