HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 f) N$ F( K& R# R2 T& A; C
* u7 M) g: ^0 p: J3 T; `1 Q) |1 ~ public Object buildActions () {2 l+ Q R( j; |$ z4 k& g
super.buildActions();% y' [" \" |4 y) @
/ j* W# Q8 ~* O. f
// Create the list of simulation actions. We put these in) A1 s0 v$ S: T& P: A+ @
// an action group, because we want these actions to be9 a+ C- D; h8 {' J- m: m% N0 _
// executed in a specific order, but these steps should
! g2 Y6 S3 p( W, B1 `, z+ b$ K // take no (simulated) time. The M(foo) means "The message4 K$ W: P& t, ^- ~
// called <foo>". You can send a message To a particular
0 z% M( I* v( b% k" F // object, or ForEach object in a collection.
2 r' c/ E6 ^/ L, m7 o7 C6 b" q
: u3 f% |. f8 C) u3 P6 G: b // Note we update the heatspace in two phases: first run2 _9 a+ d- H3 n: c
// diffusion, then run "updateWorld" to actually enact the) t# b: I3 M9 L$ S( m: y' m6 `# U2 Z
// changes the heatbugs have made. The ordering here is3 M! x t3 q# J, ~( S$ B
// significant!2 C& E, x' w) A, U( W0 ]
' J+ u4 h$ Z, ], q2 _8 J9 Z# c
// Note also, that with the additional
0 v* m2 b+ Q5 X# u7 \, A // `randomizeHeatbugUpdateOrder' Boolean flag we can
?' O4 ^. l9 t7 B: H // randomize the order in which the bugs actually run; b' t. `6 U% d& N
// their step rule. This has the effect of removing any
/ \$ W& h+ F, {" c6 ^. G9 U // systematic bias in the iteration throught the heatbug
% F5 `' J4 v3 b. d$ Y' ^# b7 F // list from timestep to timestep
- j8 e; F7 P5 i' ]" @
- x% R) x2 R9 N# w6 \ // By default, all `createActionForEach' modelActions have
. n1 N+ e3 {9 @8 @5 M/ |# r // a default order of `Sequential', which means that the
" J9 E& R9 c* u0 ~& q // order of iteration through the `heatbugList' will be
# U( @' D# w# B9 ~6 _9 N5 @, I // identical (assuming the list order is not changed% E( A% j0 V6 |
// indirectly by some other process).5 D' Q4 t$ T2 N5 C
' e3 U8 {) Y+ m modelActions = new ActionGroupImpl (getZone ());
( B( c X, k6 x k
3 j% Z, Z% |8 G" d try {: c) B( R" x# V: r
modelActions.createActionTo$message+ d" v! L1 A7 G( n& }. Q
(heat, new Selector (heat.getClass (), "stepRule", false));
+ D. t: `" D/ G! l5 I9 T6 B6 L } catch (Exception e) {
9 F7 Y) s2 k+ A6 e; S [ q1 |& E" s System.err.println ("Exception stepRule: " + e.getMessage ());% v. Y; D( a/ E$ e6 u% ]3 L1 l
}
! t+ O& \2 I& j) [! N3 \( N
) A9 P4 z( L1 O5 `5 D try {
4 f. _; h6 J$ z Heatbug proto = (Heatbug) heatbugList.get (0);( S/ Z0 u. G0 `+ b
Selector sel = / P7 |4 { L" r' H) j4 l
new Selector (proto.getClass (), "heatbugStep", false);
0 k1 L( M6 S1 l) p7 E8 w& i actionForEach =
# N6 Y. m7 d* m modelActions.createFActionForEachHomogeneous$call; B" P1 Z' e; G$ e* f/ _* {
(heatbugList,
" J2 }8 r3 y4 I, A new FCallImpl (this, proto, sel,! t% q5 G8 G' t( @( ^3 `
new FArgumentsImpl (this, sel)));
* |) B3 @6 E1 o3 M& ~) N0 Y& m } catch (Exception e) {9 Q- t) i4 Y8 ?" T
e.printStackTrace (System.err);
5 b& h1 N7 U) Q+ S& d8 W( q }7 E1 p$ A8 m% ?) x G- W
2 C+ h* \+ L, F! X7 j' B" B+ ~
syncUpdateOrder ();# g# R/ l# R" w' E' Z3 Y
" q: c- ]" b/ j/ y try {2 i. S1 i1 z+ n' n6 s* W5 d0 g5 ~
modelActions.createActionTo$message
6 m' R$ V/ W7 x/ Q+ X: D! } (heat, new Selector (heat.getClass (), "updateLattice", false));9 H& h) b; p* j( f5 [$ d# @6 s F" s) g
} catch (Exception e) {# f5 g ^& w& c5 L
System.err.println("Exception updateLattice: " + e.getMessage ());% b' U' y4 i/ B- ?; y% i
}
5 D5 E, A+ l c * e/ D5 `3 K! c, J- \
// Then we create a schedule that executes the, C8 w3 s* ?) j/ \1 q
// modelActions. modelActions is an ActionGroup, by itself it
5 b l6 i( [ l/ P // has no notion of time. In order to have it executed in8 g9 _4 p7 v- |- R$ M. }
// time, we create a Schedule that says to use the
6 U9 y2 t* M) X3 O2 j5 h( d; W. P // modelActions ActionGroup at particular times. This6 y2 a% M2 t/ H
// schedule has a repeat interval of 1, it will loop every5 @( K6 L6 B7 c9 g8 s- Y! U3 \/ s
// time step. The action is executed at time 0 relative to2 q: j0 O% t; ]2 L8 A' [
// the beginning of the loop.' u9 O. n1 v- K7 I% g% _
6 O# e1 f( j: b. }5 q" b3 R# p) d // This is a simple schedule, with only one action that is
W2 W" M- i f+ M // just repeated every time. See jmousetrap for more
1 V9 z3 |+ t6 |6 D' Q0 e // complicated schedules.
2 l5 H0 d+ c1 V+ \1 ` N3 Z& J ; H( Z# D, Q% ]- u) ?3 j
modelSchedule = new ScheduleImpl (getZone (), 1);* U" A+ S1 t* D5 }* L1 P
modelSchedule.at$createAction (0, modelActions);7 `' `) W5 l8 M# e: j$ q2 |
# L0 u) c( N. l H! g7 q
return this;
3 `7 e4 |& [, t) k } |