HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; X9 |" p8 {( @$ {) Z
- {* ?3 a& g* y1 [% S public Object buildActions () {6 {! V& w8 N8 f( E4 Q3 a
super.buildActions();
& L' t5 D: P9 f, p
( ]+ N6 _) c1 W% y: ? // Create the list of simulation actions. We put these in
6 c. @) r6 T6 t: }( d6 ^ // an action group, because we want these actions to be, L* K6 ~5 d: a6 o% c
// executed in a specific order, but these steps should
' F3 r; `" E) z: W+ c( M2 m& X // take no (simulated) time. The M(foo) means "The message
7 m7 N3 E: v9 j8 G3 U // called <foo>". You can send a message To a particular- n$ h7 `. T, H( m: w2 H: a5 R# l
// object, or ForEach object in a collection.
! U0 ~% F# v; V 2 q, _ z6 Q7 A* i9 \3 B
// Note we update the heatspace in two phases: first run
& S" l4 r6 t0 ~7 a! N+ v3 A) U // diffusion, then run "updateWorld" to actually enact the. {7 Z7 Q1 C+ ~" \7 Z6 k6 B
// changes the heatbugs have made. The ordering here is1 |& ^/ n8 w# R6 f A8 ~) r
// significant!
' O/ c1 P* y% \1 _+ P' N1 `$ I ; ~6 m9 c+ A0 M% ^0 e
// Note also, that with the additional1 b: w% x. n6 d/ m
// `randomizeHeatbugUpdateOrder' Boolean flag we can# g% B: f& p. C4 ]( Z
// randomize the order in which the bugs actually run
, h. l- I# _9 f% S; Y+ h // their step rule. This has the effect of removing any% C5 v$ L$ {% _3 k" Z$ g7 C
// systematic bias in the iteration throught the heatbug
1 ?9 w1 A7 s7 S8 X, g // list from timestep to timestep
+ w# @- a) ?8 Q/ d# K( {
! I& V, L" @7 y7 ~ // By default, all `createActionForEach' modelActions have
( |/ _3 c% j0 n- m5 S9 X // a default order of `Sequential', which means that the
4 \+ B* R; E; l1 p // order of iteration through the `heatbugList' will be
1 _* J$ c3 J7 A8 N( } // identical (assuming the list order is not changed
7 c% Z9 j+ n2 x. K i3 g) k // indirectly by some other process).
" b/ k! T- D2 @# P , D9 V7 G8 B8 e# U) \& h
modelActions = new ActionGroupImpl (getZone ());; C' _/ ?6 C6 i& ~% Y
# {' ]; U4 m( N( ^" ^3 i/ o) ^
try {, }4 w) ~) }3 H* J! j4 J. l' n8 w
modelActions.createActionTo$message
7 J+ ^+ A F L9 S' u: m8 S (heat, new Selector (heat.getClass (), "stepRule", false));5 p& H( }. K4 M+ ]* H
} catch (Exception e) {
$ U0 _" K' n9 o; C; x System.err.println ("Exception stepRule: " + e.getMessage ());& I! c# C+ x& P$ y% [+ M
}
. X/ Q1 k0 { W' {2 w) Z$ j
. O& x$ ]' ^ @( E* Q$ f try {
# t: `" R" Q. r( z& g Heatbug proto = (Heatbug) heatbugList.get (0);
6 y' \& T) z' M1 m- _. p Selector sel =
6 E6 N- I; V" j2 u# c new Selector (proto.getClass (), "heatbugStep", false);
9 k. {: O. y4 `; g' X* P: T actionForEach =
! l; ~( t+ y) J9 t modelActions.createFActionForEachHomogeneous$call
% \. G1 |0 M* E$ V7 z6 m (heatbugList,
6 D+ e2 X) k/ H2 C7 g new FCallImpl (this, proto, sel,
9 h% {& s, ?. W( l& u new FArgumentsImpl (this, sel)));$ t* p2 k( y6 z
} catch (Exception e) {% ?( U' W9 _+ x! v* E3 G5 L
e.printStackTrace (System.err);
' `, m8 M8 u- A: \ }+ f, U- R V$ ?: a
+ t3 u9 G0 R' b. f, s% i
syncUpdateOrder ();5 _' j+ D: I1 h/ m- H
7 {( f+ w# x: J. l try {6 _$ [0 @4 N0 z- G0 J! i
modelActions.createActionTo$message & \ j3 e. }* O( g8 _
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 {( _; r9 j! q: b: v" n- Z } catch (Exception e) {) {5 p$ Y; Y8 k6 X5 Q
System.err.println("Exception updateLattice: " + e.getMessage ());* {2 A6 B, F/ g u8 ^/ g f
}
4 r( o0 Y3 P- [9 V6 z9 A: x% q" O
! O1 A: o* r& S // Then we create a schedule that executes the7 r" \0 h$ W1 ^- ~# K8 Y1 _) t$ G
// modelActions. modelActions is an ActionGroup, by itself it$ A/ {+ W$ h# `$ X4 `1 \
// has no notion of time. In order to have it executed in
; `8 J9 R& f. M/ ~% M( c( z5 x // time, we create a Schedule that says to use the
# O$ L/ W6 z: Y P* d( Y. { // modelActions ActionGroup at particular times. This
5 k" A# w3 i [% k // schedule has a repeat interval of 1, it will loop every G# V4 d; o3 t [9 t K
// time step. The action is executed at time 0 relative to
! k, N- Q, E8 J1 P5 f7 `: H( C // the beginning of the loop.
. P: l; |7 [, _
. a) l5 F) s! e" z2 Y. Q // This is a simple schedule, with only one action that is
1 p, [% G, e1 J2 c4 `+ K7 s // just repeated every time. See jmousetrap for more) p) r3 P# m. E! a- H; g
// complicated schedules.4 i1 @% F. z, a# O8 m) ^
8 P, Z, A) M, e8 o: Y
modelSchedule = new ScheduleImpl (getZone (), 1);) }% c- L* ?( U7 s. k( Y8 E2 v
modelSchedule.at$createAction (0, modelActions);
0 s9 g! r2 ?1 G% K4 ~
3 n- e& ~0 C( r M8 S return this;- g: o. C5 R" }+ P7 G j
} |