HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 }+ C+ m! ], d& x) d$ ]0 j0 t4 _/ J" _6 ?" R
public Object buildActions () {+ V' ^, f" l* ]- R: {7 C1 `6 @
super.buildActions();
+ I |3 o4 I# K" f' f ; J% |+ T1 R( U9 l7 Z y* h
// Create the list of simulation actions. We put these in2 n3 y& _- d& v d2 g+ b1 {
// an action group, because we want these actions to be
( c% A1 ]3 ^9 } // executed in a specific order, but these steps should/ ]" {/ ?, G8 n! {2 Y3 y
// take no (simulated) time. The M(foo) means "The message
2 }3 u1 b" q. Q" E0 w6 r. p! f+ E6 Z/ p // called <foo>". You can send a message To a particular
. _9 Q3 \4 @! s+ ^+ a* | // object, or ForEach object in a collection.8 e& Z# K: A; M( y1 `! K
) J* {+ u; e u; w! C( ]
// Note we update the heatspace in two phases: first run- G0 K3 `/ Z2 B" d" [/ e
// diffusion, then run "updateWorld" to actually enact the
; o: S; O% ^+ A+ `0 i // changes the heatbugs have made. The ordering here is
5 g; H( G& d0 d z2 }' N" @: q2 O6 V# V // significant!& j- Y% m1 j& X% R+ {3 U2 h
' Q/ Q" j5 k4 X" p3 y5 \* E6 s
// Note also, that with the additional7 W/ j5 W7 c7 Z* z" Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 u1 M" i9 V9 e4 f; a3 p
// randomize the order in which the bugs actually run
2 F/ I) p6 [6 z // their step rule. This has the effect of removing any
: M. F9 y: U2 S4 v" ^" P* g // systematic bias in the iteration throught the heatbug; L4 t: z2 q- Y+ l: M
// list from timestep to timestep- o2 H3 G: I ~
, G% }- U1 |* B4 K' V // By default, all `createActionForEach' modelActions have
8 e/ g' I% j. p& O+ C // a default order of `Sequential', which means that the. ^7 J! [! L* s6 i) Q' B
// order of iteration through the `heatbugList' will be
* d+ r/ @( p8 t: l- C4 j3 p // identical (assuming the list order is not changed
/ |2 U" g; N X i) | c // indirectly by some other process).
. B0 O; j2 T! y5 i ]- b / H* h p, e q5 c) l3 R
modelActions = new ActionGroupImpl (getZone ());6 K: t: z8 o$ b6 o3 K; \
! x- k5 @) K# q
try {
; Y! l4 ^4 n+ C modelActions.createActionTo$message' z3 b V/ q' W9 |* }8 \
(heat, new Selector (heat.getClass (), "stepRule", false));
: l; G; I2 _* s2 M6 h% L3 O } catch (Exception e) {; o1 x: O+ G) L& _8 |- I
System.err.println ("Exception stepRule: " + e.getMessage ());
+ ^+ V- u% S$ L! R7 U f4 U) m }: c+ ^& Z, D7 S0 s3 ~' j6 p% e
) p! i1 F& C _) M6 V3 s7 b3 L
try {
* h; Y: W5 O( \ Heatbug proto = (Heatbug) heatbugList.get (0);
, j6 e8 Y5 x8 i0 T9 `' {: v% |' Y Selector sel =
2 O& z2 @2 J. b4 n) Q: G new Selector (proto.getClass (), "heatbugStep", false);
% U: Y! h0 T8 a6 e$ w% S actionForEach =4 Z$ Q; R. a' Y" V1 e7 ]
modelActions.createFActionForEachHomogeneous$call
6 o; c/ Q. Q, h) c5 E (heatbugList,
/ o; D6 t' |* l new FCallImpl (this, proto, sel,
& F% Y7 n* f0 U, \$ x9 v new FArgumentsImpl (this, sel)));* d* V/ U9 y5 d8 h
} catch (Exception e) {
5 D! f0 ?6 o0 R: G e.printStackTrace (System.err);6 ]3 X) h) c: s% T7 c
}
8 D; i* M5 n1 d+ w+ t5 \ 9 \* c" m- |% k) N4 v1 }7 F
syncUpdateOrder ();; Z9 M- R$ c; D
% P. {$ k7 u% w% m try {" Q9 J+ O6 Y, C/ e; G
modelActions.createActionTo$message
$ A7 D8 D# G3 ? T (heat, new Selector (heat.getClass (), "updateLattice", false));
, B5 }- L3 f* @% t& L: J0 o } catch (Exception e) {
S$ v$ ]" @7 U- T$ u' T* ? System.err.println("Exception updateLattice: " + e.getMessage ());
& t, Y& B. `" R# j4 t2 B }
5 l' b2 T o- }9 O. ]5 ^' B) h
! f1 E. [% U# l. | // Then we create a schedule that executes the# c6 F v: K5 L2 K6 K; O" o
// modelActions. modelActions is an ActionGroup, by itself it
% e1 x6 w7 V& a0 \4 V- b // has no notion of time. In order to have it executed in
' V9 I8 ?% g& _; X8 x // time, we create a Schedule that says to use the1 L" C: {( R6 u, A& d/ L* d
// modelActions ActionGroup at particular times. This4 M- @! P$ K. D
// schedule has a repeat interval of 1, it will loop every+ U9 f* J2 h+ G2 }$ q+ C2 k
// time step. The action is executed at time 0 relative to
" r4 X/ \5 |1 w$ ]- x // the beginning of the loop.% U) c) u' w5 M
- s$ X1 T9 u- l8 q' k0 v" r0 ] // This is a simple schedule, with only one action that is5 a$ d2 l9 d) c- K3 o9 J
// just repeated every time. See jmousetrap for more
+ C* F" D+ k% r // complicated schedules.
* N. x: {% K7 a& M ) a a% f1 N( O' p
modelSchedule = new ScheduleImpl (getZone (), 1);
" a. X. \' I* H/ I modelSchedule.at$createAction (0, modelActions);1 L2 Z1 \- N ?# C+ Q {2 t& a
$ f: {% O( t+ j5 b1 I+ T
return this;
4 F) b9 v' r) r5 X' E$ c } |