HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 H% |: e! s$ Y8 C* @9 u( S
& R% h: J- q% X% n9 W public Object buildActions () {
h, F# G; }6 d% T super.buildActions();
4 \( D: Z/ U* b5 d* c $ U, h6 X, @' U8 ]* r. Z
// Create the list of simulation actions. We put these in
p1 r' x# I7 J7 m& [) z- F" x // an action group, because we want these actions to be, H c# W) p/ m9 r& J6 h1 o1 y* I" U
// executed in a specific order, but these steps should* i* E- Q$ L3 z* o
// take no (simulated) time. The M(foo) means "The message
* m9 R( F; d: S6 U // called <foo>". You can send a message To a particular4 z2 n7 p3 G4 A& i5 ~+ s' d
// object, or ForEach object in a collection.% Y/ I' O+ y3 u! ^8 u4 e
% z b* c9 e. m8 B8 g& m5 D // Note we update the heatspace in two phases: first run* T) V7 \( o4 \* s! |
// diffusion, then run "updateWorld" to actually enact the
' i( R* O% `7 x1 \. L // changes the heatbugs have made. The ordering here is8 z- p! L3 |- c! ~) v
// significant!# ]7 f8 I1 z5 K2 g' l& p8 P8 K
- }1 k$ h1 v* Y* d" @7 Z( r
// Note also, that with the additional
3 d8 h, P. I8 D& P3 v // `randomizeHeatbugUpdateOrder' Boolean flag we can2 @9 F0 W( @: K" S# p+ Z7 j3 a
// randomize the order in which the bugs actually run- D+ h* ?5 [$ E, ]
// their step rule. This has the effect of removing any v0 G: L3 p4 H9 `! d
// systematic bias in the iteration throught the heatbug
+ O1 \5 B- H0 E: c: k2 g/ p, \. S3 o // list from timestep to timestep
" Z2 i9 P9 G3 s* ^- t, o9 L# } 7 B2 |" }! a1 c! i7 E
// By default, all `createActionForEach' modelActions have; p0 ^- o/ E: p
// a default order of `Sequential', which means that the/ V8 v3 d" ~5 M
// order of iteration through the `heatbugList' will be
5 o- O& c# f+ `* r: G2 Q2 P // identical (assuming the list order is not changed/ {0 X; I) ^7 _6 F& @
// indirectly by some other process).# t; r0 h, q5 S+ R. V s
# Z* o% Z" J- r# N, ]( q$ t
modelActions = new ActionGroupImpl (getZone ());
' I. B/ T/ j3 J1 d! [! b# Q8 g) ^0 m: E/ `; }8 Y M+ k
try {8 [2 h! A7 V1 P! Z( X
modelActions.createActionTo$message
8 G4 m, \1 G; P* ~! _# [* A8 q (heat, new Selector (heat.getClass (), "stepRule", false));. \ l* Q( f5 s# q0 L
} catch (Exception e) {5 M* P0 C/ T' s. e7 g
System.err.println ("Exception stepRule: " + e.getMessage ());
3 g; b6 U7 e. f! ~, V y" b }
9 |& p K7 H, V4 C& E6 V; r3 P+ ?
; [" {# Z" [, w7 L, B try {5 Q4 g8 t+ G+ i! G# M
Heatbug proto = (Heatbug) heatbugList.get (0);
: O$ g4 J+ L8 U& W Selector sel = . x9 }2 K# {* v8 k6 F" X) E5 l& K
new Selector (proto.getClass (), "heatbugStep", false);2 R. x' B1 T9 c0 ?1 I1 z% [4 R
actionForEach =
7 u1 r# L% j2 c2 f( J6 ]4 C modelActions.createFActionForEachHomogeneous$call$ O0 m8 \' t( }- v1 T
(heatbugList,
: |3 P9 Q v. e# _+ a; I new FCallImpl (this, proto, sel,5 x6 z1 \0 A% C
new FArgumentsImpl (this, sel)));
0 `8 W' S' O0 e/ e! @) s } catch (Exception e) {
& H% X2 t0 t3 A+ E2 h0 {1 L2 ^4 y e.printStackTrace (System.err);* [ k# h! V" [# p/ U* b
}2 f+ o% a0 Q* e" O. r+ Y5 a
5 W8 A; ]* S: o
syncUpdateOrder ();6 J4 {1 P6 h6 Y# z) ~
* |1 s8 Y6 i& ?- \) v5 ^. L( ~$ `
try {
" [2 P) F& X+ J$ n: y" J; ~! Q% M modelActions.createActionTo$message / Z" l& L4 M2 P2 [ n: W
(heat, new Selector (heat.getClass (), "updateLattice", false));
# \: h3 C. {. T$ z# |% K: q+ y8 w6 l } catch (Exception e) {
2 J/ a* Q: |9 P1 r+ x* x7 T System.err.println("Exception updateLattice: " + e.getMessage ());
) c& U5 B( @: H: A# Y }
0 v$ n4 r8 z4 H' u 0 z2 o& h k) H; ?! t
// Then we create a schedule that executes the0 `5 a- _0 {' M4 r2 V& M! Q4 |7 @
// modelActions. modelActions is an ActionGroup, by itself it
3 K) @1 B; p2 b! P3 U! M+ J' {8 C // has no notion of time. In order to have it executed in
( h4 i9 R+ z, D3 ] // time, we create a Schedule that says to use the
% k0 g+ i# Z8 \4 e8 c // modelActions ActionGroup at particular times. This g7 W* V/ G' `7 I( C6 }- ~0 W6 K t
// schedule has a repeat interval of 1, it will loop every$ T/ E. w# s9 A2 y
// time step. The action is executed at time 0 relative to8 E5 J8 T9 }) {" |4 P2 u: q1 n4 Z
// the beginning of the loop.% V. B1 j8 [/ b# X$ r. p+ _9 G1 Y7 R
% T" v% [8 U$ d8 f // This is a simple schedule, with only one action that is
: ?) H4 ]& h/ a7 ? // just repeated every time. See jmousetrap for more
% p4 N! V( Z6 `! Y0 l; N // complicated schedules.
3 x( H9 b& F2 j- [0 y9 f( y( I5 Y- \. }
2 t1 e: n% |2 F& b* y9 k& f3 V modelSchedule = new ScheduleImpl (getZone (), 1);
/ S; P9 e6 w" x' C- B modelSchedule.at$createAction (0, modelActions);8 x0 z; W2 C% x4 t
# E1 K, q" X) N- Z1 D0 t
return this;# r% U; l# e9 t2 z9 t
} |