HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. _4 Z$ p$ P0 a6 P& f8 e
! v3 ^0 B( @1 A
public Object buildActions () {
3 V; w0 M0 B4 T+ V6 a2 |0 } super.buildActions();
/ f' _, W+ @1 z
: M7 I& f" r6 h+ d5 k // Create the list of simulation actions. We put these in+ t% [( y* j6 J+ w7 |' |
// an action group, because we want these actions to be4 d& f. ?. C/ ]2 ]2 y
// executed in a specific order, but these steps should+ E& q8 s* \3 Z. I( ?
// take no (simulated) time. The M(foo) means "The message
/ Z! M( [! y: M' { // called <foo>". You can send a message To a particular. a8 ^; Z9 B0 f" o" V; r
// object, or ForEach object in a collection.
' Z6 X9 s3 \: s" i2 d* j j, u ) P3 ?4 t& g, x0 r. N' i: K o/ _
// Note we update the heatspace in two phases: first run( @. {8 E/ z( K. K' t
// diffusion, then run "updateWorld" to actually enact the/ [0 S6 B1 y& V0 p' f
// changes the heatbugs have made. The ordering here is
& R! T, q' m ^9 q$ e6 t // significant!/ K. ?( A( z, W1 F5 v7 B( ~+ m
$ V% c1 W9 t; \
// Note also, that with the additional" x# \0 ]/ i. M- z6 R
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 H2 Y7 d4 t1 F% ~ s! P // randomize the order in which the bugs actually run, t/ K2 W/ d& j5 E/ ^
// their step rule. This has the effect of removing any
; L2 k c$ B4 ?4 a // systematic bias in the iteration throught the heatbug9 O- Y" N7 Q. A1 u/ o
// list from timestep to timestep
! F1 }, V7 {: u' g4 G5 g7 p C / { P7 Q* o+ k4 C: d3 I8 ]/ @$ W; B
// By default, all `createActionForEach' modelActions have' ]8 l9 v2 j V
// a default order of `Sequential', which means that the4 f' D$ u- M* D" b9 s
// order of iteration through the `heatbugList' will be
( D" a/ N: O- e \$ y // identical (assuming the list order is not changed' _: q! @7 f0 D0 Y1 o4 b! b& {
// indirectly by some other process).
5 ^' c i8 k! B6 o
8 T n- \/ e X modelActions = new ActionGroupImpl (getZone ());, ]! k1 v! p; o X) V) W3 q0 `. }' u
1 X$ t/ B8 g6 c$ H3 [( o! | try {
- @% a0 X0 ^# z modelActions.createActionTo$message, j, ]: m) W1 K1 |9 G1 m
(heat, new Selector (heat.getClass (), "stepRule", false));
7 U3 |; c& Q! a! I4 |* T& k9 _ } catch (Exception e) {
+ K7 [9 o: ]" A0 f. m |5 t. p5 |8 O System.err.println ("Exception stepRule: " + e.getMessage ());+ z. h4 [# i8 p' x b4 T! C
}
; Q2 }/ W, c7 t! Q; E0 k! F( @, X9 _ j7 b
try {2 g- }/ W0 a( t. t! s
Heatbug proto = (Heatbug) heatbugList.get (0);7 l$ L6 b, k* J
Selector sel =
2 i' L! ~5 A# f' U; y2 H new Selector (proto.getClass (), "heatbugStep", false);0 i5 ?" O8 k" x# H
actionForEach =' \% b7 d2 Q+ B/ d5 Z$ @
modelActions.createFActionForEachHomogeneous$call
. q: y3 O& x9 S- `7 O a* l' D (heatbugList,: x& ?! F+ a* _6 W1 @
new FCallImpl (this, proto, sel,
2 ]) f% z* W; R6 U new FArgumentsImpl (this, sel)));
0 k# g1 Q0 S$ f# a0 ] } catch (Exception e) {
; E; X+ _- D: R( V. X e.printStackTrace (System.err);
" u# A0 V& t- d* t2 J/ w }6 v, C- f0 q0 p1 K5 @/ Z% w! k/ Z r
1 V5 [" {/ G. z* X syncUpdateOrder (); S- G, h; M- P6 V0 o0 L& V1 A
) j( O4 a) f$ w8 o$ E+ l: p" i8 V
try {
& H* k1 p0 K# H! C& f modelActions.createActionTo$message
' v$ p, W0 J# ?) r8 o, Y: _& ~/ \) U1 g (heat, new Selector (heat.getClass (), "updateLattice", false));
3 p. C3 a! B& y q0 Z3 X; a } catch (Exception e) {
9 E* ]8 V. t0 D; o System.err.println("Exception updateLattice: " + e.getMessage ());
5 K& n. c2 s a* ]( X }+ S) A/ i4 J7 n4 G; z; i
9 N6 Z6 F" y* v) k& L
// Then we create a schedule that executes the1 s' }+ k3 {* K" M, y, U
// modelActions. modelActions is an ActionGroup, by itself it, f! A1 D; G3 |) h/ @) h
// has no notion of time. In order to have it executed in$ u6 ^9 q M3 d
// time, we create a Schedule that says to use the
& v0 \" n' {' y1 C- R // modelActions ActionGroup at particular times. This. `- T" a$ B% Z2 \! `
// schedule has a repeat interval of 1, it will loop every
: V- ?7 l6 q+ M // time step. The action is executed at time 0 relative to* |( |+ B3 |8 s, O4 I
// the beginning of the loop. F- {. o" J% q9 B' t: m4 ^
i: z* Z3 R4 W
// This is a simple schedule, with only one action that is+ Q* H4 `9 I% f. D+ [" O' j
// just repeated every time. See jmousetrap for more
+ i$ }% z" f8 O! V5 T // complicated schedules.% f) N( |* b7 }
- a! P' b' _& Z# s modelSchedule = new ScheduleImpl (getZone (), 1);
5 \' F0 G; s: Z* c- }4 s( Y' r modelSchedule.at$createAction (0, modelActions);
/ o* Z8 l/ V" T8 l+ t, p 7 ^4 o$ s9 @0 ~4 T6 h4 A
return this;( E2 l4 n& ?5 Q
} |