HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# ^, ]* H$ L! k7 ]3 n. W
( r" I: I& f! N5 r
public Object buildActions () {2 k. K$ }# h1 j
super.buildActions();. l# M4 V) n5 Z" B/ j* w/ [. X
; b6 H0 X* G9 D* E4 o1 V // Create the list of simulation actions. We put these in/ `- C' a$ n E6 S6 z; r0 K
// an action group, because we want these actions to be
1 [$ z" I% O% K8 C4 K5 d0 U // executed in a specific order, but these steps should+ w. j% {8 r, f8 T! J8 A
// take no (simulated) time. The M(foo) means "The message
& G. z8 ]4 a! B* _+ [ // called <foo>". You can send a message To a particular8 B. M' O/ q6 {
// object, or ForEach object in a collection.) b& \- \: x% N ~& k3 u. s
3 s$ q( _' W+ ~( g$ o2 i7 Z! W
// Note we update the heatspace in two phases: first run
6 w( k7 @ U4 \+ J6 y // diffusion, then run "updateWorld" to actually enact the
2 y- k5 n& U8 K. F" ~! C: n // changes the heatbugs have made. The ordering here is8 ]! S( L; R3 T# B" I7 [ w& h
// significant!
: Z& F9 ?- B" M" c- f( E1 G+ j 6 q+ G' L/ L% \# t* _4 E9 i" w
// Note also, that with the additional
" N; I0 I7 k) [% t% z# r // `randomizeHeatbugUpdateOrder' Boolean flag we can, @% z: J+ E! u# k
// randomize the order in which the bugs actually run
2 N1 }$ k0 l+ [$ T0 ?( _- f) _* w // their step rule. This has the effect of removing any2 f* A5 U8 s$ u9 r% L5 s5 n& z. S$ ~
// systematic bias in the iteration throught the heatbug4 J+ A6 x, e! f6 e! A$ I
// list from timestep to timestep
1 Q; T* \7 F8 s, O" t
+ B. c$ a d* s, {5 u* v4 n // By default, all `createActionForEach' modelActions have
+ X) O( c, K: y' p% `$ f5 }3 i // a default order of `Sequential', which means that the
4 S& W S% t2 X4 {# A% ^# e // order of iteration through the `heatbugList' will be, _9 M; O! ^* b9 M" ]
// identical (assuming the list order is not changed: K. E- P, {+ W
// indirectly by some other process).
! R) G9 k, c6 T6 y$ [+ [2 J/ f) d % }' `8 ~+ K. W! Q. {
modelActions = new ActionGroupImpl (getZone ());7 y+ @' _" d' W6 b7 ?9 m' [
9 i* _+ \$ {/ \1 y$ J0 G1 m
try {
3 I4 P5 K5 K9 h5 T# v modelActions.createActionTo$message
# Y0 N7 m. G& ]: c (heat, new Selector (heat.getClass (), "stepRule", false));
2 @2 }8 w' A! w* B) v& A m/ O. ~) [ } catch (Exception e) {
2 f7 _% l1 }5 i$ i System.err.println ("Exception stepRule: " + e.getMessage ());
' a1 m5 e" {2 [# \& h! C2 W }
' u, G, g) k! A* ?* n8 z
) y9 ?' _- `! O3 J try {& v, v" M) V7 g, j9 p
Heatbug proto = (Heatbug) heatbugList.get (0);- ?5 D; Y! B5 x$ [
Selector sel = 2 Y' e0 O3 |9 w% ^: @3 t4 A
new Selector (proto.getClass (), "heatbugStep", false);5 O3 j6 e6 n' K+ x+ p) I: x0 y# k, A
actionForEach =9 F# o# y6 l C4 [" B
modelActions.createFActionForEachHomogeneous$call6 T: m, C/ X) A7 ]# f5 H9 c m
(heatbugList, Y; l* e3 y6 `
new FCallImpl (this, proto, sel,
2 W* }" S! Q+ h new FArgumentsImpl (this, sel)));
0 M4 \- i' J+ J3 z6 ^" C6 Q& n } catch (Exception e) {% h, O" Z/ j l8 u
e.printStackTrace (System.err);
; Z, g6 f$ k3 u7 D }
% D0 ?2 X/ I( x+ ]& E: \1 r6 s# z
7 m( H% Q( v! q" K2 A syncUpdateOrder ();
+ \7 o& l5 a3 V. J/ y) A6 u* j" L6 w! p' q6 i; z6 ^
try { t) ?& \* K5 D* c
modelActions.createActionTo$message + C0 f3 x2 j* ~ \. _
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 P$ {4 k# W1 \$ I/ O } catch (Exception e) {8 M4 _2 Z8 h/ {$ O
System.err.println("Exception updateLattice: " + e.getMessage ());
- m/ x0 Z$ J7 M6 w5 @$ V0 ^ }
5 i4 Y/ x7 d4 T& U; ~6 Q ! q, ~+ y% Z- B: n1 ?
// Then we create a schedule that executes the. ~! }- J$ f- {8 Y
// modelActions. modelActions is an ActionGroup, by itself it# T/ N9 Y6 Z+ i$ q: _; f
// has no notion of time. In order to have it executed in
, J% j& ?/ X( W% G4 S // time, we create a Schedule that says to use the! u1 S" A7 W( L
// modelActions ActionGroup at particular times. This
. m: `; _7 E- ~5 ~# f Q // schedule has a repeat interval of 1, it will loop every
( t* T9 L, L0 u2 O // time step. The action is executed at time 0 relative to
5 @' s/ s$ x2 b( n) c+ ] p // the beginning of the loop./ e! m: H& C& U6 u6 F( X
; c, l& ?1 w+ t+ d0 Y/ _
// This is a simple schedule, with only one action that is; }- [; V6 p, w4 R
// just repeated every time. See jmousetrap for more D. l: e U. M( l$ n
// complicated schedules.' \' h' i' a9 f& J. ^# V
6 a. @# w3 C/ g% V5 z5 r" L modelSchedule = new ScheduleImpl (getZone (), 1);, H4 w0 D/ b t7 B( y* I f; ~/ m8 @1 ^
modelSchedule.at$createAction (0, modelActions);
+ q4 U9 w3 n; y, H Q% k# b- t1 w. j 1 v9 U7 U3 v1 M: F! A# w5 F- J5 m
return this;
7 H) K1 Q. e6 ~ } |