HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. v& z: ]: n5 A: w3 |- A, J4 C) O* t* t: w% E+ M# L% a) U
public Object buildActions () {5 N8 L. c) G. M. Y! r6 e" i/ r7 c
super.buildActions();
5 n7 ^6 j, ~7 B6 [) a5 j
8 m9 p# W, \0 n0 d# K // Create the list of simulation actions. We put these in. p+ J; i+ G% T: U W
// an action group, because we want these actions to be
4 z6 v8 R5 o4 W0 Y // executed in a specific order, but these steps should
( t! o' N1 |4 K1 v5 i, E6 S // take no (simulated) time. The M(foo) means "The message
2 a R$ S. V! ^2 c8 C // called <foo>". You can send a message To a particular
5 o+ g A1 `7 l; T- F X; { // object, or ForEach object in a collection.' N& {/ x( S+ {0 V
- H7 v/ R# ]1 m* H) U // Note we update the heatspace in two phases: first run3 ~ _, g. b. X' J' R9 L( u- i
// diffusion, then run "updateWorld" to actually enact the- a S5 I* u; A
// changes the heatbugs have made. The ordering here is
" g0 x! J( e" Z& y // significant!
8 G4 e6 _& X: J' i% |
0 z7 w' x( K8 @& s7 _ // Note also, that with the additional
+ O1 f) I( b; U // `randomizeHeatbugUpdateOrder' Boolean flag we can% a5 z9 d! D( I' @$ X5 }
// randomize the order in which the bugs actually run
) b# F! S1 H M0 o) q3 q+ U7 F // their step rule. This has the effect of removing any
7 g. g8 L/ h, O5 | // systematic bias in the iteration throught the heatbug
3 S1 U, _7 x7 C+ p7 X) i* B // list from timestep to timestep% p3 S' s0 G: l6 N; z' w
/ `' y; t7 y- ]+ @ // By default, all `createActionForEach' modelActions have! a' B) o- |, Y7 w( P
// a default order of `Sequential', which means that the7 j0 S( w5 E% k5 m" S
// order of iteration through the `heatbugList' will be; I5 z8 l2 v% W* g' T
// identical (assuming the list order is not changed2 l% s- k4 R) q1 ~
// indirectly by some other process).2 N- a2 M8 y# [8 W- ?# s; q' k3 A
! U& ?1 [+ x( V2 r" e! F6 I$ H modelActions = new ActionGroupImpl (getZone ());
* }" M0 P0 z' d7 ?6 {" }" o' p/ f! m! N
try {- o. i7 o0 H+ _2 e1 c+ F
modelActions.createActionTo$message8 u! a" D4 e0 r5 I; V. Y
(heat, new Selector (heat.getClass (), "stepRule", false));3 n0 U1 @$ t2 n$ @" _, u$ ?
} catch (Exception e) {
+ B9 o8 C; F0 @5 o% B0 f$ R System.err.println ("Exception stepRule: " + e.getMessage ());: \; [+ b) D) C0 m' L6 @ ]+ E
}, A+ s7 P8 S# |( @) b& a' e
# u: M( n3 i( N9 N7 v, |4 X- `1 M
try {
! N4 d: d# M, g1 ^% {6 V' t Heatbug proto = (Heatbug) heatbugList.get (0);7 d' w- @0 ^& {3 e: R. C1 Y. n1 D5 R8 e
Selector sel =
s6 W# ]7 z/ h2 q6 B new Selector (proto.getClass (), "heatbugStep", false);
& m, r& v4 Z* \: p actionForEach =2 l3 ^9 Y. f% t0 P% U2 B
modelActions.createFActionForEachHomogeneous$call$ A. f6 Z1 d! _- x/ _3 X* O/ ]& |
(heatbugList,
( G# a- M% C. W) T new FCallImpl (this, proto, sel,
~, _$ Z$ b+ }3 l5 k. Q% o z3 E new FArgumentsImpl (this, sel)));8 Z; \: G: l3 W
} catch (Exception e) {
0 P' m" m$ s. A/ o2 G e.printStackTrace (System.err);' c$ G# O W5 Y% [" c! Q
}/ z; q2 ~8 N* V: {6 K3 a
5 A& {' R8 J8 e& {8 m, f syncUpdateOrder ();
! F( R; Y4 q& D* x7 i" k' @( _ L# `) W
try {
. e, |; _+ z/ J0 [- V/ v modelActions.createActionTo$message - H* h0 l4 D/ B! [$ G3 j+ W
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 _$ H, P9 v1 `1 A* W* I } catch (Exception e) {
& E# K; C, s8 n! V# G System.err.println("Exception updateLattice: " + e.getMessage ());( k1 y* @/ @0 c- r" s
}
: g6 M1 s& [* C: h & C1 M/ A( D/ h; @/ I9 p7 o% g
// Then we create a schedule that executes the
. x3 S5 V% \* x, g // modelActions. modelActions is an ActionGroup, by itself it
. i, A7 A. a( a; w9 s) | // has no notion of time. In order to have it executed in* c3 X& t6 i6 G+ r( ?8 ]
// time, we create a Schedule that says to use the
2 c0 [$ O' y+ g; Z7 [9 \7 ? // modelActions ActionGroup at particular times. This! e0 y0 e! O0 J6 ~1 P6 T
// schedule has a repeat interval of 1, it will loop every. p [3 |7 Q5 H+ B
// time step. The action is executed at time 0 relative to
& A! X+ b8 Y5 @% o6 Z, \) y7 m& p // the beginning of the loop.) Z9 M# e3 W) R- n5 k: V$ N
5 T. ]2 `- X4 S4 O/ E& N# ^$ L: v, R5 ]
// This is a simple schedule, with only one action that is
1 A Q1 {8 {2 P* w" y" T5 a& J // just repeated every time. See jmousetrap for more
. j% g) Y w8 a8 }- K0 ] // complicated schedules.
% b/ I5 `. m3 E7 @$ v: `. V ; V. h$ \3 o$ |; x* B% g' A
modelSchedule = new ScheduleImpl (getZone (), 1);. O( M# W/ c) c3 V$ R: l
modelSchedule.at$createAction (0, modelActions);
6 k1 {* j6 n- \6 i# o1 J( r& Z * X& t" I' K; s8 l* }) z& o7 i# M
return this;% e3 k* {$ j: A% R& `
} |