HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ N; T& o) S( q* I) s. X- a( X
; L+ D0 _9 Q* M& \: B public Object buildActions () {% u: k' {$ ], X3 j8 r* @
super.buildActions();% q8 T( t3 W2 e/ ]( ~
) I L2 Q+ I( i' I& c, X // Create the list of simulation actions. We put these in
" x- l% k A& v, n5 M9 p // an action group, because we want these actions to be& P7 K& L8 e$ ~
// executed in a specific order, but these steps should
6 C. t+ p0 \- \+ H! r1 `( U- V' T" O+ \ // take no (simulated) time. The M(foo) means "The message( t2 e4 U7 w. s- S1 O
// called <foo>". You can send a message To a particular$ J& b9 E3 {; T$ K
// object, or ForEach object in a collection.
7 Y1 _9 ]) q9 c3 G ( ]/ j' X- b$ ]% C+ T
// Note we update the heatspace in two phases: first run
5 ^- \/ ?* c2 w& \5 {8 ] // diffusion, then run "updateWorld" to actually enact the% N# k1 {) s# F' S4 R+ D+ F5 |
// changes the heatbugs have made. The ordering here is
% J/ ^' t9 K" u9 A2 @ // significant!& p3 |/ F( [$ }2 ?3 @
% w' w) f/ C9 W4 ^) w g" I // Note also, that with the additional
8 b& @, f" w1 o) b: @ // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 Z2 I( a: e! \$ W // randomize the order in which the bugs actually run
$ {2 V6 L) e8 m4 K, E' O // their step rule. This has the effect of removing any/ w( E% d) w( v7 e
// systematic bias in the iteration throught the heatbug
$ V% _$ y/ k* i) Q& [ // list from timestep to timestep
, [! w0 e( L1 y$ x
" C( j" l. q# F7 F9 o // By default, all `createActionForEach' modelActions have
' r. C- m/ f- V6 t* T // a default order of `Sequential', which means that the
0 d2 @- |( w$ B; R // order of iteration through the `heatbugList' will be. S# K' Y" N% S4 T! C
// identical (assuming the list order is not changed
; a8 K3 K' q0 u( g+ V // indirectly by some other process).
3 U. M" [9 G& C4 ^2 t$ X. h8 E
' ^7 `+ y' [! Y% s3 O2 l( O+ O$ Q+ N modelActions = new ActionGroupImpl (getZone ());& c' p' D, @# z0 h7 B
3 g @/ A3 C' k( _8 u9 x1 r% E' c n
try {% \ |) B1 s4 g+ @/ b' i, ?
modelActions.createActionTo$message1 ~+ @" {% B8 L; ]1 O
(heat, new Selector (heat.getClass (), "stepRule", false));3 [$ p( o4 J' |7 L9 T
} catch (Exception e) {
. V2 ~) k1 O: Q System.err.println ("Exception stepRule: " + e.getMessage ());
0 u. n) g3 ~# n/ }+ x& V }; I. o6 \% `+ c, N
5 @! j6 X) X1 s. k$ h% O try {
. u p) f+ P; S9 W Heatbug proto = (Heatbug) heatbugList.get (0);
5 l4 ?! r+ F" l. Z Selector sel =
9 J" Z9 U, B# \- B; f new Selector (proto.getClass (), "heatbugStep", false);
! X4 I/ N" E8 v- y9 h% k actionForEach =
5 c& U& U! N4 c P modelActions.createFActionForEachHomogeneous$call
$ o; K* U* I( ~3 C b (heatbugList,
5 x/ ?& f1 _6 a, n new FCallImpl (this, proto, sel,) r$ |% G8 L5 h. t% E
new FArgumentsImpl (this, sel)));
' w& q5 W( N- e5 F } catch (Exception e) {5 G% s/ h8 F' R! {. X
e.printStackTrace (System.err);/ n2 V8 P5 P* W" w, A
}
* k4 Y* z k: @& R) I6 t, [! g' _" ] 0 X8 V1 K4 N8 W! G3 C+ Z( c
syncUpdateOrder ();) Q. R- o& b$ T8 c! r! b
( K# C5 x9 R9 O try {- k) F- r( x0 w) o
modelActions.createActionTo$message
& m w7 |9 y$ i- g; Z( w2 _. T! G (heat, new Selector (heat.getClass (), "updateLattice", false));, o% V4 {3 \4 \( q7 v
} catch (Exception e) {
% w( O, C' |' l- K. K+ W- p! q5 X System.err.println("Exception updateLattice: " + e.getMessage ());0 A: Q" d" q" z' w* N# K0 C
}
# Y4 i& J1 f1 X4 Y! ]
- u1 O$ `# x$ a2 m" |1 k // Then we create a schedule that executes the
& S) _, o8 X6 N- T: l // modelActions. modelActions is an ActionGroup, by itself it
! [/ d q% X. e0 v2 s // has no notion of time. In order to have it executed in
+ \0 F, w( B3 u8 ^# _ // time, we create a Schedule that says to use the h: l3 w' F% }8 d
// modelActions ActionGroup at particular times. This
) I; }5 X% ]7 E) Q: ?* J // schedule has a repeat interval of 1, it will loop every9 C, Z. Q5 I* V
// time step. The action is executed at time 0 relative to- x- o. D( h3 f. K/ j- k( G
// the beginning of the loop.
" c2 \7 S' w7 L" f/ }5 q# d5 |) T* i9 L: G4 ]" \
// This is a simple schedule, with only one action that is. i/ C( c# u8 g: M" }( q# X" R' e
// just repeated every time. See jmousetrap for more
# C; R$ q0 l# v6 C // complicated schedules.# p9 N. `! j- n, N0 R. U7 v
% X# c9 F& L" a w" c
modelSchedule = new ScheduleImpl (getZone (), 1);
' f$ X+ b; W9 C5 h3 } C' r modelSchedule.at$createAction (0, modelActions);
% h) @) Z+ j8 l8 k
/ b% J0 d9 S* w1 N3 E+ c8 d- r# |, v return this;; j P/ |. u! a) T! B( S" h4 p
} |