HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, g- X( K: |2 @+ ~) @2 Q% C m4 X" F
public Object buildActions () {
5 Q! ~$ m3 g+ u! d9 X super.buildActions();4 z# U0 r% d+ `: O# o- @
+ K7 B1 R8 B5 X; d // Create the list of simulation actions. We put these in
9 P& ?9 L$ l& j& p, Y // an action group, because we want these actions to be
2 X' K" [/ {: k. S3 }+ ^# G/ m // executed in a specific order, but these steps should5 V- ]" p# Z. Y
// take no (simulated) time. The M(foo) means "The message
4 M3 a* W, n$ U* f // called <foo>". You can send a message To a particular( L! Q( t0 K! ~% I; J
// object, or ForEach object in a collection.( Y) s- W% s2 i2 D4 B
T. X/ X* z$ V1 r. r" P- [2 r // Note we update the heatspace in two phases: first run: ~2 G; y( s I/ S8 ^1 B
// diffusion, then run "updateWorld" to actually enact the
& E7 Q; u+ d/ V // changes the heatbugs have made. The ordering here is0 O, H4 ~5 v2 z8 V9 |
// significant!
! U9 Y) |( ~, h; k+ Z3 P- F/ M/ L ' o' z/ R/ f1 V3 y
// Note also, that with the additional5 H; A; M0 L/ ?6 L
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ \, D! Y) }4 J/ b2 e4 P0 q
// randomize the order in which the bugs actually run
* ?( r, W$ @/ I // their step rule. This has the effect of removing any6 @6 k( |! `5 u) P. A b) S
// systematic bias in the iteration throught the heatbug1 T) g; \! L/ Z
// list from timestep to timestep* j2 _' q1 y2 K" ]: `
7 U1 K- z/ t |$ R // By default, all `createActionForEach' modelActions have1 _ B( l; E+ U* ~
// a default order of `Sequential', which means that the
) Y- ^: j: l1 p9 x: v // order of iteration through the `heatbugList' will be
C% r" O. u/ E1 G3 o: M // identical (assuming the list order is not changed7 N/ s* K, Z3 l+ o: X
// indirectly by some other process).
, T5 ]: @7 x$ @7 V7 o 1 v) x7 O, w$ ]
modelActions = new ActionGroupImpl (getZone ());9 O7 }4 N# x* B; D: U
$ y; |" E# z9 ` try {
6 y9 Y$ P+ T' L- J8 o8 S modelActions.createActionTo$message& f$ U- [4 n! w, t# z4 g6 {
(heat, new Selector (heat.getClass (), "stepRule", false));% Y5 ~- ~1 e) H4 C A$ @7 i
} catch (Exception e) {
3 l8 G* S; f" n9 ]: | System.err.println ("Exception stepRule: " + e.getMessage ());! m t2 v- a8 m& U
}2 F, M$ p8 }: i2 Q3 I* o; M
- c; W6 H: B+ T$ B) v/ E: W
try {
. M ^" n7 m- k/ k: _2 \: | Heatbug proto = (Heatbug) heatbugList.get (0);
9 M8 a! H! o- C3 a _ Selector sel =
% N$ v( h& H2 i. N6 }& Y new Selector (proto.getClass (), "heatbugStep", false);
) m0 L8 j6 R1 q7 [4 l+ { actionForEach =
+ \& k9 {! E2 x! S* ^ } modelActions.createFActionForEachHomogeneous$call
9 K* O7 i7 I& h% l8 S" _ (heatbugList,
3 t8 [6 c9 C7 ]5 |/ H: q" L new FCallImpl (this, proto, sel,+ c% r* m9 e4 V) X' U" N: t
new FArgumentsImpl (this, sel)));4 ~/ B; ^' b3 E- V' E
} catch (Exception e) {
1 L, b" H6 Q5 Y- L e.printStackTrace (System.err);
$ m0 B+ \* |: a7 E }
" z0 N+ b6 s3 M& g; _9 U6 [1 i " Q: x% V* V9 q9 E3 F. f* r
syncUpdateOrder ();
/ u& z, _- f0 V. t7 R* T2 Y" l5 j0 ~# S' C* q) n
try {+ l/ X9 _: m0 {' ?( }
modelActions.createActionTo$message 6 K& [4 z1 X6 q$ E2 l: h0 x
(heat, new Selector (heat.getClass (), "updateLattice", false));
* W( N/ w: e" Q) e0 S, { I } catch (Exception e) {5 i. V0 @8 e3 X' c) L
System.err.println("Exception updateLattice: " + e.getMessage ());
' h- J3 g) ?2 z4 m* P% w }
5 |/ D1 [6 g7 { j1 f0 r) d6 p : l8 ~, ?) y' m, {0 |: P* A) a
// Then we create a schedule that executes the- w4 z3 i5 @: G' Y; C
// modelActions. modelActions is an ActionGroup, by itself it
. _) Y6 w' D4 Q# i: d) ^ // has no notion of time. In order to have it executed in& w' c2 V& O0 e- Y
// time, we create a Schedule that says to use the, q! W" L) a9 p$ c3 L/ {* O
// modelActions ActionGroup at particular times. This
+ \5 z) n( Q- t8 d" W$ U; C // schedule has a repeat interval of 1, it will loop every
2 V- |1 y( o S. D$ U // time step. The action is executed at time 0 relative to9 ?4 Z8 W9 j5 i3 B7 D
// the beginning of the loop.: d6 I! d( R% c6 C
& ~" S! Q' Y& }/ ^ u // This is a simple schedule, with only one action that is$ z3 E2 B$ |0 s$ C2 m# v
// just repeated every time. See jmousetrap for more
3 p# a' Q; |1 h$ ]6 K! y1 k // complicated schedules.
" h0 j+ N& f8 S- w. a7 @: P
2 d! V4 z# `$ ^% C4 s) H modelSchedule = new ScheduleImpl (getZone (), 1);
5 H1 a0 f) ?" q modelSchedule.at$createAction (0, modelActions);
9 V* `! r8 p6 E3 C2 B; D
. `0 E" }# c2 ] return this;
' L6 K+ y+ B, S7 }$ g } |