HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- [$ P$ |) E- O% E4 X5 C- Y! e3 Z
. h- n4 H" ^" M9 [" M9 l public Object buildActions () {6 e4 D0 l* g" m. ?; C s/ \
super.buildActions();
7 @7 t7 u6 {' I 2 X: N% R; L2 M- |
// Create the list of simulation actions. We put these in
+ M: U# a9 i- _+ ?$ P // an action group, because we want these actions to be" P5 m) y. S; x: e w
// executed in a specific order, but these steps should
W6 ~% d9 e: h1 m; T // take no (simulated) time. The M(foo) means "The message
2 p1 h) Y; N3 S! Y // called <foo>". You can send a message To a particular
' ^% y( y( ~/ _8 \ // object, or ForEach object in a collection.
9 T3 T9 e6 `4 M- C2 U3 A; G - I& g, C& f9 [. i8 i, e
// Note we update the heatspace in two phases: first run
/ q# Y! c4 M1 m // diffusion, then run "updateWorld" to actually enact the' j4 b& `. l. c; l, y, r
// changes the heatbugs have made. The ordering here is+ j& V8 u" `5 g2 c' [
// significant!+ @( B2 I+ M1 X% M; J x a# y: o3 Y. O8 \
$ f& Q- N- B @$ z // Note also, that with the additional! P1 ~. ?% @) [. B. s. F$ b4 D3 @
// `randomizeHeatbugUpdateOrder' Boolean flag we can% a& p+ i. C( `7 g
// randomize the order in which the bugs actually run
6 ^, i+ ^8 ]; z( `+ Q9 S& @- s // their step rule. This has the effect of removing any
0 ]+ p& b" [7 `# d/ } // systematic bias in the iteration throught the heatbug5 X# H5 S# W* }' K$ [+ v5 G
// list from timestep to timestep
- S6 s$ [* r( [* q 8 _5 H- y: h. q
// By default, all `createActionForEach' modelActions have0 Z4 J, N4 |% N) \0 k/ p
// a default order of `Sequential', which means that the8 c4 ?6 y3 }8 S' g$ D0 a8 \ B
// order of iteration through the `heatbugList' will be
# J; Y% ?5 \! N // identical (assuming the list order is not changed+ X) u. H! Q! t; c2 j
// indirectly by some other process).0 i/ Q) d! Z0 m
( R6 f: I" }! ^* V( o2 ]8 D modelActions = new ActionGroupImpl (getZone ());
3 M/ D2 W. Q9 H3 i+ h) s, z# d8 j& u7 `. j. R
try {, k- F8 R: _0 t, X
modelActions.createActionTo$message( a5 A/ K+ d: k1 Z
(heat, new Selector (heat.getClass (), "stepRule", false));
4 ~# @6 X9 ?/ o' l, G } catch (Exception e) {
4 W9 o) s7 q9 V9 P, k- x System.err.println ("Exception stepRule: " + e.getMessage ());" f8 ~ f' F: l4 }% C- Q
}
$ {% J& Q5 @# \' _6 a
. c- M/ C1 ?$ P- s% Q! y$ @ try {# q$ }: j _+ K3 |
Heatbug proto = (Heatbug) heatbugList.get (0);& o# w8 V( j9 v) P, M1 R
Selector sel = % T# A; _' Z! I
new Selector (proto.getClass (), "heatbugStep", false);
. o8 ]6 O6 T* _: k. X% |0 x7 A actionForEach =
/ R& ?+ |( G1 }! F9 }1 }) s modelActions.createFActionForEachHomogeneous$call! f, i) B8 r4 Z* i" }( a( ?: i
(heatbugList,
( q9 m# I2 \' e+ v7 a3 \ new FCallImpl (this, proto, sel,
4 H; i1 }6 t- ] new FArgumentsImpl (this, sel)));
' l# k& [5 }; A! g# S } catch (Exception e) {
$ G7 C& M5 d/ P! K2 W7 \ v e.printStackTrace (System.err);
5 a+ j# l+ c H' x1 F& i }/ c8 p* Y1 c( k, o
# }& M* f" |: | syncUpdateOrder ();
# { d$ s0 G/ L# k( H: Y
# n' J, ^- h1 J0 l7 q% }7 W3 p: I try {% Q- W/ g5 J* T( ~& K& F7 k
modelActions.createActionTo$message 2 }6 O( a& G7 h) ^7 K" i
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ E7 Z& ]& p' l m; k) n } catch (Exception e) {/ G) d5 U$ t) R$ B" V! |0 [
System.err.println("Exception updateLattice: " + e.getMessage ());
+ \3 A* |) f# m# T4 _# ^6 ^ }
. Z h2 @: z1 T/ z9 R" z + ~( ^% W3 Q9 g4 c. Z
// Then we create a schedule that executes the4 q: k% @9 j1 f# r' K1 h) [
// modelActions. modelActions is an ActionGroup, by itself it
4 g2 R3 A# Y! X# ` // has no notion of time. In order to have it executed in
6 h. {* @4 J+ r! W% j6 Y // time, we create a Schedule that says to use the
7 s0 W+ ?# d- a // modelActions ActionGroup at particular times. This0 |2 a& o! k1 x8 Q& D7 _7 Z! z
// schedule has a repeat interval of 1, it will loop every
: O3 D4 j; b, w0 j$ P // time step. The action is executed at time 0 relative to: @9 r# J" g( `1 A7 i3 p8 v- S
// the beginning of the loop.
8 a8 P! G @1 F' e. `7 M* O) n& u) H* w5 c; T2 O$ @ }2 d( n/ G
// This is a simple schedule, with only one action that is5 F% _6 J. G# E: j4 c
// just repeated every time. See jmousetrap for more( X1 |5 }* H( |
// complicated schedules.# |9 u: L6 w4 k' f
$ ^+ i8 p6 |" j. d% p* g
modelSchedule = new ScheduleImpl (getZone (), 1);
& d O) c4 L6 d modelSchedule.at$createAction (0, modelActions);( A$ F! L, S2 q( M. Y
; y; Z( p- i @0 @0 M return this;
) [0 a" R# D. Z) `) V+ \( b: u: X } |