HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- y# X# m5 U" c5 c. {! l c, H7 j. s; @# x [
public Object buildActions () {% \" i# K9 a! m9 S
super.buildActions();8 u. a) s# ?- K
# z6 _1 z$ M' e* E9 o! z // Create the list of simulation actions. We put these in
4 {0 m' B, }* ]: i // an action group, because we want these actions to be, {; R6 z: Z, ]. N
// executed in a specific order, but these steps should/ h6 D6 ~8 R" B* y) v
// take no (simulated) time. The M(foo) means "The message
1 D1 n/ Q7 A+ o5 G // called <foo>". You can send a message To a particular
7 G) w8 x' o- |& { // object, or ForEach object in a collection.0 f0 X0 Z- L f& \ _' A b
7 o! I4 j' w* D
// Note we update the heatspace in two phases: first run
. v6 n+ w" B0 W // diffusion, then run "updateWorld" to actually enact the, S. J; t- \. i# X9 ?9 N, y0 T
// changes the heatbugs have made. The ordering here is7 G* z! L3 ?4 k( l; c+ L
// significant!& v# H. _+ P% H/ L; m) K
& H' `0 P) H; f- x& q$ ?( } // Note also, that with the additional, g& }4 q7 H5 X5 |: b$ u
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. T% l- A: S& _! y( c // randomize the order in which the bugs actually run3 A: ^8 o7 p' K1 }1 B
// their step rule. This has the effect of removing any5 J2 z' m. m- q" Z
// systematic bias in the iteration throught the heatbug
6 v) X6 {5 W* d& J/ @; p8 C // list from timestep to timestep
7 v+ T) h8 H8 M
; x# N- y5 o( }( f // By default, all `createActionForEach' modelActions have- g+ F0 V! O9 d2 A5 X7 P: X# b
// a default order of `Sequential', which means that the
, r% f$ U+ f7 g+ H2 M // order of iteration through the `heatbugList' will be4 b& H1 R; Y: x$ W* T ?
// identical (assuming the list order is not changed3 ^( n$ M8 T d2 g! I9 i
// indirectly by some other process).
1 z/ ^( Y) u4 [. G- k9 W- Q - \- T: _7 ^& H3 J
modelActions = new ActionGroupImpl (getZone ());
7 a7 r2 V- m5 \ I
- {3 ?6 u1 y" e$ z% o try { \2 I) g' {( ~0 r. h3 P, B) Z/ {
modelActions.createActionTo$message
' r+ ]5 [& j/ `5 m) \0 P0 H (heat, new Selector (heat.getClass (), "stepRule", false));
- g% [- c& U/ M5 Z( C } catch (Exception e) {' j! K1 r4 t) q# T6 k+ I
System.err.println ("Exception stepRule: " + e.getMessage ());
+ a# n+ A$ T9 {' M }$ T: f6 \ }+ p2 r& L5 n; [& y3 N5 D
; A. j* G# u2 U7 }5 N, T try {
( p& J/ l( S1 ^1 r Heatbug proto = (Heatbug) heatbugList.get (0);
; c) M- t3 i ^& G8 Q6 N Selector sel =
* h4 j! s! Y- {$ H new Selector (proto.getClass (), "heatbugStep", false);
" u8 j9 I4 j0 w' n7 Q actionForEach =( y9 t* {5 h& [* m0 G: h
modelActions.createFActionForEachHomogeneous$call
8 M0 y1 V. s/ N0 F6 ^1 G (heatbugList,
! ~/ C7 @9 c7 D7 i9 A0 ]# l new FCallImpl (this, proto, sel,0 a8 x/ J( T7 N0 j$ @9 m* C8 J
new FArgumentsImpl (this, sel)));; \1 `2 o4 N1 P: `
} catch (Exception e) {) z. D9 Q( N! T6 x1 b( Q2 q
e.printStackTrace (System.err);
5 F" j2 C, E8 m }
# m; \) r! ^3 F* ?! x' y 0 Y6 V' r5 f- _) e) N2 z, F- O+ d
syncUpdateOrder ();, M5 U z3 n' n% q( |$ _
# i' Q6 v" U3 Z' Z5 |$ W } X( `7 ~) S try {' m; Z, ~ Q% r' L+ L
modelActions.createActionTo$message
- m0 \& \1 N4 J! w8 l" V3 W (heat, new Selector (heat.getClass (), "updateLattice", false));
6 \4 d+ E; u% Z) N } catch (Exception e) {" O f) |0 i$ [6 A5 |
System.err.println("Exception updateLattice: " + e.getMessage ());' C; d9 `8 X \5 y8 k& _& {: j
}; u9 k5 i# H' G6 {: G
n9 R' i8 }, K7 x k& {' Z! f) a0 _
// Then we create a schedule that executes the- ^+ ^/ u2 ?, M; Z+ ?9 B
// modelActions. modelActions is an ActionGroup, by itself it
% A* ]/ M0 U+ @3 z0 W* _ // has no notion of time. In order to have it executed in
+ [. _. D4 C- \ // time, we create a Schedule that says to use the% ^% e/ F2 {' H
// modelActions ActionGroup at particular times. This) B) o, w) A& z+ S/ a+ u
// schedule has a repeat interval of 1, it will loop every2 q2 m4 E5 i; W8 Y# r
// time step. The action is executed at time 0 relative to
/ A$ O+ ~" P" [8 p1 i // the beginning of the loop.0 x7 T, g* [0 G# \$ [8 S/ i) b
6 N6 C: P0 W$ ?4 V0 S+ g; q // This is a simple schedule, with only one action that is1 X: B5 C' ?+ }0 T2 ]4 [
// just repeated every time. See jmousetrap for more& D. q# B4 [7 {% {' G# e4 B
// complicated schedules.
$ k( M1 }3 ~+ v: i2 U% r
+ q/ t% @/ {) x+ R9 d8 B modelSchedule = new ScheduleImpl (getZone (), 1);+ M+ M7 _) [4 w5 P; S, b0 _
modelSchedule.at$createAction (0, modelActions);1 F! ]$ w- L: n* o( u P
( g! l" }! F4 [( e( i3 l) F return this;# L" I# ?0 ^/ O1 v$ ?% l S3 G
} |