HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 O4 e* d8 E$ k; V* B! m
4 c6 h0 C( [ ~
public Object buildActions () {
; v( Z5 v) C7 X1 X/ t g7 I& c super.buildActions();$ S* o4 Y# `! H$ j- a
j( \4 ]" U3 K! x6 s9 A% B+ V" z# H p
// Create the list of simulation actions. We put these in8 E/ Q% C$ X' c5 Z6 I6 F) k
// an action group, because we want these actions to be1 M1 l9 Z: E1 Q$ |# L* r+ M; t4 F
// executed in a specific order, but these steps should* M3 M" k! p- J" p& x/ q3 e
// take no (simulated) time. The M(foo) means "The message
- b, ~9 D+ f; l* a* P$ v' B, ^ // called <foo>". You can send a message To a particular7 t B1 G( b( z
// object, or ForEach object in a collection.
1 g) R: x2 Y @, Y& Z
' F8 u9 ^. R/ C. E; f: p' T! k* n // Note we update the heatspace in two phases: first run
0 j/ ?2 s* u" O! j( q! z u$ q // diffusion, then run "updateWorld" to actually enact the/ k0 x4 R- F! |) @5 o
// changes the heatbugs have made. The ordering here is
! m& P7 d* {5 _1 c // significant!
1 q7 u |3 d& I9 T
o+ q9 A: H3 Y. u2 c( s // Note also, that with the additional
0 T2 ?/ b& \7 Q8 M* [ // `randomizeHeatbugUpdateOrder' Boolean flag we can
% ~" |% y. O0 Y0 `- `7 o9 O // randomize the order in which the bugs actually run9 h5 F0 @: S) |3 B, @! ]
// their step rule. This has the effect of removing any! _. ]/ ~& {. i: R' Q+ i
// systematic bias in the iteration throught the heatbug
% t4 t8 o* k X) Z // list from timestep to timestep/ ]: f2 y+ h% T0 F
/ x6 d" f# D. V3 ~0 s: N* y' I% Y( D% ]
// By default, all `createActionForEach' modelActions have0 U) h' p" ^: n
// a default order of `Sequential', which means that the2 C% [5 m& }( q0 x; p7 L9 U- U
// order of iteration through the `heatbugList' will be
U. e+ d5 s$ X // identical (assuming the list order is not changed! V- A: I8 I' Y1 H# t/ W
// indirectly by some other process).6 h9 h3 C9 z" w$ D3 G' }$ i
) m7 z9 r5 K( S
modelActions = new ActionGroupImpl (getZone ());2 U) S7 s# v M. @% g g
& S+ l' f% ~+ p( f6 M try {
( ?. @+ R' @' Y0 X modelActions.createActionTo$message; n& d6 [8 T' J; j- S9 P' l
(heat, new Selector (heat.getClass (), "stepRule", false));/ N$ s7 g3 P' @* V d# x% O! S; L
} catch (Exception e) {
6 Y3 a8 o6 V0 H% T System.err.println ("Exception stepRule: " + e.getMessage ());" W b5 R. H( o L% r3 j
}
* z- }( W. n% {$ e/ j! I
3 f6 f5 ]% A2 }3 e try {) k5 q6 O/ h; t9 R- Q& ^+ ]* G
Heatbug proto = (Heatbug) heatbugList.get (0);$ J. _6 n9 h) _+ G/ G! k1 [ s1 e
Selector sel =
8 i3 D, ?% K" i* s+ E; P: h. k0 { new Selector (proto.getClass (), "heatbugStep", false);# N C# E. _! @0 D+ M' i' z' B
actionForEach =5 w S) S7 C* B, `+ w6 D0 N$ g5 a
modelActions.createFActionForEachHomogeneous$call
8 k; T. y* B6 ~3 V, o0 s (heatbugList,; p: V# B t2 ^! O8 C
new FCallImpl (this, proto, sel,
$ |. c1 ~. j1 _5 _& l new FArgumentsImpl (this, sel))); e! f7 Z( J7 z3 j$ S
} catch (Exception e) {; O# F M( a& u" y' k5 y( H$ X6 j
e.printStackTrace (System.err);; D$ S. U2 `0 I" F- {8 Z8 }0 O
}
; b- ]$ W9 B+ S( k8 f0 Q, S
8 t+ b5 j2 g( d5 k" u$ J syncUpdateOrder ();
! u/ s4 P7 E s" D2 }3 d
6 Q' g! h6 ]9 z; r9 P- p |6 Z try {
. E2 \. W4 E1 W. w, T# c modelActions.createActionTo$message # {0 t- s6 |: |$ r O' i" D0 d
(heat, new Selector (heat.getClass (), "updateLattice", false));
( e* P& C. M; H9 _0 S; L8 E2 Z } catch (Exception e) {
: J- I, ?4 i; E3 W$ ? System.err.println("Exception updateLattice: " + e.getMessage ());
- X! O Z# v/ }7 W) g }
9 J3 n: ~0 U+ l # `7 F3 |! l. ^! `/ A- |
// Then we create a schedule that executes the
/ u% V. w5 m! r // modelActions. modelActions is an ActionGroup, by itself it
8 f9 q/ \1 a: C/ U( _1 N' w // has no notion of time. In order to have it executed in. s, Q' ?3 E( L; b2 Y; s5 h
// time, we create a Schedule that says to use the
) H3 B. }" f- X6 c( ^* m- O // modelActions ActionGroup at particular times. This
' O' T) d+ Z& O // schedule has a repeat interval of 1, it will loop every
: W) S% q6 N- V9 G& P& k) ` // time step. The action is executed at time 0 relative to4 s2 {8 d$ Q3 s9 `
// the beginning of the loop.# d( @7 e$ k1 e1 R5 ~4 m
- g2 W: v) a0 {
// This is a simple schedule, with only one action that is
0 M/ m. {5 T4 P2 q // just repeated every time. See jmousetrap for more& }4 E5 L n `/ B/ a3 G+ b
// complicated schedules.$ e5 q$ `* S! n
( c5 ?4 U. ^/ \" }9 l
modelSchedule = new ScheduleImpl (getZone (), 1);" k) `8 @) p1 O7 G5 t& j
modelSchedule.at$createAction (0, modelActions);& `5 r" d$ ?1 Y6 q
, G6 V3 R3 I2 q6 W- G: P
return this;
. Q, g/ A+ l/ K! Q } |