HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: _9 c$ i6 w9 J9 u
: x& F1 ?/ b' l- M- o public Object buildActions () {
6 I$ O3 n: _5 U6 \ super.buildActions();# x) C- f+ H0 u+ q* ?1 t9 m6 [
9 e$ T$ o8 M( F! v7 Z+ K B9 C9 p // Create the list of simulation actions. We put these in
% _" z5 ^3 S7 |: l. T // an action group, because we want these actions to be
7 T4 I7 E, S* h // executed in a specific order, but these steps should3 t/ X* c0 Y2 D/ W* T; U' z. z
// take no (simulated) time. The M(foo) means "The message
; _1 d5 q1 h) J7 R% z( x; D) Z // called <foo>". You can send a message To a particular, x \1 ~; o m) |
// object, or ForEach object in a collection." O9 Z6 `$ Y+ q, x/ ? b
9 S, Q3 T! S M // Note we update the heatspace in two phases: first run( W' B; ]0 U( p q
// diffusion, then run "updateWorld" to actually enact the( C0 P4 b) }5 G- p
// changes the heatbugs have made. The ordering here is) G" R, g# W( z# o, U
// significant!$ t6 ^1 G% E2 f+ U, O `# A/ \
, f+ k/ c5 I/ ~8 r* Q+ u' } // Note also, that with the additional- h1 [+ u: D8 A4 T6 u6 C5 O
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 R( z" I) c' e4 E V2 ^5 z$ z
// randomize the order in which the bugs actually run+ A. o% g$ w# g8 X5 P7 r4 F$ o( X/ F
// their step rule. This has the effect of removing any
' {; w( z" ]: \% @$ a, _6 B) d U. j // systematic bias in the iteration throught the heatbug
: f4 Q; ]! v# h, u. x) f // list from timestep to timestep: c0 w& c- u: B( D$ `) \
" \. {7 b; G; l2 E# ~3 J X // By default, all `createActionForEach' modelActions have t, | [3 j# ] m8 F0 U# p
// a default order of `Sequential', which means that the
9 l" z5 [$ S* o // order of iteration through the `heatbugList' will be. C4 H* @8 k1 N T% n: H/ s
// identical (assuming the list order is not changed
5 h+ }0 w5 U ^; O" V // indirectly by some other process).
) c: Q. w- a. P" @ I A$ e
& T! y6 B/ v* U" s) C% z$ x modelActions = new ActionGroupImpl (getZone ());
1 N. [5 o8 r' `8 E7 s* q; e
3 Y1 g. ^9 ^) j, ?( G I1 @$ ~# H6 L, n try {
; R7 L) D5 ~8 `4 z7 ^ modelActions.createActionTo$message
, q h! k, i" v* |8 H (heat, new Selector (heat.getClass (), "stepRule", false));
9 J! V2 B5 b* {5 O) u } catch (Exception e) {6 }1 F6 r6 L! m$ {* a
System.err.println ("Exception stepRule: " + e.getMessage ());
/ _* E2 S ]: _2 R6 H }
2 w z2 p, l; G0 r/ Q3 _9 K0 y0 V3 U
( u: }* e- w6 E) m7 e$ C: B5 T" t try {
3 k7 F) s1 }" j7 P4 E# E Heatbug proto = (Heatbug) heatbugList.get (0);
- ]9 `% Y" N: ^' x Selector sel =
3 x+ Z1 Z& o8 ]; J new Selector (proto.getClass (), "heatbugStep", false);1 ]2 N0 `- ?/ C% ~2 M
actionForEach =
1 M- m1 ?( B# t9 x+ H modelActions.createFActionForEachHomogeneous$call, u6 K! Z+ `! p+ \: T8 O6 M2 Y" c0 X
(heatbugList,
) Z }3 t2 ^% C) l new FCallImpl (this, proto, sel,
@! L/ \- v5 H. |2 b. L9 V new FArgumentsImpl (this, sel)));' b/ }5 |/ c! s" e
} catch (Exception e) {$ b8 M* R W9 E3 S6 W$ [
e.printStackTrace (System.err);
- m: ?. ?8 c1 ]% [; f+ r }2 X7 ~; p" Y3 g
; A( o7 T$ B8 O
syncUpdateOrder ();
" G. ^) O! o7 M1 K
, |6 S) l+ F0 W+ l try {0 N. H8 P7 l, Y. e
modelActions.createActionTo$message
2 v7 Y( M/ W. u/ d; o (heat, new Selector (heat.getClass (), "updateLattice", false));
% q3 `. j, p9 v* F } catch (Exception e) {
& b# p( h- v( x7 O, Z: @ System.err.println("Exception updateLattice: " + e.getMessage ());
/ U( n$ @8 C; [$ ? }
) o ]/ v/ {+ S. Q* m; ?( B , G/ D* ~* h3 u# c/ g% c% K( y
// Then we create a schedule that executes the. ]$ c- v5 o3 |: j6 D5 \( d
// modelActions. modelActions is an ActionGroup, by itself it
) s: Q$ U0 R/ Y d // has no notion of time. In order to have it executed in
+ u8 O3 W# y% W+ c3 z // time, we create a Schedule that says to use the
3 _( \/ S+ @( y U // modelActions ActionGroup at particular times. This
V# F$ ^" c2 p/ V; j2 h // schedule has a repeat interval of 1, it will loop every4 a+ g* r. b4 [3 G% u
// time step. The action is executed at time 0 relative to
; b0 j7 w! i/ | // the beginning of the loop.
2 @; C7 i8 q: S: e
3 x: r8 c9 ]4 S! B3 s/ j. P' G5 { // This is a simple schedule, with only one action that is
q0 |- I% A) a3 ^, x // just repeated every time. See jmousetrap for more
7 h% n( j1 ~0 j8 k5 _ // complicated schedules.
7 O5 N5 R# s1 m M4 J- C
4 |0 d( I3 z. s modelSchedule = new ScheduleImpl (getZone (), 1);( k9 l! B9 C9 s2 |5 W0 i8 {* j# Q
modelSchedule.at$createAction (0, modelActions);
& _3 H0 ~/ q8 b& B$ f , f. x) x' }; n# I' `3 ?4 B7 F
return this;
7 e7 b, m' ?/ n f) u } |