HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 g$ A- P/ S- D1 j: a/ n8 H! O4 O# f+ }' V$ G$ _; F; u* D
public Object buildActions () {
! x7 Y: }6 Q/ } super.buildActions();9 ?4 `; e3 x8 q# x: Q# L
8 v7 x9 N. s: D( ^5 ]6 ? // Create the list of simulation actions. We put these in
1 l& C' l1 f' }1 f9 C) n! I6 X5 M // an action group, because we want these actions to be
) C! `% _4 |) i2 O0 t6 | // executed in a specific order, but these steps should) I1 L6 s5 Y2 P4 N9 b u
// take no (simulated) time. The M(foo) means "The message
- r9 K* w; w6 k3 P- ?* L // called <foo>". You can send a message To a particular1 |$ U* F ~, d: x0 O# c
// object, or ForEach object in a collection.
O, ~2 H; y$ o9 m3 r3 { ) L! d$ ~, a6 Y" i# |6 h
// Note we update the heatspace in two phases: first run( I; y7 k, ^ S2 d1 e4 H8 d4 ~) P
// diffusion, then run "updateWorld" to actually enact the
6 g1 J% i1 ]2 b0 j // changes the heatbugs have made. The ordering here is; B Y9 U. ~9 Y5 e
// significant!6 V+ r& a0 V% U5 O0 ]# q, U: w
* k9 D6 ~ Z2 g$ V2 q0 W% T% l // Note also, that with the additional8 d! O; h. ^9 _9 j* X! h
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ a$ [! @5 Y( F9 T& O- `
// randomize the order in which the bugs actually run
/ s* \. ~( l0 E$ f) D0 P4 w // their step rule. This has the effect of removing any
; y) f, I" D) \" B( F4 \/ v // systematic bias in the iteration throught the heatbug
: f5 n0 A7 Z8 t // list from timestep to timestep
; R" Q8 k+ A" v 0 p1 s: ~! F, U
// By default, all `createActionForEach' modelActions have* z: i& Z. d; A0 Z
// a default order of `Sequential', which means that the
) N9 s) k' X- |, C2 a0 [9 T( y4 Q // order of iteration through the `heatbugList' will be2 q6 m0 f9 T6 e1 [9 g5 z& G
// identical (assuming the list order is not changed% ~# r( k- [2 l( D5 y
// indirectly by some other process).
5 q( D# S& g% F3 B9 u
" z' C" ?; C3 V* u; N, Q modelActions = new ActionGroupImpl (getZone ());" P ~, w1 w# P" `1 `# N
" M s, B) o' N) d* q% B- f( R
try {
8 o" ^. n4 P; D3 ~7 B1 @ modelActions.createActionTo$message8 V) Q' s! {" y6 Z' h
(heat, new Selector (heat.getClass (), "stepRule", false));9 [; d# k; w& M$ s
} catch (Exception e) {
3 f# h; V/ \3 N. z System.err.println ("Exception stepRule: " + e.getMessage ());) W6 H$ H) x, i1 r* B; E
}
3 l8 n& C8 ^. r: A# p: T
2 ]- X! ] n+ n2 q, r try {
% v$ A3 }) I5 g5 j Heatbug proto = (Heatbug) heatbugList.get (0);1 u) ~, }' A' c5 }3 T9 r) Z
Selector sel = % _0 J, L8 q @% i- }; A) c
new Selector (proto.getClass (), "heatbugStep", false);
* X0 W" U; a- K3 v3 ~; h actionForEach =2 j/ p M- ]& u/ n7 f) N
modelActions.createFActionForEachHomogeneous$call
5 ~# v8 g# b9 f8 m (heatbugList,
+ H" C* C2 m+ Y- S( `- A6 t" I new FCallImpl (this, proto, sel,! ~. ~# q, d8 B
new FArgumentsImpl (this, sel)));
! `# L' U; y- O7 }; E+ M- Z } catch (Exception e) {0 e6 ]& m( s, R" A2 o
e.printStackTrace (System.err);
1 \1 D Q R) c8 [$ T/ r }$ ]8 m' n6 \) G& n6 [
- F' K5 } W; k! t3 v- P! q; Z
syncUpdateOrder ();
{& l. |& h3 `6 m1 m0 X4 D2 o4 x( x
try {0 A! J8 H" _' L
modelActions.createActionTo$message
* Q# P0 m" \7 x8 m (heat, new Selector (heat.getClass (), "updateLattice", false));
) p6 m/ C F; \& t# A1 e3 E } catch (Exception e) {% A) t. r1 A9 o/ i9 z' {; R
System.err.println("Exception updateLattice: " + e.getMessage ());
, U3 x) t& @1 [# P g }4 _+ S" F5 N* h! h, _/ ]- k
* ?5 s5 v" G" M0 K* U% U" e // Then we create a schedule that executes the
! X: n! i1 `% ?9 {: a! i) C; \8 R // modelActions. modelActions is an ActionGroup, by itself it5 F# M2 I5 x; M& Z2 ^
// has no notion of time. In order to have it executed in
/ K% N& ~4 O0 a _' E // time, we create a Schedule that says to use the7 B. \9 w) x. i
// modelActions ActionGroup at particular times. This8 E1 r: _ N; y1 r
// schedule has a repeat interval of 1, it will loop every3 x1 k6 U1 G8 q b. X
// time step. The action is executed at time 0 relative to4 d0 w( t/ r" c+ l7 M$ ]7 a- x
// the beginning of the loop.
0 ^5 U2 o- L0 f) w
, b1 I9 @! n; }: C- p$ Z$ v" @ // This is a simple schedule, with only one action that is1 V4 V3 w, T; z& ?9 W, W' N$ R; J. a
// just repeated every time. See jmousetrap for more
8 u% c' K0 p* `; _% g // complicated schedules.
: d* c# m7 a) J9 C1 Y
1 U7 K" I& G p: j; ?! @$ s modelSchedule = new ScheduleImpl (getZone (), 1);
) [0 p$ M! u4 R8 Z* i, s, | modelSchedule.at$createAction (0, modelActions);
4 ^2 w E' J* O+ B- r" l( g % p2 U6 k2 z1 A
return this;. q2 R5 D4 R. x( N; J0 T
} |