HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- v5 t+ q9 ?9 D+ Z0 b1 F/ }. z7 H
1 y: w% E3 D5 R; n public Object buildActions () {
3 p1 }+ D. |! l% g/ z+ Y3 i super.buildActions();
. u* ]$ c' L. U! n - n+ ~, \# c# ^8 a8 H1 n- {% d. w
// Create the list of simulation actions. We put these in, C8 a8 g+ u' ^+ F4 d
// an action group, because we want these actions to be
$ X& z. N/ r$ e0 R // executed in a specific order, but these steps should
* f. A4 N. a: d% ? // take no (simulated) time. The M(foo) means "The message
/ a1 K: u6 E. R# u. @9 f+ c, U // called <foo>". You can send a message To a particular: j1 ?; \2 m9 B+ r4 o; |
// object, or ForEach object in a collection.$ F8 H7 h8 I8 j; s5 f
' g# H" {5 K! m
// Note we update the heatspace in two phases: first run0 e' ?7 f' V) y1 p
// diffusion, then run "updateWorld" to actually enact the1 B+ B- N$ \( `" s# n& [0 s% `: q% a
// changes the heatbugs have made. The ordering here is
" C; v! M0 {7 m/ Z$ `" Z) W- e // significant!5 b. c9 C! s9 p) A
5 |$ o7 f9 H% ~; `5 D4 H0 I. u // Note also, that with the additional# R9 A" W4 E, {6 G9 S
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ _4 ^& z, j C) N: o
// randomize the order in which the bugs actually run
: ~, d, S& O; f // their step rule. This has the effect of removing any
) z9 n, K* i* a* d ~' | // systematic bias in the iteration throught the heatbug1 _) T! @# a J8 V
// list from timestep to timestep
5 A* F# D* t" N$ h9 B% i6 G1 `% O
& N. g+ o K$ G9 Q6 X. ~2 |* P // By default, all `createActionForEach' modelActions have
* ?- { E8 s; G // a default order of `Sequential', which means that the
1 m# P; T! f) a# F e, m. | // order of iteration through the `heatbugList' will be) ^. D/ f8 J8 k2 u0 \5 I. \
// identical (assuming the list order is not changed- y9 I. Q% P7 C" J/ g: H; c* B
// indirectly by some other process).
) G. T2 m& j$ O L/ W6 F
0 z- |- ~1 f3 Y1 W3 s modelActions = new ActionGroupImpl (getZone ()); h* f O- \* N
* i* z8 _' X% x( r; T% o
try {* t- Q" _% s; ?
modelActions.createActionTo$message
' P5 W1 a! U$ u6 A4 b3 f' e5 I (heat, new Selector (heat.getClass (), "stepRule", false));5 X8 A0 i4 p! _. y- C2 o2 j8 c" a
} catch (Exception e) {
. l4 f, C$ k% F: b' ?% f6 |: r- Q System.err.println ("Exception stepRule: " + e.getMessage ());# J) E/ t/ C2 i: ]: H/ H
}- {8 T* \. b; _7 n/ M4 e
0 x& z8 m8 s* g* ?. @- l+ t try {
7 V" P# E" ~- W8 C Heatbug proto = (Heatbug) heatbugList.get (0);
, e: a' j+ d1 F Selector sel =
" e9 G7 q5 m* |" W8 | new Selector (proto.getClass (), "heatbugStep", false);
9 o) n% `# o1 E* T. J actionForEach =& K$ |/ e2 b8 Q
modelActions.createFActionForEachHomogeneous$call
7 _" T: x. P* N( J, |; ~ (heatbugList,. D+ x) s, x( f6 j- O
new FCallImpl (this, proto, sel,
) ]7 }0 p5 B) ]% `8 q7 \4 Z new FArgumentsImpl (this, sel)));
7 [# c2 Q. [3 K2 I Q% u& o b9 W } catch (Exception e) {) f5 g7 {& p9 [# w
e.printStackTrace (System.err);. d% l; K$ B! C
}, {0 l) e& f7 y/ V0 O
9 l& [# ~1 I. c syncUpdateOrder ();
% p& V4 {- Z9 ^/ H/ A2 @- w1 n3 s
try {
) b u8 }# D+ U& ] modelActions.createActionTo$message : w7 n$ w) J- U3 g8 c# v* d C
(heat, new Selector (heat.getClass (), "updateLattice", false));' n) f$ t/ p {8 y$ I: r- {' ]
} catch (Exception e) {6 C2 p& ?$ R% i
System.err.println("Exception updateLattice: " + e.getMessage ());
: J# y" l2 _, N9 v0 a: Q: \3 M$ u }
- m( \' h0 m: H; I# S% L
8 u% R' |8 g) d // Then we create a schedule that executes the t. e8 x( H8 E
// modelActions. modelActions is an ActionGroup, by itself it. ~# |: z1 r9 m$ i3 f/ a
// has no notion of time. In order to have it executed in
# u$ k5 h7 b3 p3 Z, M) z- ^% C% ` // time, we create a Schedule that says to use the
( A) V7 v. @' V, Y2 @0 V // modelActions ActionGroup at particular times. This
1 {" V3 x h, R- K+ L! Q. V0 @ // schedule has a repeat interval of 1, it will loop every
6 W. r; @) t' Z# [* [) U // time step. The action is executed at time 0 relative to
& d4 G, E! u# B1 m* z3 Q // the beginning of the loop.
1 W: k6 I! z1 G* s' `* Z
" y4 ]( E8 `5 `, }& v: f6 E/ D // This is a simple schedule, with only one action that is
# u$ l' }- w# Q8 I4 p // just repeated every time. See jmousetrap for more
. G) B: H: P+ X% _: @ \* l // complicated schedules.( V. h: g8 y) v! Q3 W0 o' W$ R, {, I
% ?# @ ^* g5 t# u3 w! G modelSchedule = new ScheduleImpl (getZone (), 1);+ s% |4 m Z; F C' w1 b
modelSchedule.at$createAction (0, modelActions);
+ S1 u5 ~+ y% B- y4 _6 L; o8 V
* i: J# K" @0 u return this;3 U- ^4 S- R% }
} |