HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% L! n) K' o/ x
( F% u0 _) i4 U9 u1 `& g+ O public Object buildActions () {2 w m* Y; |+ \% y$ @
super.buildActions();+ \1 R8 b b( @0 E r0 i& F: E
2 v8 r/ [) T3 h& Q // Create the list of simulation actions. We put these in
; v# L1 k7 U) Q- }" @ // an action group, because we want these actions to be: n' j9 q% O8 I5 k7 N8 m) i. _; I
// executed in a specific order, but these steps should0 v; b) _& z2 w% ^
// take no (simulated) time. The M(foo) means "The message9 |3 o! C5 V) Q3 P
// called <foo>". You can send a message To a particular H, B7 d* f4 i1 P3 H( i
// object, or ForEach object in a collection.1 T; W P3 B5 s& ~% R. R8 A
/ D( M+ ]$ N! @
// Note we update the heatspace in two phases: first run
9 K: i0 p8 ~) L! b% c7 p // diffusion, then run "updateWorld" to actually enact the; |6 n; o$ }% L7 C; E; N3 c
// changes the heatbugs have made. The ordering here is `# T; v9 @! |: X% y: R( M/ f
// significant!
8 t- S! F7 f7 C; p# i9 j6 H 7 P0 D* S6 b0 e% z: K
// Note also, that with the additional# J! l5 w) f3 c" m' b
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 N0 V4 F+ f4 y B( R
// randomize the order in which the bugs actually run
; b9 B8 n6 ]5 R( j9 }( E9 e // their step rule. This has the effect of removing any
: a% d- m% T+ l4 t2 G3 u0 F. f- a! ? // systematic bias in the iteration throught the heatbug* Q2 z& _- i/ F1 }! J( y
// list from timestep to timestep
6 P# i1 A% T; z5 R; W3 t- ] - d& g, P" A$ k* d8 s: ^, Y
// By default, all `createActionForEach' modelActions have( P2 ^( Y$ W8 k# L- }" w3 {( x
// a default order of `Sequential', which means that the
! D# w! }5 g' R% | H% s! g' a // order of iteration through the `heatbugList' will be
7 @' \( S6 I) _8 a // identical (assuming the list order is not changed4 ~+ P4 F! N+ F
// indirectly by some other process)." S, I0 O: w6 l3 l% ]' X4 @
3 t/ Q/ {; K$ \: @( j modelActions = new ActionGroupImpl (getZone ());
+ D3 h+ ]+ v y7 T9 S- D# L c" P, I- F2 [+ q8 g4 m0 h/ a
try {- C1 y7 a% S W' J0 D
modelActions.createActionTo$message5 M) _/ b+ m/ F. O. |
(heat, new Selector (heat.getClass (), "stepRule", false));
7 q( k' X3 d; `! t+ e } catch (Exception e) {" `7 n9 @, L" h! i! A% a7 v
System.err.println ("Exception stepRule: " + e.getMessage ());1 Z& R' i2 ~3 w8 F" O* `( _; a
}5 j9 h% Q P& c" ~
5 I! @3 X/ d- r7 F8 {- Z9 W try {+ q) F/ r6 w/ ~
Heatbug proto = (Heatbug) heatbugList.get (0);
( P5 ]+ a: c* y4 n" F# H9 l Selector sel =
9 k9 U0 m4 _1 s5 g8 A+ N0 K new Selector (proto.getClass (), "heatbugStep", false);( ?, C5 t' m2 X4 H7 o' S, c
actionForEach =* ~! }: ]2 [7 _; [" r& a
modelActions.createFActionForEachHomogeneous$call# T- i. [: X: D8 i3 e$ p
(heatbugList,
, Y1 {# P. t* T) ? |( n) K new FCallImpl (this, proto, sel,
. r' _6 ?0 U7 S3 o new FArgumentsImpl (this, sel)));" H. B( r7 j; R+ Z- m; `2 n8 V
} catch (Exception e) {, P( |0 [+ o, p7 S6 o
e.printStackTrace (System.err);1 F! ~2 b6 M& X1 b. m
}' ^$ _- F8 D! x% t, r2 u
2 O5 s3 D! S2 k) `+ v0 h5 b* c, ? syncUpdateOrder ();5 m% e, s3 V* ^& R6 K
( B7 u6 f% {2 s5 D+ ^6 G try {
7 z5 W4 O- q& e1 o modelActions.createActionTo$message $ W W, F, b. t6 o2 z8 S
(heat, new Selector (heat.getClass (), "updateLattice", false));
; G7 i6 V) m; G7 v7 R+ x7 o } catch (Exception e) {
6 n% Y3 X5 Q U/ ?/ n; r System.err.println("Exception updateLattice: " + e.getMessage ());2 U- E- u( J% S4 t. V1 g y, l
}9 O8 N7 }2 h" B ~' d
$ i6 R& }; l% G5 M$ z
// Then we create a schedule that executes the1 K+ j% X! W5 J- N$ M ]1 X! S
// modelActions. modelActions is an ActionGroup, by itself it1 O4 ^7 {5 m& L! _
// has no notion of time. In order to have it executed in' G& b2 j( R" [( O2 r2 }8 U% l; m8 b, n
// time, we create a Schedule that says to use the9 u N( u" R l9 H+ E
// modelActions ActionGroup at particular times. This
) J0 a' _0 ]) E4 |5 Z# _ // schedule has a repeat interval of 1, it will loop every3 d: ?7 a5 Q/ E: A3 ~- T" X
// time step. The action is executed at time 0 relative to2 J8 `0 k( V* X- G( q8 E6 b
// the beginning of the loop.. G9 U# [# G! f8 Y% {
3 y2 F: l5 E: u // This is a simple schedule, with only one action that is
' L" C* {4 Q& B- d; }6 O // just repeated every time. See jmousetrap for more! E$ u' o6 K& B) w( e; k
// complicated schedules.7 H5 ^3 k% w& ?9 t! J/ L
& a2 ]& \/ ^$ d5 I1 r
modelSchedule = new ScheduleImpl (getZone (), 1);
5 u8 j* W" y, Q4 }% `* V: W1 r, E modelSchedule.at$createAction (0, modelActions);/ P( ?! E: I6 ]4 y) x9 J& o
) }2 L8 [) ?! w4 V, ] return this;- F1 n1 H1 {1 V8 a' c
} |