HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# g6 u! q Z; ?9 b* x# ?- X9 v. U: I, j1 K3 ^ @8 A
public Object buildActions () { v$ E# o; @& o; O2 ~1 T
super.buildActions();
2 M. N' t3 O4 y! L3 y! Y7 [4 `/ P
b3 G w4 ?4 o. P2 u // Create the list of simulation actions. We put these in
' s1 c! T6 c+ E& { // an action group, because we want these actions to be: Q! X w( r+ d9 C9 p
// executed in a specific order, but these steps should
# h$ l1 l1 M8 y$ u3 O3 f; ?/ V) B e0 k // take no (simulated) time. The M(foo) means "The message+ n0 ~* _ g, t: Y0 i. d, p
// called <foo>". You can send a message To a particular
3 s( X+ w& b7 W2 z9 @ // object, or ForEach object in a collection., J6 u0 E. u2 G( G% ?
5 ?1 O5 @, H0 P2 g0 I6 }6 {
// Note we update the heatspace in two phases: first run0 P& L3 N* w& ?2 B
// diffusion, then run "updateWorld" to actually enact the8 I A( m7 Z* B" r. L
// changes the heatbugs have made. The ordering here is
( n1 i9 H( n& t, t: | // significant!& K; O9 w8 ~$ ^! j ~
, T/ u) U. o) t! I
// Note also, that with the additional9 K0 K: ~& q8 w5 J- L1 |
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ y9 q u$ D1 \# V7 Z // randomize the order in which the bugs actually run
1 T1 t% t1 m6 W1 ^. Z' P // their step rule. This has the effect of removing any" v1 s1 f# I! Y5 e8 o+ ^* q
// systematic bias in the iteration throught the heatbug
5 f9 t: P1 [+ Z" }4 ? // list from timestep to timestep0 \' S$ ]: I8 Z, \ }
% F( j. ~) X) c5 p) a3 A8 J. D c // By default, all `createActionForEach' modelActions have' q/ c( Y9 c5 N: D" c2 w3 o/ K$ u' e
// a default order of `Sequential', which means that the$ x$ G, b {6 b5 K$ @5 ^
// order of iteration through the `heatbugList' will be
% b% D$ |7 Y. m) z. \, u3 D // identical (assuming the list order is not changed
# P8 W6 g6 ?) b( m; F! E6 ? // indirectly by some other process)./ |( ^* ?! @8 m: T
: w' D) h$ M2 g5 ^ G( f
modelActions = new ActionGroupImpl (getZone ());
. L7 y* R( H3 ~: W* X% _/ L4 U7 g% X8 `
try {
' J2 Q, H8 C5 l) I1 _4 w0 x modelActions.createActionTo$message
3 I" d) X+ s* q/ ~, u* M (heat, new Selector (heat.getClass (), "stepRule", false));, y0 z0 k; h1 V
} catch (Exception e) {
! q) L$ L' v% u7 c1 E' L6 X1 y System.err.println ("Exception stepRule: " + e.getMessage ());
5 {1 C* ~( j# p$ V$ Y }5 N; |( r& W; ^. q' {' a. B' ~% `
4 S* C* h* e3 {# K# k- T
try {
z$ \- d! F; s, s8 Q" {: l Heatbug proto = (Heatbug) heatbugList.get (0);
, }1 |- w0 g# Q9 Y: } Selector sel =
" a( {# q! e+ J7 C new Selector (proto.getClass (), "heatbugStep", false);
6 ^; M( K, H3 n actionForEach =: i0 _1 U d# W" G
modelActions.createFActionForEachHomogeneous$call! f8 [1 A- @- z+ j( C
(heatbugList,2 o0 n+ Y6 F0 h" z8 j4 n
new FCallImpl (this, proto, sel,
0 A+ u4 @; @% N' l% O' W& f new FArgumentsImpl (this, sel))); a) f* o ]0 I; p- r& {. T
} catch (Exception e) {4 m0 [& z# s1 Q& ~& T2 J" A6 C
e.printStackTrace (System.err);
3 V, b% V$ ~9 U' M, R. ` }+ V7 n' @. ?8 g6 ]% S7 a
3 r0 X# H1 w2 u" I syncUpdateOrder ();
: Z% s8 I/ b# d, t3 v' i
5 j" Q$ S: |* ^4 k! ` try {% R1 @, O7 Y+ r- I# l
modelActions.createActionTo$message
, E, f7 X0 x: M( `. P/ _ (heat, new Selector (heat.getClass (), "updateLattice", false));
2 f$ f9 r4 w4 S6 s0 f/ i } catch (Exception e) {
3 w8 ^' S6 H6 \2 ^$ ~& r! D: Y& J System.err.println("Exception updateLattice: " + e.getMessage ());: \6 J# p* o( x# f7 N% Q
}
( g) [3 m) o) {
3 R$ @ k& ~- J5 E- y // Then we create a schedule that executes the* `' n% \( r+ z/ _: Z
// modelActions. modelActions is an ActionGroup, by itself it4 I6 X1 I' e+ y5 C( M$ h/ h
// has no notion of time. In order to have it executed in
! h3 R! l: G; t) f7 Z // time, we create a Schedule that says to use the
; [* Z; g! T2 Z, c* b! T // modelActions ActionGroup at particular times. This# X# Y' ?' n; _
// schedule has a repeat interval of 1, it will loop every
0 h1 d6 n5 w6 m; d // time step. The action is executed at time 0 relative to
: a/ V' O% Y8 y // the beginning of the loop.
7 F& J+ t8 R) M1 c+ m& D1 F: F8 R5 {1 s
+ ]( y9 b5 E+ K: P // This is a simple schedule, with only one action that is, y% B& @! Z+ d( N$ C9 B% O
// just repeated every time. See jmousetrap for more
$ F5 m8 E) A5 f# N: G5 j. F // complicated schedules." a z* I% Y4 K3 I/ x( ~
6 r' u. A; r }+ B) m( m modelSchedule = new ScheduleImpl (getZone (), 1);1 b; ^- }5 g7 r% g
modelSchedule.at$createAction (0, modelActions);1 ?/ ]4 ?/ G( s1 _1 s7 i& h6 D3 f
! R; d/ P& F' D1 v& g4 p1 u return this;6 C. Y5 z4 {# H3 J$ k% ?
} |