HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ a6 x9 l# F* C* P. E C) o) ~
0 b% E" V2 @ K public Object buildActions () {
* k7 w& i! k# y6 D super.buildActions();
! {) I' I6 B7 Z6 P
" m5 ]* N$ j. p% |9 q6 j8 q9 a& F // Create the list of simulation actions. We put these in
9 [* H! |; N' v ^ // an action group, because we want these actions to be" W D4 @0 o' z8 @( H! K) F1 u5 c5 Z
// executed in a specific order, but these steps should2 s" o/ i% I- V5 L* a5 A
// take no (simulated) time. The M(foo) means "The message
5 W+ r' F- A% y* Q3 n: Y4 c# G // called <foo>". You can send a message To a particular$ T2 w& r! F8 I
// object, or ForEach object in a collection.* t3 f% s: L! s5 p2 D
. G& j, C6 K' a% n
// Note we update the heatspace in two phases: first run
I8 d9 d# j* d) C // diffusion, then run "updateWorld" to actually enact the8 }) \/ Z8 B% L
// changes the heatbugs have made. The ordering here is
~# ^# ^" Y, b% V // significant!
) Y* w/ o! g4 N' R0 i/ a # b* u2 N+ {' ]/ |+ P( ]
// Note also, that with the additional
- T( }0 d- a1 y7 }& U: N // `randomizeHeatbugUpdateOrder' Boolean flag we can0 o; O f7 e& Y" A; @' k2 G! m
// randomize the order in which the bugs actually run8 r, z& W$ k+ J) m- t0 e
// their step rule. This has the effect of removing any4 N' g; u/ P+ f
// systematic bias in the iteration throught the heatbug
K- C S( M6 j; c3 l% [ // list from timestep to timestep
; R6 A. s* a1 v' a8 M J; ?
' W" |5 u+ F9 I8 c- A2 M, c // By default, all `createActionForEach' modelActions have% s; N/ j2 {* K' m
// a default order of `Sequential', which means that the
4 t7 j$ f9 u; ?) N // order of iteration through the `heatbugList' will be" V! J7 \5 F; D/ D3 r1 q
// identical (assuming the list order is not changed. p" F+ t" f. Z4 o; Z/ e' W- q' [
// indirectly by some other process).8 |4 n( Y: L# P3 e* s4 A% E
: I; F$ x F. L( ~3 z modelActions = new ActionGroupImpl (getZone ());4 P5 `* ^) L8 Q/ J2 v, Y) x
3 b& F; W! G% `2 v8 c) T( F# b try {
5 E* ?" _9 A; B9 y modelActions.createActionTo$message
. ~2 \) ~" Y# W: ~% `8 \1 e (heat, new Selector (heat.getClass (), "stepRule", false));
' f# ?3 z8 }& [ } catch (Exception e) {5 G- x# y' \0 |3 P
System.err.println ("Exception stepRule: " + e.getMessage ());& ~# ~( l4 ^1 w) r5 \
}
4 `0 M! J% g: F' A' q
7 z9 G, m! v( H! N+ S o, g try {
8 b( s% @9 t# m3 A Heatbug proto = (Heatbug) heatbugList.get (0);2 J4 |% s! V( c. r! V) n3 G. d
Selector sel = , ]5 S H2 E) A3 i
new Selector (proto.getClass (), "heatbugStep", false);
/ P [. {) U; z/ g) ^. h actionForEach =
; G- E" J5 C. k3 b' \; ]+ T modelActions.createFActionForEachHomogeneous$call
8 U/ T Y" A5 p# P Z0 a# y (heatbugList,8 i8 K# |2 G, [) ^) \8 }* t
new FCallImpl (this, proto, sel,
1 R6 r) }0 b \* {4 C8 E9 e new FArgumentsImpl (this, sel)));) E8 d5 d) t1 {- r
} catch (Exception e) {3 V5 @8 a+ E! V' y# M, J; y/ s
e.printStackTrace (System.err);
1 b s' [8 P9 N# m8 R& V7 P9 | }$ G# n7 A7 d; @4 p% F0 I( q/ T' p6 s
5 j: U, w+ Z6 a1 j9 {% p
syncUpdateOrder ();
* I8 i% P Z1 j' U; j% m! S! T3 J- L, \4 A. V! L8 e0 {; [
try {* D& _! z9 j A8 p$ E3 J4 b
modelActions.createActionTo$message
C# V3 @4 P9 n$ e (heat, new Selector (heat.getClass (), "updateLattice", false));( d! E5 W3 m5 C! t- a' T
} catch (Exception e) {
: Z9 ^; @2 |: X* d" m System.err.println("Exception updateLattice: " + e.getMessage ());
7 d% f- s1 [9 u3 d }
& N1 o* q" v4 @" _0 d; w' W
; w5 ]$ ^, r2 x r) h4 J // Then we create a schedule that executes the+ ^( m% e; ^2 K8 x+ @; _* Z
// modelActions. modelActions is an ActionGroup, by itself it9 z, u3 _+ z! o, u9 G2 ~
// has no notion of time. In order to have it executed in( Y, i2 p; q% n1 p r3 z
// time, we create a Schedule that says to use the5 v8 Z" V4 N: g, |' M
// modelActions ActionGroup at particular times. This
/ h1 W8 O& k. x0 w0 T' |! l // schedule has a repeat interval of 1, it will loop every' r S5 Y: k/ G$ I: J
// time step. The action is executed at time 0 relative to
7 Q+ K$ s3 u4 f& ]4 H // the beginning of the loop.
7 X- @8 d" q: ~4 g0 C. q# r
5 M8 f) J& V) U1 t // This is a simple schedule, with only one action that is
- t% z9 x) C5 X# L$ `7 R // just repeated every time. See jmousetrap for more$ u1 b! R" P0 D9 E7 M
// complicated schedules.
- m4 x F5 f0 v( P5 s2 R a8 O r0 X8 P# N% C, x
modelSchedule = new ScheduleImpl (getZone (), 1);
& D$ v/ @, T4 c( b& o0 F7 R2 `0 Y modelSchedule.at$createAction (0, modelActions);
' ]0 A0 ^4 B. w% t# Q6 ?7 I% j% Q
0 j9 S- z& z' H" f1 j* j: [ return this;
+ W- z2 F% U* V7 R( m. Z } |