HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% S2 \' `5 Z: I. C) k' i. }
( R# c0 z9 ^# |3 } B) k public Object buildActions () { r$ O! K! P1 Y8 l2 W" ~. `
super.buildActions();; y, s) q' A7 @4 s9 L0 f
) g0 n( _. |, I0 ]! h) S7 O8 w // Create the list of simulation actions. We put these in
& ^7 L0 \+ Q, h9 Z; y9 S // an action group, because we want these actions to be: n! k$ U1 ]0 J8 {' t3 p2 y- `0 J
// executed in a specific order, but these steps should. {+ D- d, E9 i" F3 z; k& }/ v! l
// take no (simulated) time. The M(foo) means "The message
) T- r' s$ f+ k7 i( p // called <foo>". You can send a message To a particular: h, H9 g" y* |0 k
// object, or ForEach object in a collection.3 s1 u* Z/ [; U) L9 D& Z4 ]
$ P& [) \# ^" i7 ^
// Note we update the heatspace in two phases: first run9 y$ A/ Y/ j. d3 }3 I0 }
// diffusion, then run "updateWorld" to actually enact the3 q5 f2 L x( ]* G) t( A4 M
// changes the heatbugs have made. The ordering here is
" L5 A" V0 U' i4 J5 g8 ~" d // significant!
, U/ c+ Q& |( z8 L/ F5 X9 A0 W% v 3 L0 y( c- k( D. ^/ j8 N
// Note also, that with the additional; d& L* _# c" ?& \
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ H P: ^. _' w5 m: q: z/ }- q
// randomize the order in which the bugs actually run
8 D% f% t' k- P/ w6 Z+ p$ c) Q // their step rule. This has the effect of removing any% |( b, o" Q9 n+ x) s: d
// systematic bias in the iteration throught the heatbug! j; P9 i, P2 k6 O6 Q$ D/ ?6 K
// list from timestep to timestep. K6 Q3 i; E+ r9 ]% r
+ K1 u; e( i. g: l+ _' d' K
// By default, all `createActionForEach' modelActions have
/ }+ I' l8 l* h // a default order of `Sequential', which means that the- G/ w3 e L8 E; e! D+ P, a4 I
// order of iteration through the `heatbugList' will be
" j% \5 W$ K/ L" I) I // identical (assuming the list order is not changed
' ?7 X% b" E& Q8 Q8 A6 D // indirectly by some other process).
& w3 r" W* D; C+ r 4 O" X5 T! k1 _/ b! m7 o# |
modelActions = new ActionGroupImpl (getZone ());
$ m" l& k9 ]% w+ u7 h1 n5 _, w& h, A3 I
try {# ?4 E: J$ }, S4 ~5 H h
modelActions.createActionTo$message* H+ ^" c# P( d$ |8 f- C) R2 w
(heat, new Selector (heat.getClass (), "stepRule", false));# r' H) w/ F* g: |( ^
} catch (Exception e) {5 r3 X& ?4 X T5 `5 W: @
System.err.println ("Exception stepRule: " + e.getMessage ());
! E0 `1 m1 p& [' s0 c4 _ }. ~ H& j# s& \, I! R- `
7 n( s# \0 U4 P y: `
try {
' M: N% D$ l3 ~2 F0 P% k Heatbug proto = (Heatbug) heatbugList.get (0);- q+ x0 E% `% n
Selector sel = : R" U ]. m# F: P6 T1 k
new Selector (proto.getClass (), "heatbugStep", false);
5 B2 Y: C: n( f/ l* m- \ actionForEach =: V6 t) k" T% }" r! y
modelActions.createFActionForEachHomogeneous$call9 J/ M; j( g" t/ t, q
(heatbugList,- t8 R. ]' P, t, c8 x
new FCallImpl (this, proto, sel,& F- l6 ~: d( p+ P) q! C% n- H
new FArgumentsImpl (this, sel)));
0 {5 y) g) y+ R+ R) o ?2 O } catch (Exception e) {
4 v# a& ]8 y. i, V7 e3 r e.printStackTrace (System.err);$ _- {+ V3 `: z3 M4 o1 W
}2 Z6 C: y3 |( M' r6 l
# z) }# z( W6 B9 z6 \; ` syncUpdateOrder ();
8 D+ r$ L5 H+ J' [1 }$ [- p6 e' b3 X' g& m, L2 N1 i" r9 a
try {# H5 Q4 Z1 }+ b" w. |, {/ p
modelActions.createActionTo$message ! g* }1 ?2 A% W" x
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ P$ O# h( V9 q7 R" m X7 y } catch (Exception e) {' s& _ E$ U! S) q* Y5 h6 o4 D
System.err.println("Exception updateLattice: " + e.getMessage ());; P8 u2 g" |) K6 Z
}1 v' X) F# i0 z( b+ v, w
8 x* r( @, y0 h1 n0 z // Then we create a schedule that executes the
8 n9 _4 ~ r" j+ G. D* ^9 c) j1 M // modelActions. modelActions is an ActionGroup, by itself it5 O. _7 K' d4 I8 W" s0 J3 w" a
// has no notion of time. In order to have it executed in! S+ f, _* U- H" h' x3 S& B
// time, we create a Schedule that says to use the
S" O6 b8 _, L( f5 E" y // modelActions ActionGroup at particular times. This/ P1 a2 k+ L, P9 j' ^
// schedule has a repeat interval of 1, it will loop every
! o% E+ g; P) _# K+ d // time step. The action is executed at time 0 relative to3 ` S& B8 ?; s' N9 n% j. Q/ a
// the beginning of the loop.8 _8 w K s: y7 }2 ?/ W, f
8 o' b+ h* D3 H" q0 K; C
// This is a simple schedule, with only one action that is
( n W6 @ \5 s' u q- H5 Q* U // just repeated every time. See jmousetrap for more9 O6 Z' F' R( ^1 c3 l: T
// complicated schedules.
5 U5 m/ T0 S5 {! L" R6 `& M : f6 O% P: t8 X p- e
modelSchedule = new ScheduleImpl (getZone (), 1);
9 W/ P! o$ u. M, P/ S modelSchedule.at$createAction (0, modelActions);9 R& r1 I0 o) {2 c
! c0 [. [" _5 I+ H return this;! ^0 J' S3 f8 l1 o
} |