HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& c4 F/ o2 G. H4 f d1 e8 w2 }0 G! K& c( _' g1 U# ]
public Object buildActions () {: [( o0 d/ Y! X$ W: n
super.buildActions();
) m" \; ? z2 g2 m8 A6 w; }% _' X
; R& S2 A/ _2 I // Create the list of simulation actions. We put these in
; K3 @; P4 e$ a* q& Q9 d // an action group, because we want these actions to be
. R8 x' C' W; X; l // executed in a specific order, but these steps should! n- K& ~* S/ a
// take no (simulated) time. The M(foo) means "The message
9 v/ x9 y! C! s% ?3 _0 n- { // called <foo>". You can send a message To a particular( f! o! I) V: d1 a6 T2 \
// object, or ForEach object in a collection.
e0 k; Z9 f1 h' r B # j$ v/ s" A1 x- I
// Note we update the heatspace in two phases: first run5 t4 W9 H5 Y o' V; a- M
// diffusion, then run "updateWorld" to actually enact the6 p6 q2 R8 t$ ] O. Y9 |+ j
// changes the heatbugs have made. The ordering here is$ }$ W& R* D+ w/ y
// significant!
) }- a- Z4 \, a+ m; b5 Y& j. C" E " M/ K& A( f' p2 b
// Note also, that with the additional
. A4 M5 C9 o" v3 a j: |7 a // `randomizeHeatbugUpdateOrder' Boolean flag we can# r5 J; S2 d) w& U( ?; _
// randomize the order in which the bugs actually run# S& G# N8 E) V5 C) ]0 W
// their step rule. This has the effect of removing any
0 ]7 i2 l l! P, _ // systematic bias in the iteration throught the heatbug
4 h6 d7 j1 n' K2 M( G5 c g // list from timestep to timestep6 G$ B2 z* q7 f- W
+ M5 j3 x& N( ~ N, Z // By default, all `createActionForEach' modelActions have# g" I9 r! C3 s2 u7 e
// a default order of `Sequential', which means that the4 Q y0 Q* }9 k" v
// order of iteration through the `heatbugList' will be& E$ z' c1 B* a% @
// identical (assuming the list order is not changed" ?1 c( `5 f& q* e
// indirectly by some other process).
h) H; k% I+ ~. S2 ^) C3 T" t6 |
: k6 ~. V5 ?) W Z; W% L3 z modelActions = new ActionGroupImpl (getZone ());( O+ m* A( `9 N5 {+ C
' e3 k6 L! P# O, x2 l
try {+ v3 m8 Y- p; f- k' ?
modelActions.createActionTo$message# Y& P0 a9 C% Y. O
(heat, new Selector (heat.getClass (), "stepRule", false));
5 x: p+ P7 t$ Z6 w6 w0 H. ?0 y } catch (Exception e) {
6 F/ K5 u0 f; g( N4 l1 `- h7 L System.err.println ("Exception stepRule: " + e.getMessage ());
2 c" V; w- z& W. ^ }
/ [4 N1 k# V K& Y0 `
8 K# ^- o; m- I* h+ x$ p try {7 w8 w8 R- H5 b: M3 B! K
Heatbug proto = (Heatbug) heatbugList.get (0);
$ W5 d# Z8 ]0 f0 A- @! Z8 }) D Selector sel = 9 `* f. Z. `, ]) c
new Selector (proto.getClass (), "heatbugStep", false);
* v, k3 N/ ~* a% r: y# q: |# f actionForEach =
/ A4 R+ [3 K. t2 F% y+ d modelActions.createFActionForEachHomogeneous$call/ `1 b" K/ |: B2 E; M
(heatbugList,
8 I/ t+ Q8 l1 L new FCallImpl (this, proto, sel,
1 O Z- }2 ?& y% _$ c+ D new FArgumentsImpl (this, sel))); {8 E3 L* d4 T0 F* a
} catch (Exception e) {' H; N5 V: a2 u
e.printStackTrace (System.err);+ f( e; z# Z+ f
}
2 p; m; F8 y( m ' e6 `% X, Q# m
syncUpdateOrder ();! \0 Z0 D- y, u% [( }
; u+ i% e! l2 r- ^! I* m. Y
try {" S. W9 P' W5 j" {8 g
modelActions.createActionTo$message
3 K5 D' w; _. d/ ` (heat, new Selector (heat.getClass (), "updateLattice", false));
: T9 V/ b. J, ?, N/ ` } catch (Exception e) {3 C3 ~1 [. d- x' E
System.err.println("Exception updateLattice: " + e.getMessage ());
: B7 L, Z u' Z }# s# m+ z% r4 @* F4 A1 n
( n: s1 Q; w% ?; R, L% i
// Then we create a schedule that executes the/ d+ R) @$ Z) Y* C; z) x
// modelActions. modelActions is an ActionGroup, by itself it4 M; [7 r7 f8 `6 j( U$ k
// has no notion of time. In order to have it executed in/ d! R4 m* \$ Q
// time, we create a Schedule that says to use the9 v) x, y' U+ E) L1 M
// modelActions ActionGroup at particular times. This0 L# S" `2 w G9 e3 q
// schedule has a repeat interval of 1, it will loop every
, I0 `4 j$ l. S# s# y6 k+ W // time step. The action is executed at time 0 relative to
( {3 u/ R, J# I& J: Z // the beginning of the loop.
+ [3 B6 q* n8 q3 ~7 x: {: t( V* R0 h8 f" ~" V5 |" M
// This is a simple schedule, with only one action that is
; H8 \, E$ a0 p3 g/ D* M // just repeated every time. See jmousetrap for more
) [+ c* E: q# o1 E; ]8 h+ m$ f // complicated schedules.
) m5 K; j! ]# ?# v
7 E% V! S7 j) N+ }- R: W modelSchedule = new ScheduleImpl (getZone (), 1);
+ t4 A! j! _- ^$ {" Z. ~ modelSchedule.at$createAction (0, modelActions);4 ?. x7 t8 |& Y' ^
* P/ O6 S" S+ @+ t0 C' ~
return this;5 y) p! e6 `4 a8 u$ e1 d8 W
} |