HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- m3 K! B+ P# Z1 x, B9 [
& v8 j+ Y( V+ ~8 M# g$ d" G) E
public Object buildActions () {+ j- ^7 O& R, h& h5 Y7 t2 U! E, V. \
super.buildActions();
, u* O" x& y) v: v9 x5 ~# X
, |7 ^# x* w- z5 \4 J // Create the list of simulation actions. We put these in
8 i- a7 L% |$ T- D" g // an action group, because we want these actions to be: F: ]6 ~' ^! p& k
// executed in a specific order, but these steps should3 _6 Y9 r1 W: }4 x, P6 k: X8 y
// take no (simulated) time. The M(foo) means "The message- J" z0 C5 K: {9 f$ [
// called <foo>". You can send a message To a particular8 `8 w P' y/ e
// object, or ForEach object in a collection.
/ R: q3 { R p# `& m6 Q f ' a- J# E/ @" }4 c8 _+ | f
// Note we update the heatspace in two phases: first run$ t0 N! t% r0 q% z# B% z
// diffusion, then run "updateWorld" to actually enact the
' z/ h- y" r0 | // changes the heatbugs have made. The ordering here is9 E1 m5 S' z& g" F
// significant!
5 v3 C9 b5 [" c0 ~( M/ l$ e
" |- u' w2 @. j7 x, X // Note also, that with the additional8 S7 c& f2 I! v, }# l/ A1 T, b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 U/ T* j: S3 T% F; R // randomize the order in which the bugs actually run* v( v! O3 w2 ~: M/ B# B, j
// their step rule. This has the effect of removing any6 T8 `0 Y) T: O8 V. _* p1 K+ b
// systematic bias in the iteration throught the heatbug
3 r) B3 A# R. i0 M0 u& N: H( Q7 I // list from timestep to timestep
7 ]! F" N7 j3 q; b; d& \) Z Z , _* Y) _/ \) {# K" A5 }& X- `
// By default, all `createActionForEach' modelActions have6 X* a0 Y B! {$ ]5 @$ A, ~
// a default order of `Sequential', which means that the$ g; V- @8 T& W, M! q6 j
// order of iteration through the `heatbugList' will be
( \' u/ c( g) e& { // identical (assuming the list order is not changed5 g; o ^0 T! E' C; ?
// indirectly by some other process)., V! G8 R- b# l H: K
3 G( o+ g6 ?) {( B modelActions = new ActionGroupImpl (getZone ());! J2 G( J+ \) } M
! c- A8 t9 b5 L, h: i6 C0 \
try {2 W5 J3 x% I. G
modelActions.createActionTo$message
% E/ @; D- ?$ Q (heat, new Selector (heat.getClass (), "stepRule", false));' G: H3 D0 j: g! D. N3 w+ D
} catch (Exception e) {
/ y6 k( Y: U. p- _6 K9 s( o System.err.println ("Exception stepRule: " + e.getMessage ());$ Q( u. J! i+ ~
}: Y6 A0 O+ r* I1 W1 a
( I9 s4 e0 Q7 ^' s try {
- _# }' R1 W- c/ O. S7 \ Heatbug proto = (Heatbug) heatbugList.get (0);
; i1 g0 \2 { f) s1 ]5 \' C6 ] Selector sel = 9 W8 c! C3 J L) K9 t/ C4 o& f
new Selector (proto.getClass (), "heatbugStep", false);
6 m; e. B* b# @5 b% w3 @3 c actionForEach =0 \; j4 H+ G# f5 X& K" U
modelActions.createFActionForEachHomogeneous$call
4 W; l( X w: Y# }: {% u! O (heatbugList,+ x% R3 _$ ?+ |) ?+ t+ H5 ^# x( N
new FCallImpl (this, proto, sel,
$ X- Y# d) Y& y9 D9 G) D new FArgumentsImpl (this, sel)));
+ q6 Q/ M, _7 i- D/ ^ } catch (Exception e) {
0 a$ {4 m7 }+ y9 C! x e.printStackTrace (System.err);
' n, {6 J |1 n5 H, W' C# K6 n }4 g8 j7 O8 w, Y5 y# A" b( @: j; R7 N
; |6 p( y7 b r& {. `2 L+ z
syncUpdateOrder ();& I& h% u$ K }/ {# W0 ]' `4 l
; P. I+ ^" b: r Z p+ E. o: H8 U
try {
2 q" G/ q6 m, ^% D) I, `; p modelActions.createActionTo$message + ^: x$ s5 I: ]4 O0 H* J
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 b0 p) s, r# I& { } catch (Exception e) {$ S+ b8 e) _' P+ I9 b
System.err.println("Exception updateLattice: " + e.getMessage ());
7 Z3 m0 q) e) G. q }4 d0 x0 Y6 l7 k, B8 B
& D- _: @; y* F, t2 |. t // Then we create a schedule that executes the; g) |( ^* f0 V; Y! o
// modelActions. modelActions is an ActionGroup, by itself it3 B* n2 f! q2 s* q. J
// has no notion of time. In order to have it executed in
8 }6 A, v5 w3 E/ f // time, we create a Schedule that says to use the" \# m/ a$ r" Q# R
// modelActions ActionGroup at particular times. This
: J. a0 H& Q6 n1 @9 V9 H // schedule has a repeat interval of 1, it will loop every5 N% J+ k8 f8 z. P% c
// time step. The action is executed at time 0 relative to3 B$ }+ U8 l% A" D3 }9 E# x6 o8 z
// the beginning of the loop.: b7 o$ G% |( r0 |
9 a$ [2 a! n. V8 m
// This is a simple schedule, with only one action that is6 I; M! j9 C; r9 k
// just repeated every time. See jmousetrap for more0 B( S2 \" [# X5 |7 d+ B/ Q8 V1 D
// complicated schedules.
' e9 Q6 Z" X% I: x+ a1 E, z6 @
* A$ ^, m7 C# r( e8 G modelSchedule = new ScheduleImpl (getZone (), 1);' |( E6 X2 {5 F& ~! k* I$ o" ?
modelSchedule.at$createAction (0, modelActions); G+ A( d8 `% g, Q2 \% j
% y7 f s8 x# G `) |
return this;# j/ P9 v D8 t
} |