HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) t; d6 |# E) V9 Z3 _" v7 O: S
. k( h% r% S# r/ K
public Object buildActions () {
& ]* ^: @' y9 A7 W+ O% c super.buildActions();& h/ P9 M0 ^) D/ Q% D% \
. N' g$ C" j$ d: E; O' m // Create the list of simulation actions. We put these in( P" A$ @0 \' ?' {) C
// an action group, because we want these actions to be
$ v' \9 I* A. q* h6 D3 o // executed in a specific order, but these steps should
' E: d: s1 K0 @, S // take no (simulated) time. The M(foo) means "The message- w, s% Q( v6 k v; i
// called <foo>". You can send a message To a particular' Z' K$ g( P. M! {- S
// object, or ForEach object in a collection.! b/ E* o5 B& b
/ \2 n) D" g' D0 B2 j: {) E // Note we update the heatspace in two phases: first run" I& S! H( X2 x
// diffusion, then run "updateWorld" to actually enact the* Q. p% ]) c7 x, {, O/ F7 F) u
// changes the heatbugs have made. The ordering here is% \( h8 a. E1 K k1 m. |
// significant!6 S7 k( Y( G8 n- ?
8 E7 E* E" z! C% A. N* K
// Note also, that with the additional" y, E! y. J0 z. I# q" G* g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ f6 X' L- F0 i' R5 [. Y // randomize the order in which the bugs actually run
- d; \5 Q5 o& F( ]2 C0 B5 r( V' r9 I0 l // their step rule. This has the effect of removing any
! h+ B7 ~+ D2 X // systematic bias in the iteration throught the heatbug: J8 E, {2 b/ I
// list from timestep to timestep3 k( ?8 r$ Y+ G( I/ a
2 d* H* T# P+ w2 } ` // By default, all `createActionForEach' modelActions have& \7 [. \9 S$ o( b
// a default order of `Sequential', which means that the
# l3 R6 P, m U& ^0 o3 _$ t+ W# t* I // order of iteration through the `heatbugList' will be
- c: p4 a0 J, w+ c# _ // identical (assuming the list order is not changed) z; v/ q1 y) Y# k6 c
// indirectly by some other process).
# s9 R3 A' t! K( ]- s$ D3 `
7 M5 l4 ~2 w! n5 _9 P modelActions = new ActionGroupImpl (getZone ());
, J9 @, n$ q' T B' Z6 L! ]* k: I ~9 X \8 ?
try {; k q: F9 l b6 u: P
modelActions.createActionTo$message
" |1 c+ i& }+ j9 Y (heat, new Selector (heat.getClass (), "stepRule", false));
3 a0 s' k- D# O; D1 l- B) D } catch (Exception e) {/ T5 B! ~: P, ]$ D! K
System.err.println ("Exception stepRule: " + e.getMessage ());
' S; `+ ], }5 s B* | }
* `4 Y/ \1 ~8 `8 U) V* ]- }
! U' ^/ g2 ^; l0 j" h! r try {& P3 _; l0 A, e j( n4 `- C, E* Z( Y" r
Heatbug proto = (Heatbug) heatbugList.get (0);* x& }* l; N+ ?6 x, g' h6 x
Selector sel = . ? I& ]: ? ?% ^* P: d7 F8 D7 G# p
new Selector (proto.getClass (), "heatbugStep", false); F. u0 z+ g6 w$ ?
actionForEach =9 A/ U+ h& z' l" d' y6 {, n
modelActions.createFActionForEachHomogeneous$call+ i- X0 J7 l: m6 d% f
(heatbugList,
8 c6 _! H x+ x5 h$ O7 b; d$ w new FCallImpl (this, proto, sel,
- a f, _: x2 c+ U7 y, }" Z new FArgumentsImpl (this, sel)));3 ]( H, Y f& F" D# R/ d/ p
} catch (Exception e) {
! w. _5 R* l) j e.printStackTrace (System.err);- T0 E# d5 y5 [0 D$ T
}) I0 B9 T$ x" H! N
' @6 }5 {3 L9 H. F. l' ?+ ]1 i/ ` syncUpdateOrder ();8 d4 L1 m2 h. L+ I& @8 n
* `' H3 \3 a3 l: e9 f O9 L
try {
% R, h* B2 ^+ H: `* P% K modelActions.createActionTo$message
4 _8 N" p( m( D (heat, new Selector (heat.getClass (), "updateLattice", false));/ M' X0 U2 Q4 ^% ~4 \* m7 @8 t
} catch (Exception e) {% p) O/ H( x, |( V) l! e
System.err.println("Exception updateLattice: " + e.getMessage ());# S9 o+ e6 h, e" n1 b) M. H0 Y
}
. \9 G1 y4 O9 p; G3 ]9 P5 T. [% T
8 D) Q2 R2 z9 Z: Z+ t# l // Then we create a schedule that executes the
7 ~" r) \, E6 D% z$ k* Y // modelActions. modelActions is an ActionGroup, by itself it7 _0 r# l; x5 x% G8 v: [ K7 I
// has no notion of time. In order to have it executed in
$ g5 G) S5 f* h6 o1 Q // time, we create a Schedule that says to use the
4 N5 E8 y/ N0 u4 g // modelActions ActionGroup at particular times. This3 p4 `6 y$ ^" C$ q2 S8 h& p
// schedule has a repeat interval of 1, it will loop every0 F- t) H7 ~5 w# t
// time step. The action is executed at time 0 relative to4 x2 y: Q3 E8 C/ N. \
// the beginning of the loop.( ]& C( a1 V( ]' z2 o$ ]
! c+ i- w: w) z7 _# Y/ @
// This is a simple schedule, with only one action that is" a( e2 v6 P) X2 S3 f
// just repeated every time. See jmousetrap for more
% M( v W+ V V+ Q' } // complicated schedules.: J" G+ }+ g3 L7 u
- Q; P$ i; F- }0 B: e: E7 ?
modelSchedule = new ScheduleImpl (getZone (), 1);% @8 N/ C& S' b9 o E
modelSchedule.at$createAction (0, modelActions);
$ t E# ~) P0 b" j) |* f {% M- X A
. j. w& t; Z' _& K/ @4 F return this;
9 G# h2 s: w& Z0 C! g; v# ~$ x& J# B } |