HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 g- o7 M N0 l- F b1 ~ g F
) [ F) v# V1 [" m6 A
public Object buildActions () {# Q- j1 V! ~7 P
super.buildActions();; ~& F6 x4 S8 [' [+ w. b% J
8 ?8 T" ]0 K% W9 e. A
// Create the list of simulation actions. We put these in: h4 S% W% C' g
// an action group, because we want these actions to be
6 `! z' g+ _) N3 g" p // executed in a specific order, but these steps should
& [8 G0 w/ \. z9 h, I$ I // take no (simulated) time. The M(foo) means "The message! y, s: Y3 i: O0 V, b8 T
// called <foo>". You can send a message To a particular4 ]+ S6 Q) M! R
// object, or ForEach object in a collection.
( X- q9 V& U' h6 N$ L1 G % U! _% f) s+ q
// Note we update the heatspace in two phases: first run5 w1 f$ s% @1 a X% z# o
// diffusion, then run "updateWorld" to actually enact the- c H6 s% B' |6 t- o8 Y, g
// changes the heatbugs have made. The ordering here is, u2 q" H9 W* _; B0 S& q+ ^$ r
// significant!2 P( `, @) d3 U# ~
/ V$ R) v7 O/ N // Note also, that with the additional4 y2 c) n5 q- G, h1 Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ ~% d' \2 q+ \0 I4 P k2 r
// randomize the order in which the bugs actually run3 L8 D" l, H+ ]# e+ D2 H- C
// their step rule. This has the effect of removing any9 U" F4 c+ l u# T
// systematic bias in the iteration throught the heatbug
* O3 ^ E- {6 E& U3 X( | // list from timestep to timestep
/ B4 U6 M$ v7 Y
, P# a: T' b2 l6 o( \ // By default, all `createActionForEach' modelActions have+ i7 b }! e" j% Z
// a default order of `Sequential', which means that the
7 ?* ~( [% w' E4 N // order of iteration through the `heatbugList' will be
2 W5 ^* v- L) u0 H$ i. U // identical (assuming the list order is not changed
! q9 Q* S& E* ~1 L1 g% ^ // indirectly by some other process).
0 d) ?: L0 `1 f6 d+ Q; m1 Y. K
9 D9 D; `# ~9 @/ y. t' A% e/ Q) a modelActions = new ActionGroupImpl (getZone ());
# |4 \: A6 o0 _' l5 r; i3 e/ B! e( o( H2 ~
try {
. m! z S5 |) i modelActions.createActionTo$message
% i; ^ w" T+ M6 N! z (heat, new Selector (heat.getClass (), "stepRule", false));
8 U5 V( d2 u5 i: H3 B } catch (Exception e) {
9 m9 A9 Q, y$ L$ \6 T/ y, f; |; z9 O System.err.println ("Exception stepRule: " + e.getMessage ());8 s7 j) J8 d& F0 |6 \+ y" O* K
}3 _7 C8 Y% f; e9 F+ r
! y {5 E& Z3 D5 W' |. ?: W
try {, K' [* P1 f6 J( E5 T
Heatbug proto = (Heatbug) heatbugList.get (0);2 O- ?$ z% n) R7 }
Selector sel = 2 t# i' e% D/ ~+ f6 V, Y& ]
new Selector (proto.getClass (), "heatbugStep", false);) o! ?5 q) x. F1 n y
actionForEach =
/ F/ |+ I% f+ D( z: i0 a4 R, M modelActions.createFActionForEachHomogeneous$call
( x5 ?9 R5 @4 {9 p+ {5 A5 ? (heatbugList,
' d: \3 ^0 F: \( g2 U new FCallImpl (this, proto, sel,6 R+ y( C; d. V) ?" d7 ?2 D+ S) h
new FArgumentsImpl (this, sel))); x: u/ P. z6 ~" t
} catch (Exception e) {- Q, C$ N! Y5 p& k; m
e.printStackTrace (System.err);2 T+ u7 J; Q5 f$ d' l F
}
0 S& N% q: l8 a4 f 3 D- s8 ?. d' ^5 ?: @+ F
syncUpdateOrder ();
" q9 s( C+ `$ f" j7 Y2 G+ G, Z, A6 x7 p1 @& J. p1 [# ]- H% L" w
try {
4 E. F1 g- e% [* z* V6 }3 i modelActions.createActionTo$message
6 f# ^% D( i' G* q (heat, new Selector (heat.getClass (), "updateLattice", false));- p# P3 n) p$ }" i+ T
} catch (Exception e) {
{- r2 q! E3 s System.err.println("Exception updateLattice: " + e.getMessage ());! D7 l* p" F& j. w; F
}
R& J; d( v$ U. |$ f+ v) w # h6 ~/ p$ Y0 D H0 W5 R
// Then we create a schedule that executes the
! b9 k, O7 S' E3 i0 r" d // modelActions. modelActions is an ActionGroup, by itself it& d& w- Z, G* \! _) N- W
// has no notion of time. In order to have it executed in
) D( ]8 j( I. I // time, we create a Schedule that says to use the
" V6 G8 O0 v( u9 {3 N B- k8 a // modelActions ActionGroup at particular times. This
; K0 t) R6 t( H r2 d // schedule has a repeat interval of 1, it will loop every, l7 @( K! ]6 [3 r5 D: `
// time step. The action is executed at time 0 relative to
* ?$ w8 q- F( s2 @9 b1 \0 L // the beginning of the loop." J8 c3 ?0 I0 b
0 [( }) D3 Z6 \6 h // This is a simple schedule, with only one action that is% i& |, }, i* V! k: P
// just repeated every time. See jmousetrap for more3 D0 `1 a" |8 Q$ R0 q3 Y$ {0 g
// complicated schedules.
) O7 [- a7 o) r! l N( @ / i9 {2 w# B3 }0 T% [' N
modelSchedule = new ScheduleImpl (getZone (), 1);3 T$ b# Q# N9 M: U$ H' J6 ?
modelSchedule.at$createAction (0, modelActions);
T) W1 t1 _0 D+ H3 l6 D
: |/ k7 w( ?6 U l& t/ r7 f9 g return this;
' Z* \) C3 `% t: I3 N- N( b } |