HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& i6 K' Y) [/ D' X& J
" S: T X" k! @* t: c* i public Object buildActions () {8 P4 L% `3 h A6 I5 V! E
super.buildActions();! z/ {. @6 f0 W7 }! |- Q7 z/ o
5 ^& X' i% E8 o // Create the list of simulation actions. We put these in( f" T* z/ k7 C1 v9 q9 w" c
// an action group, because we want these actions to be
! u' M8 z/ c1 \; W // executed in a specific order, but these steps should
# z/ S3 p8 l. y1 x1 g2 d$ m // take no (simulated) time. The M(foo) means "The message
( T* B/ K9 p; @- R! ` // called <foo>". You can send a message To a particular
/ R( N$ p/ R- V% V4 S0 { // object, or ForEach object in a collection. Q' H- e* h! V& Z$ c
! K* G: [; A% M" k4 t! K/ u2 `" E // Note we update the heatspace in two phases: first run
2 Q' \# X: S& W9 R // diffusion, then run "updateWorld" to actually enact the; ^5 y! C/ P" g& U* A+ p- C& Q% \, [
// changes the heatbugs have made. The ordering here is
) j9 W4 M; X/ j& e, g3 c // significant!
# X0 V! S% u/ A" K- V/ Z# e- C8 {
6 f1 j x9 w5 {! s* J! j. G5 x; J // Note also, that with the additional9 a# x$ F9 R, J6 v' z$ v) D
// `randomizeHeatbugUpdateOrder' Boolean flag we can# }: q/ _2 Y) @1 A6 c) [' B3 D
// randomize the order in which the bugs actually run
9 l8 ?' R0 `# R" \+ Z3 n7 u // their step rule. This has the effect of removing any
6 V; |: D* {3 ^3 H+ C3 C* H: | // systematic bias in the iteration throught the heatbug
4 Z% W- E3 T7 J) Q // list from timestep to timestep* g3 G- e7 m6 [7 o: y
3 x H" j& Z2 u% {& g- T% R' A |
// By default, all `createActionForEach' modelActions have
. D1 k- ]- x) R" I // a default order of `Sequential', which means that the8 \: q! F1 ~8 h, t! k, t
// order of iteration through the `heatbugList' will be
- q; V$ j; N1 m; @. c# _3 g // identical (assuming the list order is not changed
' {/ N% J1 }/ O1 ], } // indirectly by some other process).6 Q* ?4 O' s8 n& h/ a: T4 M
7 i4 O. {7 f+ e& }3 h5 m% t. a modelActions = new ActionGroupImpl (getZone ());
6 F; w; t9 X; ~
' |, \$ J1 x$ y2 R try {
/ M2 t' c: y( d modelActions.createActionTo$message
4 E$ h- l: |4 N# v& S (heat, new Selector (heat.getClass (), "stepRule", false));
# Q5 M+ H# d) L0 {: r } catch (Exception e) {. Q+ ]* b1 f# C
System.err.println ("Exception stepRule: " + e.getMessage ());+ L9 O( X5 O" \. m9 n* C* r2 t
}
* B- c2 y7 y* c. j3 e! U" B
& |/ t3 p; H; T/ @( v# k. W try {
+ U- b7 P; ^, r. x Heatbug proto = (Heatbug) heatbugList.get (0);$ o* v" ?8 M- B4 y/ D- o' a O
Selector sel =
# }: i1 Y7 ]( {: c: H* V new Selector (proto.getClass (), "heatbugStep", false);
- D2 p( h6 I+ t" y0 \; i9 m actionForEach =$ H: A: r. B4 ^ ]4 l. F
modelActions.createFActionForEachHomogeneous$call
6 \ @# X+ A. i% J. a$ K2 C" h (heatbugList,
; _! g9 R- ]% }# \; O- Z0 C7 a: ` new FCallImpl (this, proto, sel,
# j9 f( a# [3 ?0 b% ] new FArgumentsImpl (this, sel)));# j) W# k f' E: c3 a8 B
} catch (Exception e) {
% z" d1 }7 E' ]1 ?3 S) d e.printStackTrace (System.err);' h. X; d3 x$ ?. }1 ~, {
}$ t) p1 v# T3 q) k" Q
7 J5 E' l p; T4 E( X syncUpdateOrder ();
& j, C u5 `1 \
5 e& z' z) S5 Q% G try {" W* I# j! u: i$ ^0 o+ ~. l# R
modelActions.createActionTo$message 4 ` l* T7 r4 A, y6 u$ a" X
(heat, new Selector (heat.getClass (), "updateLattice", false));
. Y0 T4 M: b$ @6 B } catch (Exception e) {1 f# r. `8 k! c
System.err.println("Exception updateLattice: " + e.getMessage ());2 Y# [; e* o; f2 M# z
}# v; n6 J3 X2 H1 J. {/ e8 O7 }
! H5 B: d0 N/ J1 y% v // Then we create a schedule that executes the: J1 B% s, q9 S0 C R3 Z/ O. S
// modelActions. modelActions is an ActionGroup, by itself it0 H/ v9 x9 C& [- @( F' G& ?
// has no notion of time. In order to have it executed in0 E! K9 Y& i. j/ q; n3 x
// time, we create a Schedule that says to use the1 T* z7 T8 E7 P3 O8 g6 E; C: Y4 Y
// modelActions ActionGroup at particular times. This
8 h$ \$ N6 @3 a9 _ // schedule has a repeat interval of 1, it will loop every
; {1 ?$ J: U G8 a W& K; ` // time step. The action is executed at time 0 relative to
! k& C1 H$ _; c& G- S: A // the beginning of the loop.
2 G2 B/ O Z2 c3 R7 p1 I4 p
, o3 M, [ a$ c: V: _8 t // This is a simple schedule, with only one action that is
( F" n7 l# }# c& l" [ // just repeated every time. See jmousetrap for more. @+ ~8 Q. r" x$ E' m3 L
// complicated schedules.( o0 Z! S0 I+ K/ w5 p$ U8 u
$ i4 W2 @8 q! @/ f( N9 K: ] modelSchedule = new ScheduleImpl (getZone (), 1);8 _$ N6 z" ~. j( N! \+ _ ?+ l
modelSchedule.at$createAction (0, modelActions);
8 k1 G) |* \+ j* \8 u
/ X3 ]# \. B9 j; e( X4 M4 o7 @' C M7 U/ C return this;
3 u9 ~# H( _+ y& a4 e" e } |