HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 z* r* F6 d1 j o ~1 p* C( x; m. v* X2 }2 U! ]$ I& ~: q. Q3 h, d
public Object buildActions () {
+ H% F0 t$ f6 D) n l super.buildActions();6 [# m" c) n9 ]
5 U4 P; [, y5 Y4 W$ j8 A$ G; y
// Create the list of simulation actions. We put these in
& V1 e- ?( n" U$ I2 k$ r' v/ Z // an action group, because we want these actions to be1 U2 }- A9 c4 f7 ]. c
// executed in a specific order, but these steps should
7 Y4 F: |! b( d! [ // take no (simulated) time. The M(foo) means "The message
0 ]: n$ R6 e/ y# r* u0 y' n // called <foo>". You can send a message To a particular2 y/ d/ i% |. P' w H j
// object, or ForEach object in a collection.
5 q! b$ i, x' @' S" T# R
. {& D% q+ k, l- J // Note we update the heatspace in two phases: first run
7 A8 }8 {# U. e0 i) T5 F6 l. I // diffusion, then run "updateWorld" to actually enact the
8 q; d& U% ^0 g, w // changes the heatbugs have made. The ordering here is
5 A+ b& Z: t& x& }: @ // significant!
2 f+ o! N4 y" [: z! ~- N
, K8 D6 g# { X, C4 F# P // Note also, that with the additional
% E: N; d b2 U8 | // `randomizeHeatbugUpdateOrder' Boolean flag we can6 R2 X$ Y/ q8 u/ j8 i
// randomize the order in which the bugs actually run
& Y/ ~0 R Q# y* B) e // their step rule. This has the effect of removing any0 J) @ L2 O! A
// systematic bias in the iteration throught the heatbug5 V7 A0 s8 M: L" f% R6 b
// list from timestep to timestep* |( Y8 ~$ I9 Q1 m! J& ~
: u( Y$ C5 z/ [( h6 Y$ P' p // By default, all `createActionForEach' modelActions have/ v) e5 p# f* s
// a default order of `Sequential', which means that the8 @ S3 j' A' N- e0 n+ R5 l
// order of iteration through the `heatbugList' will be
0 C1 Z7 S: o% c& a6 K2 n // identical (assuming the list order is not changed- {) A. @2 ^" _
// indirectly by some other process).! [6 n. ~6 a5 D6 I3 t
- ?& n4 J* S+ U7 l modelActions = new ActionGroupImpl (getZone ());- C" A3 u' h" U# A' J; O3 @
3 ~0 y$ W4 t( i/ u4 r try {5 _" U: A6 z7 }7 c
modelActions.createActionTo$message1 r+ R* X8 \% k: n0 J6 G+ c% y- h2 J
(heat, new Selector (heat.getClass (), "stepRule", false));1 Q2 l$ ] P9 f1 }" k, b. c
} catch (Exception e) {
4 C( \, w, t% D* j System.err.println ("Exception stepRule: " + e.getMessage ());
" ?9 u! @9 Z" a1 i# f } t' D$ x- z$ j! |& g
# g( Y6 ?. W& g3 j& ]0 i' ^
try {0 U, U" C- j: [; d
Heatbug proto = (Heatbug) heatbugList.get (0);$ g0 K! B9 m9 k1 c
Selector sel = 2 ^( S$ e1 [; h' o7 z7 S
new Selector (proto.getClass (), "heatbugStep", false);
$ ], w7 O t" o' x* R8 ? O actionForEach =/ p2 }% |3 [2 n% C0 g! m
modelActions.createFActionForEachHomogeneous$call
, @0 j) N) a, I# y (heatbugList,) k( @3 F& H+ |* h( t
new FCallImpl (this, proto, sel,
( P$ ?5 {# M- T% A' Q4 h# Z! q: R new FArgumentsImpl (this, sel)));
4 f" U8 S; S. T! E" Z- f/ v } catch (Exception e) {6 N7 T: R" ]( p/ j
e.printStackTrace (System.err);# X! b/ X7 F. R( K' E( a7 Q
}
" j* q, {+ Z' O- F6 |" J+ e 2 X5 K, J7 N7 _# R# z9 Y
syncUpdateOrder ();1 s: y9 k8 g1 Z/ N
; `( c4 x6 {* n! p& V- n
try {
" e6 b6 E2 X) _% s% f5 K modelActions.createActionTo$message , q' U: f" O7 R
(heat, new Selector (heat.getClass (), "updateLattice", false));& X% m, M+ Y$ b Y
} catch (Exception e) {6 }0 E1 V+ R; V* }
System.err.println("Exception updateLattice: " + e.getMessage ());
4 {! Q) t( X) H. U* [ }
! P9 d* O$ _0 c0 q6 i! Y , z4 W1 q% I/ P2 K
// Then we create a schedule that executes the" X8 o9 l$ q* W2 n9 X* W7 v
// modelActions. modelActions is an ActionGroup, by itself it7 @3 ^7 o( t' w& d
// has no notion of time. In order to have it executed in
5 m& ]7 g2 I# t7 \+ ^2 N // time, we create a Schedule that says to use the
! m$ N* a( j& P- @: s) s w // modelActions ActionGroup at particular times. This; Q1 q/ p4 M9 s: L$ s
// schedule has a repeat interval of 1, it will loop every
# F3 O( \6 B3 {. Z# C( t/ Z // time step. The action is executed at time 0 relative to
# _+ |# Z/ ]) Z: C) k // the beginning of the loop./ F7 G* l9 |2 T) ?3 P
" W" M. K/ T: S' ~7 T, @
// This is a simple schedule, with only one action that is- Q$ c4 `. g$ \# ]$ e" l% a/ N
// just repeated every time. See jmousetrap for more
, S) X! `0 m; s4 c" ]# S // complicated schedules.4 Y8 ]; [& C1 J/ y; P
! n1 r' `4 S7 }0 `! o
modelSchedule = new ScheduleImpl (getZone (), 1);1 ~& l& F' o G% t6 Q8 Z7 V. e
modelSchedule.at$createAction (0, modelActions);8 B) R3 L/ _- C3 \" T1 B' c* A
' N4 T9 W3 ^" U; W! X# k return this;
. X# ~# D; S& {$ k } |