HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( ^' |" n( a6 S
" E* U5 c4 ^% @& Q8 W8 M5 {5 c q. x public Object buildActions () {
; L/ ]( G T+ X$ Y% u super.buildActions();
& r" \. r" f+ y3 P6 x& Q4 f + }! P( g- X2 r# v: O$ |( N! ?
// Create the list of simulation actions. We put these in
7 z; D7 t7 ]* A& ^ // an action group, because we want these actions to be E' ]' P F' F! O
// executed in a specific order, but these steps should' R% A- @% n; a
// take no (simulated) time. The M(foo) means "The message! K4 C, Q& ~% f* |9 K# f+ n
// called <foo>". You can send a message To a particular- j, h2 t4 _! e
// object, or ForEach object in a collection.2 _# ` D' F6 A% x, l6 n+ N5 E) g
+ n# K i# [3 [: D8 B$ { // Note we update the heatspace in two phases: first run
% B' _' s' s3 B! [ // diffusion, then run "updateWorld" to actually enact the8 Q" k3 C) y: w' t- a/ X- W
// changes the heatbugs have made. The ordering here is5 r1 G# C: d" q; i# I* q
// significant!+ |$ [7 ^8 x+ y8 [/ I
. V5 k! o% L) U2 S$ z7 l
// Note also, that with the additional F9 ^* K7 S7 J/ a7 y! y
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 C- [2 a0 E9 {, }) \# J
// randomize the order in which the bugs actually run
+ y8 w) O7 k7 M, c8 g( Y+ U. { // their step rule. This has the effect of removing any
, i1 C: D9 P8 W. T, W6 i' @: r // systematic bias in the iteration throught the heatbug
' T4 s( _7 a; e // list from timestep to timestep$ I$ s% ^! n7 A' V2 u! z; n
6 i5 X; O$ f7 i* z- V; ~ // By default, all `createActionForEach' modelActions have, L2 y. O8 F9 U+ t" U& ~$ k1 j3 X
// a default order of `Sequential', which means that the* n J: G$ M) [9 {2 c
// order of iteration through the `heatbugList' will be
% Y) x( p, C, |; f+ i+ O2 B // identical (assuming the list order is not changed
% J; }. K$ C5 t8 `5 o e // indirectly by some other process).6 ]0 D$ f. V) a( W" j+ f, o6 b& U
$ D, G4 g/ `9 s9 k% k$ c4 k
modelActions = new ActionGroupImpl (getZone ());
- n, U) E! M9 G4 T+ I( ~( C) M5 V8 d
try {* I4 ^- ~/ u) p- Z4 N" {; x3 k
modelActions.createActionTo$message0 A9 K# c1 s: `7 `( y1 r3 _
(heat, new Selector (heat.getClass (), "stepRule", false));
4 ]0 {0 r7 E: c# G0 R+ i } catch (Exception e) {8 H1 t' n% d. [* y O
System.err.println ("Exception stepRule: " + e.getMessage ());. m! W8 [; d% Z: d; Y% u# k
}
6 k( N7 E: D' n7 @, W8 n4 ]2 F: P
try {9 t, t/ b7 C3 _0 I0 B: o
Heatbug proto = (Heatbug) heatbugList.get (0);
2 T2 E: x& t9 ?3 R8 j, O Selector sel = & @# h6 C9 X9 [. M. H- F f3 U
new Selector (proto.getClass (), "heatbugStep", false);5 t& B" \; A4 W1 h
actionForEach =
5 G. h1 j; v; T& r9 Z- h. E6 ?5 K; \ modelActions.createFActionForEachHomogeneous$call
5 [/ L8 @2 |2 ]& C (heatbugList,& N* F) b# p, B, Y
new FCallImpl (this, proto, sel,
: _ M- c; \9 s new FArgumentsImpl (this, sel)));
1 A+ w7 S, g- T: {- c( J' y0 C7 F } catch (Exception e) {
* H. M" r3 x. o3 F5 @# v! W$ U e.printStackTrace (System.err);
2 ]" j2 v8 H# l# P$ K9 s- j F }0 F# w+ |6 p+ I |
% r+ S$ M9 F9 h- U3 r% S# y$ T/ L
syncUpdateOrder ();4 D4 B: _' K, o& r: `
& Z3 |* _! B' }/ y; i, ^ try {* c3 g7 T* X: N
modelActions.createActionTo$message
: Y& W# K: U' I/ x3 q (heat, new Selector (heat.getClass (), "updateLattice", false)); W: {& l* B }3 w
} catch (Exception e) {
, H, p* a2 h! m* |* m8 l/ s- i System.err.println("Exception updateLattice: " + e.getMessage ());3 l3 z' t9 k! h2 \6 q8 o' j
}: v: K" D2 t/ Y0 q" G
' g0 q7 n( V5 g2 O% B
// Then we create a schedule that executes the
* a' g1 y' o9 i // modelActions. modelActions is an ActionGroup, by itself it
& F$ k ^) w" p! U* ~5 \ // has no notion of time. In order to have it executed in
7 X! w2 Y; k; E( k, D- x1 I7 V' h& B // time, we create a Schedule that says to use the" ^" l9 `: c( o. p
// modelActions ActionGroup at particular times. This
6 P3 E* h% }2 T9 {* l // schedule has a repeat interval of 1, it will loop every/ \. H+ z( k9 f
// time step. The action is executed at time 0 relative to
( Q/ u$ Z$ @* A! d' | // the beginning of the loop./ _/ N" H' s% }& m9 F
* }7 B; K' n( x6 `: ]3 G& T
// This is a simple schedule, with only one action that is
7 t) |) T# ~; i$ C! V // just repeated every time. See jmousetrap for more
! {$ u8 _$ g6 H' u/ z f // complicated schedules.
1 e/ t; v }& |. F" O ! i: |6 f7 I5 _: Q
modelSchedule = new ScheduleImpl (getZone (), 1);
/ S7 { ~8 y" G8 F1 y modelSchedule.at$createAction (0, modelActions);, d$ I- m1 y4 E0 l# X& H
& P) U' C/ M1 y$ O! E
return this;9 E& C7 m& E6 U' t Z, O M
} |