HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 ~0 q! P) Y- R1 q! q# h6 _5 M' b9 T$ K# y2 y A
public Object buildActions () { |* l2 \2 H/ c$ R$ M) @
super.buildActions();
3 ?+ I+ g& v y8 j: d4 R/ P8 J
# d( ^' f7 h8 Z$ \/ C // Create the list of simulation actions. We put these in
1 Y6 L, G7 }# Z% G1 S6 I* Y. ? // an action group, because we want these actions to be. V3 u3 V' b; ^& G' M4 @* Z
// executed in a specific order, but these steps should2 a/ i# X9 y9 b+ b U$ k0 [$ \ I, e
// take no (simulated) time. The M(foo) means "The message8 t x0 }2 j% ]% a5 |. t F- i& P( ?
// called <foo>". You can send a message To a particular) _0 u% s1 ]8 r. f& u
// object, or ForEach object in a collection.
# j, n& k, @( z% J
* G$ G9 W, h3 B% } u: M // Note we update the heatspace in two phases: first run: T/ M9 [( _8 j/ k d6 O; b% _" j
// diffusion, then run "updateWorld" to actually enact the& U1 E# J ^; b8 I
// changes the heatbugs have made. The ordering here is+ o' A M8 \" q1 b# M
// significant!8 Z ^* w7 g) V2 ^
# h |+ P' R, e2 v3 x j$ Q
// Note also, that with the additional
2 L( F1 N" S- }: t. C( V% b // `randomizeHeatbugUpdateOrder' Boolean flag we can7 s( I9 T& I( U; _; @ @" ?% |
// randomize the order in which the bugs actually run
) C4 `& M% r# U v% U // their step rule. This has the effect of removing any
) ^) s$ c: u+ f. D9 \ // systematic bias in the iteration throught the heatbug
3 h. v! G( E3 r d+ S5 Y( | // list from timestep to timestep9 a4 L, `' x, _8 S. h' Q
! H9 \3 o6 _& R* M // By default, all `createActionForEach' modelActions have
}% _" g' [: c$ c+ y // a default order of `Sequential', which means that the
& A U8 K }' H0 a // order of iteration through the `heatbugList' will be
% G9 F1 t9 ]! p( R // identical (assuming the list order is not changed
- x* l" c, x# L // indirectly by some other process).
& J& E+ y. Z% \$ [% n0 R9 ?
& b% f3 P/ W+ ?* J& D9 | modelActions = new ActionGroupImpl (getZone ());
5 y, W; H/ P3 I3 M. V5 J9 ~7 z% i- ]" z: j. r6 q1 u
try {
) |4 k8 ~0 z( |6 l S modelActions.createActionTo$message4 c0 M7 H: Z' _1 m2 i) h& w( w
(heat, new Selector (heat.getClass (), "stepRule", false));) N P! ~& y2 y. d/ }
} catch (Exception e) {
Y0 F2 R/ k5 y, t0 l& _( L, o4 h System.err.println ("Exception stepRule: " + e.getMessage ());
! D5 ^2 P. d: x" i' [' I% C* T }
6 i* p% n$ |5 N; k5 j5 \) A& ~& a# Z P$ X% ]
try {& x3 W! l/ I% n
Heatbug proto = (Heatbug) heatbugList.get (0);
) J/ s. v5 \' D# v9 d Selector sel = ! r0 I8 U; {2 Q, l9 R$ Z
new Selector (proto.getClass (), "heatbugStep", false);% Z. T6 I* @4 g" k. x8 c
actionForEach =
6 n R5 J Z) s+ R modelActions.createFActionForEachHomogeneous$call
7 p+ ~3 a- b8 z P* N( z, { (heatbugList,0 C# S+ {1 ~; I& Q
new FCallImpl (this, proto, sel,1 n" Z9 Z; f3 n- b6 O
new FArgumentsImpl (this, sel)));- M A* b4 P6 D
} catch (Exception e) {! R6 {. e6 K8 Q
e.printStackTrace (System.err);
9 u h0 g# h" m& r6 L; K' R9 z/ u }6 Z* d5 T# u9 p; S/ M
3 l- F' H5 n- `
syncUpdateOrder ();
+ Y) R' [* a# s0 T: h0 d# A9 f3 U/ T( Z$ X
try {% q3 N5 l) S( W T1 E
modelActions.createActionTo$message ~# q! S* c" c# n, y
(heat, new Selector (heat.getClass (), "updateLattice", false));* n% a! Q' l; d" x: V
} catch (Exception e) {7 Y a9 C/ m( P1 G8 `
System.err.println("Exception updateLattice: " + e.getMessage ());
0 o {: k8 b) B }
3 |3 D, p2 Z9 X' _, X
5 n/ d1 c" i8 h' F // Then we create a schedule that executes the
' \: f. o/ B8 u* c3 @9 }: p0 p6 f // modelActions. modelActions is an ActionGroup, by itself it9 Z+ f- z8 e8 l' o( U
// has no notion of time. In order to have it executed in" a, C4 M d8 h2 a
// time, we create a Schedule that says to use the+ U( ~8 P9 |3 m. k) {8 S
// modelActions ActionGroup at particular times. This3 L; D! E. s& V8 K8 Q
// schedule has a repeat interval of 1, it will loop every# X* P; P: G+ B
// time step. The action is executed at time 0 relative to
( F* z+ y. u. m // the beginning of the loop.9 c( B. ]7 L8 h" ~# K% q3 [) l$ h& z
7 O6 x% |8 ~% i& q. d% u% q, n // This is a simple schedule, with only one action that is1 M% ?9 X$ R% _; {
// just repeated every time. See jmousetrap for more
0 B6 Y- B+ a5 ~- b" Q // complicated schedules.6 h; J! W/ C+ Q. R0 ^
5 _7 I1 v+ O) Q }4 B
modelSchedule = new ScheduleImpl (getZone (), 1);
& O/ ]! b" G3 x* I! q+ R3 G# ? modelSchedule.at$createAction (0, modelActions);
7 R7 t9 e0 A+ s
3 N% g# o* W/ h3 v L( ~1 i return this;3 ^% r! n' c: ?, K* _) i3 B" _
} |