HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 J& ?. _1 |( Q. g t, u) u
B# Z% o0 ]2 d8 [- n. v public Object buildActions () {
- p% p, w2 y1 [7 O; } super.buildActions();4 p( V! u% w6 @7 c
& ?8 \- N3 G. m5 p# M6 }" Z" P! m
// Create the list of simulation actions. We put these in
- b0 d. U4 N- W p& ^2 ~ // an action group, because we want these actions to be9 I8 H0 _, m( c! J7 G- F' F2 ~
// executed in a specific order, but these steps should
) ]2 @3 @/ Y! c. X! k1 f // take no (simulated) time. The M(foo) means "The message! l: D0 G6 C+ L0 q
// called <foo>". You can send a message To a particular
3 h4 w8 E9 |% _3 ]1 n6 e // object, or ForEach object in a collection.: y1 J: U9 P! k5 L8 r
1 N0 i3 q- k5 J8 O9 d' {3 r3 r // Note we update the heatspace in two phases: first run8 b4 Y# n# e( c& r
// diffusion, then run "updateWorld" to actually enact the
! E/ g- @) k( ]" B% B# E: R // changes the heatbugs have made. The ordering here is; q1 O9 g: `' c9 [* C( t J: l
// significant!, `3 ]0 K2 R: Q/ ?5 H( `( p
% P6 N+ l0 }2 c! T
// Note also, that with the additional- J8 @$ |$ A: M7 J" A5 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 m2 J$ E, T. \2 v4 L f
// randomize the order in which the bugs actually run9 }+ a; d; \7 ?* {3 {
// their step rule. This has the effect of removing any
2 C' Z; g# [ x s // systematic bias in the iteration throught the heatbug
9 j$ L% |! G* s% c+ a# H // list from timestep to timestep
+ ?: l; c. B9 f+ Z. t( J: l r % \8 b4 I" q3 T
// By default, all `createActionForEach' modelActions have7 K4 r8 n1 M0 j* b9 g
// a default order of `Sequential', which means that the
& E# F0 w+ i4 y // order of iteration through the `heatbugList' will be
, P& N- ]6 j8 X // identical (assuming the list order is not changed
9 a+ m; \; g9 O0 ? // indirectly by some other process).
$ L3 Q2 S! |; j& j2 }
, f- @+ n! ]- j, O! q3 T" ~/ d0 j modelActions = new ActionGroupImpl (getZone ());
5 k: O) K; W$ W; z
* b6 |+ [0 M" s4 ?# p$ P try {5 I" w8 c& G1 x5 Z
modelActions.createActionTo$message8 L" R" ?& K' u, F: i! {
(heat, new Selector (heat.getClass (), "stepRule", false));
& _4 N6 \* E0 q' n& v8 L* A3 ?! Z } catch (Exception e) {4 W1 w1 _- b+ C+ w
System.err.println ("Exception stepRule: " + e.getMessage ());
6 j9 ]( O* l7 C! M O+ g2 q }
0 R6 G- R4 A6 b5 N* I
; |( ^+ I3 R) ~8 v try {( l" K: A4 U& e- s
Heatbug proto = (Heatbug) heatbugList.get (0);5 g; ]9 {( Q3 `" [1 q2 K6 [
Selector sel = ( `, _- q+ Q4 N: J! r; h
new Selector (proto.getClass (), "heatbugStep", false);
. r7 }" p E+ H; u actionForEach =
1 H; Y' w% G+ A9 Y modelActions.createFActionForEachHomogeneous$call% ?# E0 z1 K3 o% j
(heatbugList,
) d8 j( m7 A9 F( ?# f5 E. H5 e Q" ] new FCallImpl (this, proto, sel,7 |* f3 B% y4 x, f5 ]
new FArgumentsImpl (this, sel)));
$ z% V5 X [8 ~ } catch (Exception e) {- B; I% x% X& C; S0 B' C# X+ w/ h
e.printStackTrace (System.err);
' p1 p' Q& h5 _/ o' w( b; ^, |' S8 M }
- S7 N `/ I- h' [4 n, i- `
" |0 q) B# |- d, [$ \) J! W# u0 w syncUpdateOrder ();
' D3 ]- ?& v" P* }
/ c6 _' T9 Q, {5 _+ A/ g# b: g try {
5 y0 R1 h/ Z5 d modelActions.createActionTo$message
3 k7 k0 [. \! M: s( F+ k (heat, new Selector (heat.getClass (), "updateLattice", false));& i; P1 F# Y, G( O! B8 Q
} catch (Exception e) {
* f( y0 z0 x( e System.err.println("Exception updateLattice: " + e.getMessage ());+ h& ~7 b- e/ t! c8 m; U$ M
}) L' z$ v/ f4 L* Q( R
& W+ u3 g% u2 ?6 g0 p# v
// Then we create a schedule that executes the2 D. ?/ P! @! j/ M. J
// modelActions. modelActions is an ActionGroup, by itself it) ]. {8 s/ B9 i @8 X0 h8 V
// has no notion of time. In order to have it executed in8 P0 N k" \1 L
// time, we create a Schedule that says to use the! R# e: ~8 G6 ^1 J2 H, c+ ~$ p; C& W4 U
// modelActions ActionGroup at particular times. This2 D) T" O6 {6 n5 \) L
// schedule has a repeat interval of 1, it will loop every+ L/ ]: j1 J! X
// time step. The action is executed at time 0 relative to
( R" a- D/ b/ C9 \: y* l // the beginning of the loop.
8 G& W" m. ]5 q3 ~3 p9 }3 ]! @( ~+ C& ~ O. F! E4 [
// This is a simple schedule, with only one action that is
; t* i. [. F& n4 \, q, y // just repeated every time. See jmousetrap for more N0 `- h% a# ^; \1 V; w
// complicated schedules." F3 e0 H, I- n h) X. @
+ r& I: r$ W$ M% i6 r) y
modelSchedule = new ScheduleImpl (getZone (), 1);- s7 x' Y( U) _
modelSchedule.at$createAction (0, modelActions);
: I/ a& G: ^* G) Z1 R9 j( D* O
5 p ]6 {3 \5 q4 J# [) ] return this;
, h/ N$ E! F$ s2 S) {5 C) M } |