HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; W6 _8 ~- F ]7 B' z$ {3 ]/ X- x* I4 v, b4 ]
public Object buildActions () {
R* r' J7 s" d$ L" u super.buildActions();
* j7 Z! ~; f# Q; O / H/ r2 ]) u$ O4 Y" {9 |( {; f
// Create the list of simulation actions. We put these in; c3 b T* N# f! Y* ?' q
// an action group, because we want these actions to be
( @- h: I( u" b. ~* a+ Y# X+ G- K // executed in a specific order, but these steps should- `8 A" Q) `, J( `: M: T
// take no (simulated) time. The M(foo) means "The message$ b# H9 E& U0 l6 N% w% u3 P
// called <foo>". You can send a message To a particular
# T1 X; P! J1 {+ i d4 w T( t6 A // object, or ForEach object in a collection.
. h. `" \) a, `, u+ A
. \9 p" z5 H7 D // Note we update the heatspace in two phases: first run6 d2 S9 x4 f% ?% ?8 `% h! D1 c& v% T
// diffusion, then run "updateWorld" to actually enact the: f+ t* {' c# K0 r# K5 L% K! }
// changes the heatbugs have made. The ordering here is
5 P! d( A5 r; ?( [ // significant!- _+ {! A& {9 Q1 H: c8 z
$ |5 q7 F. ]6 y8 G! t* p. G& F. R
// Note also, that with the additional, o. ?* f6 [, L, E" C$ ~# t& X
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ y4 c! t# [( x: S T* a // randomize the order in which the bugs actually run
. j% t% A8 d. u2 H // their step rule. This has the effect of removing any* Z' [6 b$ a, l" z% I' s* W
// systematic bias in the iteration throught the heatbug
9 v1 H3 l; K7 L // list from timestep to timestep
3 E9 ^+ i- v ~0 ?; m+ P$ W2 ]
$ Z' }* z& E7 U2 L // By default, all `createActionForEach' modelActions have
' ]9 s2 m9 t' b) E* k8 ~ // a default order of `Sequential', which means that the
3 s( }! u) l8 a // order of iteration through the `heatbugList' will be% M }. L1 q' K' y O, t
// identical (assuming the list order is not changed
7 [3 J4 O" b: j* N8 a1 S! Z0 o: H // indirectly by some other process).+ k" }+ ?( m* \ ^
, f0 P5 H. U0 k. L
modelActions = new ActionGroupImpl (getZone ());
: \* v9 e5 j1 N# }- R% u3 g" u; n( f: T4 R. V% R, l# S! I7 T$ v6 }
try {
, C8 l1 W) \8 p modelActions.createActionTo$message" c, \9 W* b9 e8 L7 a4 P a0 b Q
(heat, new Selector (heat.getClass (), "stepRule", false));' t/ h) v t& G! \
} catch (Exception e) {$ ]. l( v: ^; B6 `+ b4 i
System.err.println ("Exception stepRule: " + e.getMessage ());) c w" m) f! K4 a
}+ l" A0 p* d N& C& \1 V
7 y* x/ h3 w5 b+ [
try {
! |* E( {% A( Y7 Y Heatbug proto = (Heatbug) heatbugList.get (0);! @, ]3 z& t7 x* d! j0 n& c; i2 ?
Selector sel = 7 [' H5 h1 l+ q
new Selector (proto.getClass (), "heatbugStep", false);$ s( X3 b! U/ |% [" O
actionForEach =
7 N4 y& | p! \" G) y modelActions.createFActionForEachHomogeneous$call
& g/ c' Q" V8 F4 O% @ (heatbugList,
6 S1 J2 @, V" c new FCallImpl (this, proto, sel,
3 P- x. W) E6 X' D. T new FArgumentsImpl (this, sel)));7 s3 U3 v Y d; i, `$ X1 }$ v
} catch (Exception e) {* y4 J9 y7 R+ H7 P$ i8 V' }# X
e.printStackTrace (System.err);1 `% l: t& D" F: z1 B% _
}
3 y0 r/ w( S- ]/ }( }2 R
& T2 y$ ^( o; [ j* i syncUpdateOrder ();
: B9 F. G1 P- G9 t3 q& u& b6 G; s* D0 ?- O q& y9 G9 _
try {
" z) _& R% t7 W' f, r- N% c3 W modelActions.createActionTo$message ' q9 T' g% {- N: `: d$ k
(heat, new Selector (heat.getClass (), "updateLattice", false));0 y$ `+ y2 R4 t; [
} catch (Exception e) {
/ x7 r7 b+ y8 c8 E, Y s System.err.println("Exception updateLattice: " + e.getMessage ());
& p) x0 ^# ~( E& j7 s }8 b! u: {, j# S. E; f
; p/ k- ?1 k7 `3 S; L" _! S
// Then we create a schedule that executes the
1 v Q( I$ O0 U) u; w // modelActions. modelActions is an ActionGroup, by itself it
3 V$ Y$ q8 X* |1 [ // has no notion of time. In order to have it executed in
7 l0 Q6 ~ g2 J9 w! s- b // time, we create a Schedule that says to use the
% h& V5 J8 z0 S, c* d1 O // modelActions ActionGroup at particular times. This6 v+ G) f% Y ~9 F6 g! E
// schedule has a repeat interval of 1, it will loop every' ]; v [. f F% _0 H6 U2 D+ w
// time step. The action is executed at time 0 relative to
/ f9 ?1 S6 H0 {% U // the beginning of the loop.
& o4 B- B. s O( J2 h+ T
- f" i8 ]' @3 D: b! o // This is a simple schedule, with only one action that is8 d O% u# W' L8 n8 ~, l
// just repeated every time. See jmousetrap for more
; y7 E# `( _3 F- }& b // complicated schedules.
: o8 I' a2 t' [ & C8 J, s5 ?9 m% _+ v. d7 k
modelSchedule = new ScheduleImpl (getZone (), 1);
( t, v( y: j6 a' l0 D2 Y+ D' y modelSchedule.at$createAction (0, modelActions);
/ q5 f: Q+ h( J( Z/ y Q9 F4 `7 N8 d: d H
return this;
6 X/ @# ?' S! v5 d! n/ u7 k } |