HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; P; I6 q) Y; ^6 w
5 o1 l# E2 w$ o8 I2 ]; q
public Object buildActions () {/ i5 W4 ~7 B1 w4 H- k X
super.buildActions();
! n; K% t0 L: g y5 q8 j& f ' E( Y' s2 {9 A/ {. q, T
// Create the list of simulation actions. We put these in
7 d- E7 L6 \, X$ d( m' k, Y8 Z // an action group, because we want these actions to be
: E% l1 b5 W0 N // executed in a specific order, but these steps should
8 u! }) z9 R/ h% c8 ?$ x3 G // take no (simulated) time. The M(foo) means "The message
, K) V, [6 u) b: { // called <foo>". You can send a message To a particular
5 T- T: K5 b- u( j // object, or ForEach object in a collection.* m }8 Y# r8 ?6 v3 S+ `/ U
7 `# w! ?! I0 s. Z, h* W* k // Note we update the heatspace in two phases: first run
! M0 V5 f9 e5 Z; U+ X, x% j // diffusion, then run "updateWorld" to actually enact the2 K5 @/ X3 ]$ u3 a, I
// changes the heatbugs have made. The ordering here is
1 [% K: [/ U5 r // significant!
( u0 U& j% f4 V+ r! Q # E8 I9 F: h g K& T/ \
// Note also, that with the additional
. {: l9 ~. _8 c9 W1 w+ C // `randomizeHeatbugUpdateOrder' Boolean flag we can
: h, M1 R* | X- W/ U // randomize the order in which the bugs actually run/ [! c) h( q+ G5 D5 w; v: k$ _
// their step rule. This has the effect of removing any
3 {- R g6 s0 y+ m. A% w8 z // systematic bias in the iteration throught the heatbug9 d& _; j) s# ?7 [' r
// list from timestep to timestep
0 W* d$ C. V/ m: i6 F
! U6 e" _& U# U" b4 M1 v" W& { // By default, all `createActionForEach' modelActions have( ~1 o0 M; r9 P. B! L& F0 ]4 \# M; i$ t
// a default order of `Sequential', which means that the5 e4 r9 w" [6 J- ~
// order of iteration through the `heatbugList' will be
5 U6 f; E$ z% ~. }8 L) C9 P // identical (assuming the list order is not changed% x9 d* m" j& O: l; O5 y
// indirectly by some other process).) @2 _; ?- \5 Q h
* [6 |& F: F8 }) o: u modelActions = new ActionGroupImpl (getZone ());
1 C' p" `* B9 ^) Z- V" w4 V8 P' _& [4 q! Y6 d
try {2 P' d1 i; |& a5 e, s1 `
modelActions.createActionTo$message a: N3 q) |% i w2 g2 ` Y% t% y" S
(heat, new Selector (heat.getClass (), "stepRule", false));. C y6 h6 k6 s
} catch (Exception e) {
* d, W' ?8 K. t! [0 Y4 A9 E System.err.println ("Exception stepRule: " + e.getMessage ());- x8 z0 u3 p5 |! h, L* Q
}, C% g& O- t% N/ Y5 L+ F
- g/ P) E# H) e& c try {% G3 h$ R2 s |
Heatbug proto = (Heatbug) heatbugList.get (0);
" |5 F# ^ f+ M3 h" A. `) j Selector sel = * ^2 h# t! @3 l2 \5 {
new Selector (proto.getClass (), "heatbugStep", false);) {" T3 \' Q9 s* }2 T7 W
actionForEach =7 ^: `0 t6 Z) A. T! A
modelActions.createFActionForEachHomogeneous$call9 B9 \1 b9 U) v* L1 ]! a% p1 y
(heatbugList,% D) ~* G$ W, G. Y1 K9 F
new FCallImpl (this, proto, sel,
4 {# j* V' o4 D& H" d$ S. I# @ new FArgumentsImpl (this, sel)));
: p) J' {( Q8 k/ M, E } catch (Exception e) {( D! q6 X7 ?7 V0 M
e.printStackTrace (System.err);0 E. n5 ]" E( M) F4 L& G
}& c( Y1 j7 B g9 @0 K3 f7 w6 k
7 |, W- L6 x' e5 ~2 h
syncUpdateOrder ();
2 F4 O3 L' ~( v/ `0 L0 O/ b6 ]% o. m0 e) j
try {7 d0 U/ H3 e+ p5 y3 l
modelActions.createActionTo$message ) Q; U/ G# r" S; q( {
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 U. _# B4 D x- Y4 h, ]6 r } catch (Exception e) {
8 e$ J3 X% h8 [* O3 F System.err.println("Exception updateLattice: " + e.getMessage ());
( @0 ]3 h7 C$ T0 x3 d, ` Y; J7 D }
, g+ `: L" g3 [8 [
$ k, i1 q7 g# o- a; h7 E6 d& ? // Then we create a schedule that executes the8 Z4 Q, k; i& [4 \6 R( S, p9 [5 n+ g/ d
// modelActions. modelActions is an ActionGroup, by itself it1 `6 @ w$ h s) N O
// has no notion of time. In order to have it executed in
: n/ \4 X' C% R9 A6 [; d // time, we create a Schedule that says to use the) }0 f" v( }1 ?% R0 s, E
// modelActions ActionGroup at particular times. This8 `. r; j- B% d- T
// schedule has a repeat interval of 1, it will loop every$ k; o# A0 u0 N4 H
// time step. The action is executed at time 0 relative to9 Z$ ]9 N+ {* v8 v
// the beginning of the loop.
* g! l/ M) R+ Q1 x% g
# L- _+ T: V& A A3 f7 v1 X/ O // This is a simple schedule, with only one action that is6 n- w* R% c2 }# q
// just repeated every time. See jmousetrap for more
( {- ^! N+ Z: N$ {" g // complicated schedules.& ?, d+ k. H8 ^: `0 M$ [
9 q5 k+ m, Q o5 z modelSchedule = new ScheduleImpl (getZone (), 1);
- A; ~" H2 F& M( u& d8 \$ Q5 N modelSchedule.at$createAction (0, modelActions);/ B/ j7 t. \) \3 T# ]3 @) g
' R- W# E# @+ w: b1 _
return this;: T' m% i# M5 \$ Y
} |