HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 _5 ?$ _' f. K
9 g: W3 t1 t2 y public Object buildActions () {
, J8 _1 }, W) J& U super.buildActions();( }, ^7 m" v0 w4 {; r- Y
8 m! {. A( ~+ D; Z; n // Create the list of simulation actions. We put these in: s2 S. X! Z8 X! z/ t# ]' ]$ E% F
// an action group, because we want these actions to be
+ d- t% a1 {: A# M: H // executed in a specific order, but these steps should8 ?# _7 Y* ~0 L) b! g( R6 i8 C" {
// take no (simulated) time. The M(foo) means "The message& L7 ^4 {3 I/ F8 @: j; u1 {
// called <foo>". You can send a message To a particular
; \( B" g7 i/ v8 i; g5 ~) p // object, or ForEach object in a collection.
7 b) j# }0 r7 t0 V' w' q1 D 1 {% x( d0 f% d' g$ k8 y/ D7 c
// Note we update the heatspace in two phases: first run
9 g. K- U* {" p2 Q // diffusion, then run "updateWorld" to actually enact the
8 W' s/ z' _. l2 R5 c8 S // changes the heatbugs have made. The ordering here is* L% I# J+ v0 `% ^2 t4 b- d
// significant!- }/ @1 q3 I; x0 b5 ~
D& r3 v# R5 a. ] // Note also, that with the additional
: n# m+ {9 ~ ~" [* \3 A // `randomizeHeatbugUpdateOrder' Boolean flag we can& W' K$ K3 @% X+ _* A& g* \
// randomize the order in which the bugs actually run& S8 z9 Y! X5 c1 h$ r
// their step rule. This has the effect of removing any
+ J6 t" L* ]- P( ^5 Z5 t // systematic bias in the iteration throught the heatbug
; t% Q `$ N" L // list from timestep to timestep
" d6 t: X* t# n( ?) _- V - f3 _8 f5 E8 [- q- V
// By default, all `createActionForEach' modelActions have& y X% Q& \3 S) i& o3 H! x: o: o ^
// a default order of `Sequential', which means that the; b: |3 }7 P7 K6 y
// order of iteration through the `heatbugList' will be
5 S; S9 P; s6 M7 A // identical (assuming the list order is not changed/ z+ h/ a0 z, e: i d7 H3 {* A- ~
// indirectly by some other process).( D' b: \: L2 B7 v2 }% d! K
7 l- E* P j4 M. v4 r; i: S- U; w modelActions = new ActionGroupImpl (getZone ());
i1 X* H+ j9 A( U6 D! q' y U7 x' `
" D0 }$ q8 W& u* X8 D; F try {; F6 F0 q& V0 w5 T
modelActions.createActionTo$message
+ H0 Z8 M( v" H0 X2 T% s (heat, new Selector (heat.getClass (), "stepRule", false));
; B+ s3 I0 {, ^ | } catch (Exception e) {
- ^; w" N3 D" | System.err.println ("Exception stepRule: " + e.getMessage ());
7 [+ {9 S+ f* Z4 x }- \6 U) u# E# l" g$ y8 O0 `, O
; [7 C/ f% L" u. `
try {( A6 m- h+ i5 K, p0 U
Heatbug proto = (Heatbug) heatbugList.get (0);
+ l4 Q+ i- L: T/ `+ E Selector sel = 3 ^% u* Y d% Y6 H
new Selector (proto.getClass (), "heatbugStep", false);
* l4 W) Q: w1 u. H actionForEach =% N1 \0 D h# E2 A- B& ?& I
modelActions.createFActionForEachHomogeneous$call6 l/ x* e$ u. e" Y
(heatbugList, c- \+ O1 G, i L: Z
new FCallImpl (this, proto, sel,8 m. d# r7 X a, _: Y
new FArgumentsImpl (this, sel)));
5 x' }3 G" g: Z6 q% q6 K# U$ Y } catch (Exception e) {
1 ^9 p9 H. V1 M! \2 ^. @ e.printStackTrace (System.err);
0 b5 Y$ q1 O; u |' z( }; Y }
- M6 j& k; O$ f $ a$ ~' ~* u& |8 {% q* K0 e
syncUpdateOrder ();
; x7 r2 M) D, R5 V) u" N8 D9 ^- K( {* X6 K+ w1 m Y
try {$ C5 q% L9 ~; a0 Z2 @; B
modelActions.createActionTo$message
* A Q3 O: Q2 d4 o0 F (heat, new Selector (heat.getClass (), "updateLattice", false));" C& J% W4 w# t) D8 U: k
} catch (Exception e) {+ c1 N& E- j6 J; y# E
System.err.println("Exception updateLattice: " + e.getMessage ());
$ L9 L+ g; P0 S: Z$ P# R" M5 O) @# o }/ E7 v" I. x1 e" E& _7 ?' Y
$ n% s0 a: s% E // Then we create a schedule that executes the
2 M6 `+ B Q n% }5 y // modelActions. modelActions is an ActionGroup, by itself it
8 }& t; i5 y: p8 e // has no notion of time. In order to have it executed in
9 ^* T% t* _5 I // time, we create a Schedule that says to use the7 B2 ^9 U& [) k
// modelActions ActionGroup at particular times. This2 \6 ?' v% ^. ?% n7 ?8 g
// schedule has a repeat interval of 1, it will loop every
! E4 ^4 {$ k0 Z G0 z. w7 w2 d // time step. The action is executed at time 0 relative to; n5 s6 T; v! S4 P0 D- I3 q* q7 A) v
// the beginning of the loop.
+ M0 q$ f+ O7 J7 g" m; V2 N. e( k7 m; W
// This is a simple schedule, with only one action that is. U# x, s5 \3 S& Y6 V; b3 I, }
// just repeated every time. See jmousetrap for more% x& \$ {: I/ _! w
// complicated schedules.! P" Q5 q1 d$ f( s# a
3 f9 ^* E6 W- O+ j- U- r modelSchedule = new ScheduleImpl (getZone (), 1);
2 C) P1 c0 l4 f+ U7 b* P" O modelSchedule.at$createAction (0, modelActions);
e/ a: Y6 m. L% H6 \ 1 F( ]6 Z8 h' d" `- ^) v, Q
return this;! u6 z4 P# N) N1 J$ h% F. G
} |