HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, u. T9 [/ Z. b3 R
5 v( w/ x: S P" e4 w6 d7 a; [9 g public Object buildActions () {- C9 L7 E# l; N8 O2 t! B" _
super.buildActions();! ]9 a6 v3 }. o( x% o- Z8 j8 b
8 T0 K7 T; H5 j4 F // Create the list of simulation actions. We put these in8 d& E9 G& n/ y+ i# C
// an action group, because we want these actions to be
8 w, Z: ?/ Z4 I% @3 ~ // executed in a specific order, but these steps should* v5 S8 |! o4 b5 ]+ B
// take no (simulated) time. The M(foo) means "The message( N! x& ]7 n1 }
// called <foo>". You can send a message To a particular6 Q! k4 r' N2 U0 D
// object, or ForEach object in a collection." D. `" ]1 F; g5 H' i8 [
/ d/ C) P/ n( P z0 W! r9 `$ I // Note we update the heatspace in two phases: first run a2 d. I5 O' a
// diffusion, then run "updateWorld" to actually enact the7 @ i& @) w' z' _+ |. p6 s: k
// changes the heatbugs have made. The ordering here is& n: s$ g2 v. r. B: S3 d
// significant!2 \. c/ B* c6 H) I
) a9 V' T( b2 T2 C* e // Note also, that with the additional- x( f4 v+ }% E/ q! p
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 P6 \( @* `4 R# Q" @5 D0 N
// randomize the order in which the bugs actually run
4 J4 g% F5 t# Y7 g // their step rule. This has the effect of removing any( g, k) x! Z" @6 J. v$ R; L# Z
// systematic bias in the iteration throught the heatbug# b7 U: ]7 x& [, p i- D2 S
// list from timestep to timestep% h B6 d5 t) M8 P$ E
- |; {2 n4 W5 Y! M& n6 I/ o // By default, all `createActionForEach' modelActions have
4 v) J& H9 l+ t y // a default order of `Sequential', which means that the9 v2 H0 s( }2 O
// order of iteration through the `heatbugList' will be7 Z$ p( w6 ]! U- e2 R9 h$ r
// identical (assuming the list order is not changed
! b8 P7 l- `$ w // indirectly by some other process).
, h3 Y$ f, d7 |) e7 B" P : c. }! Q- z! ?! q; O- j% K$ V
modelActions = new ActionGroupImpl (getZone ());
9 o- C' J; W. ?% l d
( b, X2 r( P. _/ c3 G try {- Y; M2 \) o2 I3 A6 v
modelActions.createActionTo$message. e9 D! G& X' s; ]2 h2 H: Q0 {! {
(heat, new Selector (heat.getClass (), "stepRule", false));
* B/ Z* u* p) r1 e* K4 b+ x( l } catch (Exception e) {
- |3 W0 I6 M) @! V% T x System.err.println ("Exception stepRule: " + e.getMessage ());2 H9 ?! F2 Y# B# P8 M
}
9 t2 e* ~$ J, v; G
# _- [! J. O$ M+ n) [, N try {) |) S" ^/ ?, v/ o3 n: W
Heatbug proto = (Heatbug) heatbugList.get (0);+ g" z5 j$ ^; h' `! {
Selector sel = : \+ e* j1 l( K
new Selector (proto.getClass (), "heatbugStep", false);
( M- X7 p% t8 M' w actionForEach =
% H0 `1 a; N* n4 @6 F) `8 ] modelActions.createFActionForEachHomogeneous$call5 H+ { S" f2 i% B- x
(heatbugList,; D' g" g% u% ] ]# e6 A
new FCallImpl (this, proto, sel,; H* s3 z& n; _0 n
new FArgumentsImpl (this, sel)));1 \# O/ f u4 z( z2 h, x# _
} catch (Exception e) { ^' k0 s- D& K, |
e.printStackTrace (System.err);
* |6 S \; n2 v* m }+ N9 t$ z; P x# E! T5 R( u+ B+ d
3 A! `* G4 }. X! o, Q7 S3 w* u# m syncUpdateOrder ();8 K! ~6 r: m) j+ v# y% O
! ~- K& h+ T& j2 I( w4 Z8 M! j3 }
try {
6 J# J' @9 |, C0 E, D! Z3 C' N. D- J modelActions.createActionTo$message b: f7 q& T5 A9 [: P" _, `
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ l- v: j" X% h } catch (Exception e) {
; m2 w. J; I) Y* I' S8 n System.err.println("Exception updateLattice: " + e.getMessage ());
8 g7 b$ C8 f/ ?: z7 f$ z }
* G7 z- _# w% K* n* ~* n4 z
3 L8 C# u* |9 n# A- G, C4 a( n- ^ // Then we create a schedule that executes the8 \2 t( h4 ^" L; C" Y9 \* V
// modelActions. modelActions is an ActionGroup, by itself it5 T/ _+ F8 B1 [9 M6 e
// has no notion of time. In order to have it executed in
$ T1 f4 t9 I' h* R // time, we create a Schedule that says to use the) h1 d( n3 g( I" v0 }, j8 \
// modelActions ActionGroup at particular times. This
+ h; z/ g" X& p2 P, U+ k // schedule has a repeat interval of 1, it will loop every
6 \* _9 B: ?) z // time step. The action is executed at time 0 relative to# \ X: W( W. J- W
// the beginning of the loop.! E9 c9 X( u- Y/ k) {
- l% s4 j8 M# e. f1 ]+ _% V4 i: _" j // This is a simple schedule, with only one action that is1 c5 i- v5 ^ w
// just repeated every time. See jmousetrap for more7 y( W' x6 ^7 T8 E0 N0 ^) M4 V
// complicated schedules.5 N9 A& |+ d$ K7 r# a8 C4 Z
, d) s7 @% K) n0 a; U2 d modelSchedule = new ScheduleImpl (getZone (), 1);
, ], K& G! E4 C: j! [8 ]( | modelSchedule.at$createAction (0, modelActions);
& J. {* S. Y- M, w+ F1 x / U: l$ E4 f# ?; A# O
return this;. {) L# O% y! k; h, L* ^
} |