HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ ~2 J9 p* c1 M1 z' O
" x. Y' u& `' O/ F( d0 G3 B
public Object buildActions () {' D" Q" y3 p0 b
super.buildActions();
7 J# U N6 Z' { 8 s8 O5 }# m1 s# h# k
// Create the list of simulation actions. We put these in
( I$ a* B* ?* c) P // an action group, because we want these actions to be3 y, F% D: _% V" v, B5 n7 c* ^
// executed in a specific order, but these steps should
: m' I; r3 V R1 q& n // take no (simulated) time. The M(foo) means "The message6 Q3 M* u& _ p7 s4 C/ Y+ @
// called <foo>". You can send a message To a particular, R- d, n1 L( s$ c2 ]; h% I
// object, or ForEach object in a collection.* _2 c/ F3 W3 J# ?3 G
' h z" L' u& T+ M // Note we update the heatspace in two phases: first run
3 F1 [1 I. O/ ^ // diffusion, then run "updateWorld" to actually enact the( K6 u7 {+ e: F: Q& u8 L
// changes the heatbugs have made. The ordering here is, Q3 V1 ]9 _ h, i
// significant!7 |: x) B. U% ^
) S* Q2 J0 X4 ]- J$ S // Note also, that with the additional9 x6 C4 n) C2 W% z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: Y+ K0 T. p* X2 x* b7 l // randomize the order in which the bugs actually run! R2 H. q# ?$ D1 j% d
// their step rule. This has the effect of removing any) n& ^& n4 m2 _! ]0 l3 a8 z
// systematic bias in the iteration throught the heatbug
, Y( h& e0 `8 l5 l- {7 A# Y8 ` // list from timestep to timestep, B/ z6 p' S* A. Y8 z# v W
$ E6 I8 L4 ?) R // By default, all `createActionForEach' modelActions have
' Y6 K6 o2 n, w$ m4 W, l' B: Q9 F1 s, Z; k // a default order of `Sequential', which means that the* _# [0 ~0 @7 Y" J* M8 e
// order of iteration through the `heatbugList' will be9 U. C% C( Z5 M, S
// identical (assuming the list order is not changed3 r; R5 q: N! B8 \ ?
// indirectly by some other process)." C( I5 _( {: U- B) d
8 `' q5 p/ m: j( ^5 h& J
modelActions = new ActionGroupImpl (getZone ());+ n' _/ @! }2 c$ w4 [
, A* _; ?2 q! v9 J8 | try {
; n! F9 o3 I& a1 H I9 [" w modelActions.createActionTo$message# u6 Z2 w. `) ? V0 n, U) m' k
(heat, new Selector (heat.getClass (), "stepRule", false));( {+ S/ K/ p8 S' `6 E
} catch (Exception e) {
, y- p- p9 O' V) \ System.err.println ("Exception stepRule: " + e.getMessage ());0 P" ]9 T2 e2 J: \4 W3 v
}8 D) ~# r+ f" G; n; \4 y; b7 ^2 H
! G% D) C) w; R7 A6 @. E try {
6 i( M# f/ ?& x+ F2 C Heatbug proto = (Heatbug) heatbugList.get (0);
8 Y! ?5 R% B i- d# g Selector sel = - K, I# ~7 H L4 g
new Selector (proto.getClass (), "heatbugStep", false);0 c% a: C) z- o' K p: K, X
actionForEach =
5 W* o; o" G* M/ z% l: W modelActions.createFActionForEachHomogeneous$call
2 r) M4 A: \( ^" `: K (heatbugList,
1 G4 C5 V5 H% f new FCallImpl (this, proto, sel,
6 H4 Z) ^$ L' M2 s/ H new FArgumentsImpl (this, sel)));/ c0 x( Z/ _, K" b# f" K, p5 I
} catch (Exception e) {
+ j5 |9 \& k9 R6 r e.printStackTrace (System.err);: c" q& O/ |$ I
}! n/ Z0 Y( H( a+ K; S A' P$ `- i
1 p% W. V0 u/ b) O
syncUpdateOrder ();2 q; h/ q5 z o, a. s5 i" e
+ H" o5 q, I5 a8 e* A3 o$ M try {0 h, z9 Y& R" ]/ d( A
modelActions.createActionTo$message 1 c% X/ c' R0 O6 |
(heat, new Selector (heat.getClass (), "updateLattice", false)); A0 c) Q6 t. G- Q5 T* M
} catch (Exception e) {
' X @" b3 C- A' ^8 e System.err.println("Exception updateLattice: " + e.getMessage ());
& m- u3 ~0 k. r5 F }. X# M. ? y& x% R2 z' K
! O! G: R& u( e2 p // Then we create a schedule that executes the$ k% o+ D0 x( q- L! D$ p5 j
// modelActions. modelActions is an ActionGroup, by itself it; m6 I, Q# ?# h& a
// has no notion of time. In order to have it executed in- Q( v) v6 h) B
// time, we create a Schedule that says to use the- Z) Y; N6 a7 e
// modelActions ActionGroup at particular times. This
% N/ `/ [! N5 A' B // schedule has a repeat interval of 1, it will loop every
1 g/ R1 G9 y. A, J9 l1 T0 s* M // time step. The action is executed at time 0 relative to6 g. j( g( k' S, a! I3 ]$ a% n
// the beginning of the loop.2 k# u$ I9 C5 k1 b9 @" K( n
4 N t. K! k: V( |% e) v // This is a simple schedule, with only one action that is
# U B# O9 Z5 C, h // just repeated every time. See jmousetrap for more8 \5 u9 Z& Z6 M2 n
// complicated schedules.
/ Y( V9 O! N; p' U- ] `( \8 @% E , J R) R Z* e/ j
modelSchedule = new ScheduleImpl (getZone (), 1);) `, ~: J. j( L1 b6 h; H
modelSchedule.at$createAction (0, modelActions);- E7 Y" ?! z7 c/ I
) R7 ^& J: ]. J; ? return this;' d4 A# E# ~0 \: U/ s
} |