HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: @6 C" A' [2 T4 |# R) a- E- L! R! s
public Object buildActions () {
/ i; H! _% G# @/ v super.buildActions();
0 D6 o7 n# t: W4 ~- ~ 3 \4 a ^' ~! u& @) x
// Create the list of simulation actions. We put these in
, h6 \5 e3 E. r) @- E$ y8 U // an action group, because we want these actions to be* m, p4 t* ~! O" }
// executed in a specific order, but these steps should
' h" o, z) w* D, _) F+ E" z // take no (simulated) time. The M(foo) means "The message
- D2 E2 s, f$ Z // called <foo>". You can send a message To a particular
" E2 c Y' Y6 O) ] // object, or ForEach object in a collection.
/ u9 `, m$ Q+ x; R
3 @; C3 E5 G5 y8 b' C g' X( I f // Note we update the heatspace in two phases: first run
( B5 V. @' l0 V e( x // diffusion, then run "updateWorld" to actually enact the
& ^2 P* V1 d4 T! M // changes the heatbugs have made. The ordering here is
8 N+ e! X5 i+ H6 {" U; Y+ ?1 \ // significant!; }$ a0 j. j6 t1 k3 O3 \! V: q
, z K! D1 U \* A _1 d // Note also, that with the additional- s6 \! z \, K7 H
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 s d; h* C0 j8 t5 W" _- ~% v
// randomize the order in which the bugs actually run S$ A1 m& g# _; m- H5 y
// their step rule. This has the effect of removing any0 { Q. ~2 k4 f
// systematic bias in the iteration throught the heatbug, w8 P$ q- f$ m/ |' ^$ X5 x B
// list from timestep to timestep4 `5 i9 z2 r* m6 [7 r7 W) Z! x
6 s V9 P" T& U$ Q4 W; H // By default, all `createActionForEach' modelActions have0 m! o# s2 L. R" b/ X( Q0 a, n
// a default order of `Sequential', which means that the3 {6 [6 C4 M6 H; g
// order of iteration through the `heatbugList' will be
( {7 ~" j0 k3 _ // identical (assuming the list order is not changed
. _: K8 _$ l; ?5 W // indirectly by some other process).6 z' c( F4 k0 t; w
" D. G& D. j$ S4 ` modelActions = new ActionGroupImpl (getZone ());
3 N2 V$ P: T$ n$ I# k% p' A& n1 ]1 W0 d6 O& Q4 K8 A8 T
try {7 y2 g0 V/ l& e% y) ~
modelActions.createActionTo$message9 _2 O. _' B) x
(heat, new Selector (heat.getClass (), "stepRule", false));6 y6 C1 b+ B" a; o" A" _, [! t6 b
} catch (Exception e) {
; y& L7 M" y% W v System.err.println ("Exception stepRule: " + e.getMessage ());/ k+ P! @7 w0 j* _( X# A
}
7 ]" L) R6 P {
5 f5 X0 s+ J [* L: w try {( `. v$ }8 V" J9 V
Heatbug proto = (Heatbug) heatbugList.get (0);
! B A- W0 {+ c; c5 A8 D0 Z" t L* K1 H Selector sel =
3 y( O p" ^ y v- e: Z' | new Selector (proto.getClass (), "heatbugStep", false);5 S7 ?, w, a( {# _, E# `' F6 N4 q
actionForEach =8 L$ ^9 H1 v5 q; u
modelActions.createFActionForEachHomogeneous$call
. c2 j& W4 N, q8 \ (heatbugList,: b% [% M) |" Y8 F9 [0 [0 d4 g
new FCallImpl (this, proto, sel,, ` e# b6 _4 f) q
new FArgumentsImpl (this, sel)));
1 E' q4 k9 ]$ e y } catch (Exception e) {( z5 V, d5 g4 k
e.printStackTrace (System.err);
; P, \4 B! ]: S9 {5 Q1 f% O: f* u }
* L& ~; E1 ]8 ? ) a7 W, s; S7 ^+ U" }, r
syncUpdateOrder ();% V: y' |. R' {" }
( h, e. C8 k2 X0 e try {) r4 M' V- X4 e, ^+ o' J) g
modelActions.createActionTo$message
- x& f# M5 t# b5 u% S2 {9 s (heat, new Selector (heat.getClass (), "updateLattice", false));
m9 Y3 b9 w. w; c } catch (Exception e) {
+ q# g; J. o2 M System.err.println("Exception updateLattice: " + e.getMessage ());7 F* Z/ r. D3 e a% D% i
}5 s: X9 H" X/ f7 n5 E
- r6 t! [. ~: ]+ o) Q, T3 }2 d9 A) i
// Then we create a schedule that executes the) P# V* u6 c6 x- r
// modelActions. modelActions is an ActionGroup, by itself it- |( f. G% Y& C/ N+ s+ q; A
// has no notion of time. In order to have it executed in
* l; U4 h+ a% z% ^0 I m$ \ // time, we create a Schedule that says to use the
2 y& P* l& D; @0 d2 I // modelActions ActionGroup at particular times. This
/ l9 F8 p2 b0 v3 u6 F' f1 Q // schedule has a repeat interval of 1, it will loop every6 m+ V- q, O E! G4 t) U
// time step. The action is executed at time 0 relative to
! g/ r# o- R* x* u+ D // the beginning of the loop.
5 G$ o0 }) ~' @4 n9 ?4 B& ~) s9 Q% v S0 ]) Y
// This is a simple schedule, with only one action that is
) M7 l1 H# T) K K // just repeated every time. See jmousetrap for more& z* k# z K2 ]; p
// complicated schedules.
. [5 E- D" l% c$ U }, O9 R/ ]3 f
5 `$ K3 j/ ?( P1 V modelSchedule = new ScheduleImpl (getZone (), 1);" ~1 _2 w! i+ ^9 K1 E
modelSchedule.at$createAction (0, modelActions);8 I1 n. ~. a& ]* C, j2 g( X
. `4 n2 D" S+ P, t$ z3 M2 h return this;" s$ K* i( k' }. G3 E
} |