HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 t4 F+ j/ d- `3 Z" Y; N( i
4 }% F6 j `6 x; p& {
public Object buildActions () {
5 i! M9 {8 `( Z5 i% n1 H. s* Q super.buildActions();' v2 z; E2 [6 G
% n0 |3 x- o+ [. m
// Create the list of simulation actions. We put these in, C6 J1 H2 U1 j2 F6 U
// an action group, because we want these actions to be
3 H A2 r9 G5 }, @# M4 j0 X // executed in a specific order, but these steps should2 E/ I+ Q. R' R/ L- k
// take no (simulated) time. The M(foo) means "The message
" M" K- l" a- F // called <foo>". You can send a message To a particular( ]& w" I3 x2 u) ?& F
// object, or ForEach object in a collection.
# \8 T# q4 K2 b8 a
6 N q" @/ t: ^: m0 J' \" O3 x // Note we update the heatspace in two phases: first run y K( L# r8 W
// diffusion, then run "updateWorld" to actually enact the4 H0 h' j |# P! C
// changes the heatbugs have made. The ordering here is
a0 j! Q N) d2 R, S k // significant!; o8 u# G5 d$ t, ` ~
& Y+ ^. d7 e r! f
// Note also, that with the additional0 S2 `) O% m" y, f- D- s
// `randomizeHeatbugUpdateOrder' Boolean flag we can: |2 m2 @/ q$ t- J& g
// randomize the order in which the bugs actually run
" N. H. [2 c' ?8 Z( K // their step rule. This has the effect of removing any
. ~* B# } A, l" s( H+ F" Q8 T // systematic bias in the iteration throught the heatbug/ x( j( C/ W$ _1 T: k" [
// list from timestep to timestep
7 b& @, }: A6 G; q$ |) C : G7 i$ N; K4 q# x* t/ C
// By default, all `createActionForEach' modelActions have) S( j) k3 p5 Y: f& b
// a default order of `Sequential', which means that the* M [. T! [$ r6 `8 F
// order of iteration through the `heatbugList' will be
! p$ k( `6 \: r$ z1 I // identical (assuming the list order is not changed0 }8 s1 Z/ ]' I4 g' z% R' {5 U
// indirectly by some other process).5 ]4 X/ c @0 P+ T, c' z* L
& P: e- z& o3 U1 s0 T
modelActions = new ActionGroupImpl (getZone ());
: w, m$ \% N6 K3 z! [1 I- p+ W4 V6 c
try {
7 W- W! a' P/ V8 u# [ modelActions.createActionTo$message
3 W& v% c- C, O# Z2 {, q (heat, new Selector (heat.getClass (), "stepRule", false));
# S1 I8 R0 u) d } catch (Exception e) {
" F! X9 m& f- U, M6 d1 j System.err.println ("Exception stepRule: " + e.getMessage ());2 E4 y6 k- G# o+ @# z: t) Q
}
$ @$ g+ O- x5 i# l6 Y
) ~( s$ R) \" Z) e try {
0 @* ~% V' D7 {0 \& m Heatbug proto = (Heatbug) heatbugList.get (0);2 ^2 t, h- W- q' E
Selector sel = 1 Z5 K! m# N6 L) Y: O
new Selector (proto.getClass (), "heatbugStep", false);
3 J6 q( r; b! V c3 t+ [ actionForEach =
' r/ u! J+ c: u modelActions.createFActionForEachHomogeneous$call
% q! S! Z7 f8 w0 v (heatbugList,
/ k( {: n! v6 T F/ h* t8 m new FCallImpl (this, proto, sel,
4 L# ?/ ?' l5 e6 g8 x new FArgumentsImpl (this, sel)));. ^& i3 |7 b2 f9 O! k! q6 f
} catch (Exception e) {
' p9 `" N [. ^; n' s u e.printStackTrace (System.err);2 A, L# Q2 |6 a: C; r: D9 B
}. a( E* N$ z/ \. e3 o* f% l, J
k" c- q. b: ]! U- E9 F( z syncUpdateOrder ();
5 C9 W; L$ I5 R: p7 n0 e
- d V" d' i6 L6 l: ^ try {# r5 J* c: Q# C! g3 z: c
modelActions.createActionTo$message
7 H9 Z, U/ r9 _( w/ E, P& c# Z: a" w (heat, new Selector (heat.getClass (), "updateLattice", false));
5 W8 e- ^( m# I \ } catch (Exception e) {
; L7 [; N/ r1 q System.err.println("Exception updateLattice: " + e.getMessage ());# {6 M# H7 b, g4 C5 L
}; a7 T5 [6 Z2 {. o P3 a
+ \6 ?/ W3 l, `- u5 V6 s
// Then we create a schedule that executes the
& m7 m) S3 t/ V! R // modelActions. modelActions is an ActionGroup, by itself it
" g8 G$ g, n, D J // has no notion of time. In order to have it executed in R/ n! _ n! z9 a$ s5 b8 Z% s$ K
// time, we create a Schedule that says to use the
/ q# L$ {. @/ N7 W& L8 _: s // modelActions ActionGroup at particular times. This
6 k; T5 t' p2 q* H2 i // schedule has a repeat interval of 1, it will loop every
/ M, L9 n# `/ \: d0 Z0 f' M- {% O // time step. The action is executed at time 0 relative to
1 L- d8 k! S# d0 ^ // the beginning of the loop., g! u1 l2 Z6 q0 ^
' J _: Q$ M. r) h% f. K, w# { // This is a simple schedule, with only one action that is
/ p+ s, i. N* I6 P, q% A/ b // just repeated every time. See jmousetrap for more
0 \8 Y% C7 ]+ k; ~ // complicated schedules.
! L8 `5 I& {' q' D2 i# k
& G( P3 h6 ~8 z3 J( N# ^ modelSchedule = new ScheduleImpl (getZone (), 1);1 Y1 P, l2 T2 h# A: p
modelSchedule.at$createAction (0, modelActions);0 r/ E' I1 r' L3 C& g
5 j# h( B) i( v; N2 v return this;
8 A, S1 Q4 ^9 o9 |" i( c# l! x5 l } |