HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# x) Y, b; w5 C
: \/ ?. D! s0 ?
public Object buildActions () {' D8 G( e: d* S: ?; P* Z0 K4 C* g% f
super.buildActions();
, L. J: R' D! R5 G
% A" _% S! M; R) O // Create the list of simulation actions. We put these in3 G: U# C% h+ r! l2 @+ B
// an action group, because we want these actions to be
# i9 N V4 K7 L* m4 V5 v4 s // executed in a specific order, but these steps should
2 m7 m/ `) ^6 s t4 M' j+ U- l // take no (simulated) time. The M(foo) means "The message
5 M3 C* y0 w9 F2 _& W/ q% D) ^ // called <foo>". You can send a message To a particular
9 Y" F9 t6 s( D0 M2 i // object, or ForEach object in a collection.
' T, }8 F" X j, [5 |. q
6 Q) z+ y% C& K1 ^9 k2 m // Note we update the heatspace in two phases: first run; }: i6 E7 Z G
// diffusion, then run "updateWorld" to actually enact the
, {8 v8 o5 _) l4 ?/ F* E // changes the heatbugs have made. The ordering here is6 m$ N) x: n5 u' B% [% v: |
// significant!+ N3 t/ n+ x& d6 k/ S9 ?+ t
% d# m" s+ p% h+ F // Note also, that with the additional
; }4 n# \' ^+ N9 W0 w2 N8 g/ g% c // `randomizeHeatbugUpdateOrder' Boolean flag we can
& P2 X, e/ Z- o1 L9 D, _% W3 T9 U // randomize the order in which the bugs actually run
: P3 }/ R: m) h6 x! Y // their step rule. This has the effect of removing any3 x6 W$ q( W" @
// systematic bias in the iteration throught the heatbug0 ?1 j# U# R6 R3 w" p0 E: w( u
// list from timestep to timestep
2 H6 J# q, a9 o2 b j7 ]& e , w0 g- G+ G( c. R( H. J7 T
// By default, all `createActionForEach' modelActions have
# X& Q1 {+ K( U) q, u // a default order of `Sequential', which means that the9 h( z3 e+ a# m6 ^ I
// order of iteration through the `heatbugList' will be$ k* {4 n- H/ L* `/ `
// identical (assuming the list order is not changed3 E8 ?- o u& Q1 C" M
// indirectly by some other process).
/ d9 ^) @( D- C3 s' q8 b $ b2 T W( D8 A
modelActions = new ActionGroupImpl (getZone ());
, O' E& E( |1 v B3 f1 M* V! B
- {6 H: F6 L& ?+ \ try {
. h: B% y L ?0 [" t modelActions.createActionTo$message; `) o% S; l5 }
(heat, new Selector (heat.getClass (), "stepRule", false));
" h/ q& z7 H& L } catch (Exception e) {
- a' R8 c3 y" _2 Q2 I" \+ k System.err.println ("Exception stepRule: " + e.getMessage ());
; x$ g, Y; _3 Y5 j7 o& u) p }
( b0 X- A1 K; \: K' a8 a$ {1 n) {: C& w
try {
2 P5 k# X2 n [6 R* Q+ v- Y Heatbug proto = (Heatbug) heatbugList.get (0);
8 x$ S |4 h* S3 J Selector sel =
3 T3 v: e$ ?( R" l new Selector (proto.getClass (), "heatbugStep", false);
% [/ G0 s" t$ I' m; n actionForEach =5 ^/ y8 o' c( o6 i+ I4 E
modelActions.createFActionForEachHomogeneous$call5 B% @) L" T' s
(heatbugList,% B+ E/ k! Z' N: W
new FCallImpl (this, proto, sel,
+ Q/ c; d, S, _0 @" I# J; y new FArgumentsImpl (this, sel)));
2 N! ^: h' p3 g/ w' t } catch (Exception e) {
" L/ f/ S1 y2 B" m" l4 r e.printStackTrace (System.err);/ d1 ]) X& _( C; R, D6 ~; s' w
}
5 z* {1 H! J6 h/ j* S
+ D; M0 _+ X% Q syncUpdateOrder ();
& O) o0 d6 `( n! @; l D! L. l
! J, E! a* Q& c& H" }! y try {; o" R( V: b' T) E
modelActions.createActionTo$message - m/ m; g/ ^- n- c8 S& B% [
(heat, new Selector (heat.getClass (), "updateLattice", false));2 ?3 ]; w; z8 j8 O$ G
} catch (Exception e) {$ i3 k3 W' n9 P; |) X7 f
System.err.println("Exception updateLattice: " + e.getMessage ());- k" e4 \" s4 u) Q" ~8 O O
}
Y2 l% p$ \. j( T+ u* @, E e& I7 [+ L" M9 O9 ~. e6 Q3 n
// Then we create a schedule that executes the
5 f% W1 L/ B5 z // modelActions. modelActions is an ActionGroup, by itself it/ \9 I+ o( @* H6 }) s' C, `
// has no notion of time. In order to have it executed in
" C# f" t! Y6 L // time, we create a Schedule that says to use the
6 B9 Y0 I+ g$ v // modelActions ActionGroup at particular times. This
' i& c8 n9 I: Z+ B" V# b" q9 n // schedule has a repeat interval of 1, it will loop every- `! V* L, g# j4 c; g
// time step. The action is executed at time 0 relative to$ K! W( p7 T+ w W
// the beginning of the loop.
0 J% | S( o8 e6 @
. b \. C, Z% i // This is a simple schedule, with only one action that is
$ g9 V" I2 t' C // just repeated every time. See jmousetrap for more p5 o: N4 {% Y5 ~$ U6 p) l8 d
// complicated schedules.$ ^8 c7 g; \. @6 `
% I. g# O2 \# Y2 j3 l0 _
modelSchedule = new ScheduleImpl (getZone (), 1);5 s4 H9 r7 C u& n$ R. L
modelSchedule.at$createAction (0, modelActions);
1 u- M! j$ x' h5 \' V: H U
% N# f. m& _. @. Y, T" ]/ j. l return this;/ H7 ]5 Y- |5 l" T/ \: A' S
} |