HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; k$ C: w. ]) L n1 c/ q
2 g3 S" R9 A8 Q% ~/ w9 c1 w8 U% ~, W public Object buildActions () {% B* W$ I; M) u0 \' }: {2 X+ V
super.buildActions();
: Z% i, o# |% E6 U 5 D1 b7 \) f% M
// Create the list of simulation actions. We put these in2 ~0 G+ m$ i$ K
// an action group, because we want these actions to be9 K) P& o* C: f+ X6 O/ v
// executed in a specific order, but these steps should
! |' }" K; ^1 E- _ // take no (simulated) time. The M(foo) means "The message
( x0 B' [5 ?9 w( l9 C // called <foo>". You can send a message To a particular/ h) k/ l$ i# J) f. f! j1 G
// object, or ForEach object in a collection.
' U+ ?' G8 Y+ ^5 p! Q
* _% C- m' n" d6 O. X. h // Note we update the heatspace in two phases: first run
3 s4 v6 M& Z. i/ q1 {1 \ // diffusion, then run "updateWorld" to actually enact the
% e2 r* R2 b( o8 }: Y& ~ // changes the heatbugs have made. The ordering here is9 Y S) {6 ]) Z7 o7 k, T
// significant!; d5 N {/ }* `8 v( w2 [6 P) Y+ t: d
3 f) C2 \2 ~, F9 ?5 O2 Q3 d // Note also, that with the additional9 H: S3 Z$ f7 u4 _ P1 N+ v* l2 J
// `randomizeHeatbugUpdateOrder' Boolean flag we can! {9 Q" z2 {( g e$ c9 s$ i9 a
// randomize the order in which the bugs actually run+ e! }( S0 N) m+ W
// their step rule. This has the effect of removing any8 M, }' G% @/ ^5 ^; N
// systematic bias in the iteration throught the heatbug, [) I4 Y V6 Y0 l6 G3 j
// list from timestep to timestep0 {1 c4 ^, Q$ h D7 D( n, k: \
- u( L& j1 n& d( q. f // By default, all `createActionForEach' modelActions have
4 Y' a: S4 E) f- S$ q6 }/ f8 Q6 T, q // a default order of `Sequential', which means that the
) B% x+ \* X7 }6 T( u3 d* u // order of iteration through the `heatbugList' will be
4 u3 k+ h6 h, G9 o // identical (assuming the list order is not changed6 H$ a3 X1 A8 ?6 {6 y
// indirectly by some other process). S: ^% ]8 v# H/ e1 `1 \
% I: `" [0 c. J* N
modelActions = new ActionGroupImpl (getZone ());
& S2 T% p; Y0 }$ [+ b( N- @5 t) @6 @) w/ V7 X
try {1 N& e" d+ J/ E! ?
modelActions.createActionTo$message/ L! Z. W' W0 b5 d& U+ `% g
(heat, new Selector (heat.getClass (), "stepRule", false));
3 w7 ?: W% h; f% l3 f } catch (Exception e) {) G7 {0 @( F& B6 C
System.err.println ("Exception stepRule: " + e.getMessage ());* u2 x" V2 i, C, b s
}
. I6 ^( B1 B4 X& M4 }4 h$ g. M/ `: T5 W4 M# t7 `; ?
try {9 r! T% j0 K/ S# U" t
Heatbug proto = (Heatbug) heatbugList.get (0);7 F' G$ O1 K* w6 B; ?4 e
Selector sel = 9 |. U8 C# @8 O0 m# S5 V
new Selector (proto.getClass (), "heatbugStep", false);
, P( h, [2 c! q" z* n$ u8 S: l actionForEach =2 f ^6 V, b2 n y
modelActions.createFActionForEachHomogeneous$call( [- ~" J& d+ F- f3 H; v4 l+ f! o& C
(heatbugList,: d! r% Z: [% G$ @, _- Z) {) _
new FCallImpl (this, proto, sel,
7 \! [7 ?7 h- O4 C9 u$ E+ `6 A l- N% \ new FArgumentsImpl (this, sel)));
5 i" X' x" M$ n0 a! R& y9 G. F } catch (Exception e) {" N1 |& W/ c9 B3 C# R- B' o3 I
e.printStackTrace (System.err);
, x& [7 N- S3 W1 A }
; ^, ?% t% I0 T' f 6 m$ e, J! J# M( W
syncUpdateOrder ();
; s! n$ N7 c4 H% p4 ]" T. ^0 y8 B3 x7 _) k6 c9 o* }8 f
try {6 s* f* D+ ]: U
modelActions.createActionTo$message . e8 }9 W! {! J+ U
(heat, new Selector (heat.getClass (), "updateLattice", false));
# P* P% E! M( ^0 }( S$ R! ? } catch (Exception e) {
3 H8 M1 \. B1 F J, P System.err.println("Exception updateLattice: " + e.getMessage ());5 W5 y! n! N' a2 f( j4 l1 W" s
}* q2 w& P! I, X6 _0 ~/ ^% R
I1 T0 r! K, J$ A4 F
// Then we create a schedule that executes the8 D+ i ]2 z4 U0 J" F5 s# H, h
// modelActions. modelActions is an ActionGroup, by itself it
5 R4 ?8 Z9 r$ p, O$ ~ // has no notion of time. In order to have it executed in$ N& Y Q0 u- K- ~
// time, we create a Schedule that says to use the
! G* Y/ ^8 V( }/ [ // modelActions ActionGroup at particular times. This G A* R: S% T! L: ~, u5 U2 w9 f
// schedule has a repeat interval of 1, it will loop every
, @( \# ~# B2 c, f // time step. The action is executed at time 0 relative to
2 j7 R% ]5 a6 `/ {1 K0 Q# W // the beginning of the loop.& B" V! b4 R3 y7 l+ ^/ E( `
* ^6 d0 |: G w
// This is a simple schedule, with only one action that is
. M- i h4 I' z9 q5 F // just repeated every time. See jmousetrap for more+ p% X8 M( w! Z' \2 ~' g
// complicated schedules.8 d+ m" \. V# q# D) ^; w
+ n( C6 q5 o4 C$ e7 E8 \' E6 ? modelSchedule = new ScheduleImpl (getZone (), 1);
" d" E5 P) |6 L* {" v/ p; ~ modelSchedule.at$createAction (0, modelActions);. J' j [8 ?4 ^1 S( N/ n
2 P$ J# ]" ^- d- Z# J# m( }
return this;% E, Y' X2 P4 W* Q6 X! X
} |