HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 f7 `" {3 V0 v6 |5 q& e* F$ {7 d) [( d
$ j( x( G. s9 H9 E! b public Object buildActions () {, `' _" i' _( h
super.buildActions();3 N& ?/ _1 t0 U9 o8 Y
/ i: p7 k$ r7 r$ y% Q
// Create the list of simulation actions. We put these in, O6 A; d/ i, R+ J
// an action group, because we want these actions to be1 F4 {6 t5 s1 D( N4 \: ?" D
// executed in a specific order, but these steps should( [; c% Y4 {9 q8 K% r' i
// take no (simulated) time. The M(foo) means "The message5 [( G) a4 i4 U5 l0 S, J$ R: ~2 N- o
// called <foo>". You can send a message To a particular
% R( W9 u: S: q // object, or ForEach object in a collection.
+ z% c' c+ ^' f; v " l D: [) r, t) f' Z; ~* n) ~
// Note we update the heatspace in two phases: first run
! k, D; ^8 w9 R2 A( v. f" ^4 W // diffusion, then run "updateWorld" to actually enact the
% K' ]0 v: d' n# W, A( P5 S' ~ // changes the heatbugs have made. The ordering here is8 k/ p" {& g* c
// significant!! S7 u$ O7 I A
+ r ?- e, {) E: q // Note also, that with the additional7 Q8 m2 }! \& m
// `randomizeHeatbugUpdateOrder' Boolean flag we can! F/ J+ N' V5 l/ f7 x! r
// randomize the order in which the bugs actually run1 D m9 k/ ~6 p0 m7 M9 F
// their step rule. This has the effect of removing any
: l1 X' `9 ^: I) l6 g // systematic bias in the iteration throught the heatbug
7 g! E Z& y' h6 d6 P // list from timestep to timestep- f X2 L# T7 e0 L* Z* R
4 [! G' W, H, O1 k$ p; u# m
// By default, all `createActionForEach' modelActions have9 @5 n. V4 C$ {( y4 H# s
// a default order of `Sequential', which means that the" z5 U) S/ K8 [* R
// order of iteration through the `heatbugList' will be
h+ [7 W8 y, U5 j5 G" X+ B // identical (assuming the list order is not changed U0 x( u1 w# `! f- m8 s5 Z
// indirectly by some other process). L7 w! G7 q4 }8 T2 d4 U) Z
% i. @2 J2 u7 _9 y' ~ modelActions = new ActionGroupImpl (getZone ());$ Z# x+ [3 `% e r
2 A/ h9 @& j& @, Z, {/ }8 \" K try {
$ a( T+ u3 }4 M- n9 o modelActions.createActionTo$message
4 x) m1 ^* ^7 Z8 A (heat, new Selector (heat.getClass (), "stepRule", false));1 I/ S0 t& Q. s/ [* b6 ~& }4 A3 a
} catch (Exception e) {( }, Y! K: I- R+ m. i; ^
System.err.println ("Exception stepRule: " + e.getMessage ());2 M) l: X; C9 T$ C
}+ {3 Z9 ~, _' X, e
3 t7 Z. O6 v# ?$ g7 L try {
8 O0 j& {2 N5 g& E# t% {/ F Heatbug proto = (Heatbug) heatbugList.get (0);) `; u7 {1 \* A9 X* q
Selector sel = + v. y( T* |& Z7 ]7 Y
new Selector (proto.getClass (), "heatbugStep", false);
' e% m; p5 f' @/ W1 { actionForEach =: G5 r# _: \% U
modelActions.createFActionForEachHomogeneous$call( D( j- i0 ?( \' v
(heatbugList,1 c! Y, W+ }; ]7 S7 ~4 ?/ V( }
new FCallImpl (this, proto, sel,
* K" w! y3 Z. q% O0 w new FArgumentsImpl (this, sel)));
8 u5 t1 }( o" U- N9 A) @ } catch (Exception e) {
! J4 r/ `% _: }: `( }* t e.printStackTrace (System.err);
' Y- J3 u( m4 b) }* f/ T+ B }0 [/ {( z( q/ Z z; t/ o' P1 G* Q
/ J! d) T+ F' I8 n7 S
syncUpdateOrder ();
- {! N& I" a+ T
$ w4 {/ z4 Q2 L( ] try {
9 W0 T( s- G" [! B' U4 t+ H4 w modelActions.createActionTo$message . @) `8 D% q0 s! C# a& H
(heat, new Selector (heat.getClass (), "updateLattice", false));; A$ w0 T+ {' l
} catch (Exception e) {
* R4 c. b! j, z! S0 R: B System.err.println("Exception updateLattice: " + e.getMessage ());% D2 }) q/ Z _5 l0 T
}
- M( j7 Y+ c1 ^# t" `; I4 c; I1 @
( z* H9 z+ r' A7 ^4 ^ // Then we create a schedule that executes the! }( V. c( B5 [& k7 H+ m3 Y- f
// modelActions. modelActions is an ActionGroup, by itself it& h, |; | ?; M2 i* P
// has no notion of time. In order to have it executed in
! b- N. l; _0 B! F6 Q // time, we create a Schedule that says to use the6 k9 ^% \/ X; `8 C2 e
// modelActions ActionGroup at particular times. This
8 a& v( E# f3 n0 D& D // schedule has a repeat interval of 1, it will loop every
% g X a: ~+ ?0 }. b3 i L- s // time step. The action is executed at time 0 relative to
8 [8 M2 S. j* J( @- ^: L a: T) { // the beginning of the loop.
; x0 ~4 M3 p, K# |! m
, j$ I0 C! j2 @+ a5 B a$ P3 s- D1 O& e // This is a simple schedule, with only one action that is2 k9 o4 J1 X, F, Y! [0 N
// just repeated every time. See jmousetrap for more9 n/ T, T. G% }# m, l
// complicated schedules.8 C* v; Y/ Q1 F5 |
+ V1 h! U9 G8 o4 [8 ~& c1 m( ?7 Z
modelSchedule = new ScheduleImpl (getZone (), 1); ]/ ]" |/ j7 z" q
modelSchedule.at$createAction (0, modelActions);3 m- Z- H* X( R. J
9 S8 s- O) H+ `( U, l1 C return this;
2 J3 @0 W8 p- P' L } |