HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) p8 ^9 h& T5 Q
$ ]7 I/ n$ F& D public Object buildActions () {2 w0 D- O* l7 B$ c8 H
super.buildActions();
8 t; ?; o* C% j ]; d6 r. q0 |* M2 @. {
// Create the list of simulation actions. We put these in* _3 E. o* R: @4 E3 s" w7 z
// an action group, because we want these actions to be9 v$ @2 T m' {! u1 [, |6 I
// executed in a specific order, but these steps should# a W+ }. s1 Z. y m2 {6 |
// take no (simulated) time. The M(foo) means "The message$ O- X5 W0 U9 [0 [1 E c" K0 j
// called <foo>". You can send a message To a particular9 ?- [ w% g# ?5 G$ F
// object, or ForEach object in a collection., F) [5 F/ L7 g! K- P& e; B8 q
- w) c+ e6 f& Z4 S/ y4 n# { // Note we update the heatspace in two phases: first run( G' R, |! W! e6 w' H! P+ z) p
// diffusion, then run "updateWorld" to actually enact the' \- P6 Z0 f$ U$ I* B
// changes the heatbugs have made. The ordering here is
: V) n1 ^7 v+ S* M0 u* n6 E // significant!) _" @; p% k# u$ D
+ S8 S: j* z( K
// Note also, that with the additional
4 J' e, I; }8 z1 V // `randomizeHeatbugUpdateOrder' Boolean flag we can( J( Z! {" x: u& D) }
// randomize the order in which the bugs actually run
7 K& M: {( C4 {9 [& ^4 U! z // their step rule. This has the effect of removing any- H6 i% X$ }+ C2 h }
// systematic bias in the iteration throught the heatbug& E. R9 K. |" V- H4 \
// list from timestep to timestep
& u4 G, V/ ]" `
$ R* ~5 F! ?$ | // By default, all `createActionForEach' modelActions have8 [( c W! E9 g3 s- r2 t# q) Q
// a default order of `Sequential', which means that the9 F& U+ V* ?: u& v/ c
// order of iteration through the `heatbugList' will be
& l+ \5 B+ f! r* ~( L8 z# H // identical (assuming the list order is not changed; A, U! d, l# E3 V; N' V; L0 b
// indirectly by some other process).4 s6 Q& @/ y4 m! I* v* u
4 n/ h2 H: o5 o& F$ x ] modelActions = new ActionGroupImpl (getZone ());
9 O% y! U* E" G( W0 P- _( ?, D; k2 ~
try {
8 W& W% C. v9 X% j, \# O% x3 Y modelActions.createActionTo$message$ R' F1 w+ M8 L3 u. X M6 o
(heat, new Selector (heat.getClass (), "stepRule", false)); z& ]5 V5 V" `8 ~% f3 q4 {2 C
} catch (Exception e) {" s& j1 r) f" I8 M! P
System.err.println ("Exception stepRule: " + e.getMessage ());
# F* e/ a/ ~+ @' T* t }
8 t) n8 R y* o' k) d- }- D* t. J3 d/ P/ v& l
try {
8 [" F# Z$ d C7 k- s J Heatbug proto = (Heatbug) heatbugList.get (0);7 [7 D4 Y* a2 J r/ V& Y
Selector sel =
2 u I- D3 q5 c, U new Selector (proto.getClass (), "heatbugStep", false);
* z! c0 C% ]& x, _0 [ actionForEach =
/ P6 F; y* ?6 r0 U/ g+ t$ \2 Z9 @7 j0 I; H modelActions.createFActionForEachHomogeneous$call
$ m; a) N S k' A+ S4 f5 M (heatbugList,% D3 B, j. Q- O8 d
new FCallImpl (this, proto, sel,
% l3 x: @0 n8 F! g6 w' | new FArgumentsImpl (this, sel)));
" j6 `, m, ~ n7 Q) J } catch (Exception e) {8 R: N. m3 H& {- n, I
e.printStackTrace (System.err);, J7 U7 V9 x, V* K/ k8 @
}7 j: E8 j8 k) Q* B0 C9 t2 I' r/ ?
; j8 p% l) [3 o
syncUpdateOrder ();
+ n. n6 k* _, z) r2 e3 J% g+ U; P( R% r) B$ Q& d
try {
/ z* W6 v2 ~& V5 O4 m1 a modelActions.createActionTo$message 7 E) |6 B2 x, @& K( H
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 v- x' Y o& p( ~ } catch (Exception e) {
B3 H( n: }" ~' t System.err.println("Exception updateLattice: " + e.getMessage ());2 P1 U/ a0 c S' r R' }+ _" c
}
. p, ?& _. v. R: P. E( a% f) A
. c, Q1 K9 Z) l5 n- d& ` // Then we create a schedule that executes the7 t! @0 Z- j" U$ L
// modelActions. modelActions is an ActionGroup, by itself it) d' N& m! F0 m0 h+ k9 s
// has no notion of time. In order to have it executed in
1 F6 v- [( G7 U3 |2 A // time, we create a Schedule that says to use the
' r$ R5 Q4 l% ^8 r N // modelActions ActionGroup at particular times. This, P$ A8 f0 z: P9 y3 b1 U( ~4 e# V
// schedule has a repeat interval of 1, it will loop every
9 [! m! F$ W" V // time step. The action is executed at time 0 relative to
. t+ w0 n: R0 r: Z3 J // the beginning of the loop.0 U( ~ k2 ~! G9 G H
V7 C4 U% b6 O& f6 [+ N, a3 ~
// This is a simple schedule, with only one action that is- h7 Y" P9 y0 l
// just repeated every time. See jmousetrap for more3 e# T0 K3 d2 A) Y7 O
// complicated schedules.3 \' V1 v2 I3 y) g' {
* Z4 A: |% G) R0 N8 b modelSchedule = new ScheduleImpl (getZone (), 1);
: B9 g5 h s8 |" j: ?: e: V- `9 `% D: E modelSchedule.at$createAction (0, modelActions); V+ Z- \2 k) B# ?
' G) v0 w2 l( ?& f return this;: l( d, i' a) Y& _
} |