HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 K6 N: f1 d! R }3 q
2 O1 o8 D! Q7 d- w. g public Object buildActions () {5 Y- N6 I* h- R _) u/ w" d% t( e# q* K
super.buildActions();
# u& z- W( F& |" I: l1 y. g4 [
0 g+ N+ u2 z9 c M$ t# R // Create the list of simulation actions. We put these in& I7 l5 G7 `) _ d0 {
// an action group, because we want these actions to be
* ^+ Q* k. }1 w // executed in a specific order, but these steps should
9 s- h8 Z2 G, r8 T9 U: r/ w# k; q // take no (simulated) time. The M(foo) means "The message' }: z/ j- d- W1 b2 [) ]4 U- o/ g
// called <foo>". You can send a message To a particular
( n5 P6 p1 U& f6 q5 _% Y // object, or ForEach object in a collection./ Y! w# K4 Q8 P/ l9 {
3 N6 w2 Y4 T% Z" ]; Y& e // Note we update the heatspace in two phases: first run
7 G1 V1 q0 j e) l! h9 a: A // diffusion, then run "updateWorld" to actually enact the3 W% u, X* ^( }( k1 W7 h8 O
// changes the heatbugs have made. The ordering here is# d% h- ~, O2 i9 u2 B) B
// significant!
3 d# `8 f7 d$ _ # G+ @$ g2 k1 c4 ^; P
// Note also, that with the additional8 k3 p$ M$ y3 x. a. P+ D1 H
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 C4 E5 Q9 O6 S+ |3 T
// randomize the order in which the bugs actually run& O/ b+ c# B" v8 l7 {! S% h
// their step rule. This has the effect of removing any0 S; G( C- y$ K5 E+ ~
// systematic bias in the iteration throught the heatbug; E8 y' h, e0 s
// list from timestep to timestep
5 z0 ?* ?. O9 m. j0 b% x8 {6 g
7 o5 f$ h6 r6 Q" J // By default, all `createActionForEach' modelActions have
% U% v$ e h: Q* ^6 d" ]% Y // a default order of `Sequential', which means that the
. z: C0 Z5 |7 O% I5 N // order of iteration through the `heatbugList' will be
1 A9 `5 B1 H: `& ^, y& |7 O" i // identical (assuming the list order is not changed
8 X% M# B3 j. t4 E4 \. n' R$ r // indirectly by some other process).8 D5 H9 a8 t* }
3 e$ w1 P K" q$ [ |/ e) N
modelActions = new ActionGroupImpl (getZone ());0 F- b" _- P1 l! ^; R+ u
1 }+ b- M$ a6 B, c6 C
try {: V: Q8 M- L4 v4 n% F1 O7 ~
modelActions.createActionTo$message1 s: _: O. n) e' k( p/ }7 G
(heat, new Selector (heat.getClass (), "stepRule", false));: h' Q( H9 X d" e2 {( `! o9 h# M
} catch (Exception e) {
( ^ s9 n. O. P' r2 Z, u- \. b System.err.println ("Exception stepRule: " + e.getMessage ());7 S6 |7 X& Y. l6 _) |
}8 V$ y: S! O. P ?( g6 E' @
! F0 X0 e# R# |4 g9 v
try {
# S- o3 ^! X- w; m$ {2 ^1 H$ e Heatbug proto = (Heatbug) heatbugList.get (0);
# J% w I# y5 t+ h! H8 t Selector sel =
: y2 w( r \7 V9 C$ J new Selector (proto.getClass (), "heatbugStep", false);
$ W2 K! K8 C! A ^2 k v actionForEach =
9 A) f1 L8 I9 ^1 ?6 P+ c modelActions.createFActionForEachHomogeneous$call$ T3 K, G* T- U/ Q
(heatbugList,9 T4 p! d+ {" ?
new FCallImpl (this, proto, sel,; n: T$ q. W# L3 a
new FArgumentsImpl (this, sel)));
3 I; n; y: k# A7 l } catch (Exception e) {
0 G% M+ o8 ]0 R6 y( X8 ~1 f1 S e.printStackTrace (System.err);
2 P1 ]8 I7 X: {9 U: X1 { }
9 Y+ k5 x3 y. h2 m* p, z. i - h4 p/ r: E$ o8 t$ J) j
syncUpdateOrder ();7 N# F, j( Z; d/ p) q! X
y( O- B6 ~" R$ e8 X, Z6 | try {& C/ B' k. i8 H
modelActions.createActionTo$message 6 _3 O4 V `6 U; S0 ^$ F. {% ]$ ~
(heat, new Selector (heat.getClass (), "updateLattice", false));
* E& l+ C7 ]9 ^8 f9 F8 ^' D } catch (Exception e) { e0 F6 F. S0 \1 i. j' X" W/ P! w
System.err.println("Exception updateLattice: " + e.getMessage ());
1 [; `% e3 {4 \; B6 K6 ^9 s, T% o8 I' A }
$ r. k6 t! [- ?5 U
+ U2 {( H) u: z6 S- g // Then we create a schedule that executes the
( \+ y& A6 m1 [9 A; q4 ^: X // modelActions. modelActions is an ActionGroup, by itself it
" F+ Y3 V2 e, `) X* F' ~. P // has no notion of time. In order to have it executed in
- L- K$ B5 w) k1 D# v# b! } // time, we create a Schedule that says to use the
( C: i. w n+ C2 @5 S // modelActions ActionGroup at particular times. This
. d$ {/ C4 [& m: o // schedule has a repeat interval of 1, it will loop every
' M8 d* p. n4 A7 w" l2 C% o // time step. The action is executed at time 0 relative to
# Z* \* B. K; Q" @4 _2 [ F // the beginning of the loop.
" u9 l [- _# _0 f; x( g* g, [! C* r9 F$ G4 e) y9 Q
// This is a simple schedule, with only one action that is8 b$ Q# J" ^5 y& f/ n
// just repeated every time. See jmousetrap for more
; _$ r" ^, Y8 u+ H, I0 ` // complicated schedules.
7 ]1 G7 a: z) @- }5 X6 |0 c
* a! ]2 t( A* Q8 z# Y* h modelSchedule = new ScheduleImpl (getZone (), 1);, f7 o: i1 h( `
modelSchedule.at$createAction (0, modelActions);" J1 a+ f) o& y
5 ?8 `( d! M8 M. W0 K _
return this;) S! V5 Z. m6 A
} |