HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( B. r# D" f* x+ o
: ^% f; Y D0 C3 h- t+ {, x& g public Object buildActions () {$ }% J8 D! I; c: u5 Y( C6 H
super.buildActions();
8 K$ A. J! I9 Y) [ ; w! C# Y" Y- ]7 y* \
// Create the list of simulation actions. We put these in0 {2 G5 ~) x( P; s& L3 Z) Y2 H
// an action group, because we want these actions to be
$ y1 D: T: _8 C* P: a // executed in a specific order, but these steps should% a, D, \& g; [3 @$ M% S
// take no (simulated) time. The M(foo) means "The message
/ _) I1 j N$ c5 E' S( N- W' G // called <foo>". You can send a message To a particular
4 c2 ]5 o1 T# C* q, r // object, or ForEach object in a collection.6 r/ M, } C4 }9 V
. \( f; L7 B0 Z) L! G // Note we update the heatspace in two phases: first run
$ U/ [# H4 H9 M- O$ q# V6 H- {* Q // diffusion, then run "updateWorld" to actually enact the
: K* E% f9 U0 ?3 F! l // changes the heatbugs have made. The ordering here is
4 i$ X8 {3 |8 k) g3 H3 I7 ^( h/ p // significant!% I8 G- J4 f V7 ?% L
/ a% w$ `6 \' q( Z; { // Note also, that with the additional; {+ C& J# {+ ~. `: N. g; u
// `randomizeHeatbugUpdateOrder' Boolean flag we can! a2 T+ K" ~! r2 ~. {+ ?
// randomize the order in which the bugs actually run
' d+ y% ?8 ]8 R( e% ? // their step rule. This has the effect of removing any8 v6 G) f% Y- X7 t: r
// systematic bias in the iteration throught the heatbug8 l7 o' @# ?! A# j& O1 h
// list from timestep to timestep, _5 B# c2 e7 ]# ~2 s* i
" S ]/ S) s* I4 p9 z
// By default, all `createActionForEach' modelActions have' t0 P3 R& z$ J# I: V+ `5 [
// a default order of `Sequential', which means that the
1 |& R8 j* ~+ e0 C7 M% j5 ?7 X6 q // order of iteration through the `heatbugList' will be
' o; C# i" \1 Y. D. ~ // identical (assuming the list order is not changed
2 k) L; w/ m* ]" ~ // indirectly by some other process).5 X) f+ J; g& Y% o: N
$ {0 O7 v7 {+ i( C3 i' _$ F- y
modelActions = new ActionGroupImpl (getZone ());
1 i* }$ u& Q4 w
9 j* j u/ j+ W try {
; _, G4 T. t5 N* n modelActions.createActionTo$message5 A: _8 @, X0 R; e" b
(heat, new Selector (heat.getClass (), "stepRule", false));: e1 N) R4 G" |
} catch (Exception e) {! P7 i9 Q+ Y5 D' P7 \! B' a* \
System.err.println ("Exception stepRule: " + e.getMessage ());
0 W; a) N$ g! k8 D* S }
/ \/ f6 [$ N S* ]7 @9 j) m$ R
& h+ ?' @& p( d# w& X/ }& o try {
! X1 b. i' Y5 o2 L6 A8 g Heatbug proto = (Heatbug) heatbugList.get (0);! P' I9 Y/ w( }+ a4 t( _, e
Selector sel = % F: c0 a7 b! \. K& _- M
new Selector (proto.getClass (), "heatbugStep", false);& Q7 v; g+ b8 _% _" b
actionForEach =" v" X4 x+ U; m- h' A c6 b, v
modelActions.createFActionForEachHomogeneous$call
6 Q/ h3 L: X4 ` d/ S (heatbugList,8 V+ ]& D$ g, q/ _ E
new FCallImpl (this, proto, sel,' v( I% N+ N( s. E0 @
new FArgumentsImpl (this, sel)));* ^0 P3 I" A/ D, ]# v: {
} catch (Exception e) {4 v- G! \: R! `; n" y
e.printStackTrace (System.err);
; k" m7 P* N, ?4 b+ ~; D }, V2 `; x0 U" k+ T0 L& O7 d
: Z4 P. o$ K$ P6 X& D syncUpdateOrder ();
: q& X* c6 n) M: ~" [5 L
$ G* g1 S& n; l6 W& b try {/ l7 b3 ?8 z; M7 m& P" ~
modelActions.createActionTo$message
& E6 T, q" M/ K1 U( h (heat, new Selector (heat.getClass (), "updateLattice", false));- p$ L' L: S' q% p7 O( l
} catch (Exception e) {
8 V5 Z1 L$ D4 p1 j/ y \6 i) I System.err.println("Exception updateLattice: " + e.getMessage ());1 ~* y' z9 ~$ X: h8 u, r+ d9 [
}; W2 d) D" V! z. W+ _8 D Z4 B# ?
5 Z# K8 |3 @9 L5 N- B' U% f1 m& T, u
// Then we create a schedule that executes the4 w) h! q% o: f; O+ j9 g# ?
// modelActions. modelActions is an ActionGroup, by itself it5 `; G% l Q: I3 J5 \
// has no notion of time. In order to have it executed in
9 ~/ F3 |7 m2 ]* e; L# R. C& `- b // time, we create a Schedule that says to use the
) O c7 O, n6 S8 y* ~/ l- p // modelActions ActionGroup at particular times. This1 s& x3 i9 H1 P4 ?. I( j% j' ~
// schedule has a repeat interval of 1, it will loop every( s4 g+ A& H j3 W9 u
// time step. The action is executed at time 0 relative to
* P& U1 p1 ~9 ~1 v, P // the beginning of the loop.
, l( Z5 n! j6 m- K" `9 H+ \: t
$ v5 O) m& S+ Q: r // This is a simple schedule, with only one action that is
$ d# L% V. G3 ^9 X- F // just repeated every time. See jmousetrap for more( a3 W" o$ Z' g" T6 a+ _
// complicated schedules.- B4 Y% | L% h1 j2 T+ D2 a1 \/ ~& Q
' j' |1 T V y$ x5 R modelSchedule = new ScheduleImpl (getZone (), 1);
" U" C9 D- \. n; K0 Y4 ^0 g) f modelSchedule.at$createAction (0, modelActions);7 m$ j% t. i% }
+ @$ k0 a/ m. {# s$ V0 h
return this;
4 i+ q% D- v* R! r# k- [: ]3 Q } |