HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# y3 N, B/ d5 ]$ Q
& m ~: z. i: {6 s! F W& _7 s5 {, N public Object buildActions () {
" d0 I2 L8 o6 N5 J1 Z0 n2 ?1 t super.buildActions();7 V! v. l2 X& w# C! H/ p3 K
: s5 z L3 N, t( i+ J! o // Create the list of simulation actions. We put these in
" B" T; u4 }% Z8 U8 U3 R$ [ // an action group, because we want these actions to be _$ P4 _/ O& T; f- ~
// executed in a specific order, but these steps should1 _7 P8 {4 Y5 l' {" o6 }* m- \1 x
// take no (simulated) time. The M(foo) means "The message \+ U5 L3 z F1 m2 `
// called <foo>". You can send a message To a particular
2 n9 e3 }" R& e0 A. ~" M // object, or ForEach object in a collection.
" o2 \( t9 F: V+ r |& f4 A# c $ O' B' U: W# U$ r5 e9 Z5 X4 l: d
// Note we update the heatspace in two phases: first run/ `) ~+ T0 y, q3 ~
// diffusion, then run "updateWorld" to actually enact the7 r. J+ i9 K5 K, c
// changes the heatbugs have made. The ordering here is1 u" x* F d1 k! u: A* a
// significant!
1 h+ j- q, N: g6 m- B+ G / e/ M9 Y" x2 \( b7 g
// Note also, that with the additional
' _5 D5 K6 v: n' X // `randomizeHeatbugUpdateOrder' Boolean flag we can
, |. m' m& O7 \& Q- _9 u // randomize the order in which the bugs actually run1 P0 H, Y a- x3 H& r
// their step rule. This has the effect of removing any* [; }* P; {$ u
// systematic bias in the iteration throught the heatbug
1 j. J& U* m) R6 P9 N) E3 z4 Q // list from timestep to timestep$ U2 n: u2 t% m+ T/ I' b
+ e4 R- s5 Z- o" X* d // By default, all `createActionForEach' modelActions have: |* e2 i2 y! h! j. W1 }0 L5 B, L
// a default order of `Sequential', which means that the
. h5 {6 C7 ~ [. w# p$ _2 d // order of iteration through the `heatbugList' will be( z; H- {- c/ w3 p4 e
// identical (assuming the list order is not changed
- Z% b2 B* A0 F3 S // indirectly by some other process).
7 Y% y3 v, @: a! g
3 S9 P9 z0 d% D; V4 X2 j modelActions = new ActionGroupImpl (getZone ());
: ?; ^! M9 T0 [$ U. Z: q. x5 O. e! q# V
try {9 K0 }8 W5 o: x1 d1 U
modelActions.createActionTo$message: S2 c+ [2 {* y' z" n
(heat, new Selector (heat.getClass (), "stepRule", false));( N" D0 p7 B9 c, s, Z; ~
} catch (Exception e) {& ^6 Y( g5 _1 ^
System.err.println ("Exception stepRule: " + e.getMessage ());
+ j! |! U. i- K. ] }# I& f* L) [& P. R% T
7 v: v8 Y h5 J' g
try {
9 ]3 K$ ^% A# Z- X Heatbug proto = (Heatbug) heatbugList.get (0);
5 N7 Q/ u7 Z7 K; G2 x/ x/ N3 g9 a Selector sel = % H- X, S9 ?% V/ c
new Selector (proto.getClass (), "heatbugStep", false);, H/ P6 D* o. B D! ~ }$ Z
actionForEach =: O- L: r9 m; i4 ]* x( s5 a% w
modelActions.createFActionForEachHomogeneous$call
3 V; l _8 o! ~ (heatbugList,
6 ?" \& B5 [. s* ^& l8 h* G new FCallImpl (this, proto, sel,% f2 |- f3 {& F% x: E' q
new FArgumentsImpl (this, sel)));' i t1 d- y5 o2 O$ Q! p9 w
} catch (Exception e) {
# g. i4 ^$ ]! u% f3 F% Q3 B e.printStackTrace (System.err);
& F& Z; I, u" h5 f8 R! O1 R }+ r- j# R7 k- o* l
$ [6 j9 R( a \6 k syncUpdateOrder ();( J" e+ c C) [0 [ t
7 G: B: o% `3 {. r% ?& h5 W' G try {
6 X* l( r; q+ V! q4 A( h( T9 u modelActions.createActionTo$message " M5 F0 `: z9 C5 m3 H
(heat, new Selector (heat.getClass (), "updateLattice", false));
% {: N% n! Y9 H: \1 _ } catch (Exception e) {4 }2 n4 h) k7 ^ b
System.err.println("Exception updateLattice: " + e.getMessage ());
2 A: N1 S3 A7 s }3 [! h2 e0 Y) \# l2 B" _, r+ d
# n0 q9 S: ?' X1 n$ Q7 Y4 O, E/ [
// Then we create a schedule that executes the
* m$ y" \) ]( e! ` // modelActions. modelActions is an ActionGroup, by itself it
$ b6 k- i& s! h M2 f6 b // has no notion of time. In order to have it executed in
2 a" ~1 B! T; e5 S // time, we create a Schedule that says to use the
5 y# H0 ^5 \9 m3 ?) N // modelActions ActionGroup at particular times. This
7 V% X5 o/ M C6 p( @ // schedule has a repeat interval of 1, it will loop every
6 L7 z4 f# C3 A5 Z // time step. The action is executed at time 0 relative to
* E0 {3 C' w: P A' } // the beginning of the loop.7 ^ v8 m( |4 F. r5 [( U
( _6 c4 X8 S$ F3 W% \( y
// This is a simple schedule, with only one action that is
( i: P+ g1 x6 c9 m: e9 I4 ~ // just repeated every time. See jmousetrap for more
9 D8 P! I. E. [1 Y4 k& s/ Y // complicated schedules.
6 |( X- ]' D- F, ~) Q X
! B, f" t0 q1 [( `7 N L% k! ` modelSchedule = new ScheduleImpl (getZone (), 1);2 y% O7 m3 @6 t! m
modelSchedule.at$createAction (0, modelActions);
. U) Q- G9 Z) _ 5 D# o! ?+ }9 y. B5 ~! |
return this;7 u& c+ l1 `0 t; y1 w
} |