HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 o, ^6 o( b- e2 e2 X* v: @& D
& J9 g- j1 X3 W4 O5 h2 f4 p5 K) | public Object buildActions () {- o2 ~9 r9 r) m- ?+ j$ X
super.buildActions();
( Z2 a& ~9 j) U0 Y , a# V, t1 F+ X! N7 t2 V
// Create the list of simulation actions. We put these in
0 z) T" D w1 \& J2 x // an action group, because we want these actions to be4 f/ k/ \+ }+ O# I/ @
// executed in a specific order, but these steps should
% x" i" m- B* [& f4 L // take no (simulated) time. The M(foo) means "The message' v6 _3 G% X g( m. {7 z5 ?: ~, \
// called <foo>". You can send a message To a particular
6 D2 W0 g' _3 F' H // object, or ForEach object in a collection.4 N& a1 g. ^/ e% ^: D9 \8 R' A
+ o% i; R8 ?$ A: d // Note we update the heatspace in two phases: first run
2 l0 z3 E2 w: ^ // diffusion, then run "updateWorld" to actually enact the. v8 K4 H4 M3 Q* g; a7 N7 i
// changes the heatbugs have made. The ordering here is
; e4 I* @; x) ]' O! A, P9 @0 P3 \# k // significant!$ s; m4 k! x/ G) @2 x6 d, b6 i: m
) ]" a* g) z! c* p4 v& D$ @8 F // Note also, that with the additional7 P `. y% l9 h1 r2 d0 ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 G( V' X8 x+ c // randomize the order in which the bugs actually run
4 X( X+ n3 m U5 \ // their step rule. This has the effect of removing any3 C& x! u. I( P$ X0 G
// systematic bias in the iteration throught the heatbug
% V6 M6 ~" W1 I; V$ g+ H // list from timestep to timestep
3 d5 B( X6 N1 z0 q* K; V+ } " o$ U, x) W5 X( ~1 M+ X" {) L
// By default, all `createActionForEach' modelActions have
* f& J r, `& ~" g, N" G E# q: o* P // a default order of `Sequential', which means that the
1 c/ ]2 W2 P# |0 G // order of iteration through the `heatbugList' will be' ]6 C. D# p9 r. L/ u4 e
// identical (assuming the list order is not changed
8 _7 J( l$ o1 I3 y // indirectly by some other process).; @3 H8 Y9 P& U% _5 x" c+ x
% ?- x: @+ g6 q3 j modelActions = new ActionGroupImpl (getZone ());2 K- z3 i" l+ Y) ?, X' m
' r q1 R1 ^; {, S9 t0 a; w: v
try {3 L2 m% F1 Y! c
modelActions.createActionTo$message: s* s/ X# U! N
(heat, new Selector (heat.getClass (), "stepRule", false));- K. @' _5 V8 R9 x/ E) s6 l' R
} catch (Exception e) {
9 @3 D7 S1 q; {& L System.err.println ("Exception stepRule: " + e.getMessage ());
$ p9 U' o w5 s/ }6 Y } d8 l9 ~7 m, t" i5 u6 w/ y' K+ \
( ^ f+ D+ ~- @+ D
try {
( k$ `2 y* T- C( q" b Heatbug proto = (Heatbug) heatbugList.get (0);
# L) y/ R& f9 X4 E Selector sel =
/ ?3 n3 m; b4 Z! H new Selector (proto.getClass (), "heatbugStep", false);9 W& G+ `% P& [+ i, m7 c
actionForEach =% H1 r2 R( m4 k( H9 A. U
modelActions.createFActionForEachHomogeneous$call
r0 Z6 S8 W" ^& C+ Q2 ?. ? (heatbugList, K6 ?& b" {7 \$ P+ w2 ?7 O- h
new FCallImpl (this, proto, sel,
R$ S( a2 D; }9 m( ?$ c new FArgumentsImpl (this, sel)));9 F3 @* E: h0 A* r$ q$ \5 {) {
} catch (Exception e) {
# b7 O( x2 x3 s: \9 m0 |% o5 m8 M e.printStackTrace (System.err);
6 n+ M5 m7 ^5 u/ l } h5 I0 V5 r0 W+ G0 o1 _6 q
) Q/ n h4 ^1 n K2 Y
syncUpdateOrder ();3 F3 P/ q! ]) Z+ _# Q( K
+ @$ c- ]" V9 c( x7 M try {
1 Y& {9 m( ]9 t& b7 j3 W% b modelActions.createActionTo$message
3 [& R0 @4 q% W B: A (heat, new Selector (heat.getClass (), "updateLattice", false));6 `' X+ s0 p) ]. y8 s
} catch (Exception e) {" B( z; Y/ _* k. {4 F- v' r! I
System.err.println("Exception updateLattice: " + e.getMessage ());) e! ~* L% V' ]/ E( q3 I$ H; ^! r
}
9 ?! s( i4 G4 l7 ^
. G0 L( R0 U( c3 M // Then we create a schedule that executes the
5 `2 H3 o: W7 [3 ] // modelActions. modelActions is an ActionGroup, by itself it
O0 ?3 k' L/ t) L! |5 y, D! g // has no notion of time. In order to have it executed in$ y$ E* q. Y5 ?( g' @- _( W! X
// time, we create a Schedule that says to use the
! c5 d4 K; p* i; m' V0 p: |4 ~ // modelActions ActionGroup at particular times. This& z: u& `9 [. s3 ?+ S7 `) ~
// schedule has a repeat interval of 1, it will loop every
' v- C. }- X) O% l! X // time step. The action is executed at time 0 relative to1 G) \4 r6 b0 f
// the beginning of the loop.
% F$ G8 W- P1 i! t d
) T" [5 u- t5 V H% \7 L. f& @ // This is a simple schedule, with only one action that is# T: u0 c) w9 \8 S' Q# O# B
// just repeated every time. See jmousetrap for more
& e8 K5 U m2 t# P! |& R // complicated schedules.
: n% d- @# M: ]0 ^' @ # {3 q0 H9 x$ h0 @
modelSchedule = new ScheduleImpl (getZone (), 1);
# Z7 G! |7 f6 D: \ modelSchedule.at$createAction (0, modelActions);
; n$ M# |" M' _6 \" J+ z1 Y, z8 M
+ X, h3 v8 j5 Y: w9 m! L return this;
% A& T! l$ ?3 _ } |