HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 `5 \' a& A" W& G
# W4 |7 K( w$ {- V r* G
public Object buildActions () {( |/ m4 e! b/ }0 K( s% Z
super.buildActions();: o/ E6 J6 N5 G6 u( D" d0 Y2 M& X2 X- x
i1 i! E1 g( f$ _
// Create the list of simulation actions. We put these in/ X% z6 `8 u. v8 T& v
// an action group, because we want these actions to be
: C) z' ?6 M0 g$ H // executed in a specific order, but these steps should5 N1 G* Q3 s+ j" l* G
// take no (simulated) time. The M(foo) means "The message
% i6 }% p- @+ }' s: @* E // called <foo>". You can send a message To a particular
2 L4 ?1 i* E8 \% X' F0 Y9 J // object, or ForEach object in a collection.) L5 S1 T! D+ \& b3 W7 C
$ b) ^% ^ t2 B( N$ J
// Note we update the heatspace in two phases: first run
: M& R: a8 H- Q/ H$ M/ n // diffusion, then run "updateWorld" to actually enact the
% k- P6 ]0 ]. ? t6 { // changes the heatbugs have made. The ordering here is
E" ^* @5 U: W6 u // significant!3 F' p# l' }0 j8 T1 c- Z
" }3 \2 p$ g3 t$ E
// Note also, that with the additional: n* l2 V- J( a! _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 } x) K I- n5 i // randomize the order in which the bugs actually run
2 i: p) R) j& u# E // their step rule. This has the effect of removing any! k7 q- M) p( c% H
// systematic bias in the iteration throught the heatbug
6 E- E/ Q+ v1 l% \1 A5 c) { // list from timestep to timestep
' u' @) R4 P9 o8 c
( a/ E7 B7 `7 }+ M: }9 |1 Y // By default, all `createActionForEach' modelActions have9 w% e. m5 C1 O R7 t3 H5 M% K. S
// a default order of `Sequential', which means that the3 `- O- c. U b
// order of iteration through the `heatbugList' will be9 D# ?6 y% _ |1 D
// identical (assuming the list order is not changed. a( A2 h# l2 {7 ]3 L
// indirectly by some other process).
6 g; v1 D/ L7 r- ~8 T% x5 C
4 U% I2 u! a+ `& [' B1 s$ G7 P modelActions = new ActionGroupImpl (getZone ());8 t' o* v/ h. K' u
: E. y% @0 M |& j
try {& S# Z6 k9 z) p: T2 s* R- @
modelActions.createActionTo$message% {5 `- D2 j7 K1 ^5 X1 {$ a# s4 k
(heat, new Selector (heat.getClass (), "stepRule", false));
1 g9 J# n4 ?6 O% T& M } catch (Exception e) {$ K- a! I6 W" P0 U4 ]6 N6 d: i1 L" P
System.err.println ("Exception stepRule: " + e.getMessage ());, S% `1 ?' j& j4 r& b( ~$ k0 {
}9 ~/ N% u2 M9 p! A4 a0 y
# Q' n$ @+ W, B+ j
try {
8 ]( b9 J1 I' b' f8 f Heatbug proto = (Heatbug) heatbugList.get (0);
! O1 |9 L% X+ r Selector sel = 6 {0 Q* X" {0 T4 r6 R; u. E
new Selector (proto.getClass (), "heatbugStep", false);
H- a3 W7 z% U" ]0 M5 i% } c actionForEach =8 c: X$ [: G9 d
modelActions.createFActionForEachHomogeneous$call1 s& m; t3 O2 U5 X
(heatbugList,0 I4 d* o; e1 q9 m) g3 |
new FCallImpl (this, proto, sel,$ L9 `' i: A! u/ m9 I3 |
new FArgumentsImpl (this, sel)));4 P: o4 z, W, U. Y) C7 c! E
} catch (Exception e) {
! u v7 y# y% j$ j1 j$ Y e.printStackTrace (System.err);9 f0 {- V( z9 N* V
}
; C& ?$ C' v6 J' Z* R + g7 r. L: ^+ Q9 j' f
syncUpdateOrder ();
( a2 ~$ h- ^2 ~3 P7 ?- }, a
* x" y/ j$ U& ~, b try {: z. B' a' y* _4 u7 K
modelActions.createActionTo$message ; E0 L" o+ C8 x
(heat, new Selector (heat.getClass (), "updateLattice", false));
a) X( J. l5 X1 j+ t: k) [ } catch (Exception e) {
0 O5 s5 s0 @% Z System.err.println("Exception updateLattice: " + e.getMessage ());
* [- ^8 S* z) h" T# e. g }" @/ X5 a! d2 Z/ \
! q6 W+ P# k/ E9 k0 i1 c; _
// Then we create a schedule that executes the. n7 n4 N1 ^& P7 h1 }' }
// modelActions. modelActions is an ActionGroup, by itself it5 F5 L, N/ {8 N* E7 R6 h5 q
// has no notion of time. In order to have it executed in$ t( B, G1 W) U3 @9 ^* a
// time, we create a Schedule that says to use the: j2 W7 }0 h0 o+ V8 D& l9 v" H( C
// modelActions ActionGroup at particular times. This. ]3 E8 N2 w0 X
// schedule has a repeat interval of 1, it will loop every
( i4 V" u: H1 R/ ] // time step. The action is executed at time 0 relative to% x% ?; g) K" R+ K
// the beginning of the loop.0 c+ ^, j6 K" b( d
) r- s; L! ]9 T% ]4 ^! g // This is a simple schedule, with only one action that is
& [2 v6 a! m e7 z; T // just repeated every time. See jmousetrap for more
* C- _9 e$ n1 n. ` // complicated schedules.9 t2 }1 _! x( c( Z
]3 L+ n) s( C1 H
modelSchedule = new ScheduleImpl (getZone (), 1);& a- U& R, F1 H3 c7 v$ i6 V: ~
modelSchedule.at$createAction (0, modelActions);
! ?" k6 O! \' c6 L6 \- Z
9 r6 p" J ~8 S2 |1 d7 x+ r return this;, Y F9 v- _% k1 ^* {
} |