HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, Q. T1 z4 a3 I( `4 c
& r2 S4 M' \9 ]" r; e1 k, ?* A public Object buildActions () {
. N; Y. c1 y& \+ R" I- g: k2 T super.buildActions();
: L/ P8 G6 Y0 C 3 t" F* w! \# R @
// Create the list of simulation actions. We put these in3 f& y# i$ e6 v$ b
// an action group, because we want these actions to be" x R+ H6 X; e/ m' a5 c
// executed in a specific order, but these steps should
( g- {) V: u1 z' o& a1 u // take no (simulated) time. The M(foo) means "The message
9 P% f- u/ K4 D) y9 J9 n$ [; t // called <foo>". You can send a message To a particular$ o+ E3 e5 j5 H% N
// object, or ForEach object in a collection.
8 e/ ]( l& `% M' e/ q : R" N$ q. _/ w1 x" l
// Note we update the heatspace in two phases: first run, P" k8 R% U3 F+ {
// diffusion, then run "updateWorld" to actually enact the
0 \& Z1 s6 h6 E( ~6 e5 H // changes the heatbugs have made. The ordering here is- x8 [ ~& D) v5 j
// significant!* g" J8 q$ F* }. E. P: N
- r$ s$ @9 u, w: F
// Note also, that with the additional
1 i& v1 f5 |; N: F( I1 t0 d // `randomizeHeatbugUpdateOrder' Boolean flag we can( |4 U: `! _/ O7 W& M8 U4 w
// randomize the order in which the bugs actually run
4 S. D/ c6 t2 U; H // their step rule. This has the effect of removing any
) D9 U* n J' ~4 C // systematic bias in the iteration throught the heatbug
3 [* z: h6 d2 h8 ` // list from timestep to timestep
0 v; m7 T! I2 ?: I9 H% y 4 _ I9 q, j+ M2 ^" S
// By default, all `createActionForEach' modelActions have( L% C% `( \: p$ `9 b8 B
// a default order of `Sequential', which means that the7 \3 x p* ^ ~* X0 K$ H1 V, Z1 C
// order of iteration through the `heatbugList' will be/ H2 w' a% D; T" v {+ f; f: I8 B
// identical (assuming the list order is not changed q C9 |3 ~7 O' R2 n8 Y
// indirectly by some other process).
# G1 V2 f+ H, o% f! n& O
" ~, J' f- t! B9 q) _ modelActions = new ActionGroupImpl (getZone ());
2 @4 u! c6 I& J
! T( ?) ~* W" l& r% o! S) l8 y try {
: h% J1 Y7 [4 F modelActions.createActionTo$message3 ~) i' f M# k, v4 W" y' M
(heat, new Selector (heat.getClass (), "stepRule", false));( ]4 d% z2 o/ L( ^; ~
} catch (Exception e) {
! F( @* t( \ [. e3 U System.err.println ("Exception stepRule: " + e.getMessage ());
/ W# w2 K6 l/ q5 n6 t9 K: d2 n }
" p& I6 T6 H7 Y
; S6 C3 a% f& t9 o' ~% e2 a7 V9 {4 } try {
$ ?# t, M& s' Z- R Heatbug proto = (Heatbug) heatbugList.get (0);
+ J: k/ U/ e& ~" R Selector sel =
$ K5 U% x$ j3 Q: `) ] new Selector (proto.getClass (), "heatbugStep", false);
; o1 x/ E7 W* c3 y actionForEach =
$ o z: v% d8 C `1 ~ modelActions.createFActionForEachHomogeneous$call
! ^1 Z# t7 V8 L& I3 }0 u (heatbugList,
* M/ Q6 i8 X5 U& X new FCallImpl (this, proto, sel,; v9 D9 g. \. Z
new FArgumentsImpl (this, sel)));8 i7 I" o- d8 i
} catch (Exception e) {1 {+ `2 Y' w `0 s8 K; N' i/ N C
e.printStackTrace (System.err);' R( G5 S! N$ N1 N# r
}* W! a2 n9 Y p! V
6 A- ]2 b( _0 ?+ p- L' U5 `
syncUpdateOrder ();% y# ]0 i% B1 s+ x/ d
! g7 F% o4 ?9 f g e. P' r* ]
try {
- v- g. I3 b8 H modelActions.createActionTo$message $ {0 m. s( A- G; c3 P( {9 x/ f
(heat, new Selector (heat.getClass (), "updateLattice", false));
' [" U3 O7 N9 `" G! G } catch (Exception e) {
0 b$ ^* L' Z- b5 G* Y- @" w2 I System.err.println("Exception updateLattice: " + e.getMessage ());
, c; j7 I& E. F$ a, S; d }
! `& M$ y7 D; |* q # Y. Q$ Q# @" B( A# D% ^3 X) D
// Then we create a schedule that executes the
( \4 j/ b0 m. P$ F9 t! @( h // modelActions. modelActions is an ActionGroup, by itself it' Y2 B- P0 |9 v: J
// has no notion of time. In order to have it executed in k. `6 i& q; i
// time, we create a Schedule that says to use the8 L+ B4 s& ^2 U4 U' a# i
// modelActions ActionGroup at particular times. This+ ]1 R' C6 N: ?4 p+ o
// schedule has a repeat interval of 1, it will loop every1 K e/ M' Z0 b$ w3 [& ]
// time step. The action is executed at time 0 relative to5 e1 ~; {" T: a" U6 G2 j
// the beginning of the loop.) T6 j3 J8 s- C" F! [( H2 V
6 l0 E) B2 h) R$ B. V! O$ E I
// This is a simple schedule, with only one action that is8 v% P2 x! n3 C D+ I5 n( z
// just repeated every time. See jmousetrap for more
" a2 W; \- \9 W- @* x // complicated schedules., h8 @+ f/ M4 }8 A- r8 Z
% w& X) d7 U1 l% w# F- v& t modelSchedule = new ScheduleImpl (getZone (), 1);
, J3 C$ z& h z) Z R% R( o modelSchedule.at$createAction (0, modelActions);
: I- |7 }/ j1 x4 y( b- b% \ 5 b& Q1 k( O/ h, b: O6 ~# V
return this;" c# ]# ` g! [. O. W
} |