HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 e4 U0 D9 m- J% H: P/ k$ P
, n2 J8 |' Q4 u+ J public Object buildActions () {5 t7 }) O# M, N2 R* K. ~! h
super.buildActions();
+ U3 r) {* s$ `5 `$ B) [$ B' ^ 7 E2 R- v- m, `/ ~
// Create the list of simulation actions. We put these in
; k( x6 z" P# I- r3 u/ c* m6 _ // an action group, because we want these actions to be
; \: I. {0 E' q4 I" q* S$ _0 i // executed in a specific order, but these steps should; Z3 e; W3 E, `3 f9 k
// take no (simulated) time. The M(foo) means "The message
* }" Q0 m6 ~8 H" W1 T$ ~ // called <foo>". You can send a message To a particular! h# M2 P/ h p" [5 R
// object, or ForEach object in a collection.2 c2 \+ |) i/ J+ Y" C/ }
9 s; y# p! f/ Q
// Note we update the heatspace in two phases: first run
) t# G& Q- F9 n1 c$ @5 }! b // diffusion, then run "updateWorld" to actually enact the
0 e# r+ e' \5 _, {* U, Z$ u // changes the heatbugs have made. The ordering here is
" m1 [) X9 E/ u( X. W! ?2 q // significant!: b" ^8 F N5 v' u( t* R" p
* g$ f# R# j1 V
// Note also, that with the additional
- d3 J* J4 c- W* D* R) r1 H // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 @2 `0 u4 F0 q8 B; o( Y // randomize the order in which the bugs actually run$ H3 h9 }! d) ?( f0 t3 ~/ t
// their step rule. This has the effect of removing any0 c1 T" j. A3 c) i0 b R0 D. h8 D0 m
// systematic bias in the iteration throught the heatbug
. P+ j3 x- k0 `) c // list from timestep to timestep
. B8 A" a; |/ W% g ?5 \, {8 a 0 J6 t3 [/ D4 V8 A* q
// By default, all `createActionForEach' modelActions have9 }$ Q4 T' D& B9 \
// a default order of `Sequential', which means that the
9 q- @& M+ I4 \1 G4 @3 S/ H' g // order of iteration through the `heatbugList' will be
) F- ~; ]$ i; M* T' q // identical (assuming the list order is not changed
0 y5 I5 E) `8 t6 U' e } // indirectly by some other process).
. H9 [: p( g H
- S6 w! B, J5 H i/ `7 K; |: O$ o' `+ Q modelActions = new ActionGroupImpl (getZone ());( I3 @0 {* q- C- U) S7 n2 B$ M( n7 B
9 T! W, u" @+ _% ~, b: R try {
* \( \9 y4 g9 c modelActions.createActionTo$message
# a8 O* w1 V' r- K (heat, new Selector (heat.getClass (), "stepRule", false));9 o5 T- s% M9 M2 {; z' _
} catch (Exception e) {3 i) M0 X/ ^% J
System.err.println ("Exception stepRule: " + e.getMessage ());
$ d( ~, R; M! w& J4 X# L* Y* i } d6 R. d8 t/ ]9 x
+ N1 W% f* a0 E% `) B& w/ l/ P try {
4 z, H" k7 W x, u5 @ Heatbug proto = (Heatbug) heatbugList.get (0);/ S5 ~* s8 I# V3 @. Q, B1 n
Selector sel =
) j+ X7 G2 R( O3 u+ \. P- I% M( ] new Selector (proto.getClass (), "heatbugStep", false);
4 K* O& m" N, H( z4 G actionForEach =" N% e W# U. ]9 M
modelActions.createFActionForEachHomogeneous$call
7 i/ `- D, @+ c9 q$ C7 h. w (heatbugList,
% i2 Z& e2 [" f9 ~ new FCallImpl (this, proto, sel,! C8 Y6 M* j! c L0 |( Q
new FArgumentsImpl (this, sel)));
2 y: x/ r' e5 U5 K' k } catch (Exception e) { P9 W" {/ |' e1 C R8 p) W
e.printStackTrace (System.err);
; j; `: b3 h% |5 X* a4 l }
" n% `9 w/ Y( s1 |7 g/ n $ Q: g& \* G0 p5 [# X
syncUpdateOrder ();
2 V. C) ]' ^% U! v2 x$ ~" f9 y/ r- }, x
try {3 I4 I+ P6 A- w6 m
modelActions.createActionTo$message + `! H* i5 s4 T- o
(heat, new Selector (heat.getClass (), "updateLattice", false));3 a( X) ?5 ], W$ b$ ?, [1 B( ^
} catch (Exception e) {+ A/ s0 M/ O [4 H0 ?
System.err.println("Exception updateLattice: " + e.getMessage ());
5 E' n: _4 R' ]# p3 e& x9 S4 ]+ _ }
4 q& Q6 ^/ c$ Q, t ) ` n& o( D$ F8 Y- D
// Then we create a schedule that executes the
$ h) ]6 j) W' K! D // modelActions. modelActions is an ActionGroup, by itself it
6 r1 p4 v1 x. ~/ W: o // has no notion of time. In order to have it executed in
y" g x! x3 w // time, we create a Schedule that says to use the
k6 a1 j1 k* Y G* o, b // modelActions ActionGroup at particular times. This) y7 Q, ~/ H) y8 H0 [: |9 l
// schedule has a repeat interval of 1, it will loop every
' r/ t- y: J/ c; W // time step. The action is executed at time 0 relative to0 F5 M" W8 z: h4 G4 q
// the beginning of the loop.
4 ~5 y; K6 I5 L6 B( B0 e: j
( j$ Q8 H7 B! R! n8 X/ t6 h: t- ^ // This is a simple schedule, with only one action that is
7 n' ^2 w# B4 E, D // just repeated every time. See jmousetrap for more9 v) S8 Q/ q. t7 [+ Y
// complicated schedules.
3 C$ p. a5 e. O2 V
' S7 \7 l Q! G/ n! O d modelSchedule = new ScheduleImpl (getZone (), 1);' C. o$ U1 L& W- {! u. W
modelSchedule.at$createAction (0, modelActions);
2 z; i. M [+ ?- A
( V$ `2 {$ @5 l, l$ O3 w6 ] return this;
4 ^7 j+ E' x& x; f, S! L7 t- y } |