HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ A( ^2 R* r% I3 d
R# e2 b; ^$ I" u5 F6 d* H public Object buildActions () {$ q, X6 G, V/ h* L# }
super.buildActions();
8 P2 T2 V! z8 P7 ?/ a4 \1 K9 N + z9 a; D9 A) x: @# s
// Create the list of simulation actions. We put these in
7 \: t. Q2 z: N7 C/ `* g$ Y# o // an action group, because we want these actions to be1 J0 V" Z, D% h
// executed in a specific order, but these steps should
' {6 a( g5 ~1 J8 j0 i3 n9 z // take no (simulated) time. The M(foo) means "The message
: v8 O8 w0 g+ X/ q) H% S5 t8 O // called <foo>". You can send a message To a particular
5 x7 S! Q9 o" ?, ~/ ~3 p- x: a // object, or ForEach object in a collection.
1 \* |- g4 {3 d
. f+ g4 `2 e% D // Note we update the heatspace in two phases: first run
4 q1 i$ \7 C6 N& R& i0 { R // diffusion, then run "updateWorld" to actually enact the8 A8 G ]" b. C
// changes the heatbugs have made. The ordering here is+ Q8 B1 R1 y" B% P5 h( u
// significant!( a' m: c) S$ z0 q2 s( v/ w
" j# O! U7 K9 x F( U9 B
// Note also, that with the additional
n9 g4 K. l" q: ]$ B# F& `. A" y% [ V, k& a // `randomizeHeatbugUpdateOrder' Boolean flag we can5 _. y# Q, m: [& ?
// randomize the order in which the bugs actually run: b( z* c( h# D9 Y0 ~; K6 Y
// their step rule. This has the effect of removing any1 F2 d" f% k5 q7 c$ F/ S
// systematic bias in the iteration throught the heatbug3 C# B9 ]# f% e8 t
// list from timestep to timestep& V6 j ^: S3 U7 Z1 [) C
& R, O& M# J s5 R+ |; t/ j // By default, all `createActionForEach' modelActions have/ L9 Z. ~: _3 F% }: }
// a default order of `Sequential', which means that the
% X; m/ i+ _# L& X/ _& u( R: d // order of iteration through the `heatbugList' will be7 [9 ~4 {( ]* y8 J
// identical (assuming the list order is not changed* O- e4 D. M4 \
// indirectly by some other process).
( e) Q4 M6 d7 E. R$ b
# T9 A; L! [" W modelActions = new ActionGroupImpl (getZone ());, D; ~1 _+ z) M' w# [
' \$ P8 Y7 S. w# x8 s4 K0 W
try {0 o/ ]% b7 b5 S7 S# x6 H% R
modelActions.createActionTo$message) Z# J" C, z6 a/ ]5 @/ I
(heat, new Selector (heat.getClass (), "stepRule", false));
2 {8 d' \9 ?) b& ` ?( U } catch (Exception e) {8 Q4 l- N6 b2 B8 Z! {( }& C
System.err.println ("Exception stepRule: " + e.getMessage ());* T. A8 M' k) s4 L
}
$ l; w4 A6 g( x5 ?1 @7 ]5 _ c
2 ^* o5 E+ n: K6 M+ B try {' R4 _' q# ?9 a2 ?' q. {* s
Heatbug proto = (Heatbug) heatbugList.get (0);( \& q1 o$ {: z' H$ c
Selector sel =
7 w& A6 P) F6 L. v3 s' s0 }' \3 l4 D new Selector (proto.getClass (), "heatbugStep", false);; r, c3 J0 u. d5 J" d6 H
actionForEach =
/ U9 |5 A& T( t! w, U modelActions.createFActionForEachHomogeneous$call/ u# X. V$ u+ q( @5 k0 e9 Z; v0 t
(heatbugList,
& @* s5 T: ^8 s- ?% X4 Z# V* s new FCallImpl (this, proto, sel,
$ }6 T" t9 X8 Q3 N" t% j new FArgumentsImpl (this, sel)));. ^/ I5 R3 Y" V& d: K* F- m) j l* p8 u
} catch (Exception e) {3 N" a' h7 {) C0 |
e.printStackTrace (System.err);: U0 S3 i; C& i' p
}
, F6 {2 o+ e7 p( c9 o& o: v
; Z4 m/ U' e$ C! ?: |5 R syncUpdateOrder ();
6 W2 q c. X1 e0 v
( |7 x, K- h) B9 ?7 K try {
- L+ U/ S# q1 X1 L8 {) } modelActions.createActionTo$message
6 S* O. _- M6 H' t+ l, V. v (heat, new Selector (heat.getClass (), "updateLattice", false));
# j; I& L' W3 r7 X d- v6 h9 i+ k } catch (Exception e) {
. a# x, \6 ~5 J System.err.println("Exception updateLattice: " + e.getMessage ());7 k9 {4 Y# K( N) F) J
}; A1 X% d7 M- m9 I) N5 \
1 `1 ?8 ]% x5 y. o3 ? // Then we create a schedule that executes the/ w7 i3 N/ N' w; K, f( _
// modelActions. modelActions is an ActionGroup, by itself it5 Y6 f5 P5 u* L9 t
// has no notion of time. In order to have it executed in/ y6 v9 _; I6 P5 Z; L' W8 R% I
// time, we create a Schedule that says to use the
3 g0 U; E3 _9 @* V9 H // modelActions ActionGroup at particular times. This4 {* J$ @# B; h$ n
// schedule has a repeat interval of 1, it will loop every Z/ p: L [7 E+ R
// time step. The action is executed at time 0 relative to5 @# D4 o0 j* S9 N
// the beginning of the loop.
: N' Q2 R0 T3 l: R6 T
0 p M/ x2 s9 ` // This is a simple schedule, with only one action that is* P/ Q% u9 e3 |7 S% @
// just repeated every time. See jmousetrap for more
9 ?. \% v# O( \6 }1 r8 E4 f // complicated schedules.
) \. K- I/ e( P0 y% l2 M. P1 Q 8 \& L8 I6 f, n9 e' k8 E
modelSchedule = new ScheduleImpl (getZone (), 1);
: P/ @( v( ]7 T* e7 Y modelSchedule.at$createAction (0, modelActions);& u2 O8 L3 n3 T' k3 y6 ]; s
9 a) T; D7 N0 j7 T& ]. X5 ?8 O& _
return this;
" u% ^( L6 Y, u0 i! _ } |