HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: g' u! {- F0 a
D! R# T& u7 [- ^# a# r
public Object buildActions () {. T1 E6 t+ b% Z$ j$ |. B
super.buildActions();
' z) A, a2 d9 a6 [; A
5 O0 x }0 a0 Q5 l$ _! I. j // Create the list of simulation actions. We put these in* c3 @% Q1 L4 D4 R7 ?
// an action group, because we want these actions to be
; s, \# F- _1 i. q0 o7 L$ K5 D6 N // executed in a specific order, but these steps should) n7 t2 l- `0 `1 k. B" y
// take no (simulated) time. The M(foo) means "The message
: T! |/ s# e- |! n // called <foo>". You can send a message To a particular/ c2 D7 [. r& x0 S2 @$ H
// object, or ForEach object in a collection., l: T! R0 `% P# ]/ w A
6 ?; t/ c! G7 v6 } // Note we update the heatspace in two phases: first run5 M8 X. G7 W; q* j/ G4 S
// diffusion, then run "updateWorld" to actually enact the6 Q5 F! Q8 y' g) [2 F; i
// changes the heatbugs have made. The ordering here is% i" e: X) x5 ^ S
// significant!
* T) U- G+ j; H# R
, T; v* `9 j. J# K, s" Y+ x // Note also, that with the additional5 C% R S6 w- O2 N# s; h8 _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 u- N/ T% I( } // randomize the order in which the bugs actually run" `. a. G, k: f* |( {4 E5 ~" P' i
// their step rule. This has the effect of removing any
* K8 ~: p5 v1 _4 t) K // systematic bias in the iteration throught the heatbug
7 U! t# c1 n1 } // list from timestep to timestep
( o: l+ C) ~4 T& n. ] , ]8 ?1 ^6 ]* r; K4 z
// By default, all `createActionForEach' modelActions have
+ n# s) X: {! ^7 S3 J: V' R // a default order of `Sequential', which means that the! B* e1 m9 e5 }
// order of iteration through the `heatbugList' will be
% e$ n+ z& n W8 C. ^ // identical (assuming the list order is not changed
! A) D w* U1 |6 L. g* R // indirectly by some other process).
0 f/ t9 \. I& K
5 o% B/ {: L- C6 }) m- F. Z modelActions = new ActionGroupImpl (getZone ());
7 h' ~1 [+ I" l8 J
- M) Z) A; B3 A" w1 D try {
: G3 M# A$ R/ n& {/ S modelActions.createActionTo$message
9 z9 T7 }! V3 y( U7 ]/ B }9 b0 d (heat, new Selector (heat.getClass (), "stepRule", false));3 V& N ^5 E# S% p* r6 M
} catch (Exception e) {
# b5 T3 E2 t+ P0 P6 C3 |- \ System.err.println ("Exception stepRule: " + e.getMessage ());! x4 T: y3 [$ p- S, f( `" N
}" ^8 e$ u* p; W m/ ?" \" U* A
m# Q4 S! d; \) h e
try {
4 e0 S% A/ V1 Z$ a1 v Heatbug proto = (Heatbug) heatbugList.get (0);
: u) d8 ^5 b3 Q. ]% x4 R+ w Selector sel =
5 S, {* F- W& R8 @' g/ k9 R& |* R/ H4 V; y new Selector (proto.getClass (), "heatbugStep", false);
8 J: P# q4 j3 \ actionForEach =8 m) h2 |* \/ I; h5 W
modelActions.createFActionForEachHomogeneous$call
6 h+ q. J. Q. p (heatbugList,& S2 D& {" G+ l8 h: @! b3 L
new FCallImpl (this, proto, sel,/ i# P3 u u! |: r/ J, }* H
new FArgumentsImpl (this, sel)));
5 d5 I% N* O( u9 Y7 r z } catch (Exception e) {5 n0 V4 ?# r9 \0 B+ b
e.printStackTrace (System.err);
! `" i& S* m7 W4 ~ }
2 z4 b3 f, ]; H! I: K) G3 ~. [; r
8 X5 W6 F h$ O) E8 n& x syncUpdateOrder ();
4 |% R7 a. E4 ?& j% J$ }+ j) B% @9 G- w; P( `- s. w% r2 T4 x+ r/ N
try {
% G$ s/ U4 i" m" B: a modelActions.createActionTo$message
0 l. C7 J2 g; a. B' P (heat, new Selector (heat.getClass (), "updateLattice", false));& V( H% z4 o' `0 P1 G* A- T
} catch (Exception e) {# M0 I" c. H! U9 h6 k
System.err.println("Exception updateLattice: " + e.getMessage ());+ l. g$ C( o$ l; d; R, _' r
}
+ k: G8 B* R& V* e4 {/ ?: T2 c# ~ 0 h& |1 `2 o5 h6 R
// Then we create a schedule that executes the
9 c& U2 e( x% M) R( K: n2 l // modelActions. modelActions is an ActionGroup, by itself it
# Z: `% P4 t* d# s- e // has no notion of time. In order to have it executed in
' _$ D, R: y3 M/ f8 I5 E // time, we create a Schedule that says to use the
, L7 J$ n% R& q3 e' w& f4 g // modelActions ActionGroup at particular times. This4 d( u( b7 b1 J' ]
// schedule has a repeat interval of 1, it will loop every7 E) D+ S4 ?# o) c4 s* I; k/ Y8 p
// time step. The action is executed at time 0 relative to+ p9 z4 O7 e" `& L
// the beginning of the loop.
! @! G" U! M$ B! R$ y7 Q- f& |& S3 C2 w$ B; _: N! Y7 D( j, u
// This is a simple schedule, with only one action that is5 h- v @3 Y. `9 u4 } N* L8 {
// just repeated every time. See jmousetrap for more
* X" g1 B# ]' _% c' w4 I3 r // complicated schedules.6 {; k3 |* A+ x: K8 T6 {
B* ~! \8 |: [: N: n& f modelSchedule = new ScheduleImpl (getZone (), 1);/ t I( |7 Z4 g
modelSchedule.at$createAction (0, modelActions);( W, {' k! b3 t) J# i3 j
! Q7 \1 M$ U# L5 N. _& R return this;
6 W, z' V6 o+ a } |