HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ P p5 t) v% X3 `3 t. S& d) D2 |0 n3 u" j% D% l& ~
public Object buildActions () {
) S( d) n7 B d$ V4 u: X super.buildActions();1 f9 F! N& s2 [
9 h/ F( ]+ b1 m; E
// Create the list of simulation actions. We put these in
1 _. K5 M& D7 ~8 a // an action group, because we want these actions to be
8 l2 [- O% r8 q( l* D+ C! c // executed in a specific order, but these steps should0 e: Z9 J$ L! `$ k
// take no (simulated) time. The M(foo) means "The message
5 [! h% m7 I% {7 h/ X9 W: l // called <foo>". You can send a message To a particular
) }4 K: x1 _* B+ o' Y; A // object, or ForEach object in a collection.
7 [9 B0 T/ ~& o5 k: Q1 a 0 D( w7 x& t8 o# O2 s$ u1 `3 `
// Note we update the heatspace in two phases: first run3 L7 f5 K! L* M9 k
// diffusion, then run "updateWorld" to actually enact the2 X# t1 X1 E/ N
// changes the heatbugs have made. The ordering here is
/ |# x4 `3 ^) g+ v6 f4 b // significant!( |% D G: Z4 S8 e% f- o
' g) U. H) N$ K0 H% _4 M) K4 f // Note also, that with the additional
8 r: e# ]; H" ]5 t0 C3 O( R) u- L // `randomizeHeatbugUpdateOrder' Boolean flag we can
& ^) d Y8 q2 e& {- `( j# D2 q1 U4 n7 _ // randomize the order in which the bugs actually run
2 T; u: V( u; O* H9 o2 y7 p // their step rule. This has the effect of removing any* B2 k% p+ U4 J/ q* k
// systematic bias in the iteration throught the heatbug" b& g, S9 Q s ~3 L9 h
// list from timestep to timestep+ l; X3 }' w. X0 Y9 T) g
0 l8 h% L2 l: E+ w( J8 W U
// By default, all `createActionForEach' modelActions have/ x1 f% N% h* l3 D
// a default order of `Sequential', which means that the( b/ o4 p) x# T# u' f: q0 Y3 ~
// order of iteration through the `heatbugList' will be% L$ @' Z" Z8 A/ l: y$ C
// identical (assuming the list order is not changed- p# I; n: H+ [+ @/ e/ k
// indirectly by some other process).
6 ]* ]& @. d0 V( _1 [ n0 H7 {" L * Q) U0 a% M4 N X7 f
modelActions = new ActionGroupImpl (getZone ());
5 ?' [: ~% O( N7 s2 a6 K# f
0 Y* d$ [7 R( [3 m. |: c8 } try {
x; V) ^! D! Q9 j0 v- {% R6 G+ E. ^ modelActions.createActionTo$message
6 t w8 g4 M3 Z @ (heat, new Selector (heat.getClass (), "stepRule", false));
' {: _3 g" M1 z% E5 `1 [) } l1 s9 p } catch (Exception e) {
/ ]; [) N( h8 r% x+ T' h7 l1 f1 O System.err.println ("Exception stepRule: " + e.getMessage ());
1 I( ~! _+ `; c, N1 \4 ` }, p) \4 g6 l6 e$ e
' b9 w, `+ h& n
try {
4 d1 \ U/ G Q( I Heatbug proto = (Heatbug) heatbugList.get (0);; t. l# c& u5 Y& P
Selector sel = ) ^, }* A& W3 F7 V5 ]
new Selector (proto.getClass (), "heatbugStep", false);8 I% p" w ^( ^* a1 N& F* ^; f
actionForEach =/ y- P5 R' ], A1 z, z! R' q& E
modelActions.createFActionForEachHomogeneous$call4 m& P% p* S. t/ u' e' F
(heatbugList,
$ c, X- G2 f3 B7 g8 Q0 a) g new FCallImpl (this, proto, sel,0 ?; r1 [' S4 ?3 l- U+ H4 z! i
new FArgumentsImpl (this, sel)));# j* V2 h2 ~! G' m* X3 f8 R. {
} catch (Exception e) {
2 O E; S, M0 e P9 s& M e.printStackTrace (System.err);
" e. Z- x* R0 g# d7 K* m) k1 H6 q }
0 x0 B+ H0 L" r, X
1 w g) e7 q8 h# s; z4 n p syncUpdateOrder ();
8 o7 R# E$ k% x m! Y5 `0 q" \$ B
+ l. _% U- C% n% N try {
3 J% [! B* \5 T) M i$ W modelActions.createActionTo$message
* }, L% [1 S7 Y) g( C2 E- [. l (heat, new Selector (heat.getClass (), "updateLattice", false));
' {- t) {. I9 o/ N$ v, M7 h } catch (Exception e) {
6 p W9 `0 X6 s4 O2 g5 f System.err.println("Exception updateLattice: " + e.getMessage ());" @* M4 Y' e4 M. J5 L6 y
}
0 S) E- J. s! o! |; D$ a7 \2 O. F
! _6 q* ~# g. u) \5 I% N/ S // Then we create a schedule that executes the
) A7 s1 l# v! J! j: s% B // modelActions. modelActions is an ActionGroup, by itself it
2 ^# A+ Z) ?$ U, } // has no notion of time. In order to have it executed in$ {2 L8 @& ?! ?5 z1 d- a6 K: _
// time, we create a Schedule that says to use the
2 p2 E4 h1 D n2 l# G/ |. p/ m+ G // modelActions ActionGroup at particular times. This
* @# a' q H1 H* Z' D // schedule has a repeat interval of 1, it will loop every/ N/ B$ Y- s$ b
// time step. The action is executed at time 0 relative to8 a- i ~1 m2 U& r
// the beginning of the loop.
) n1 K, N5 q# W2 ^/ P+ }) d2 O7 x0 P6 a" Z4 W: A
// This is a simple schedule, with only one action that is
# L( V. F {: F$ d // just repeated every time. See jmousetrap for more+ Z& H$ m! A$ S- j+ Q/ _
// complicated schedules. W5 _1 g7 I8 `6 N% i$ q9 `
$ r& A2 l" \* c" A% P1 y modelSchedule = new ScheduleImpl (getZone (), 1);
0 {. Q* b( ?0 d8 x, i modelSchedule.at$createAction (0, modelActions);: v/ G8 ^4 Y- f4 N8 [
) O' M3 J3 c1 \* `' G return this;! c2 {$ Z, w$ c+ Z; x
} |