HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 `6 S9 n1 z' W8 y: g
9 R. j( ~; V* ~& O3 ?- h, x" T: a public Object buildActions () {
) J! m* V4 N: W. T6 z% g- U super.buildActions();
% R/ W. Q/ X( g7 V0 Y; [6 a. q/ E
( r8 V* M3 O% J9 d- W // Create the list of simulation actions. We put these in
K$ [# N$ S/ m. F // an action group, because we want these actions to be/ `+ U0 `% e; V9 [
// executed in a specific order, but these steps should% j7 B6 `3 m1 L; W9 A& E& H" M
// take no (simulated) time. The M(foo) means "The message* c8 O5 o: T8 e/ o; B
// called <foo>". You can send a message To a particular; S) u6 C* E/ a% |
// object, or ForEach object in a collection.1 v! s, U" W9 f; E9 X7 y' _( x
2 L* X. l' u1 G6 p. r/ Z$ _
// Note we update the heatspace in two phases: first run
# S! u) s$ O9 k3 G // diffusion, then run "updateWorld" to actually enact the
2 K p% y4 L) B* [1 W // changes the heatbugs have made. The ordering here is
- q7 \! E3 e, k8 [$ N4 Y' e6 W- J7 W) w // significant!
& c: N2 I+ f4 G9 H! ^ + t- A: |7 Y% b
// Note also, that with the additional
) D+ {" ^) E! t* [( z // `randomizeHeatbugUpdateOrder' Boolean flag we can# `$ R; M, u+ O8 a
// randomize the order in which the bugs actually run* H. c+ L- ~+ `) \8 x
// their step rule. This has the effect of removing any
; |; B7 {/ w! M. Z // systematic bias in the iteration throught the heatbug9 t$ p" w1 s& u" S0 B' d/ B
// list from timestep to timestep
7 T- _- g7 m& L ' y+ R% }, U v/ H( x7 ~
// By default, all `createActionForEach' modelActions have: D' p* q' k2 M' i
// a default order of `Sequential', which means that the
. k4 }' V' X3 p# j) v // order of iteration through the `heatbugList' will be. ~* h& G5 N# T$ f
// identical (assuming the list order is not changed
8 @$ y: J; N$ f // indirectly by some other process).
4 v5 O! n2 u$ `) ^) ^ e3 k 1 y1 j& m8 H+ c) N5 U! d" x4 `9 d
modelActions = new ActionGroupImpl (getZone ());7 _$ S3 @/ `1 ?
& T4 {# p8 e- {: i
try {' t* c/ @, v" j) _; Q
modelActions.createActionTo$message1 m5 U' }6 \; \. X. c$ w$ ]. q
(heat, new Selector (heat.getClass (), "stepRule", false));3 ^2 Z% y) A8 d. c4 O) a
} catch (Exception e) {; P& }8 t/ k% r
System.err.println ("Exception stepRule: " + e.getMessage ());
4 e( U+ @) W* O% q/ L B" D }3 F5 n& W1 Y' X; M' j
7 s. a/ z/ I* Z& x& N try {
+ `4 l' _0 X, P0 M# u o1 E Heatbug proto = (Heatbug) heatbugList.get (0);
' w* f- V& ~. H6 U, J5 L P: ] Selector sel =
& e) |+ h8 u. q/ W& o- j new Selector (proto.getClass (), "heatbugStep", false);& k; n* s5 C6 ] S# t3 H
actionForEach =; m9 N& T; a/ e
modelActions.createFActionForEachHomogeneous$call& S( g3 G/ V$ n) _( j4 N) }
(heatbugList,' p7 r! @0 J* o1 K9 z v
new FCallImpl (this, proto, sel,1 T, g! s @& d7 B" D; c6 O
new FArgumentsImpl (this, sel)));
% n4 K! Z. g5 r2 w! }( c- m } catch (Exception e) {
- C# K+ i9 s1 D, `( l e.printStackTrace (System.err);
- c+ y0 t& G3 g }
3 y) Z2 b, N0 B. Z) j: ]! n1 Q & v- p: m, a" L6 v
syncUpdateOrder ();
" a) W9 A4 F1 B3 B# t& d4 h4 z1 g% k
try {3 L+ |# g1 y( J7 Q1 N# a9 b
modelActions.createActionTo$message
( T+ q: c2 \% j (heat, new Selector (heat.getClass (), "updateLattice", false)); o6 |. v7 \' O: L2 Y
} catch (Exception e) {
) o4 w' T5 z+ W6 e System.err.println("Exception updateLattice: " + e.getMessage ());
$ H: @ S7 M; y* i" x }
$ d! @9 X2 W5 |4 I# j- E+ [( o ; [3 d! _8 Z- X8 o6 c
// Then we create a schedule that executes the b0 ^+ N0 t4 c$ }
// modelActions. modelActions is an ActionGroup, by itself it1 X8 j. T! C# i c2 F2 r) k* u5 r
// has no notion of time. In order to have it executed in% Y& [4 D4 ?8 Z: E S
// time, we create a Schedule that says to use the
s4 C8 s3 a1 Q+ ?2 n // modelActions ActionGroup at particular times. This( Y+ @! v8 w5 p2 M$ m& V" _
// schedule has a repeat interval of 1, it will loop every
, I3 n' G: T% o // time step. The action is executed at time 0 relative to
9 c% I$ l' F" E) n) z% G6 ` // the beginning of the loop.& y1 E0 ~" u- @
7 T4 N: o1 \1 x7 b // This is a simple schedule, with only one action that is' G* r& `2 x( ] Y9 w
// just repeated every time. See jmousetrap for more% z; H, T5 [% u8 S3 s# M# H
// complicated schedules.1 a9 b* x; f) L2 R, w/ \% _# u0 T
$ W" S# b- p4 [3 r: N
modelSchedule = new ScheduleImpl (getZone (), 1);
$ C* e+ \- [! Q2 c. n+ D$ l7 { modelSchedule.at$createAction (0, modelActions);* E9 ]2 |7 X9 X) W r/ [" x" `
5 Y) p" M2 a; ^" x4 p return this;
% w( t {+ }' {0 R7 A } |