HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: T, |. A' C2 q* `
# x4 C; Y4 r! j. D' ~ public Object buildActions () {- C. i# V; u* T& q% e+ f. g: N
super.buildActions();) ^/ C8 v( p0 w: i& D# l3 s
: g+ g7 H6 ^/ j/ ?. `
// Create the list of simulation actions. We put these in
; h/ y9 k) f) D8 d' o1 W // an action group, because we want these actions to be, }1 k+ U# q; B
// executed in a specific order, but these steps should
7 ~1 S) d8 B0 }4 @- m. W5 ]6 I // take no (simulated) time. The M(foo) means "The message$ B4 k: I: E& L" [/ O) t9 h+ M
// called <foo>". You can send a message To a particular$ |" ?' } i& h+ n' g. \; h
// object, or ForEach object in a collection.
' r0 i/ L# r' p- ]: b- X
; t9 o7 J: x" y5 y3 T# x // Note we update the heatspace in two phases: first run3 z9 M2 z4 A4 L/ J& }. J' ^
// diffusion, then run "updateWorld" to actually enact the* L2 r3 \* Y$ H- L
// changes the heatbugs have made. The ordering here is
1 z- U- h; y4 k% l- N/ w // significant!) p! W9 ]# |$ G& V1 M
! }5 f! V6 V) N R1 ]$ a Z
// Note also, that with the additional
N& H2 s; f6 X: z o G" e. r // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 r1 |' n- J+ F. {; f1 k // randomize the order in which the bugs actually run
( i8 M- @4 H. \6 r' i. Z1 u // their step rule. This has the effect of removing any
) j3 e, J. e5 M/ i/ J // systematic bias in the iteration throught the heatbug! h" r& T7 b7 U" {: n/ w
// list from timestep to timestep
2 m+ A, g& e$ V: o/ y/ g8 l
9 s7 U6 Z+ r0 L9 } // By default, all `createActionForEach' modelActions have( E+ C" ^8 q. ^3 ]3 E% I+ r% ~
// a default order of `Sequential', which means that the& {/ ~) l7 D" q! v+ K# R0 E
// order of iteration through the `heatbugList' will be
n+ H7 n; u+ ]2 z // identical (assuming the list order is not changed
" t% D8 w+ k. e) Y d l8 o // indirectly by some other process).
; T* b$ ]& h* Q+ r |# e7 ]
% Q$ V( B' Y) H modelActions = new ActionGroupImpl (getZone ());
Z6 S K) ~7 T8 t
+ j2 u \7 w, V3 N- x, R! o' ` try {
9 n, |9 D A9 d- k& Y modelActions.createActionTo$message
6 b4 E; p- C0 `! V( R3 @' T+ a' W! ? (heat, new Selector (heat.getClass (), "stepRule", false));( B; l1 T# c# f j/ U
} catch (Exception e) {
7 }& y4 O5 P# y+ [ System.err.println ("Exception stepRule: " + e.getMessage ());
( F" L6 _+ y5 O! C i }% G8 @/ [, j, F) [/ }5 K$ u
/ X, L2 q7 S6 r* x- } try {$ R& p& j0 x: v( z- G
Heatbug proto = (Heatbug) heatbugList.get (0);; ]$ b9 P+ I* `# z9 a
Selector sel =
" }' s1 c& ]! ? new Selector (proto.getClass (), "heatbugStep", false);
/ r3 s# w/ D: l1 i: k actionForEach =& b# l U( @4 X; x# T
modelActions.createFActionForEachHomogeneous$call8 D% Z2 c9 n. W6 C
(heatbugList,
* V* s( v! o9 w1 n new FCallImpl (this, proto, sel,- b' x4 A* }% M( a' x4 W
new FArgumentsImpl (this, sel)));$ V8 s* [8 _8 b7 i( d4 p
} catch (Exception e) {
* }1 Z' O7 D) V& f e.printStackTrace (System.err);0 s& v! S5 L& n0 l$ B% N" @% j
}
( T8 {* o, f" o/ l. v- X7 ?7 X
* H5 ^ K$ C% W/ g syncUpdateOrder ();$ W1 e7 n- e% d4 ]6 l
5 A3 U- T3 ~! r7 K3 F
try {
0 z& ^, S: o6 I ]/ O# n modelActions.createActionTo$message
9 W( U) f; l4 m) E' N* n (heat, new Selector (heat.getClass (), "updateLattice", false));
! F, F: Y$ ]5 m7 S; } } catch (Exception e) {
. C! o& t+ H, K/ m+ G9 p7 q6 m. a9 e System.err.println("Exception updateLattice: " + e.getMessage ());# J7 J3 N5 ~3 F& B' ]: u& O7 p* F" N
}
2 M- O5 {; d+ a- I& O) ^, U1 H ' G3 Y9 V. h2 }0 H/ x
// Then we create a schedule that executes the( h" y7 g" ~( v7 T
// modelActions. modelActions is an ActionGroup, by itself it
4 @# J) }; R( {! L' H5 B. n" ? // has no notion of time. In order to have it executed in( y. W7 X" X: `2 {
// time, we create a Schedule that says to use the& ?. E2 _4 A' s+ D8 n
// modelActions ActionGroup at particular times. This& F2 F* F. K0 h+ r& O2 X5 E/ w+ ?
// schedule has a repeat interval of 1, it will loop every
; j T a* z. c$ ^* r. u // time step. The action is executed at time 0 relative to2 R+ u4 x. `) a) k- \* W0 \: L
// the beginning of the loop.' @1 ~8 ?& J1 j* F# e
% {2 q) p4 q- [) A // This is a simple schedule, with only one action that is
1 ~: p; |8 u9 P( @ // just repeated every time. See jmousetrap for more
, N6 T- R* n; [2 _ // complicated schedules./ R; a, K" d2 C
' g% I% n U% i8 P( J modelSchedule = new ScheduleImpl (getZone (), 1);$ o+ w: d, M$ y/ j6 ~* U
modelSchedule.at$createAction (0, modelActions);
7 }' k5 m& Q1 `/ v
1 @4 N# E& |, }1 ^ return this;
_! p" v5 M' R& j6 R$ Q( M3 T } |