HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* Z# S6 F' ^# {( H) s
O! U! w( v8 ]3 F5 k+ A9 D public Object buildActions () {
* s0 m" o) n) |$ | l) x6 A0 |! U8 j super.buildActions();! i) ]: z# w" h1 j0 j, r
5 P4 Q' P' i' ]; N1 e, o/ V& y // Create the list of simulation actions. We put these in
, A$ e; o/ n& j( ]9 P5 j4 N3 ? // an action group, because we want these actions to be% d& p2 s9 J C3 S) Y2 V4 ^
// executed in a specific order, but these steps should4 [9 U' t* W0 F" W6 b
// take no (simulated) time. The M(foo) means "The message
7 }% u- {) I/ P3 B6 d# x // called <foo>". You can send a message To a particular
+ |, ^- S- i! g2 n% f // object, or ForEach object in a collection.. h6 I8 P) S7 u, P8 k
$ p$ N2 y: n: _7 O7 g* L' A) O
// Note we update the heatspace in two phases: first run8 v# K' L H! p& _
// diffusion, then run "updateWorld" to actually enact the
. o0 x, d; P; \; M3 E1 [7 I- I // changes the heatbugs have made. The ordering here is h- y G& W1 ^1 X6 @
// significant!
+ J) U" F) l0 z4 u# { " K1 {5 F" J: s {( k/ P1 w! G. C
// Note also, that with the additional. ?" S) W& F6 K: \3 X+ |& k
// `randomizeHeatbugUpdateOrder' Boolean flag we can( }/ K, b) D, j8 ^2 O4 h+ x1 _
// randomize the order in which the bugs actually run
- z/ Y* e7 v( D4 C* M // their step rule. This has the effect of removing any9 |" e8 E$ q+ M: c. B& [
// systematic bias in the iteration throught the heatbug5 ~$ ^; x \; L. ]
// list from timestep to timestep& q+ M1 B" ^7 _3 t
' U* M/ m3 ~2 [0 X5 @; A* q# T3 ^
// By default, all `createActionForEach' modelActions have9 I9 t1 @4 c: U1 g8 {
// a default order of `Sequential', which means that the
' _* j# g* k; r* r) e9 x // order of iteration through the `heatbugList' will be5 G$ T& A" q% N) f( _) ^, T
// identical (assuming the list order is not changed* X l% h. c! e! ?: T' U0 Y
// indirectly by some other process).
4 |) E$ K0 z- U, z) J
8 M1 S% s* D/ w3 s modelActions = new ActionGroupImpl (getZone ());
: f/ A5 I9 b# j7 P: z0 F: _: i" }* _! K- m1 C A: v
try {
" i6 Z2 w1 [- [' i- @: h* F% C modelActions.createActionTo$message
/ N/ B* T7 L1 f" \. ?' k2 n/ M (heat, new Selector (heat.getClass (), "stepRule", false));
- Y9 V# D$ ~, Y; V } catch (Exception e) {2 H: { u- D& c% V2 y, \0 _
System.err.println ("Exception stepRule: " + e.getMessage ());" l9 I0 a$ W' r+ U. V7 j+ w3 B& d
}
, J* C: s% d8 K; l! E( ^
S4 G" \; {# h try {
6 k7 N# U' k. T8 B Heatbug proto = (Heatbug) heatbugList.get (0);- q/ v3 Q2 C0 u
Selector sel = & x+ b$ q- e1 p1 L4 g$ Q
new Selector (proto.getClass (), "heatbugStep", false);$ x6 W- A9 S0 _5 E7 {
actionForEach =
* Y- }9 i a0 G! ], |. z modelActions.createFActionForEachHomogeneous$call$ D6 p* a' d' p: A4 b8 A# l: P
(heatbugList,( z0 K* Y" x& w3 } A- I9 q
new FCallImpl (this, proto, sel,
$ w* a7 e3 C; N" v new FArgumentsImpl (this, sel)));+ Z+ n+ Q8 y+ _1 h
} catch (Exception e) {; G0 L1 ?% a; w
e.printStackTrace (System.err);
. R) a% i8 t1 T ~2 c$ B }& P. J1 `! w* ?( \- `" G& X
5 Q }* `& w- P3 }% H. {
syncUpdateOrder ();
' I/ I0 i7 p2 K9 @, m
d* \& F7 Y* p try {# s2 m6 v9 \- C Z1 a ?: [2 V7 n
modelActions.createActionTo$message 3 {6 J J6 _# w+ M" g" s
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 v$ T' W9 \( p } catch (Exception e) {
a6 j% z& r. G' z$ j System.err.println("Exception updateLattice: " + e.getMessage ());7 ?3 f# L+ Y8 u. ?# ] n
}' S$ U8 q, i+ ?8 ~! L% e
1 B: A N) k9 o // Then we create a schedule that executes the
6 [7 E6 a2 t# a6 }2 u5 d0 p // modelActions. modelActions is an ActionGroup, by itself it
1 y7 m, e9 n1 L: A7 J. i: Y // has no notion of time. In order to have it executed in- ~9 n9 [" ~) _6 ?
// time, we create a Schedule that says to use the
. @# h# @! L4 e( t* O3 m // modelActions ActionGroup at particular times. This
. N7 E1 Y+ q5 D0 q( D) g. u' I // schedule has a repeat interval of 1, it will loop every% H2 S2 C z! A2 m$ c2 k
// time step. The action is executed at time 0 relative to
: i9 `6 z6 q, q$ D" z8 ~; P // the beginning of the loop.; j$ X; i/ v6 j" G! ]
: R: `/ i. @; s% x I( E0 G. `0 @
// This is a simple schedule, with only one action that is3 x3 _: U6 E. z* V0 v
// just repeated every time. See jmousetrap for more- T. m3 d/ ~+ N& W7 o
// complicated schedules.
. |* I8 I7 W7 U ) S: @) F+ B. a8 S0 F+ `4 k
modelSchedule = new ScheduleImpl (getZone (), 1);9 r) H; w2 b( G
modelSchedule.at$createAction (0, modelActions);
3 ^6 s( i0 U+ O. V* _- _' U3 D " f6 p3 l8 m! L( `$ N W+ n
return this;, W5 ]& j6 x$ F+ ^7 q3 y' v7 A
} |