HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 [$ a. W, J: I/ R# D$ H
/ C5 y6 u3 x. | public Object buildActions () {3 z& J, v8 J) L! \. }9 }: z
super.buildActions();
* K1 \/ \: z9 ?' l 5 k$ ?( A. g8 n& |4 Z! l
// Create the list of simulation actions. We put these in& F7 |! a6 A8 X
// an action group, because we want these actions to be- g! o; t6 U) j. W+ G
// executed in a specific order, but these steps should" b& V& g" r4 X* O; e# ]
// take no (simulated) time. The M(foo) means "The message
/ ]6 A8 [- `% p! `! `1 |. x& U+ A- O // called <foo>". You can send a message To a particular
* s2 t. ]; \- r* g; |+ d8 E // object, or ForEach object in a collection.3 ^' k& ?! s' R1 ?0 i) k
T5 z1 E( G: s2 I
// Note we update the heatspace in two phases: first run
5 F" D H6 a7 ]0 c // diffusion, then run "updateWorld" to actually enact the2 B* |2 z- K9 \. ^0 m" G
// changes the heatbugs have made. The ordering here is! Y& V) Q5 b( C7 U& h& @8 X; W
// significant!
* H& b( D# p$ {3 m% M 6 p, E9 J* l# `; H( |" j
// Note also, that with the additional
- p) E- U. H5 f, F // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 h& ?) ^( @9 B4 E0 X& `9 x // randomize the order in which the bugs actually run
, k" e% w d( X; z% n8 K" X // their step rule. This has the effect of removing any% ]. u3 H+ ?/ ` _. K! g* S4 L
// systematic bias in the iteration throught the heatbug. X% o" @! c! z7 m* D
// list from timestep to timestep
: e: C- s' e( y. c; ~7 S/ @
9 t5 |: s0 Z- i2 y$ u; O; C; E // By default, all `createActionForEach' modelActions have0 m+ c7 g3 ^ {0 D J! Q7 x0 L2 v
// a default order of `Sequential', which means that the
0 e6 i& s+ W; m$ k* M: f // order of iteration through the `heatbugList' will be2 R, w3 P9 l* r) ~+ X- \
// identical (assuming the list order is not changed5 n/ k, Z" j! v2 E. p D2 G
// indirectly by some other process).
; G; L( X& F/ q2 x# R
, K/ j& |: W* U n/ v modelActions = new ActionGroupImpl (getZone ());
& K* N# O P& [8 s# q
# S5 [/ p8 M! G! u9 p* m7 U4 D try {! Z6 F* K# P8 N) d$ u& g
modelActions.createActionTo$message' |/ @3 ?6 Q2 X! n1 N! r
(heat, new Selector (heat.getClass (), "stepRule", false));- C" ]2 ?- x$ b5 t8 K, q
} catch (Exception e) {" v2 H) n+ J: q3 \9 I% [2 M
System.err.println ("Exception stepRule: " + e.getMessage ());
) g6 P# [+ o" n0 M4 Y }
& c7 E( V$ o: [& g! {* r- p) w0 ^! h' h; H$ T
try {
8 p5 @ M/ K6 f! j' o Heatbug proto = (Heatbug) heatbugList.get (0);& v; {6 w. N$ W: A3 @' ]: T
Selector sel = $ t3 M$ a3 Z) z2 Q$ j
new Selector (proto.getClass (), "heatbugStep", false);1 M4 J1 z' J4 q f8 Y# Z- r! e
actionForEach =
4 d+ Q8 v4 e- ]' C4 D S ]0 U modelActions.createFActionForEachHomogeneous$call/ \( s8 v" W! j$ f5 N* A/ K
(heatbugList,
9 \7 E; r- H* U" {; s7 U new FCallImpl (this, proto, sel,
x6 p3 S' w. m6 J: i2 R new FArgumentsImpl (this, sel)));
5 n+ p2 N! P& C* c1 ? } catch (Exception e) {# K/ s" Q6 O" k0 X0 d7 j6 a
e.printStackTrace (System.err);
7 D% r+ w1 b- E) G9 O }- x6 q L% @0 i
2 x. }5 n% z; I# o. z5 |! i
syncUpdateOrder ();7 H, N/ L: M; L- d7 z
3 M8 O. q t6 W% B+ d7 T
try {: M8 [) f. t9 n1 N$ o. P5 O
modelActions.createActionTo$message - H, q0 Z7 f" H# K1 a' W/ m% e7 I( k
(heat, new Selector (heat.getClass (), "updateLattice", false));, v& n4 s# ]& R
} catch (Exception e) {1 y2 j8 Z) Q9 w+ h: L2 @$ r! i
System.err.println("Exception updateLattice: " + e.getMessage ());" Y6 L( W) u/ D: t! T: J1 Z, k, T
}
& E/ r- c6 y7 |
3 S B: T9 F/ I q* d( c // Then we create a schedule that executes the5 Y- f$ ]8 G6 H& p+ l9 T
// modelActions. modelActions is an ActionGroup, by itself it
" J% c" N" v' ~4 c* J L. _1 L // has no notion of time. In order to have it executed in
) o7 S! h3 ^ | // time, we create a Schedule that says to use the
0 u% ?0 C/ A: U/ Q // modelActions ActionGroup at particular times. This
; W7 I5 K" H1 \4 q // schedule has a repeat interval of 1, it will loop every4 w5 A- W; K! R: k0 N+ w* w
// time step. The action is executed at time 0 relative to: C! ^8 L# F4 f7 i: |; h) t
// the beginning of the loop., N" T4 |5 y3 L8 g( ~
( @9 g3 d, ?: R( `! J // This is a simple schedule, with only one action that is
7 E; m' q7 y$ r% P' p" d // just repeated every time. See jmousetrap for more
7 l3 K8 X! G- a0 s4 C4 p' ` // complicated schedules.
% e- `# W! B/ a" V# Y* h
" G" g4 t0 v0 }0 D modelSchedule = new ScheduleImpl (getZone (), 1);0 c$ U" L0 ~6 G+ O' H
modelSchedule.at$createAction (0, modelActions);7 N+ e9 O6 G0 E: t" |9 E
0 ?% M9 l# L4 U9 A4 m! [" _" J
return this;% e4 L6 E$ @/ y
} |