HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 y$ ~) M l5 m# a) l8 ?1 q5 w/ g5 b. a
public Object buildActions () {" j1 p1 r) _+ B w1 T P8 E
super.buildActions();# z: T3 E [# r6 O4 c5 Q% u
- M) j' i% `9 Y# E% u // Create the list of simulation actions. We put these in
! x; X& g5 C) X9 v& @ // an action group, because we want these actions to be, |" `0 J i3 H8 d
// executed in a specific order, but these steps should$ ^; E( J6 G5 ?" i5 X/ \7 ?$ [ i
// take no (simulated) time. The M(foo) means "The message
2 @3 V" Q, P* k8 X/ G // called <foo>". You can send a message To a particular
6 N2 W* E) P. M8 m/ a7 J, i; H' ` // object, or ForEach object in a collection.
; c r1 h* V P, U4 T5 b5 Y o" r# v5 p' Y. Z* x
// Note we update the heatspace in two phases: first run
. b/ O$ P7 w+ \/ S* N8 l // diffusion, then run "updateWorld" to actually enact the) k( E$ t. Y1 q, b) p8 P1 ^
// changes the heatbugs have made. The ordering here is
+ g9 T) ?, p: B5 o6 ~9 |! y // significant!
6 P% V" p+ f9 L( h: r2 M9 f $ c2 B& F3 l* c% i8 f
// Note also, that with the additional
8 v+ i7 H* V* X- H // `randomizeHeatbugUpdateOrder' Boolean flag we can( w+ |) T( K! O* W- ]
// randomize the order in which the bugs actually run3 w- u! Y) v/ h) o
// their step rule. This has the effect of removing any
( |' B4 ?6 }# g5 |3 R: W" ?$ t // systematic bias in the iteration throught the heatbug9 Q- ]* d0 E* [3 Y4 k9 f
// list from timestep to timestep$ I8 \' Q4 c5 `* V: C$ y
3 e0 } t" ]! O) h# E/ Q6 G% N // By default, all `createActionForEach' modelActions have
6 S3 b+ \3 J( [: p1 k( a* K // a default order of `Sequential', which means that the
9 ~- ]% ^9 _! ]( i( Q' C% y // order of iteration through the `heatbugList' will be
9 ~- M8 r8 _( @" O // identical (assuming the list order is not changed
2 h/ n2 Q0 Y) X // indirectly by some other process).! p4 E+ p" q9 k9 ]) J! W: J! e2 s
* z: _: m! o; \& C/ i( B0 z modelActions = new ActionGroupImpl (getZone ());
) v& N, |3 P5 v U- q% a. k. k: }
3 q# O% E, N2 }$ V try {
, o& q# ] n4 Y% y' u modelActions.createActionTo$message
' ~4 p& c/ D/ \7 x (heat, new Selector (heat.getClass (), "stepRule", false));% L( k5 k/ q: _4 H0 ~( a5 o( `
} catch (Exception e) {3 z, C! d+ N8 h* i \6 z
System.err.println ("Exception stepRule: " + e.getMessage ());( U- ^: ?, w4 y, { n
}
$ F p" f) l/ [8 X7 K/ ], W2 F, N9 T8 B, C# d) N" }
try {
2 S. G$ M8 l, h! o Heatbug proto = (Heatbug) heatbugList.get (0);: K4 y. N* z: Z, o8 i' |
Selector sel =
- u9 c6 _. p$ I6 |* C: o new Selector (proto.getClass (), "heatbugStep", false);
% d% f2 }$ l$ n& U2 c actionForEach =
8 o1 H4 D: _* P# v0 P modelActions.createFActionForEachHomogeneous$call* H! j4 ?+ ]. Q2 ?! J
(heatbugList," P7 m' X* r7 q
new FCallImpl (this, proto, sel,
( J$ B: t1 f" D/ E8 v new FArgumentsImpl (this, sel)));
& ?% I6 j5 b* r" l! ] } catch (Exception e) {: s1 D1 h' l, r$ l
e.printStackTrace (System.err);' u% a1 n5 ]6 \
}. {% r# B5 Z, f
1 n) l" i- D/ B" ` syncUpdateOrder ();' F' W4 l0 \- }, K3 O
, J- \: ^' E3 E [7 v t6 q try {
8 _. y& Y2 d: ~4 E4 ~ modelActions.createActionTo$message
# P0 m. _: S, A$ m$ W& d/ u* ?" s# K' c$ e (heat, new Selector (heat.getClass (), "updateLattice", false));
2 l9 ?" y; o& L/ t3 M } catch (Exception e) {
/ S8 B0 T ^* a8 \1 h% v% J System.err.println("Exception updateLattice: " + e.getMessage ());8 H, c* r! d" L$ S; j/ c, K% l" z K
}$ z0 a; C) \- k' ~3 x1 [
) h2 {5 ]/ Q) A1 U. P6 K0 a. }3 e // Then we create a schedule that executes the
: d6 X: Z f' v; U // modelActions. modelActions is an ActionGroup, by itself it/ g' c; q* S, E
// has no notion of time. In order to have it executed in4 V D: V" V$ S, U, J
// time, we create a Schedule that says to use the1 i/ C4 _ s3 \& `
// modelActions ActionGroup at particular times. This
" o: B# l& f9 H! C* d' w // schedule has a repeat interval of 1, it will loop every
1 ?- R) @4 b/ O( m# \4 a // time step. The action is executed at time 0 relative to( z$ d% l. _/ X- L5 F4 y
// the beginning of the loop.' Z- P7 j1 z& {9 H. j( f/ j
$ U1 t I! f5 O+ j7 g // This is a simple schedule, with only one action that is
3 L$ O; A U. d0 A. W \) u1 U" i // just repeated every time. See jmousetrap for more
# l' _" {3 g7 L7 U) K1 c' u // complicated schedules.
2 e' B! c' G* P. Z' \0 r! U2 C , O, d; b8 Y, P" H; ? K+ u
modelSchedule = new ScheduleImpl (getZone (), 1);
/ k, D7 M7 i% J* B O3 A$ p modelSchedule.at$createAction (0, modelActions);+ g$ o0 m% W1 x/ m3 G4 K5 D
5 }7 Z( K! n! a1 q/ P4 x% A+ r+ s$ C return this;4 T% _+ ?3 H$ w8 |
} |