HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 K1 }1 `# L' t* F4 G) U* ~- p# I+ P
public Object buildActions () {& ^ v1 J4 g9 C. U2 ]5 U' v. P
super.buildActions();+ `2 \5 F" s% E8 z. m8 P
% c4 d7 `6 R3 B5 Y1 G1 F3 u+ B# ^ // Create the list of simulation actions. We put these in6 z+ P- X- C# d7 Q2 A" s( ]* h
// an action group, because we want these actions to be2 l, }" S( @5 B5 T; B
// executed in a specific order, but these steps should
I+ V9 B( K( i4 ^6 @ i" l // take no (simulated) time. The M(foo) means "The message- G. K/ B8 X- f+ W/ B! c) R
// called <foo>". You can send a message To a particular
, a! u( b- P2 k. ^# L // object, or ForEach object in a collection.
- x; y2 D2 L" H6 x " c/ }7 j. `0 O3 j# r. D/ T
// Note we update the heatspace in two phases: first run3 c h( z6 t& F$ m" N4 O" Q% D; i1 v
// diffusion, then run "updateWorld" to actually enact the
9 m- W+ }3 w) {6 }! ` // changes the heatbugs have made. The ordering here is
. k( J, k: x3 C4 o/ b // significant!
9 y) Y4 y$ \3 T- D" K
& N G5 d" H& t8 p2 \2 B$ \ // Note also, that with the additional
$ g6 A% k0 {5 R1 m" C- E // `randomizeHeatbugUpdateOrder' Boolean flag we can" C1 h! l* V! ~# q
// randomize the order in which the bugs actually run: ]7 I% N U* r% P% G
// their step rule. This has the effect of removing any- C; o& x# `8 B7 N
// systematic bias in the iteration throught the heatbug1 O% I# V5 U$ u' K( |6 I$ I
// list from timestep to timestep0 \+ E4 F4 Q6 {" n/ e+ f" w
9 H% u& h- k, L: x: [$ W8 B0 w
// By default, all `createActionForEach' modelActions have. j2 H9 A3 s& @9 m D
// a default order of `Sequential', which means that the9 j4 Y% c5 I7 j. q' c
// order of iteration through the `heatbugList' will be5 I3 C4 b V _8 |3 f
// identical (assuming the list order is not changed _+ l, V# @! {/ t9 @% }5 y
// indirectly by some other process).# I% a# Q+ _/ ]3 [, X8 f
9 y" M, q( Z* C, r) F
modelActions = new ActionGroupImpl (getZone ());& ^- Y6 F5 h: u7 Z! J9 k7 Y M
' v' |8 i! Z; P) j+ ^2 Q2 N# g
try {
: Q6 E2 F" B) P. p, u; v modelActions.createActionTo$message* T4 ~( Q! m& I r2 ~+ G& s f
(heat, new Selector (heat.getClass (), "stepRule", false));5 S4 d, h) Q3 {1 H* I8 S
} catch (Exception e) {
+ x; k- X7 ?, b) g' G- {: d System.err.println ("Exception stepRule: " + e.getMessage ());/ K- w, {8 k. M" I* F0 c
}
1 I, S+ R5 ^. K* m9 Q2 H, ]( `$ m1 a- [1 Q# p
try {
V' w3 {3 @4 I0 n: B. R l/ g8 `( d Heatbug proto = (Heatbug) heatbugList.get (0);3 j" T; w1 Y4 S; a: N) O$ c
Selector sel =
6 j1 @- M- _* e; O new Selector (proto.getClass (), "heatbugStep", false);
h8 G5 ~3 |- ^7 S2 D actionForEach =( g; A; W3 D" R+ j
modelActions.createFActionForEachHomogeneous$call
+ F A3 e2 p: I% e. c$ V1 B* i' S (heatbugList,& [# z- I: n# Z0 \" ~3 n5 [# S
new FCallImpl (this, proto, sel,: }$ \1 t4 m l
new FArgumentsImpl (this, sel)));% h n; g$ b9 w9 ]
} catch (Exception e) {
( |. E) o M4 z2 a% k: T$ F e.printStackTrace (System.err);
# S( L$ p! o0 Z1 J }4 J* n I1 P( \ R& r
0 f0 s, Y8 I% h* R7 U5 E syncUpdateOrder ();# [. l% h0 L1 {7 ]
9 R6 ]6 S. N6 P7 i- }8 V1 O try {
# T3 W; v" s2 \ G$ L# }/ U modelActions.createActionTo$message
6 ?5 m/ G9 D3 @2 r$ r (heat, new Selector (heat.getClass (), "updateLattice", false));; F) P7 p5 i& R H- Y. H/ J
} catch (Exception e) {( |# K0 r; z7 C2 c
System.err.println("Exception updateLattice: " + e.getMessage ());
( x: W% }0 G3 @# y- a }
9 _- i/ m0 N/ F' r! l" k. T7 p: i 7 V1 M! o2 j8 j) ^3 L( ~
// Then we create a schedule that executes the
t: m" S4 C3 i; ` // modelActions. modelActions is an ActionGroup, by itself it, |$ O3 C6 ]$ @) ]4 g: S
// has no notion of time. In order to have it executed in
4 X4 l9 R( L* B, k2 T // time, we create a Schedule that says to use the$ W, }; G: z P. ]$ {. s2 x/ I
// modelActions ActionGroup at particular times. This
7 |( v7 N; E/ x D // schedule has a repeat interval of 1, it will loop every/ W0 E# K& v% D* J3 o. e6 \/ Z
// time step. The action is executed at time 0 relative to: E- X- c B3 P5 f4 {( K; W% C/ w
// the beginning of the loop.) O' A- z) o. _. l
9 W9 e1 N& ]8 X; N" s // This is a simple schedule, with only one action that is1 q' E0 Y/ i- y) [0 f
// just repeated every time. See jmousetrap for more; ~0 |) x- R2 s
// complicated schedules.
& _) o4 g" M; H& n, r0 ^9 l% P# h! B
# s/ ~* Y! ^: w& w modelSchedule = new ScheduleImpl (getZone (), 1);. D8 A' X' [+ r, w: h0 {6 e
modelSchedule.at$createAction (0, modelActions);
. V& e, \( f! |5 _ O 2 Y6 d) l: P" G1 g7 V: L
return this;1 |* U6 ^1 \/ `0 H3 r% S
} |