HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# T- C6 o# Y% e
Y( P+ x9 F6 {: n
public Object buildActions () {7 D1 L2 ~- M0 M. `
super.buildActions();' X/ B3 L: Q: l3 D- S6 Z- h8 |; f
1 \3 `: p6 D2 c, V // Create the list of simulation actions. We put these in
) b) E- M. l& w4 e) y4 n" l // an action group, because we want these actions to be! G( g$ u {/ b3 M5 R1 ~- S2 ^
// executed in a specific order, but these steps should
$ r. n: _' w3 a // take no (simulated) time. The M(foo) means "The message
: {0 C! b2 m. I) ^( x' ^/ h2 {- Q" P // called <foo>". You can send a message To a particular
9 b0 T% z, Z* x x) } // object, or ForEach object in a collection.; p/ {- G/ o, y; N
- W% u9 z3 G2 @3 t
// Note we update the heatspace in two phases: first run, R. g1 z# a1 q# J. s
// diffusion, then run "updateWorld" to actually enact the
' y0 F' G3 p/ O+ a6 d9 V- B! n // changes the heatbugs have made. The ordering here is5 F8 |% q- Z1 }/ V, V: J/ l5 [
// significant!
2 L4 y6 ^7 K% ?- U' O9 B4 x e6 T " p4 a% T8 m+ Y k* ^' v
// Note also, that with the additional
/ `! h8 `+ g' V! ^0 i/ @3 f# k* C // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 x2 @0 r3 U( ^$ ~ // randomize the order in which the bugs actually run
5 F# [4 f" X. p9 {" d8 {9 V J // their step rule. This has the effect of removing any i% t6 v# V4 [6 E; U
// systematic bias in the iteration throught the heatbug( p' @9 H; ~' ?7 K& v5 e
// list from timestep to timestep. t' C# z& x* b; `% a
5 C. x3 N: f. `) q8 V3 `: J
// By default, all `createActionForEach' modelActions have
) Z: i3 h& Q5 V/ q& b1 V // a default order of `Sequential', which means that the1 ^: |9 x: Q) K8 |2 A
// order of iteration through the `heatbugList' will be* p: M9 s' ] }- E& b
// identical (assuming the list order is not changed( D' F. c7 l& |0 _% i; d! e
// indirectly by some other process).7 T2 s6 V$ E) c
% |! C3 k$ U' |5 m' k( W5 ~4 ~ modelActions = new ActionGroupImpl (getZone ());: Z" C' I6 t. t( ]
6 \0 ^( ]8 q8 k% M$ s+ E: C
try {( }6 e g# r+ _0 I$ y. C
modelActions.createActionTo$message: L* G( z9 j3 f# h- T, t
(heat, new Selector (heat.getClass (), "stepRule", false));6 u5 c' M* V7 `
} catch (Exception e) {
% J+ e% l. ]6 @3 {% ~ System.err.println ("Exception stepRule: " + e.getMessage ());
# M# T/ V$ W9 ~3 w }
8 d7 A; v0 J; v+ S/ E+ T0 I- ?7 P7 s- o4 F3 O& \
try {& e$ H( @, p+ R4 l5 x! g
Heatbug proto = (Heatbug) heatbugList.get (0);/ ~- `- {6 m f7 C# ^) q6 i9 W
Selector sel =
" |+ o! L% G; j3 U! ?2 M! ?4 C new Selector (proto.getClass (), "heatbugStep", false);
7 J/ _6 W( x& n. a3 ~3 z( B$ P actionForEach =
- {; L$ S$ J2 w& _* A2 ^ modelActions.createFActionForEachHomogeneous$call
5 p1 n9 X) S3 K/ i (heatbugList,5 L5 x& ` _/ |$ l: n* F
new FCallImpl (this, proto, sel,. x. C4 `8 c0 N& N: [0 R$ t7 s
new FArgumentsImpl (this, sel))); q9 g! Q6 S: o0 l* Z# ]3 x
} catch (Exception e) {; e, V2 H. p+ `3 f+ J6 c
e.printStackTrace (System.err);
) S5 T/ j" O; Q2 P/ t& Z6 D* \$ A }
' c& u' _& _" t/ Y ; A/ a4 d5 ^" M0 V3 H* @# H) f
syncUpdateOrder ();7 P3 E3 L/ m- u/ t& `
$ G$ {' G+ [; t& D' B9 P try {8 \6 a" s- ^% t' m4 j, L
modelActions.createActionTo$message $ ], M8 Y( ]! D1 e* ] g( {' x
(heat, new Selector (heat.getClass (), "updateLattice", false));( l) ^# `- K" p$ ^1 X* C7 E
} catch (Exception e) {% X8 V, [% x3 b F% S" O
System.err.println("Exception updateLattice: " + e.getMessage ());
( S& g' Q4 u. |! N" G } U' _' T y7 T; \0 B9 ~6 N
6 N$ D7 e% s7 H N$ l // Then we create a schedule that executes the* z2 Q7 s2 E4 b/ t* p* a, |
// modelActions. modelActions is an ActionGroup, by itself it+ ?, c. C* r: w; a
// has no notion of time. In order to have it executed in
A. r$ _% |- _. k, \ // time, we create a Schedule that says to use the
$ D; T+ d& L5 @% ~6 I // modelActions ActionGroup at particular times. This; h: D8 w |9 ]
// schedule has a repeat interval of 1, it will loop every8 x' n3 P! ?" M* W" N
// time step. The action is executed at time 0 relative to& I! L8 U5 u. b8 T0 Y8 S- R
// the beginning of the loop.7 L( G6 e- M r5 q, _
5 R6 v# P" c& a4 h4 H* C5 d& {
// This is a simple schedule, with only one action that is
& R1 y0 W6 ?3 E3 p. K // just repeated every time. See jmousetrap for more
9 D/ x" F) m! ?( H7 d) w1 ] // complicated schedules.
2 T' R4 T0 \' e" x y7 R+ g$ r
: B- p1 d* k! a5 [) d' X& I8 o modelSchedule = new ScheduleImpl (getZone (), 1);
4 x# Z- a# P1 n4 m) E modelSchedule.at$createAction (0, modelActions);3 m2 G3 t# K4 X. y5 Z$ |# T) k
$ R& x( V5 }& D, U return this;2 S" t; |$ y( p4 w+ B/ L
} |