HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 A# z0 o. ~- N/ W
& p" p, D0 k% F, G: [' c8 g/ U! k+ s public Object buildActions () {
6 g; M+ e: D3 r4 g$ K9 d$ _ super.buildActions();8 @ ^# z: M _* }; m6 p4 k
) [# j; B- K5 u1 K // Create the list of simulation actions. We put these in, l/ O) k# b$ F2 I
// an action group, because we want these actions to be5 T+ t9 w0 _2 k; w4 H2 ?
// executed in a specific order, but these steps should
0 k# m( b* ^) t* f( J* Y9 m. u9 G // take no (simulated) time. The M(foo) means "The message9 q+ \6 W' i2 o: s$ l. f
// called <foo>". You can send a message To a particular0 i2 T V5 [! m6 y' N. t# F8 L
// object, or ForEach object in a collection.
% W3 b2 W/ Z& o5 A+ V" E+ ~ ( K3 X7 m+ t3 @9 i7 Q: A" ]# X
// Note we update the heatspace in two phases: first run1 K; l9 ~3 x) |. z8 h
// diffusion, then run "updateWorld" to actually enact the9 e- H) f+ ]1 ~7 J5 ?" s
// changes the heatbugs have made. The ordering here is
. g" L2 g% b, k // significant!+ |' h/ i g5 z% Z
# h! a# _3 a5 w) c* O // Note also, that with the additional
H$ k1 |6 Z6 z6 p3 y% F, x // `randomizeHeatbugUpdateOrder' Boolean flag we can2 q! G1 @7 q2 O- C e2 R0 C
// randomize the order in which the bugs actually run3 H- U* {5 G3 Y W0 b! f
// their step rule. This has the effect of removing any4 j- `; G) }0 _8 C f7 _
// systematic bias in the iteration throught the heatbug
$ Q# G) X' u7 a& h: B% f, y$ M // list from timestep to timestep9 P: z$ D m6 q2 X- ]
6 S F5 @8 M3 a; e1 N // By default, all `createActionForEach' modelActions have
0 a$ Q/ u4 y) L9 O // a default order of `Sequential', which means that the+ D. [$ Q4 b2 Z1 o& ?$ a+ @
// order of iteration through the `heatbugList' will be) q& {7 D' k3 d' }7 ~' N
// identical (assuming the list order is not changed
* C" x: K5 P) c; A // indirectly by some other process).
3 M# C8 o% }$ Q0 o! c/ s, w1 D. W5 Y
9 h. i( v9 W1 h- S# m9 v) m0 L modelActions = new ActionGroupImpl (getZone ());
, _! t( J7 L: I2 x8 V- l2 i
0 A6 }5 j+ Z. i# _' \ try {! m( Y" N# Z- D: G6 v" {- {
modelActions.createActionTo$message
; d- Y. }1 c Q J/ y+ Q (heat, new Selector (heat.getClass (), "stepRule", false));% Q, J8 I( f" V8 E) n; t8 L! u
} catch (Exception e) {
2 D' e5 e! I \- p. x System.err.println ("Exception stepRule: " + e.getMessage ());. q! b2 b; w6 I5 w
}% C9 a6 K' X$ M. Y9 x- _) S. }# M
) x/ Q( A5 C2 L& _& i/ h+ S7 @
try {/ d) k( h9 b9 q) m8 |
Heatbug proto = (Heatbug) heatbugList.get (0);: C4 D& R+ J, R
Selector sel = & B& W) Q9 S, p! P% h! T
new Selector (proto.getClass (), "heatbugStep", false);
# o; h! @' S0 P actionForEach =
8 y3 Y+ M, Q2 M modelActions.createFActionForEachHomogeneous$call2 M6 n% _+ t3 A, o3 @! D
(heatbugList,* ?; a0 p, z9 O% ?
new FCallImpl (this, proto, sel,% e. z6 G4 F5 j9 y1 r9 d
new FArgumentsImpl (this, sel)));; Y5 S) s p, L% V1 C* q0 I/ e. q
} catch (Exception e) {
& @8 h( `) }! Z5 C6 Z5 l e.printStackTrace (System.err);
|, F1 U4 a$ d* u. a% q9 q6 Q. O }
& U4 ~! w6 N, v7 m, g- S3 w
# R0 R |* \- p* v8 |0 f ^ syncUpdateOrder ();
5 e& c0 T, }8 ], k- ^4 C& E+ e6 N) c2 S+ O
try {* f# B! j5 v' Q9 [/ l0 R
modelActions.createActionTo$message " L8 ?9 U6 @ @ m' \- l( f
(heat, new Selector (heat.getClass (), "updateLattice", false));
' U+ `3 E# p3 A8 d } catch (Exception e) {5 U3 h( Z- j: j3 s2 u/ D
System.err.println("Exception updateLattice: " + e.getMessage ());
0 G! S8 G2 }' X3 {! y, R }7 n, ~4 W0 L: ~4 U
# B7 B; S6 R$ Z) m: @* o // Then we create a schedule that executes the" s0 g4 c, A% d, V4 f( J
// modelActions. modelActions is an ActionGroup, by itself it
# E9 y, |3 M& L7 J // has no notion of time. In order to have it executed in
9 ]) \7 f+ D4 b7 W0 g // time, we create a Schedule that says to use the
" K" K$ ~6 {" S$ r0 @: ?- { // modelActions ActionGroup at particular times. This9 G( @" H0 D; n6 ^( N. o. D1 [
// schedule has a repeat interval of 1, it will loop every l% G& H' Q- y; Z! {# D( L+ L
// time step. The action is executed at time 0 relative to
8 ~. a: p, h) ] // the beginning of the loop., l% H" h/ ~1 |. v3 k
1 ^. f8 a; w7 D/ g% T
// This is a simple schedule, with only one action that is/ p C' \3 H$ B) k9 `) W; P& g
// just repeated every time. See jmousetrap for more
7 R! P1 ?$ p. C% p // complicated schedules.
! ]! S" @3 b6 Z* q6 G$ r* N
$ n; h. \; h, ?5 e+ E3 s modelSchedule = new ScheduleImpl (getZone (), 1);' x) P5 X" o. F6 l( ]
modelSchedule.at$createAction (0, modelActions);' r. Y% P6 v: y+ I
# A! e$ u: G# R, h* B, w
return this;+ R9 Y9 v; o: q7 b" o0 S) R% h
} |