HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# b/ A# r' N8 [7 f" C* k: h' R4 \8 H; v9 I p( V; r4 I* n! L* m7 U- B
public Object buildActions () {
! ^1 y% x, O/ z; t! ? super.buildActions();
/ {5 Z- d$ Y) Z4 S( \8 y9 }1 s ( M! X' E# E z) D
// Create the list of simulation actions. We put these in
% x: E) d1 ^, I. {( u6 x // an action group, because we want these actions to be1 q$ a+ L( C) Q0 r2 A
// executed in a specific order, but these steps should6 R9 D) E: J4 G- V/ b3 U3 R
// take no (simulated) time. The M(foo) means "The message4 t- }* Y- Q; p8 I( y
// called <foo>". You can send a message To a particular A2 C0 P. |4 {
// object, or ForEach object in a collection.
' u/ u' U4 f6 h% o 1 ?" `* y4 v* |
// Note we update the heatspace in two phases: first run
# O7 V( {% j4 u5 @$ ^/ R // diffusion, then run "updateWorld" to actually enact the
3 d0 V& ~$ b! W1 Y! C // changes the heatbugs have made. The ordering here is
# h) u% `' g2 ~% U! w // significant!
R2 f* N2 {3 p0 Y' |: y
3 D- O6 c1 I" }4 Q1 S' p! @ // Note also, that with the additional$ j- F1 q! s8 m5 p# g& n3 t& ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can* ~+ V* K& d) o! t1 }/ A
// randomize the order in which the bugs actually run
6 {! E7 G5 p6 ?! f; U1 K$ k // their step rule. This has the effect of removing any
4 Q% G& ~! e% `1 b% Z: H2 B. B // systematic bias in the iteration throught the heatbug7 ~ j# ^3 g. v* o
// list from timestep to timestep( x) |0 k$ i" l! g2 I2 x" k1 p
$ J8 I/ H! H! U/ V% B% a // By default, all `createActionForEach' modelActions have
' L% t2 x5 s& l6 m* X0 S ` // a default order of `Sequential', which means that the$ {, h. N$ u5 K
// order of iteration through the `heatbugList' will be$ b& S: _# O/ w- f% S6 ]. ]. Q& ]
// identical (assuming the list order is not changed- |. Y4 o' f/ Y) G8 y4 u/ _- H' z
// indirectly by some other process).% ]0 ^+ i3 Q- d
9 |- U% M: e) a- s9 ^
modelActions = new ActionGroupImpl (getZone ());$ i5 v/ N4 A# |+ P8 U! E! L: C
6 l0 V; n* n. A( ] Z( a" F try {, X$ Q5 _% s* R/ i, V3 V
modelActions.createActionTo$message
& b) j" }/ K9 p3 Z- P (heat, new Selector (heat.getClass (), "stepRule", false));
! o7 c- P Z/ H/ v, N- n; k } catch (Exception e) {* ~, ~/ d, d, j
System.err.println ("Exception stepRule: " + e.getMessage ());
: ~2 `; i: C2 l }! t' a# p/ i/ P- _4 s8 i& f
: g6 a% K% `. D7 A, _
try {
' X$ ^; o5 }: n& W% m+ c Heatbug proto = (Heatbug) heatbugList.get (0);
) }3 A8 k4 O5 z2 {& L) k! w Selector sel =
+ A, }7 E) \- o new Selector (proto.getClass (), "heatbugStep", false);
& [" s# ^' f3 f+ ?3 b actionForEach =
$ v6 d, ^" G1 U7 A modelActions.createFActionForEachHomogeneous$call
0 P! U- Y0 |4 P5 A& J2 A, Q9 U (heatbugList,( l I- W' l! O& l, N- p
new FCallImpl (this, proto, sel,
" Y* w+ |0 C/ f; u8 C new FArgumentsImpl (this, sel)));2 |! I7 ]" E# D
} catch (Exception e) {
+ e* u5 M. o3 H' s% s$ [) M e.printStackTrace (System.err);
' T. i* L: C$ ]" M5 G7 B5 E! o }# z! Z- J5 a9 m
5 P; S, @' i9 }/ A2 U2 K% }/ y syncUpdateOrder ();
3 O" r5 y4 N) X" a) Q4 `
- Z$ H6 G, w/ o% H; u' U5 ]( H try {( k5 l" _# m" \, \; ?: X' y# ]5 v
modelActions.createActionTo$message
1 K" c( ]! X9 d2 s2 M% K" T) I (heat, new Selector (heat.getClass (), "updateLattice", false));
, [$ L: o- e; Q. o& s3 Y } catch (Exception e) {
# \, q3 h, g! k) W0 |, s System.err.println("Exception updateLattice: " + e.getMessage ());4 h+ M* L8 F7 o5 v
}5 K: y; s) E/ t7 [$ A$ q) U: x
/ j2 c, t/ _; _2 [ b% j. C
// Then we create a schedule that executes the
2 n( ^* t- ?8 a9 g // modelActions. modelActions is an ActionGroup, by itself it: C) y5 g* k# L
// has no notion of time. In order to have it executed in! \9 A4 D" G* |$ h ?( i$ ^+ [2 ^' J. N; j( Q
// time, we create a Schedule that says to use the6 ~4 L8 c, n& f- p7 M8 _# W& W
// modelActions ActionGroup at particular times. This! b+ y+ X- Y5 L3 M4 F& ^* d
// schedule has a repeat interval of 1, it will loop every0 n( G" u* U1 L
// time step. The action is executed at time 0 relative to
" t; `+ c% a c) U6 o // the beginning of the loop.
5 Q0 e0 ^6 O3 v1 l6 j" s' c- `% e) j+ @ c
// This is a simple schedule, with only one action that is! o% [: k$ o+ m! g
// just repeated every time. See jmousetrap for more$ U! y6 ~, v+ v4 S( t
// complicated schedules.
6 W7 N. \, e" x3 r+ I
- W- H' X p+ V5 l) F. I I modelSchedule = new ScheduleImpl (getZone (), 1);
: c4 u9 a: z- J modelSchedule.at$createAction (0, modelActions);' Z. s' z' q' P
3 N" A) h( F7 x B2 M6 y return this;
2 i6 M- f) o7 k9 Z3 p } |