HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ Y _7 s$ V) P6 v% X% w
5 u" O& W" Z% _7 F A3 Y9 U9 I' Q- m public Object buildActions () {
) y* @+ K( c- E" v super.buildActions();& f0 q* J4 ?: |
& T) W6 ?5 n& G# D/ J) J) @" {0 c // Create the list of simulation actions. We put these in$ B6 r% }! A9 k- g; G
// an action group, because we want these actions to be5 v2 X8 _& w4 q
// executed in a specific order, but these steps should
1 H1 s- g3 _' i% i! h8 V8 e; I. U8 F // take no (simulated) time. The M(foo) means "The message
% m! u9 ]2 C) b! Y. G // called <foo>". You can send a message To a particular9 M1 G( D$ X6 X! \/ u; s; S! w
// object, or ForEach object in a collection.
8 ~, K$ ?( P: v( V
$ P9 t% u) l" k# ^& D3 F1 @4 a // Note we update the heatspace in two phases: first run( J: `. q; R( ~
// diffusion, then run "updateWorld" to actually enact the9 P; B& T* U7 `3 m& g
// changes the heatbugs have made. The ordering here is9 C1 f( Y; c: ^/ a/ e' }
// significant!. v1 I4 V9 ~2 w( |. d
( B) K5 j4 M0 N; J4 ]& D9 r // Note also, that with the additional2 a7 G6 \+ |0 W! J1 q b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* ~1 Q0 j: P. C5 z5 D% W // randomize the order in which the bugs actually run+ e7 J4 I @0 c) B7 m+ h1 ^2 |
// their step rule. This has the effect of removing any& D& O, J5 p7 }" B+ P
// systematic bias in the iteration throught the heatbug
( E& u! b1 e i( Q7 ^: P // list from timestep to timestep6 C/ G, U- q. ]+ r3 D
4 V$ }# ~: V" D2 m; E* X // By default, all `createActionForEach' modelActions have) R/ Z4 }+ X* Z% _, I; L- z
// a default order of `Sequential', which means that the. l% f) B8 D ?! @0 B1 I$ r
// order of iteration through the `heatbugList' will be3 M; Q5 l' A2 ?! s% c
// identical (assuming the list order is not changed
0 j3 `/ U9 O' F) h: S; t- M) ~' j. S // indirectly by some other process).
4 j4 K$ R) R( Y* W: t3 u; I
1 E" G8 T# c8 |0 i) K% M# t0 j modelActions = new ActionGroupImpl (getZone ());, A# ?- A; I' E; l# M
# _3 O- E" u; V' k5 n2 ^ try {: S$ Z" E; q, E5 y1 n6 i
modelActions.createActionTo$message
' @! U# t7 Z$ o* m4 T3 [/ T9 P% B/ r (heat, new Selector (heat.getClass (), "stepRule", false));
% y* r$ ^! p8 k# ^* g( e } catch (Exception e) {. j3 m) @& Y S- u! Y4 l
System.err.println ("Exception stepRule: " + e.getMessage ());
6 e2 c% W3 M7 N* {! X }
0 p% z) g0 g, b' G/ Q& J/ T
h# G: S$ a7 W( W! b4 J u; ?7 X# M try {
N) O/ a; \6 f% s- [ Heatbug proto = (Heatbug) heatbugList.get (0);
* _$ q1 s* I8 Q1 i" W5 F Selector sel =
6 |" | e2 D/ s( D; r8 `! I new Selector (proto.getClass (), "heatbugStep", false);, _- ?2 t. R' p7 b! d8 E
actionForEach =/ H9 W' B. z# @
modelActions.createFActionForEachHomogeneous$call
$ z' B) b) Z/ W (heatbugList,# s0 F7 c% d& ~' f K! e0 G$ X4 E/ B
new FCallImpl (this, proto, sel,
+ u" B6 y8 O% `: r new FArgumentsImpl (this, sel)));/ |8 l& U+ V7 w/ T
} catch (Exception e) {
' G' Q3 k$ A' I P+ k- O5 {) k e.printStackTrace (System.err);+ j; |3 q& x. H% q6 ^; J$ U! `
}
& m, Y2 w9 Z' P, b
! K: C$ p. \$ H syncUpdateOrder ();
0 `4 x0 [0 j: E0 i# O
% m1 ~& r9 _6 G* `' | try {2 t: B; ?/ x; J/ ^) H" x/ d9 Y7 L
modelActions.createActionTo$message
4 g# U# ]4 U" U, `- q- p9 \- t (heat, new Selector (heat.getClass (), "updateLattice", false));& U: L2 k% f3 s( w& S3 |
} catch (Exception e) {
' W2 r m2 J& l+ o2 j System.err.println("Exception updateLattice: " + e.getMessage ());5 \: f: i9 B; `& z
}7 U, P; A% C2 s
" V, \4 n$ H6 f7 ~8 ^; y8 t // Then we create a schedule that executes the
6 Q. A0 m1 F) p# q" W0 K // modelActions. modelActions is an ActionGroup, by itself it- A0 ^7 g% M% U& Q$ A4 @
// has no notion of time. In order to have it executed in
9 P2 @4 V) j: M+ [$ ?% N! X, \, X // time, we create a Schedule that says to use the
' M! h% C) \5 W; U1 M! e4 X' ? // modelActions ActionGroup at particular times. This" K; S H3 E" @" o& r* z9 h+ }
// schedule has a repeat interval of 1, it will loop every
1 n; I, x5 }+ k9 |4 `7 Q // time step. The action is executed at time 0 relative to/ c* Q1 F7 B- k9 v+ p
// the beginning of the loop.
0 @3 R' E, L9 E7 ~/ q) i) f* O* C2 M/ c5 |, X& a
// This is a simple schedule, with only one action that is5 w6 u, e% G8 W+ e$ H& o5 I
// just repeated every time. See jmousetrap for more
5 L/ z9 c( |1 [/ @! b: C' b // complicated schedules.
% ^2 J( ]; d/ Z4 X
3 ^# k3 @" a$ {0 J f) f/ u9 r modelSchedule = new ScheduleImpl (getZone (), 1);
S. F8 C. ?/ r0 @+ D0 U modelSchedule.at$createAction (0, modelActions);8 j/ B, M) |! x- |1 Z
( p, R6 E ]6 @' \
return this;. d# K- J5 i( M- O6 n
} |