HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* M* U) \" k0 \9 v) `7 c
7 T6 [ U0 F9 H8 t public Object buildActions () {) r' S$ ^0 R- d2 Z& `9 d% Y& {
super.buildActions();
. ?/ z/ v/ w; a. A6 r# o
! R+ U2 a# L2 }3 {3 h: U // Create the list of simulation actions. We put these in
A1 u1 {7 b9 ^0 g // an action group, because we want these actions to be g X S9 h8 M7 D
// executed in a specific order, but these steps should' n. Y5 F9 }9 D& B0 t- V
// take no (simulated) time. The M(foo) means "The message
; h! K3 |; g# t3 U* c! @ // called <foo>". You can send a message To a particular8 A* Q" y: t6 m, x% B
// object, or ForEach object in a collection.
$ {3 I; T: ^- O7 k% S$ \
" s& `) n# J/ \+ }% m, k // Note we update the heatspace in two phases: first run. Z5 |& t- }& T9 Z! R
// diffusion, then run "updateWorld" to actually enact the3 l, L$ u% j; t& P9 g
// changes the heatbugs have made. The ordering here is2 K' E! p1 T5 X* j
// significant!
7 K, G1 K6 [; |3 _7 [
4 s4 G! l1 g, P2 G! S8 ` // Note also, that with the additional
0 T ]- A7 |. F1 r \0 J // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 U! s5 b# L" u. N' U7 V' W // randomize the order in which the bugs actually run+ i$ y# p* b* ]. I- W/ ]1 v
// their step rule. This has the effect of removing any
6 J* F4 y4 Q# H // systematic bias in the iteration throught the heatbug; G' w! Z( H- X6 B3 {
// list from timestep to timestep; I$ r/ L& l) V+ f3 B
% J- @) B/ M! V8 C3 Z // By default, all `createActionForEach' modelActions have
/ E+ O( K" C0 [. E. g( `! N // a default order of `Sequential', which means that the
, J |9 s) K7 y' T: {) Y* }! H& i // order of iteration through the `heatbugList' will be+ A" _5 p# n9 }9 L: t t6 j- n
// identical (assuming the list order is not changed8 w, E5 c' |$ J' D- _9 @8 F$ [
// indirectly by some other process).$ p# t& n1 s: I0 _
3 c8 x. U/ W- O. Z2 E( e8 Y
modelActions = new ActionGroupImpl (getZone ());
q- i8 W: i; p/ X+ B4 }/ ?4 G: y1 T7 C
try {
, B0 s- q2 E6 Q modelActions.createActionTo$message
& U; j- \1 a) r8 }( a$ f3 ]9 ~ (heat, new Selector (heat.getClass (), "stepRule", false));# ]) U2 D3 }9 L0 b5 Q
} catch (Exception e) {- |, l' ~! y8 e
System.err.println ("Exception stepRule: " + e.getMessage ());
. f9 q Z3 |# K6 _% }' \ }
) g' d: H7 h+ M. Z% N) g2 }* M$ }5 E+ Z
try {+ f& H3 u1 {7 S x
Heatbug proto = (Heatbug) heatbugList.get (0);/ E5 m: k" }5 w% v4 B
Selector sel =
" S6 I5 Q2 t9 k' B; c ? new Selector (proto.getClass (), "heatbugStep", false);; v0 t/ p9 p% k5 f( p
actionForEach =; B6 i* Q; z! o7 D
modelActions.createFActionForEachHomogeneous$call
6 M: k; n' N6 @- r9 Z (heatbugList,/ H S2 q- G" Z
new FCallImpl (this, proto, sel,+ ~3 k1 z- a$ y$ f
new FArgumentsImpl (this, sel)));% `9 |, J8 {+ \) B; I) a4 F
} catch (Exception e) {- [" |! x" H0 A) q, t$ X, [
e.printStackTrace (System.err);% g; I5 ~0 I, \( v" @7 l) H
}4 @1 }4 Q2 J3 r+ I5 q4 Y( g
$ a- f( T1 n1 D# g; a6 Z6 o5 N
syncUpdateOrder ();
6 \- P3 D; Y1 }5 z/ K
3 `7 j) h- B) J& f6 p# t try {! [2 W" W [" |" c. o. g
modelActions.createActionTo$message " s( b+ i$ Y& b4 B7 h" r1 E) g+ J
(heat, new Selector (heat.getClass (), "updateLattice", false));
! ^, g4 J) ^% Y1 l& i; J1 ` } catch (Exception e) {
$ M, X$ M e- q, T; q System.err.println("Exception updateLattice: " + e.getMessage ());/ c# P H \8 R8 [( s+ t! }5 C
}
o- h, L0 o$ D5 V, v3 {
, ~" E! V' A1 y' o6 D4 ` // Then we create a schedule that executes the: \% w9 r, }9 |4 f! \
// modelActions. modelActions is an ActionGroup, by itself it% S% v/ d# y9 ~' I6 d" E
// has no notion of time. In order to have it executed in
7 M$ g/ A. Q1 h/ ?, {) s | // time, we create a Schedule that says to use the6 e$ `: j# T; t7 b3 h2 R: E( C# g
// modelActions ActionGroup at particular times. This
: X# F) D; b a, s // schedule has a repeat interval of 1, it will loop every
9 `6 s5 U: g, I# g9 `- e // time step. The action is executed at time 0 relative to
# ~" f5 V0 U* k* V$ @2 u; E: G% j! ] // the beginning of the loop.2 R& B: t% ?: u
" X" F# ^5 j. Z3 L
// This is a simple schedule, with only one action that is
1 E% L6 I# Q1 {! W6 q+ l, H) { // just repeated every time. See jmousetrap for more8 Q% Q: c x2 l$ w7 o
// complicated schedules.
) p! _5 n% |' v- {4 z$ v ; d% j4 a5 o( S, Q2 D9 f2 y
modelSchedule = new ScheduleImpl (getZone (), 1);. V. y% L: |& `- G
modelSchedule.at$createAction (0, modelActions);
8 \( l) J1 s% y" [+ l* m0 O/ u- S
* ?" C. T9 P# _ return this;) L( w' n* ^& G/ x, t( a% D
} |