HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# _4 W u+ f$ ~! L2 u# N7 `
) C0 W2 X# \2 C0 B( ?! [
public Object buildActions () {
( R' q, F: E N+ w super.buildActions();, K. o5 G+ S7 h V. y( } }! r
# d. h, |3 a: C% G9 Q
// Create the list of simulation actions. We put these in
' }8 J6 T( G7 k+ G5 r // an action group, because we want these actions to be( {' a+ R/ l6 W0 E0 _% {1 C
// executed in a specific order, but these steps should x8 P& [1 Z' F% L
// take no (simulated) time. The M(foo) means "The message" @8 R# }; b5 w9 Y$ ~* x' b
// called <foo>". You can send a message To a particular3 j( _+ `" Y: e& H
// object, or ForEach object in a collection.7 t' s& g: K: A8 J2 O! A9 \+ o! O
1 m D3 V1 R+ p; J3 W, | // Note we update the heatspace in two phases: first run
) c4 P9 b" |5 s0 L3 T // diffusion, then run "updateWorld" to actually enact the/ D3 j. }2 e% K5 n% i
// changes the heatbugs have made. The ordering here is
- @ ~1 b* v, i8 d" S H/ h6 {' x6 l // significant!- v* V" |) m3 y6 a$ x
7 a: J6 Q8 Z# i {" N) B/ N // Note also, that with the additional6 ^4 J! X7 P; H0 O& I% q
// `randomizeHeatbugUpdateOrder' Boolean flag we can# ]5 U- I( p- K- p' k
// randomize the order in which the bugs actually run
& R/ c6 N9 V' n6 W# U4 @6 S // their step rule. This has the effect of removing any
+ w$ D8 B, e" J+ B6 e // systematic bias in the iteration throught the heatbug9 o( [ D% v7 |& n% P3 p
// list from timestep to timestep
. H/ a' z2 p7 |9 J0 j# _ 0 }! G l7 p l, d' g& T% e! k) V
// By default, all `createActionForEach' modelActions have
$ d9 Q3 n* V# S // a default order of `Sequential', which means that the; ^5 Y3 b. I7 V$ I
// order of iteration through the `heatbugList' will be' b6 W, `: J5 n* \
// identical (assuming the list order is not changed2 S# c a: \* |/ W9 j
// indirectly by some other process).; o; U) U9 S7 j7 A3 l$ i
1 V- p& V! F5 L! x' c( j0 [( v modelActions = new ActionGroupImpl (getZone ());
) W! S2 R. u- d# L3 f0 ]0 n
; ^( m% H& t# T: h. O# ^' a try {' j- y% B& b& I0 V4 f
modelActions.createActionTo$message
# q% _( R) J) |6 y (heat, new Selector (heat.getClass (), "stepRule", false));
* y+ A" r( F/ [8 ]$ I } catch (Exception e) {. [! U% X! `; z. v% u. A4 ~. Z
System.err.println ("Exception stepRule: " + e.getMessage ());! m. G i) R p5 X7 U
}1 ?! Q% B7 Y: g6 G
. k$ i0 j/ w' f6 i, {' X9 W0 g( ?7 s
try {
6 W9 a/ Z5 L& D1 k _ Heatbug proto = (Heatbug) heatbugList.get (0);+ G, p" N% ^& ^/ B/ ]7 k* z/ r2 v
Selector sel =
, ~( d5 _( [ i/ }/ w, c c new Selector (proto.getClass (), "heatbugStep", false);
, }2 e5 ]4 Q$ [/ {, X' d0 n actionForEach =
7 e0 o3 _- v( X- F; b modelActions.createFActionForEachHomogeneous$call
# y+ N1 r# y5 h$ c% ?* w (heatbugList,+ R: }5 t* S; k3 t- J
new FCallImpl (this, proto, sel,
& c- ?" A: T( c- @ new FArgumentsImpl (this, sel)));1 u8 r6 }4 d% Z
} catch (Exception e) {
- T# k, q9 R' N e.printStackTrace (System.err);
5 f2 j) V! k. @/ i% O* x: | }
( X4 E! a" y/ K; m. `4 f: I0 N5 t & T" V) @0 F& W4 r& M
syncUpdateOrder ();
' _2 U7 ]' L- ~0 h2 l* @
( b6 e6 `; G& W try {& Y5 [) A7 p1 |: A7 z: t
modelActions.createActionTo$message
5 i4 @+ D% M9 h/ H" X% Y# J- L (heat, new Selector (heat.getClass (), "updateLattice", false));
8 A+ R- A# h0 h } catch (Exception e) {8 ^5 e( ?% w: @$ ?
System.err.println("Exception updateLattice: " + e.getMessage ());
7 S2 A2 k, w$ V: j$ o$ ^ }
% c1 W1 e1 ?5 m7 ?. C7 H( I% v
7 h. _* Q$ @' o // Then we create a schedule that executes the9 P) z6 F/ [ @1 {! ?' D0 d5 u5 P
// modelActions. modelActions is an ActionGroup, by itself it
2 `9 f6 q6 Z& y) T6 K' L // has no notion of time. In order to have it executed in
. I6 K. C4 {* B9 Y1 r. X; H6 [ // time, we create a Schedule that says to use the* B1 F. A( S0 _) A
// modelActions ActionGroup at particular times. This
) q2 k0 I1 A$ w' E // schedule has a repeat interval of 1, it will loop every9 V6 f; Z7 C" D8 `. S2 V
// time step. The action is executed at time 0 relative to `+ \9 [& ^4 C' T& ^
// the beginning of the loop.
1 ]1 D H+ M( a7 k1 L
4 j2 T& \/ k% R8 J // This is a simple schedule, with only one action that is
6 d4 j5 M7 ^/ V. h8 O, P5 _ // just repeated every time. See jmousetrap for more
% S: q1 B% z6 M* ^; s+ {/ ? // complicated schedules.: F. L6 h, S8 m5 i: B
4 B2 B" F/ l& G5 I7 g- N4 t; Z
modelSchedule = new ScheduleImpl (getZone (), 1);- F6 j3 N. `, j" d
modelSchedule.at$createAction (0, modelActions);
" u5 m+ u0 N2 U; u5 D
& n8 H2 J9 q/ F( O return this;2 D# L" e: `5 o. X' w2 }+ @ U
} |