HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) W! b/ C, |5 T6 q8 R: a
) e" |2 }, p# t# } public Object buildActions () {
+ a M( I) d0 Z2 o super.buildActions();% o. E+ d* Z0 [& ^% ]4 e
( X' L9 I; X; E8 o // Create the list of simulation actions. We put these in
8 C: v$ ]/ s$ w+ ]) E4 j: z // an action group, because we want these actions to be. c- \ f, Y5 k# S0 U, z! R
// executed in a specific order, but these steps should. c0 Z+ s% Y8 y+ `& T6 Q
// take no (simulated) time. The M(foo) means "The message9 x2 [, f2 z& X: x. V
// called <foo>". You can send a message To a particular4 r w1 t c1 ^6 `+ K! x
// object, or ForEach object in a collection.0 a* \9 U% X2 j
( C2 C1 S( d: i+ }+ l // Note we update the heatspace in two phases: first run: I, X& d# E( S% D
// diffusion, then run "updateWorld" to actually enact the6 k& e3 h) G0 _
// changes the heatbugs have made. The ordering here is
Y# @4 m: m/ N: |( l- r% K w // significant!
" e% H% Z& e! U5 E 8 p& |: |; r& b# g& O7 k0 W1 Z
// Note also, that with the additional+ ]' \; D, j1 v% N# C
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 H3 _ n8 C7 b! `" W+ T" f6 E+ Q
// randomize the order in which the bugs actually run
% D, @/ C4 z! \/ Y9 F // their step rule. This has the effect of removing any
$ j0 G" P+ c6 t1 f0 ~ // systematic bias in the iteration throught the heatbug$ ?$ x5 [1 R9 v4 `8 i7 f( S
// list from timestep to timestep- b# x( {0 h9 j* m% _
3 O6 n, l: m1 d8 z2 C! g# O, {6 @
// By default, all `createActionForEach' modelActions have
2 K- L" s" P f: e$ h! }, c // a default order of `Sequential', which means that the
' f" z( K" k: }. S4 k4 G$ g // order of iteration through the `heatbugList' will be
6 u2 a3 X+ F% L& _# ` D // identical (assuming the list order is not changed
z- W1 B7 H9 o8 n3 o // indirectly by some other process).
3 F t' V' I3 v1 I$ ^ }
+ X2 U; x+ A, ^* T9 J; E- R modelActions = new ActionGroupImpl (getZone ());/ P# i* {5 U4 v5 o* I
$ b( o- u, ]2 Q" J' O" C
try {
: K! [$ D0 e5 y t9 \7 D5 S: y modelActions.createActionTo$message
3 U2 j0 A0 N- P (heat, new Selector (heat.getClass (), "stepRule", false));+ V! U1 M" h% [: q; g
} catch (Exception e) {* }$ r* w `4 P: t! G
System.err.println ("Exception stepRule: " + e.getMessage ());/ ^8 O8 O& g0 G3 Z
}
0 ]# |& P8 p$ M- y) @% s" `
9 s, f( u9 S) ^% X: o try {
! U1 a. ~: u/ ]: C' k& e, i$ K Heatbug proto = (Heatbug) heatbugList.get (0);
9 U m3 p& D0 w5 O# l Selector sel =
5 j3 C* |) A8 x6 B new Selector (proto.getClass (), "heatbugStep", false);# C u; b& w6 o& G) H
actionForEach =
& h1 M. j) l: _6 u: z+ V5 K3 P modelActions.createFActionForEachHomogeneous$call, `6 ~7 ? E" x: S
(heatbugList,4 O8 ?3 h* d! I3 _- C1 |4 s" a
new FCallImpl (this, proto, sel,/ _) N/ L) C2 q. K6 ]
new FArgumentsImpl (this, sel)));" m$ Q: X7 d1 a: ] ?* e9 V% s
} catch (Exception e) {6 }6 w3 U8 P0 i5 q/ p ~- ]
e.printStackTrace (System.err);' F$ e% e0 v: X3 Q) f2 p8 A, I
}
7 g- r7 F& H4 L( g( y: v8 W) I( b" W
- n# b9 ]4 D w syncUpdateOrder ();
2 I S% r8 t0 u' q! P+ R
3 U+ [1 x* ^" }# J K try {
7 c2 k5 A3 B V) q modelActions.createActionTo$message
, p! j- [' S6 I (heat, new Selector (heat.getClass (), "updateLattice", false));
* W4 Z2 ~3 R: S z6 m } catch (Exception e) {5 A3 O- i6 E6 @ L$ e
System.err.println("Exception updateLattice: " + e.getMessage ());: _; Q0 ]3 N+ d6 D# z( T- k5 `
}
" |; U% Z' R+ _6 U% `
. a/ k" ]: k2 X! {, N // Then we create a schedule that executes the
7 f, _# q& ]- n) k& B' C* e- {+ p# y // modelActions. modelActions is an ActionGroup, by itself it/ J( Z s* z3 c
// has no notion of time. In order to have it executed in+ }) p% l! ^& t& p0 c
// time, we create a Schedule that says to use the5 n1 `4 Q8 v/ F% [( |
// modelActions ActionGroup at particular times. This0 k* ^2 ~9 a- S- P
// schedule has a repeat interval of 1, it will loop every+ `/ P W! @4 | @, K
// time step. The action is executed at time 0 relative to0 q+ e: R- B r3 F% p
// the beginning of the loop.
: [- C' s" v# w) ]% h( O3 u9 [7 A( p {. d- d$ a/ u
// This is a simple schedule, with only one action that is
2 l5 F% R: ~9 J+ Q // just repeated every time. See jmousetrap for more
' w& M U/ T) h: M2 Z+ K$ N; l // complicated schedules.
( @( z. H2 h( t3 j
- q% U0 i8 F/ y modelSchedule = new ScheduleImpl (getZone (), 1);4 l/ U7 D0 B5 C D3 P6 D, L
modelSchedule.at$createAction (0, modelActions);) m9 P& E- |' o% Q1 T' \% f0 S
$ r$ e6 H/ j# J return this;
% @6 C: }% z( @8 ` } |