HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ G" T# u8 t5 s
- o/ o o8 e0 }7 M2 A9 i+ ? public Object buildActions () {
% w! l% v1 p' j! ~0 _6 y! o super.buildActions();# U2 [8 Z2 y1 v6 M9 |& Y* p1 O. U
. n( m! R7 O: s // Create the list of simulation actions. We put these in
& k' Y3 [+ z. B* x Q6 @ // an action group, because we want these actions to be
: P& [. j2 o8 O9 h) C9 _* R // executed in a specific order, but these steps should
5 r: E0 {1 p/ U6 ] // take no (simulated) time. The M(foo) means "The message
' ~; ?1 I9 g* ~ // called <foo>". You can send a message To a particular: J# r: d" t, M7 m4 Q0 Z9 A7 l( C
// object, or ForEach object in a collection.
2 D5 F# B( Q/ `, x, F. R / c7 n% b6 C: U
// Note we update the heatspace in two phases: first run' l! ~: `. m7 u; N# N) Y' Y
// diffusion, then run "updateWorld" to actually enact the' f% \. [( P& t6 O# Z6 f
// changes the heatbugs have made. The ordering here is. F0 a' H9 ~0 h' d! Z T5 H- C! d3 a2 X
// significant!) U3 J" a: m5 r1 Y x) r. B2 v
0 C; D: A2 g2 Y! q" X // Note also, that with the additional5 b4 A- A0 i) E! K7 `+ @* K
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, N9 M9 t5 B- g$ u, C* v // randomize the order in which the bugs actually run
1 y2 H# {2 d6 }% l, t1 ~) Q0 F9 W // their step rule. This has the effect of removing any) U! C; Y% P$ @7 S
// systematic bias in the iteration throught the heatbug/ B- e2 E( r: [% w7 l/ b5 d
// list from timestep to timestep
: b, A Z8 f w) `# c* X1 x5 q $ s. [1 [6 p+ E m3 Y6 e o9 e
// By default, all `createActionForEach' modelActions have
3 Y+ U7 z+ n4 l // a default order of `Sequential', which means that the* w. A9 \! C; s" s0 ?2 Q6 V
// order of iteration through the `heatbugList' will be
' K+ R9 S/ E7 u5 v( o // identical (assuming the list order is not changed( c3 O1 l n4 M% A/ i
// indirectly by some other process).
& ]6 U: B' ^8 Z! [/ G5 H; } + k; i2 `( ?$ s3 F! K" o4 u3 L
modelActions = new ActionGroupImpl (getZone ());
1 S' c" n' g W" G4 R5 s9 u
. E O; I6 ?0 c6 ^% ? try {2 H* k7 a+ u' X
modelActions.createActionTo$message
- n1 s* C4 L- w (heat, new Selector (heat.getClass (), "stepRule", false));
% S# v5 o6 p+ ~" W6 \9 {* ] } catch (Exception e) { ]. d/ ~/ v( D
System.err.println ("Exception stepRule: " + e.getMessage ());
" }+ a8 y, d. Q" U' h: Q# D; Z7 j" B }
& i8 c% o1 J0 i. p0 L% V8 ^. v
6 G6 o5 C/ H- Q7 L( Q6 W try {5 _* e0 l% M h" z
Heatbug proto = (Heatbug) heatbugList.get (0);
* M6 k! G7 F2 |& S9 l# h# n1 q. w Selector sel = # r) s0 E( @6 g4 M% r
new Selector (proto.getClass (), "heatbugStep", false);0 `4 Q! H; I# N3 a1 r$ S
actionForEach =
) r% R$ a6 T/ k, r* E( h modelActions.createFActionForEachHomogeneous$call1 q; K& o V h$ \
(heatbugList,$ q5 t4 A" K" s3 ^3 C5 N5 r6 n
new FCallImpl (this, proto, sel,1 d4 x" E2 W5 }$ J, l1 g
new FArgumentsImpl (this, sel)));
" j1 H) ] c( h. i; d) I Z* a } catch (Exception e) {3 y$ O8 @% c. i$ z0 R2 }! O$ r
e.printStackTrace (System.err);
. Y) H4 r7 {( k }
4 M, H. N; h2 s2 K! i6 b$ r
$ f! B h2 ]2 X8 M8 |6 }1 e4 |% i syncUpdateOrder ();$ e9 d, } p0 e
( [7 K& }8 J) K! ] try {. F. _8 S4 Y% w% l
modelActions.createActionTo$message - n8 i O& ` q6 }* }2 R! J2 T
(heat, new Selector (heat.getClass (), "updateLattice", false));9 V# I- F! h/ ^4 O
} catch (Exception e) {; i& y& ~8 C2 _. ^4 [- q9 j$ g
System.err.println("Exception updateLattice: " + e.getMessage ());+ a9 X* m% d& J( a
}( `$ G0 S" w! j& s
, s) R6 N- j! x9 K4 X! \9 w* X/ v // Then we create a schedule that executes the
3 ]0 G9 [) O3 j+ s* D+ \8 S // modelActions. modelActions is an ActionGroup, by itself it
. D k- N+ \6 D // has no notion of time. In order to have it executed in
1 g2 t4 U5 r) l* |# _$ ~0 n, Q // time, we create a Schedule that says to use the/ _7 a3 `- C' N' e( P5 y G
// modelActions ActionGroup at particular times. This3 c" d( c0 z8 [* ~6 I
// schedule has a repeat interval of 1, it will loop every, z* H: d- I! d9 N3 |( @- B
// time step. The action is executed at time 0 relative to
7 R! \/ r' P; j( y# i9 L$ P4 V // the beginning of the loop.
; A* d# O3 I/ C6 A& R: l( O8 E; w& ~: h" V* N6 e9 {6 N
// This is a simple schedule, with only one action that is
4 b% h; k8 v, v$ g5 ?' c( V& [! d% D // just repeated every time. See jmousetrap for more- l. {5 ^. C: f" `, U" x
// complicated schedules.
$ C; ?, x. v5 m( V
' L0 o* Z% w$ J: } modelSchedule = new ScheduleImpl (getZone (), 1);% S* y5 H1 y" t [
modelSchedule.at$createAction (0, modelActions);! \: I R, N- n$ j9 |. _
( a# W* W5 [) q9 k
return this;
4 P) D9 ?1 K# x- A: u" b% q3 J0 b } |