HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; O5 Q9 f1 i# N: ]; X: ~" o2 i/ b; C/ W% m2 p
public Object buildActions () {
2 r" r* i7 }/ x& L super.buildActions();( V8 O6 {/ {7 A
6 r& d8 }5 [! v
// Create the list of simulation actions. We put these in( n3 Z1 x; m& r9 v1 T
// an action group, because we want these actions to be* {7 V, a# ~0 X# q4 r
// executed in a specific order, but these steps should
, C5 ^0 ^9 X+ w0 S // take no (simulated) time. The M(foo) means "The message
- J( Y* t% T. R% i( e8 S; x // called <foo>". You can send a message To a particular4 {- x3 V) u$ a: H: |
// object, or ForEach object in a collection.0 S; N9 g4 o3 f9 [8 Q& c
2 t* i% l( T' a$ e) |% N
// Note we update the heatspace in two phases: first run
3 ^, d& x) G2 y // diffusion, then run "updateWorld" to actually enact the, {# R5 E. a" r7 J/ b* u+ L
// changes the heatbugs have made. The ordering here is/ I0 ?8 A! }7 h& A9 [1 O' Z
// significant!
# s8 P! I3 m: |7 ^, c' ^" h1 E+ u9 } 3 D$ u6 [5 T" T2 N H- l( \
// Note also, that with the additional
% Z$ k! @* s7 u% R2 f // `randomizeHeatbugUpdateOrder' Boolean flag we can
( w$ t) m, i" N2 C // randomize the order in which the bugs actually run
R: n# n+ M5 U // their step rule. This has the effect of removing any. t( S0 `1 h. [) [
// systematic bias in the iteration throught the heatbug! _; |) o2 E1 m& R
// list from timestep to timestep
& [5 P5 H( {' i8 s/ c - R7 b8 ?8 ^% F! G- O
// By default, all `createActionForEach' modelActions have
; M8 F# ]+ h, h( X0 ^4 x$ K! T // a default order of `Sequential', which means that the" m9 T* c( }& p) q. W9 W
// order of iteration through the `heatbugList' will be
/ \( f8 u! R2 R7 l/ m4 { // identical (assuming the list order is not changed: R; m9 O/ l( }9 s u
// indirectly by some other process).! I1 c* o3 m& l& Y7 y
; v" L, ~+ ^) n
modelActions = new ActionGroupImpl (getZone ());+ K. S+ I, r# g+ z7 j
8 P9 I' B A1 ]; I try { ]" U# ] ?& P6 c5 O, r! C! N2 \
modelActions.createActionTo$message
% N! a3 E3 a& _- H (heat, new Selector (heat.getClass (), "stepRule", false));
/ K6 Z* M% d; n% }7 y9 {2 W } catch (Exception e) {
8 O, i0 M9 E2 G System.err.println ("Exception stepRule: " + e.getMessage ());
! y: A" K* v1 v5 c1 r5 C. Q) d8 I }* v/ m4 f& v* M* L: e
' i; C8 H+ }. @8 R8 f! t- ?: f6 v try {: q; @$ p% ^6 P9 \
Heatbug proto = (Heatbug) heatbugList.get (0);4 B; Y7 | O# s. r
Selector sel = * N5 l' `, J; S, P8 U- {8 j" u9 ]! i
new Selector (proto.getClass (), "heatbugStep", false);9 t8 l, |- s6 D8 L3 b+ h |
actionForEach =! r8 a- `3 t! {
modelActions.createFActionForEachHomogeneous$call" m. G/ W' z+ e/ L: L
(heatbugList," U( R; ]) s& U3 f5 a1 E4 z
new FCallImpl (this, proto, sel,
" A( }8 A# B$ N; U, r5 f new FArgumentsImpl (this, sel)));/ ~- M' ]" g6 I* _6 o$ o% O. P
} catch (Exception e) {
% B: U H$ W& K V# ]) Z" E e.printStackTrace (System.err);
9 u4 O" a. @# z8 f }
5 A$ g4 K4 k: _; o
5 m7 x+ y- _) @+ { ? syncUpdateOrder ();
& a! ]( o- g7 k8 g5 D2 n3 p
5 M. Q5 ~5 d: A0 o/ N: n& t try {
0 M+ |- g! M* x7 A$ i$ m+ E2 O4 C& i modelActions.createActionTo$message 3 a, o8 r7 ~) {+ Y
(heat, new Selector (heat.getClass (), "updateLattice", false));4 q/ A. n4 b% `0 x: H
} catch (Exception e) {
2 o7 M8 O7 a) I8 H System.err.println("Exception updateLattice: " + e.getMessage ());# u' g$ `" r2 @. W. O
}
" K6 V" t5 F6 [0 W & w9 s9 p$ i8 O; E$ Y$ s7 e
// Then we create a schedule that executes the
* L4 L' |# H/ A* H( v: v // modelActions. modelActions is an ActionGroup, by itself it
- n5 o5 [0 ]+ s" N! r // has no notion of time. In order to have it executed in
$ V+ _5 L* G6 K) `4 D // time, we create a Schedule that says to use the
. a: P1 w8 f8 a! {# X7 Q // modelActions ActionGroup at particular times. This
- v8 q# q" B4 J: l; | // schedule has a repeat interval of 1, it will loop every
1 O8 s' q" J/ K$ M0 ^; x // time step. The action is executed at time 0 relative to
% B% R8 ?) o4 n! K9 K C // the beginning of the loop.
% X' w* k7 p" T8 J0 F% f
5 c+ e' l N# O: s: T$ T2 P) W3 i: ~ // This is a simple schedule, with only one action that is% S3 q7 Z& }) D: T, Q) h
// just repeated every time. See jmousetrap for more
7 [: z" \) s$ c) b% @ // complicated schedules." r! q( Y7 I2 \$ X( ~4 F9 M
8 l1 j% w9 g# v
modelSchedule = new ScheduleImpl (getZone (), 1);- t* ?+ W8 i0 C c5 ?6 Q
modelSchedule.at$createAction (0, modelActions);
% }# H0 Y+ I. S2 ?$ V, Y, w * i: C" G+ R7 b, h
return this;
B- G5 `* I6 F3 c! i; b. W } |