HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 M' e3 O& f8 c1 V- v' B
) j; P/ j; ]; Y6 n public Object buildActions () {
9 R$ P1 X7 Y) [5 T/ |9 q1 |; Q super.buildActions();9 z" Z! E: L6 z: ^1 R+ Q
: d3 h0 i$ S5 W& \# j9 g
// Create the list of simulation actions. We put these in8 |" [, n& w# P1 G' g/ N6 L
// an action group, because we want these actions to be) M4 ^ A0 H! B
// executed in a specific order, but these steps should: N9 C+ u4 L6 F/ G% q
// take no (simulated) time. The M(foo) means "The message
7 \2 c9 r6 l+ h8 m/ W6 t. ?* V // called <foo>". You can send a message To a particular+ i8 Q) O& i! ]8 X
// object, or ForEach object in a collection.
4 B; E# b: h5 a6 N9 Y5 B+ J" g
2 o- t3 _& { T$ ?% J // Note we update the heatspace in two phases: first run
+ o( t0 i' j: R- t // diffusion, then run "updateWorld" to actually enact the( [0 l) a& ^2 C7 q0 v% w$ Q- F
// changes the heatbugs have made. The ordering here is
% V0 H8 M. Y/ J3 E) N // significant!' n. i; }% L2 B9 v, d
. N4 `. ~3 T7 `$ O% M // Note also, that with the additional3 A8 H, D3 Z' l2 ?- ~ l4 I' W* R1 a+ F
// `randomizeHeatbugUpdateOrder' Boolean flag we can d- w# o, j3 V" A G
// randomize the order in which the bugs actually run+ C( G& [, [" }" p3 m
// their step rule. This has the effect of removing any( g7 z: G7 y& W1 g$ Z
// systematic bias in the iteration throught the heatbug) x% i/ A$ l1 J q5 Y2 J
// list from timestep to timestep1 b7 c/ @8 H% K6 G! N8 c3 k5 Q7 U
2 y1 g+ J% `0 r // By default, all `createActionForEach' modelActions have1 ]: V- w2 b9 m( Q6 e+ u3 N4 ?
// a default order of `Sequential', which means that the
. G* p9 [* i4 c9 s3 E2 U // order of iteration through the `heatbugList' will be
( ?# q# I, F& T1 ^# P! h* D // identical (assuming the list order is not changed3 G0 r" M) Q7 _" z+ N+ `6 C7 \5 O
// indirectly by some other process).: y5 V6 D R/ B; ]6 t0 g7 u+ F
9 X) K+ H( V1 S9 v/ H1 K. R modelActions = new ActionGroupImpl (getZone ());
+ N$ M g2 `5 ^% d0 z1 J4 }; {1 S6 b" j m* w2 a
try {3 `3 n/ I* E& e4 l
modelActions.createActionTo$message
. h# C5 l n$ c0 j8 s- ^% A (heat, new Selector (heat.getClass (), "stepRule", false)); V' E2 @3 A6 e2 O
} catch (Exception e) {
* A" _/ I$ H( ]3 Y% f# G System.err.println ("Exception stepRule: " + e.getMessage ());
5 d9 V" A' N; z6 ?5 E }
P5 N; P" K1 w& Y
O7 O2 n/ g6 R( w try {" V% e; `$ s$ y& A0 e. K
Heatbug proto = (Heatbug) heatbugList.get (0);
; R1 T |& m( q5 ~" m Selector sel = 5 G; x4 N% ]% {0 N
new Selector (proto.getClass (), "heatbugStep", false);
1 m: `' P6 I- C9 t$ `; o- ^, Y! A; O actionForEach =& E! n W' ` }- b8 R8 i0 ~
modelActions.createFActionForEachHomogeneous$call+ \+ V3 z0 ^1 |3 R
(heatbugList,
5 I$ O, c$ A. \% R% K new FCallImpl (this, proto, sel,
4 j" Q, X5 [* r% @ new FArgumentsImpl (this, sel)));
3 t) j. d) p: S. M Y$ u } catch (Exception e) {& E2 O/ E2 @9 K2 P! q
e.printStackTrace (System.err);* u" D: H7 V5 V9 ~+ s
}1 @& m. f# X- @( t/ ^
: {0 S& r- d1 k) R1 c: \/ e
syncUpdateOrder ();
% d! G) X- b. {- k
4 X- ^' J- K3 A: H, G1 i8 I try {3 |# q9 C, N& R( x2 \
modelActions.createActionTo$message ) {! j; M W; h( D3 }' S- A
(heat, new Selector (heat.getClass (), "updateLattice", false));
) e0 Y0 ?2 Y" _5 Z } catch (Exception e) {
( d, M0 o9 m* p- D System.err.println("Exception updateLattice: " + e.getMessage ());1 \) u+ F2 T' N J
}
5 G) g$ ~0 X6 k8 N, e3 T6 m
7 ]: H/ W& e& J7 N: x // Then we create a schedule that executes the
8 ^' Z5 k* {" e // modelActions. modelActions is an ActionGroup, by itself it
; J" |& \4 x: ~0 R' I, c // has no notion of time. In order to have it executed in
( K, A8 }7 g$ f0 x' m1 V // time, we create a Schedule that says to use the$ H; o4 X+ [# k
// modelActions ActionGroup at particular times. This
% Q. h0 B7 P& y* G7 D: y5 n; _1 X // schedule has a repeat interval of 1, it will loop every( G- o. [) h2 t r- y& ^5 z
// time step. The action is executed at time 0 relative to
* u# S' z% g, X' H( b% Q // the beginning of the loop.
9 @! j: m4 ]3 H' A* X0 k) B% X& I9 y
$ G% D4 E1 N: s' y // This is a simple schedule, with only one action that is
- x- x, {* s! i/ L7 g$ \: I // just repeated every time. See jmousetrap for more4 A( u# x& f" }4 d6 k5 v
// complicated schedules.
, u, o# C. A& F% U, s % O0 R" \+ ? ^) L, R! j
modelSchedule = new ScheduleImpl (getZone (), 1); P, f0 c( U8 c/ h. j; r
modelSchedule.at$createAction (0, modelActions);
) _4 i/ F i$ a
$ M' v9 _2 {. N% H e: k* P! U' A' N return this;+ M3 B. Z# t3 a6 X9 M
} |