HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( w: s/ G# J/ y7 |% g. D, A
# b* n4 t5 D" n8 g% o: a public Object buildActions () {# `% g' X4 [8 ]' E( l% ]
super.buildActions();& O! h; }) n2 u1 j7 d- h9 _; ^% ]
U5 N3 z$ O3 n // Create the list of simulation actions. We put these in
( T3 p6 ]: Y3 B& `& Q. _ // an action group, because we want these actions to be, U/ K, V, o }# }% [& L
// executed in a specific order, but these steps should8 P$ {. a" V& q$ q
// take no (simulated) time. The M(foo) means "The message
$ o. l, c2 z2 C. h; E9 j& i // called <foo>". You can send a message To a particular( `! z9 _' ?( p, K
// object, or ForEach object in a collection.0 ?0 f! u* |2 B' Y( a6 X
/ S2 ]- T I7 }. y5 q7 X
// Note we update the heatspace in two phases: first run
. z6 y/ O* O$ J$ r% G0 E // diffusion, then run "updateWorld" to actually enact the
$ t8 D* U: J0 x // changes the heatbugs have made. The ordering here is" y C; e5 C) n. y5 P
// significant!
5 }% J4 c5 Q, Z- X5 n {
S4 J5 ~% A! E! l8 ]! X // Note also, that with the additional6 a, ], `( i$ ?& G0 B; t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ Q+ k2 c+ S% Q' B // randomize the order in which the bugs actually run B, W+ y# W) N- i( s+ f
// their step rule. This has the effect of removing any2 f, N8 x3 k' s& H9 `8 ]- P
// systematic bias in the iteration throught the heatbug
) C, M" A" G# u- I // list from timestep to timestep7 r# k$ s4 |& L9 W3 u/ k M
L' x* P& o4 H
// By default, all `createActionForEach' modelActions have7 M- j9 r# e% m& P
// a default order of `Sequential', which means that the( \5 ?' N T! Q: }/ @ g1 t
// order of iteration through the `heatbugList' will be2 h6 r' A0 d+ F
// identical (assuming the list order is not changed
+ f, X3 \5 V9 u4 w // indirectly by some other process).
+ r% @/ [* n; X/ P, b3 y$ C
4 n1 e, V4 A- z3 I2 d modelActions = new ActionGroupImpl (getZone ());
5 W, g9 n9 T7 `+ }$ u, R F2 ?1 L" ~% A+ F/ w3 n: u5 l
try {
+ t- V* \0 W" u7 ~ \3 [ modelActions.createActionTo$message* ?9 \# K& z$ J: v1 r6 ]) v
(heat, new Selector (heat.getClass (), "stepRule", false));
! {. }0 \3 h; X } catch (Exception e) {
/ v2 N: ~8 [. m4 ~ System.err.println ("Exception stepRule: " + e.getMessage ());; \: |, _1 Z" n. ~4 {) l4 `
}
+ f* Y2 i( [+ }. r" e
4 C& F$ i5 N; e4 C8 | try {
/ Z$ d/ H% p: L& p: @8 b7 s: U" @ Heatbug proto = (Heatbug) heatbugList.get (0);& L9 f4 x1 I2 B
Selector sel = $ X7 U$ j. k) A, k$ X4 ?% k: k
new Selector (proto.getClass (), "heatbugStep", false);
/ s5 F- t# i: k' e, {2 ]( n9 r actionForEach =1 Y8 w- J7 x1 J, m( a
modelActions.createFActionForEachHomogeneous$call
. s2 R5 F% t* K+ I5 x5 V (heatbugList,& }. k& E7 `1 v/ S/ I- d
new FCallImpl (this, proto, sel,' ]! d1 P) K0 G4 P1 ?
new FArgumentsImpl (this, sel)));
$ B0 D5 q/ f7 }/ L7 H, l, l# u9 ? } catch (Exception e) {
3 N% v: m7 V& |8 z/ h, n" d( a e.printStackTrace (System.err);0 X6 A- s: S: j
}
7 h. k. n( ^" L- V7 j , e3 T/ R: m- d- G% _) M, M
syncUpdateOrder ();
- u0 g2 Z3 F2 m' |3 r# l# z: w' w9 [4 a" i* g8 m
try {- l( w7 e) N. D8 t9 d. c! V2 K+ ^" {: f& u
modelActions.createActionTo$message 4 K/ x6 B3 y9 F2 T
(heat, new Selector (heat.getClass (), "updateLattice", false));
# x6 M, ]7 W) w: d5 |/ I4 e0 P } catch (Exception e) {
$ S- ]! a% \1 U8 G System.err.println("Exception updateLattice: " + e.getMessage ());; x' X6 {; M o3 n
}+ U0 a8 @/ \. L; p6 Q/ r
* Q( w- V4 R: n3 d d6 P
// Then we create a schedule that executes the
; E: l! i2 y$ Y6 p& w: z // modelActions. modelActions is an ActionGroup, by itself it2 f" n1 g, e; y, r
// has no notion of time. In order to have it executed in D) @, G+ D! ?# O) a7 o# M: t4 }
// time, we create a Schedule that says to use the2 d, H7 I# {& w* `8 s8 l
// modelActions ActionGroup at particular times. This# I% q0 a3 w3 ]# l
// schedule has a repeat interval of 1, it will loop every
. V! K$ Z) @; Z' F4 T% @, v // time step. The action is executed at time 0 relative to
% j: Y, l) E7 n# R4 Y6 i9 e // the beginning of the loop.
' r$ _2 _) Q0 s0 B1 x8 q4 E8 h: J" U% n/ _5 D3 W
// This is a simple schedule, with only one action that is
2 J; N, P7 a& m& s7 C8 n // just repeated every time. See jmousetrap for more. O8 h" u" D z- k9 B7 Q
// complicated schedules.
' ~ F* Z: e. `) H6 R% a& ` K
5 W. I; a' G( M! _2 D2 I modelSchedule = new ScheduleImpl (getZone (), 1);7 |) Q: X! Q2 A+ p; ~
modelSchedule.at$createAction (0, modelActions);
* Z% M$ r! Z6 x
4 X. S/ g& h9 N* `. m. O* [( M return this;
5 F- U f/ i, v, N } |