HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& x2 Q% F) M! D: s" N$ y
/ t" S4 V# I) P/ Q* c1 n4 q public Object buildActions () {
6 X8 u% ?3 |9 D! V0 s: G8 ?% t super.buildActions();
3 V) \; j$ o' D `% h x! f, O ; f" d: C: W) t2 A, B- Q
// Create the list of simulation actions. We put these in, U- b: l% i% `/ b
// an action group, because we want these actions to be' l/ N* }$ O3 ~/ I9 y. k
// executed in a specific order, but these steps should- X7 `- _; }9 x
// take no (simulated) time. The M(foo) means "The message
! u& l; i% M8 D! q // called <foo>". You can send a message To a particular
" l& E' \4 W, s6 N' ]6 D // object, or ForEach object in a collection.
+ G' v+ x( i) J$ b" l7 e, s
3 [1 e% R4 L1 z% ]3 Z( h // Note we update the heatspace in two phases: first run
# O6 ?% ^: C5 b // diffusion, then run "updateWorld" to actually enact the$ b% d9 S8 V1 i1 G
// changes the heatbugs have made. The ordering here is4 q: r! L4 I- g" S
// significant!/ N e. o2 G0 k; I2 F2 `
7 x9 c8 f% K% A: Y7 i9 C
// Note also, that with the additional0 G" K# C7 _$ r( A4 s
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 \' ]) n0 o9 F0 P5 [" \ // randomize the order in which the bugs actually run% L9 B' }; `% M
// their step rule. This has the effect of removing any+ B2 u8 d0 p* \, M
// systematic bias in the iteration throught the heatbug$ e; H- z/ t9 g1 f
// list from timestep to timestep$ w8 R, K ]/ s7 T4 U& h
9 _$ v6 r+ J9 T9 t4 G // By default, all `createActionForEach' modelActions have6 f, A7 n$ u6 y. ^) ?1 p
// a default order of `Sequential', which means that the
, U; m+ \# y! l7 ]7 \ C // order of iteration through the `heatbugList' will be! c! a: N2 ]! O5 K
// identical (assuming the list order is not changed. A$ M( X% s4 M9 P9 f3 V: U A
// indirectly by some other process).
9 Z) ]4 J8 V) i5 [' V . L# {3 _* [0 {1 i5 z% I7 D
modelActions = new ActionGroupImpl (getZone ());; M' Q; c0 Z& c$ V; a. F
& w+ f" p0 Y1 \) t; e, e try {7 B, S1 d- ]$ m2 L& O
modelActions.createActionTo$message
* Q+ H# D$ z7 w9 [1 p (heat, new Selector (heat.getClass (), "stepRule", false));
, z8 A% r$ \) T8 N' j; T! G2 ~ } catch (Exception e) {
! z7 v$ q+ \4 R: D _ System.err.println ("Exception stepRule: " + e.getMessage ());
7 e; w; C$ h% y5 r+ ]" h9 d }
$ f! C: q% n) C% e* u6 S" u5 D* ?8 g8 i% k& y1 S9 K& Y
try {: L" @$ y. D# Y2 l
Heatbug proto = (Heatbug) heatbugList.get (0);
3 O! n: b8 B4 c; ` E; A Selector sel = / l( o! y9 }8 n0 A4 m! H" ]$ N% j
new Selector (proto.getClass (), "heatbugStep", false);) x" F, l9 W5 C
actionForEach =( G- P8 p n% b: _: E1 f4 e
modelActions.createFActionForEachHomogeneous$call- i0 R, L# ^: U, o5 M
(heatbugList,
- C3 ]4 d0 X8 u% A4 u; ?4 Y6 Q# j4 \5 p* c new FCallImpl (this, proto, sel,, `* C; F8 S$ A, Y
new FArgumentsImpl (this, sel)));
: g) @3 w# t$ N; _8 v2 t } catch (Exception e) {% T6 r7 S" }' g( q" p
e.printStackTrace (System.err);
h/ T; m5 O5 }# d' |* S! G }
d9 x \& M" g- Z% Q/ {( v7 z2 X8 r
, T1 X$ g4 R# k7 w' z- \( q7 r) u syncUpdateOrder ();
! A7 A: h* c0 k, k6 K
$ \( u: o6 q! C8 }9 m' K try {1 c4 e1 F: m3 G9 r+ u& N9 c. ?
modelActions.createActionTo$message
, o. o& j( d) h (heat, new Selector (heat.getClass (), "updateLattice", false));
- z" J) j9 C6 f5 \& ^8 R# e } catch (Exception e) {; t2 H" T6 k5 U4 T
System.err.println("Exception updateLattice: " + e.getMessage ());$ h9 v" [9 s- x: }7 u
}7 g0 }$ K. o* D' a4 G
" l& i8 w5 \ B5 p // Then we create a schedule that executes the: f8 f9 K' f* @) ]8 K$ T! t
// modelActions. modelActions is an ActionGroup, by itself it
+ N! h# f& `: X( M2 A5 n/ i' v // has no notion of time. In order to have it executed in
+ _/ p9 t4 x* a$ k- D4 S. @ // time, we create a Schedule that says to use the
- f# x) D0 i, w- {+ e: F // modelActions ActionGroup at particular times. This
" G* d- U1 a9 D. N( S, i // schedule has a repeat interval of 1, it will loop every' E- [ C9 t* X9 n$ Q( `
// time step. The action is executed at time 0 relative to# q: i- g3 r. N
// the beginning of the loop./ F* V5 H3 W0 W3 N5 O. w
# o, Y4 x# T/ o // This is a simple schedule, with only one action that is
; K6 A" _$ B5 i // just repeated every time. See jmousetrap for more7 o2 e4 {( I5 x2 y. ?) T
// complicated schedules.' x; Q# f* Q! }; d: @0 h, z
" O2 l5 K( q3 V/ b modelSchedule = new ScheduleImpl (getZone (), 1);8 j0 ]1 S0 L- }7 y- R: G
modelSchedule.at$createAction (0, modelActions);
9 {6 x$ D, T3 A* M7 J/ r ) |, m8 o! U7 I( J7 z5 ]
return this;, [- B O5 `" S" `
} |