HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 Q# R$ ?6 y3 h2 U# \
7 r8 h$ N" \/ M( k: w3 I& z7 Y ] public Object buildActions () {
6 e6 Y" Q, f5 I$ ^ s- _ super.buildActions();2 @9 z4 n) v' C. [) z5 |
. }) _1 |$ X0 U5 x7 a% p3 } // Create the list of simulation actions. We put these in: o9 D5 V2 T+ Y' a) c& N- d
// an action group, because we want these actions to be9 Y9 y/ ]- g6 {2 ^ q
// executed in a specific order, but these steps should
* C. b* R4 X+ r8 y // take no (simulated) time. The M(foo) means "The message
3 @' E: I* F$ t) q6 l9 G // called <foo>". You can send a message To a particular* L1 U0 n, ?! @# f
// object, or ForEach object in a collection.
$ p+ A6 F& r' @% [+ r+ Z, v
! x) i; j8 D/ B% z6 D // Note we update the heatspace in two phases: first run
* [& ^4 P3 a2 V0 X, o- c$ M3 W4 b- o // diffusion, then run "updateWorld" to actually enact the- E; h. C+ \4 Q
// changes the heatbugs have made. The ordering here is
: C4 k+ \, G- Q+ @! L! w // significant!
p/ H$ S7 y. {
& E f4 ~: a% i" w // Note also, that with the additional
" U* J* v1 R9 Y7 ^- ?( Y1 } // `randomizeHeatbugUpdateOrder' Boolean flag we can$ t7 ]+ b( U3 V7 c
// randomize the order in which the bugs actually run5 v$ B( R) Y# {* n! g
// their step rule. This has the effect of removing any
8 B$ N) _# U: ?2 N* `0 k3 i& n$ a // systematic bias in the iteration throught the heatbug
# g1 F$ h& s6 z8 l* j: U2 r3 G // list from timestep to timestep6 x' T( l9 @& ?; e$ G/ M
, D, ~; X0 K E- F% t g7 e
// By default, all `createActionForEach' modelActions have- a2 r3 O. A9 D
// a default order of `Sequential', which means that the3 b7 h' [) p4 D, l3 a
// order of iteration through the `heatbugList' will be& r4 I- m/ \4 k: J
// identical (assuming the list order is not changed! `, Q: g9 I+ g% S% {% q
// indirectly by some other process).
! M, {2 z! ]. z& I1 m7 K
% s5 o2 y F) }& ~% M) l modelActions = new ActionGroupImpl (getZone ());
: ]+ _2 U, e0 w' U* n) L' T9 P( g1 k2 g$ e, g8 }+ L. O$ r
try {
k8 F1 z2 L9 u4 O B+ f' z modelActions.createActionTo$message* R$ L+ J; R/ ^3 K' @, `
(heat, new Selector (heat.getClass (), "stepRule", false));
+ m! @' d; x4 h1 J1 v( U } catch (Exception e) {0 o+ h5 s |8 d% @' {' A( _' B
System.err.println ("Exception stepRule: " + e.getMessage ());" ~, m# J# m! w3 ^7 M5 n
}# U6 e g! Q# N
( |0 ~' ?3 H) ~! q try {- p. c+ P1 h5 b+ J4 {
Heatbug proto = (Heatbug) heatbugList.get (0);" L5 J# J n/ j: A1 V0 K. o' A! \
Selector sel =
+ |' ] R! z' [6 h8 a new Selector (proto.getClass (), "heatbugStep", false);$ S- m$ @5 Z( o3 N! Q
actionForEach =
, z" W5 Z+ K& d, K% ~' L2 e" Q0 S F modelActions.createFActionForEachHomogeneous$call
& w+ E, Y5 Z( a (heatbugList,' ?5 e; m: H. O3 d0 Q9 l# R- G6 M
new FCallImpl (this, proto, sel,6 E |( P8 p( F1 B: {
new FArgumentsImpl (this, sel)));
+ @0 i; |1 E' Y9 b$ p& }- `( q } catch (Exception e) {
- C7 {8 V* ^" u* b6 c/ N e.printStackTrace (System.err);
1 \4 X" N% \( s7 q, [+ l0 q }" [0 a2 K7 w8 Z% Z2 o6 a
0 ]8 m( l* x8 O) G- t) V* n
syncUpdateOrder ();: W2 f( p7 A. Z
# Z6 c: I0 a7 g* b5 P, A# `
try {4 R& p- _# p+ v
modelActions.createActionTo$message ( L7 H& Z0 i" S" V2 X1 Y+ t
(heat, new Selector (heat.getClass (), "updateLattice", false));
" L7 o/ C( [+ L3 n7 | } catch (Exception e) {
/ Y, v3 @* Y: S$ K System.err.println("Exception updateLattice: " + e.getMessage ());$ N) b: h( [6 r& i
}' e9 P) H! [. H& [! q9 g& b
* I, p0 w1 p% X9 Z q0 {
// Then we create a schedule that executes the
7 }6 r0 T/ I2 _3 c0 t6 l/ ^$ `5 l0 o // modelActions. modelActions is an ActionGroup, by itself it
. \2 M/ Q* L1 v7 l5 G% ?% w // has no notion of time. In order to have it executed in
8 h# {, j' {* N6 A6 f // time, we create a Schedule that says to use the
6 b. T" ]; h" Y // modelActions ActionGroup at particular times. This) ]; H5 I) a. E. }# Z% i
// schedule has a repeat interval of 1, it will loop every
0 W" y4 n/ Y- C2 a) r1 x // time step. The action is executed at time 0 relative to% L2 b3 z* M1 A8 `* Q9 Y$ _1 l
// the beginning of the loop.
4 | I* s, P$ k1 F$ B9 J) {% k/ E8 i7 T- m/ n* ^; G: I' ~1 C+ e1 {
// This is a simple schedule, with only one action that is
# t- ` M. g* F // just repeated every time. See jmousetrap for more+ e H* P# J& z: X$ Q7 z
// complicated schedules.
9 U% K1 d* F6 W6 r% i6 N: v* P8 W t) _% M4 K6 L2 I! [ F2 P
modelSchedule = new ScheduleImpl (getZone (), 1);4 g* g7 ]! i% k: w, I# d) e
modelSchedule.at$createAction (0, modelActions);
7 `0 g. L/ d3 ~6 F
! K' |2 Z; l5 Q9 ~$ D x return this;
0 i7 Y, s; N6 M3 h, V } |