HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
j l b4 L! n# H! o" M' ]% b5 q- A- l1 n0 g6 u7 X3 k
public Object buildActions () {1 G; \( t/ R/ M; q1 O, Z
super.buildActions();; c( h% Y) b1 c! A# s% d
5 o) \; i3 J1 [ // Create the list of simulation actions. We put these in' s; H- s- W: t" ^
// an action group, because we want these actions to be; z* G C3 `6 t5 k( r ~: {
// executed in a specific order, but these steps should
" ^) E7 v! r7 [ // take no (simulated) time. The M(foo) means "The message& T# v4 `1 ]0 d1 F+ I* t0 c& y
// called <foo>". You can send a message To a particular3 n1 x* d) C: q9 ]" A. L
// object, or ForEach object in a collection.+ b% b# Y; P; w: u9 @+ O
% r: j" H: b: J- L // Note we update the heatspace in two phases: first run b* b7 A# g' L( y8 v
// diffusion, then run "updateWorld" to actually enact the
" W4 j& d9 c. S1 A( z9 |9 S // changes the heatbugs have made. The ordering here is
* _2 E' J: v7 M // significant!
0 f3 _% v [) m- m* R) F . h* e9 U% K6 U9 u9 i
// Note also, that with the additional& }. C- G+ G4 k2 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can' B% L! n* g! O ^% A
// randomize the order in which the bugs actually run
# ^9 K8 ~5 d" D- G$ U7 b // their step rule. This has the effect of removing any% C/ w5 h2 S3 S" I" i. G8 s8 s
// systematic bias in the iteration throught the heatbug
4 K# h, n& y8 k // list from timestep to timestep# l7 x+ v, m. M$ e6 q0 o. @ t
/ i" {( r0 S, M J
// By default, all `createActionForEach' modelActions have
i- C) M. G, n6 t // a default order of `Sequential', which means that the
0 j& O$ u8 T* [4 @" o- f1 } // order of iteration through the `heatbugList' will be
4 k2 {$ |" x( d2 p8 v // identical (assuming the list order is not changed
; N8 I, O! Q! @ // indirectly by some other process).
' Y0 I9 w8 x0 |/ V8 V" x/ y- k 5 m) a. c( U. h& u
modelActions = new ActionGroupImpl (getZone ());( y0 L% ?$ D, a/ p9 H, o' n
8 e, O. S, Q" w' N
try {! X% Q% `7 G0 X6 a* F5 `. ~" y
modelActions.createActionTo$message; B& Y0 C: |7 E! ^* x3 o
(heat, new Selector (heat.getClass (), "stepRule", false));
. x+ u7 k, _8 @ } catch (Exception e) {7 g* e+ I) r4 U7 x1 q; T
System.err.println ("Exception stepRule: " + e.getMessage ()); G9 S- Y3 p3 e" l# a2 P
}! o( ^* C& @& O! A8 k# `
2 V# P t3 b- |- E( |- }6 u
try {
2 F; A% [2 P0 n% e Heatbug proto = (Heatbug) heatbugList.get (0);/ H% Y; E5 ?- t' X
Selector sel =
" L3 e- R5 V9 I1 ]) e9 r new Selector (proto.getClass (), "heatbugStep", false);
7 _( o) _$ i5 o% _# [; r; C$ n actionForEach =& G1 h! O4 p4 h& ]2 b% t
modelActions.createFActionForEachHomogeneous$call
~* X' W2 n' J (heatbugList,- K' U0 [: ?, q/ z: G
new FCallImpl (this, proto, sel," Y0 X' C; D$ _5 e1 P3 L
new FArgumentsImpl (this, sel)));
! P1 A% m. y2 b; U4 a8 E+ b } catch (Exception e) {
$ W9 \" [, I5 o8 ]3 e U3 A e.printStackTrace (System.err);8 n- O, @5 U! N. a* R
}
6 z6 A: {8 S( _: u' }5 d- T
/ \- A- Z: W1 n5 d syncUpdateOrder ();
3 P1 x. h z0 W2 u5 F* d
5 r% v1 Y# L9 P( K, V try {6 @& ^/ }+ p% n9 v& L: Z
modelActions.createActionTo$message
, R* e' A6 Y8 {! U: E& G2 j( p (heat, new Selector (heat.getClass (), "updateLattice", false));: b$ ~* \4 P2 i/ g8 F1 x! o% d
} catch (Exception e) {: H# z4 h; A2 C0 j0 b. b+ f
System.err.println("Exception updateLattice: " + e.getMessage ());
1 `; W8 ^$ Q8 h) l, d }! Z- p- {" q- z7 _5 S) z$ C X
( d" J8 K6 k4 I* w2 C& i. Q
// Then we create a schedule that executes the/ k" Y$ f$ c4 T
// modelActions. modelActions is an ActionGroup, by itself it! I, l/ _/ k; r
// has no notion of time. In order to have it executed in* T2 N- |/ q a6 T3 l! `( s
// time, we create a Schedule that says to use the' q4 u) n# P0 ]
// modelActions ActionGroup at particular times. This
* w) i% S1 N. `$ V: u# W+ y // schedule has a repeat interval of 1, it will loop every
- _: D5 b. |( i" S: t x // time step. The action is executed at time 0 relative to" {! T7 ^# {) w) E# F) n2 ^' z: }
// the beginning of the loop.
; Q8 R, I! z2 m3 |3 b9 i E) n1 f9 [7 q3 \& T, \
// This is a simple schedule, with only one action that is( x# z; s) t; y% D% J1 B' c3 j& q1 k/ q
// just repeated every time. See jmousetrap for more! j' X% f1 g3 g' \
// complicated schedules., p& R. F" R B; i
8 G2 w2 p4 }5 T2 k1 D
modelSchedule = new ScheduleImpl (getZone (), 1);$ \+ f/ d0 F* H: Q0 V
modelSchedule.at$createAction (0, modelActions);
1 h( b$ |/ P Z
6 B. f) d( c6 ]* ~& _ return this;( k5 ]& W/ i6 z4 a, h) m5 }
} |