HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# f; m- F. w# l. q
. `6 |4 h8 D$ T V. R7 ?- [ public Object buildActions () { Y2 T' F. G& N9 t' ~* O
super.buildActions();
! b4 \( U& \# j' _3 [% L5 p
; P8 ]& l" O* u. Y1 F // Create the list of simulation actions. We put these in, W" a3 w" o. X6 A8 [
// an action group, because we want these actions to be2 H( c5 H/ k6 ] c/ x
// executed in a specific order, but these steps should0 T( u9 H/ R( I7 y4 ^
// take no (simulated) time. The M(foo) means "The message
( y1 [( ?( b2 X8 P6 C // called <foo>". You can send a message To a particular. h9 Z- B) ?+ A# \+ u
// object, or ForEach object in a collection.
, Y0 E2 `: H3 D2 v) J
8 b% ~) @9 O" ]& Y // Note we update the heatspace in two phases: first run
% D) A' o) a& m$ D) L; E // diffusion, then run "updateWorld" to actually enact the1 o0 W+ ^. b9 Y/ n# a$ e6 g1 f
// changes the heatbugs have made. The ordering here is4 Q4 { k1 N* ^- T! b2 k
// significant!
6 D& `) j+ E3 ? x5 }* z T ( ^1 E/ W0 w; C$ p. E
// Note also, that with the additional
' L% }% U: a" E9 l // `randomizeHeatbugUpdateOrder' Boolean flag we can2 ~3 {8 f( s9 ]
// randomize the order in which the bugs actually run. s- g& O0 [& M' M/ b3 c3 P
// their step rule. This has the effect of removing any% f; ?' h* P2 Z# C- V4 o
// systematic bias in the iteration throught the heatbug7 Q# _* T8 t7 R3 I* b" [ {9 `
// list from timestep to timestep+ K- |% j* w2 @* w: w0 d6 b S) P9 k& k
2 p9 y @( b9 N* m4 F, t5 u) c% Z4 g/ I
// By default, all `createActionForEach' modelActions have2 O; Z) A+ M4 r# H: @4 o6 M
// a default order of `Sequential', which means that the
7 F+ k* R/ `/ U) \1 r9 L6 c // order of iteration through the `heatbugList' will be& }# |5 o9 m* g
// identical (assuming the list order is not changed+ s8 o: X8 i* n& B' |
// indirectly by some other process).
3 s ^, b F* s2 | % V3 `. P9 v+ h- O5 q" T2 [
modelActions = new ActionGroupImpl (getZone ());, A5 y# Z$ O# E+ P# q( s
. D: P0 _- k8 I4 C7 `& m try {
9 L* \0 q; S5 b4 x1 T6 _8 r+ P" i; P modelActions.createActionTo$message) ~. c5 _# p, x3 P( @- R
(heat, new Selector (heat.getClass (), "stepRule", false));
% N+ V& h n) p( q) F+ N0 W$ d) u } catch (Exception e) {* Y8 B7 ^, ~& J3 O0 W
System.err.println ("Exception stepRule: " + e.getMessage ());
* Q2 ]: P6 i; O' P }
* N6 |3 R+ a( a g$ B; a" |' _" q# I% ?+ w e, |7 k5 _
try {' K1 d/ C. O# Y5 ]) z3 f
Heatbug proto = (Heatbug) heatbugList.get (0);
1 X6 W. r) p' U Selector sel =
; D1 X5 O2 Y' ]4 B new Selector (proto.getClass (), "heatbugStep", false); _+ a& z& ^7 r' P
actionForEach =
6 e1 Q7 L* Z% m3 q modelActions.createFActionForEachHomogeneous$call
& }9 C' Y! R5 `7 i: T' a: \( g7 T (heatbugList,
# n6 I: P0 B5 g+ F new FCallImpl (this, proto, sel,
+ I) z) r# k8 Y |! c) O5 P8 t+ d6 j new FArgumentsImpl (this, sel)));$ `1 d8 C, E! x! Q- ^+ E# ~& f
} catch (Exception e) {, r& S, x9 q4 |0 a5 G E
e.printStackTrace (System.err);" _: \4 J9 @0 \3 K
}
3 o7 I) U/ t3 C4 i/ q# u 0 B6 l1 r T) `
syncUpdateOrder ();
: ^3 ]5 P( a# Q. [, e D2 \/ K% u$ q) q% c/ A
try {7 {- @4 B9 u" J& C$ \, [
modelActions.createActionTo$message 2 h& s, {+ u! }$ ]4 d
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 a) o& l9 R5 L( n } catch (Exception e) {
. e6 A( s, t, p! \7 f# f- u9 m System.err.println("Exception updateLattice: " + e.getMessage ());
, _% e+ x! I+ L }
& Z- G; O; c3 V) H- a
$ K9 w0 Q' L+ U! b) ~# ? // Then we create a schedule that executes the
: O6 J% P. u' Z$ k. e // modelActions. modelActions is an ActionGroup, by itself it
8 K' x5 d& a4 J# k6 H // has no notion of time. In order to have it executed in
\3 x" F# N; ]( p1 V // time, we create a Schedule that says to use the
( c& C( l' w% T8 V, E // modelActions ActionGroup at particular times. This
5 [, X2 {2 C9 T* Q // schedule has a repeat interval of 1, it will loop every2 p: k# {3 U9 \
// time step. The action is executed at time 0 relative to
2 C! _. S% k/ C9 Z. h3 F // the beginning of the loop.
! H& N6 U" g) C' ^: v
% E# q" E9 k; e4 s( n // This is a simple schedule, with only one action that is( P+ c2 w7 w, w
// just repeated every time. See jmousetrap for more" |3 L3 W& D y( B1 K
// complicated schedules.
7 s' l. L+ H8 R+ C0 g; c
5 ~/ ?& Z4 u* s( O3 P; @) I& {$ l6 A modelSchedule = new ScheduleImpl (getZone (), 1);
: {. O% N* {7 N9 j, `" y modelSchedule.at$createAction (0, modelActions);
2 z5 Z) S; E6 O' {0 t 7 M7 s! e1 f, s6 W. {6 w; I
return this;
) ^, `% g, g p$ a& P } |