HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: ^9 x4 }" V3 r/ N8 _' \& S8 m: l$ I' `" F8 B) K
public Object buildActions () {# K6 y& H6 }4 A$ [; P. }: t5 P; w9 b+ c
super.buildActions();
; I- d$ ~0 h. b # M# g/ { F. s
// Create the list of simulation actions. We put these in
) _0 X. C& G) M0 W( S // an action group, because we want these actions to be1 J$ A, g a+ ^0 s. s9 ~4 t; A
// executed in a specific order, but these steps should6 d8 c* G% V6 E' ~
// take no (simulated) time. The M(foo) means "The message
7 z$ R' X6 y M9 J' B( r( m! N' q // called <foo>". You can send a message To a particular
3 X+ f' ~% o. |' B // object, or ForEach object in a collection.
& v( ~/ V' M2 _+ ^! u% v
3 P, R" W; v% m1 Y! S // Note we update the heatspace in two phases: first run
3 } c( x: E# B+ o, N6 `6 z // diffusion, then run "updateWorld" to actually enact the) f8 c0 R6 k4 I
// changes the heatbugs have made. The ordering here is
* |' R7 H) v4 p# O2 ^% d6 _* B // significant!7 }/ u) W& ?* p9 t9 @& t
2 [3 ~. q1 Y9 j0 R) Z( u" I5 L' O // Note also, that with the additional
/ F3 f$ M2 o L" w // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 Z; F+ y' W* f" y // randomize the order in which the bugs actually run
# Q$ C4 r8 Y: O; ]1 L! |, U // their step rule. This has the effect of removing any
+ j9 L7 @1 l4 D4 J( B // systematic bias in the iteration throught the heatbug
. B9 k4 z1 A1 _1 t6 T2 L/ f // list from timestep to timestep
; x, Z+ h6 Z6 b6 d2 x
6 Z) t+ t4 A+ R7 Y9 }7 s // By default, all `createActionForEach' modelActions have$ E: O7 J, l8 ]% l! _
// a default order of `Sequential', which means that the$ L4 E; e$ j/ B+ ~
// order of iteration through the `heatbugList' will be
0 i, ?/ ~6 @* p4 ~0 H5 u5 w // identical (assuming the list order is not changed
0 ~4 [, y4 G0 T( f' G: N // indirectly by some other process).) d3 g8 n# [% @! ^
2 q1 c" Y6 V! @; `: l modelActions = new ActionGroupImpl (getZone ());* o$ }2 X# w' k# ]2 d0 f/ d! {
# i! Y. X* v# B$ {
try {
, J' C/ O# I- a8 I* x modelActions.createActionTo$message( E3 d7 ?/ q) _. @
(heat, new Selector (heat.getClass (), "stepRule", false));
& Z+ Q$ }6 o' a6 c: O( A4 W V } catch (Exception e) {
9 q8 l3 K1 n0 n8 J0 }. Y; O System.err.println ("Exception stepRule: " + e.getMessage ());
, c. u4 {1 b9 I1 W }9 Y! G( H7 B8 A
p6 w/ v9 Q' ]$ E7 u' ` try {
2 f& U/ \. f8 B, l: L Heatbug proto = (Heatbug) heatbugList.get (0);; E0 i8 a( s* \) z8 D: w
Selector sel = 7 K& H" ?6 K- ?9 P
new Selector (proto.getClass (), "heatbugStep", false);0 ^9 c5 c+ C6 u; s0 }$ f
actionForEach =* ~3 b* K: I1 C
modelActions.createFActionForEachHomogeneous$call
- y& u2 H" `) u, s7 Z* e8 P (heatbugList,
' p2 s+ e# i t: Q" N ~1 s new FCallImpl (this, proto, sel,( o# r+ r% S u% Q/ ?* D4 a
new FArgumentsImpl (this, sel)));; }* d5 c4 h5 e2 B
} catch (Exception e) {
. `$ x- [* N, M) d7 @/ F e.printStackTrace (System.err);
6 r% g+ b' e0 M. H9 {/ a }" s l# N- z) A# s
, e2 i7 i1 e/ R8 h/ K) q. s
syncUpdateOrder ();. J6 h' ?& C; x7 k7 G& L& L
; F; Y$ o, y+ h+ i1 ^* U, `
try {
7 o- Y) J1 e* v) r _ modelActions.createActionTo$message
/ s% i7 t! Z1 w8 C- k) b (heat, new Selector (heat.getClass (), "updateLattice", false));
6 }( w9 ?8 s" [' E! K2 k7 i4 d ^1 x } catch (Exception e) {
k x. j& Q1 X: \: e1 y6 ~# [ System.err.println("Exception updateLattice: " + e.getMessage ());, [. o ^* Y% z
}7 V: v6 {5 q7 A' N: ~8 a# K
: s- n( [7 w$ B3 r% S3 h* s
// Then we create a schedule that executes the
& v1 @) r8 p: ~- S: M // modelActions. modelActions is an ActionGroup, by itself it
* y( g1 V3 |$ _* M1 M. M6 P; J // has no notion of time. In order to have it executed in/ i2 j/ |0 T) m
// time, we create a Schedule that says to use the
" V9 c0 L5 _" Z, Y4 ?' H8 ] // modelActions ActionGroup at particular times. This- b4 K) l; \$ I2 K
// schedule has a repeat interval of 1, it will loop every+ b- t/ h) Y" V L1 t! Y- N
// time step. The action is executed at time 0 relative to
' o; ^3 O- u3 f' k: { // the beginning of the loop.
( Q% k7 w% C) f: Y! E. O; ^3 c7 @) f& g8 v
// This is a simple schedule, with only one action that is# x1 y" b: h8 J8 _5 e0 ]
// just repeated every time. See jmousetrap for more* t h! S+ J, E4 N
// complicated schedules.# v7 ~( \3 w) |' q1 q* S K' e
3 R5 }. x& V6 V9 F
modelSchedule = new ScheduleImpl (getZone (), 1);
) Y7 V, S: S! t7 }& Z# a modelSchedule.at$createAction (0, modelActions);6 h* a4 L7 R( ^" ^9 t( V, P
( t1 }) ^ S+ u% }
return this;% f! T8 d( r6 e
} |