HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# E1 F( Z2 }# C! S0 ]
% `* s6 m. {+ G# b2 G% e1 O
public Object buildActions () {
0 ^; Z1 g. C2 k* y& o I) @ super.buildActions();
+ F* q( }! z% S0 } * w7 D$ R* i4 z+ X2 @6 n7 ~
// Create the list of simulation actions. We put these in, ~* }* K; h+ b8 T/ M0 |9 h
// an action group, because we want these actions to be& e% ^6 O" R( S _% L/ W
// executed in a specific order, but these steps should
) G* f2 E# H9 w7 @7 b6 y, B; p( e // take no (simulated) time. The M(foo) means "The message! W4 O# a p7 k" ~( E5 s# j
// called <foo>". You can send a message To a particular( Z# n5 c; i, [* z2 {# i: v
// object, or ForEach object in a collection.
2 ]/ w! p* S& J8 M2 Y" m+ j
8 g3 `! T$ g% K# w- L1 s: w9 j8 _ // Note we update the heatspace in two phases: first run
3 c+ V/ a* _- L1 e- R // diffusion, then run "updateWorld" to actually enact the
, f' G4 n. |! o% M- r* o" N // changes the heatbugs have made. The ordering here is2 t) j, R# L0 Y1 p
// significant!
, b0 r& j1 M3 E4 c0 H/ {
: h& }, u! K3 A& Y // Note also, that with the additional
0 l* v: R6 }5 x9 H // `randomizeHeatbugUpdateOrder' Boolean flag we can3 F5 t' D" V1 k* _
// randomize the order in which the bugs actually run+ e$ ?) ]2 ^7 e8 A6 y5 Y/ n) a
// their step rule. This has the effect of removing any8 Y$ J9 y8 e! A8 @4 p6 U* Q' n
// systematic bias in the iteration throught the heatbug
/ U; T8 B0 D; W) D" d C' l+ ] // list from timestep to timestep
# q+ ?5 H# H3 g# W' O! h4 W! c$ b9 X2 j
5 S S# @: r1 ~% M1 g // By default, all `createActionForEach' modelActions have
6 q B' @3 A5 U9 o // a default order of `Sequential', which means that the) ]& N0 r' u; v2 X
// order of iteration through the `heatbugList' will be
: Q `/ [7 J8 x" e" | // identical (assuming the list order is not changed7 D/ |9 E2 w* `! B- h% @& k3 T6 [
// indirectly by some other process).; ~# O& e. V. g. F/ Q- q. f1 d
: [ ] o$ V b- @ modelActions = new ActionGroupImpl (getZone ());0 Q* G B; a& t
) C, }4 M2 h( ?5 w* A& d! X
try {
6 C( d; f: P0 ^! A7 W- H; [ modelActions.createActionTo$message4 [) E* P# O8 u9 D
(heat, new Selector (heat.getClass (), "stepRule", false));/ |1 K R5 G: a# t
} catch (Exception e) {
) t( Y1 y* v2 |4 @ System.err.println ("Exception stepRule: " + e.getMessage ());
4 o3 S. f# g. O) L% F6 b }3 U- Z _, V. C, U
9 X. O5 p6 N r
try {
7 o+ p. i$ s# o0 u Heatbug proto = (Heatbug) heatbugList.get (0); i! B; {' b9 K8 r# z7 k
Selector sel = ( X- b9 }: j0 f$ z, @: b
new Selector (proto.getClass (), "heatbugStep", false);3 u. V0 @! Y# B) C
actionForEach =
6 T0 o; J6 n9 v modelActions.createFActionForEachHomogeneous$call8 `% g) C+ @. h$ l9 o, c K6 `
(heatbugList,
! H% D& f2 E0 ? d* I7 J( C new FCallImpl (this, proto, sel,
6 v9 D1 R v( f) } new FArgumentsImpl (this, sel)));
. y4 r/ C G( `4 u2 x/ F& E! R) d } catch (Exception e) {
" |. \* Q& A/ w( Y e.printStackTrace (System.err);
0 t3 P8 `5 z- @& Q- }2 o }. I+ y' {, r+ V% `; f7 v# n, t r
O' s; `) R9 f2 r5 L6 a2 s; L
syncUpdateOrder ();
) H. i' I9 q) I) O& f7 `& }, s) q9 A) N* g. F- l& K
try {
2 v( v( v& ^+ Z6 W$ w4 G modelActions.createActionTo$message % y$ G2 a; v" U8 u; N2 E
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 t+ R: f- X: r0 r8 D* ^) e } catch (Exception e) {
3 t3 S0 W D2 m System.err.println("Exception updateLattice: " + e.getMessage ());
) u! o1 A8 S# S/ c# S4 S" e }
* `& G% Q' E7 A0 A2 W 5 N( l6 H: e) n0 D7 W
// Then we create a schedule that executes the
# J, k9 a8 g% w) Y; D7 q0 U5 z // modelActions. modelActions is an ActionGroup, by itself it
6 E2 k' ?9 n# k! u/ ^- c' r6 q // has no notion of time. In order to have it executed in6 ^+ F& L& a$ ? F+ g ~: c2 L
// time, we create a Schedule that says to use the
. q0 I! U- O6 b // modelActions ActionGroup at particular times. This
, Q) O8 J' l6 j( X& F // schedule has a repeat interval of 1, it will loop every" H; ^( q7 Q* ]) y
// time step. The action is executed at time 0 relative to8 F- U# }* Y% K" \3 C( f% B
// the beginning of the loop.
7 {* L# Z$ S# p j/ P$ C2 x2 `3 y% m! a9 Z7 t8 E
// This is a simple schedule, with only one action that is) J. x8 }/ r }( P4 H
// just repeated every time. See jmousetrap for more# i" ?. l. q" s( r
// complicated schedules.
% v: X9 k& ^* p ( f( n+ [; a; P1 h/ e( Y7 h
modelSchedule = new ScheduleImpl (getZone (), 1);
: f8 f* B6 K* X& { modelSchedule.at$createAction (0, modelActions);! o8 T: h$ l3 P2 s5 [' z
- h ?9 i/ D w" `
return this;; F3 S/ z( p; [
} |