HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' s2 C& A: e' `; q5 ~: n) x/ ~
/ [8 s# d- \9 }. q9 F public Object buildActions () {
: V6 m: Z4 i2 O super.buildActions();
8 v% b0 A) S7 t8 d & u, Z/ C. c7 L; C$ t. Y/ ]) J
// Create the list of simulation actions. We put these in' e! K# X) b% Q. x& f
// an action group, because we want these actions to be
5 m3 d+ U: v: L1 z! ~ // executed in a specific order, but these steps should, U) I( V) a" H
// take no (simulated) time. The M(foo) means "The message
& i% o- Y! P+ z/ l // called <foo>". You can send a message To a particular
2 \7 k# c6 N+ D0 e j" E // object, or ForEach object in a collection.
: f+ Q$ F& `5 J% D5 ]' y+ E1 U ( l; Q7 ^9 O% f6 G' t. }' V1 R
// Note we update the heatspace in two phases: first run
9 Q$ `! f/ D% |& y( K0 D D // diffusion, then run "updateWorld" to actually enact the% m' H3 ], C _% N
// changes the heatbugs have made. The ordering here is; s6 Q$ `3 V- }- a2 d$ z
// significant!$ E5 [4 o6 L' o; u" I6 a
* o+ J; N1 A- z' h9 C3 c
// Note also, that with the additional& D$ ?/ ]3 i2 R: x0 e3 K! O
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ E6 P; p4 o& T9 f- q. c
// randomize the order in which the bugs actually run2 o% ^& M8 |, v0 C
// their step rule. This has the effect of removing any
) L! f7 i" u8 v2 a( l1 N // systematic bias in the iteration throught the heatbug
) `( C2 ~# t8 W" o: ]6 W4 U // list from timestep to timestep
. Q! O, l) ]9 E ! G& n+ o' ]5 R; a# y4 C
// By default, all `createActionForEach' modelActions have1 `+ `* y0 t: ~+ U, F
// a default order of `Sequential', which means that the; @8 }; ~8 ]3 E. {
// order of iteration through the `heatbugList' will be! `" m: o U$ ^ h( B& _4 D
// identical (assuming the list order is not changed u9 B& N' d) j
// indirectly by some other process).
' ?" n' j5 `6 ^3 H
) a6 s3 n- U b9 |, w modelActions = new ActionGroupImpl (getZone ());0 y& j( _ v8 r
& I$ k# K$ G, w3 D$ E$ @7 S2 Y& L0 v) l
try {0 L* Q/ Q* R3 E
modelActions.createActionTo$message
8 ^' V6 R; A/ u2 w: B# u (heat, new Selector (heat.getClass (), "stepRule", false));
/ ~" o- b2 Z5 F% t } catch (Exception e) { {. }" |: p4 E3 y& I* [6 d3 t7 Y( }
System.err.println ("Exception stepRule: " + e.getMessage ());6 j" N& D* X) F% k6 a
}" t) ^% w% f- w2 m
: ?3 L6 j' S, T2 t/ L; o0 V6 f# V
try {
; D0 s* ]- r( Q9 q$ d Heatbug proto = (Heatbug) heatbugList.get (0);
2 J- ]- b6 q: l, r0 H; C8 k Selector sel = , i( q) g! \' C: B
new Selector (proto.getClass (), "heatbugStep", false);. w: B8 L: G2 d: \2 F+ ~
actionForEach =
+ _) v" v4 h5 h2 [ modelActions.createFActionForEachHomogeneous$call; a- j/ x6 u- }, j# K
(heatbugList,
9 z' G/ R w4 z& N9 k G5 Z$ S new FCallImpl (this, proto, sel,
; j. t* z5 t, F new FArgumentsImpl (this, sel)));
3 b8 o( b4 |6 Z- X0 x& h } catch (Exception e) {
. r! j. w/ b. e3 \) U1 i% G e.printStackTrace (System.err);
3 e- G, d u" K0 ], w) m# ~ }
4 @1 J/ V* C, t 7 y2 A. \$ O6 V) k
syncUpdateOrder ();
% S2 F9 f' `$ E9 t6 d
' B" |! Z1 V! Z7 \0 h3 w try {
?1 E* e9 q6 z5 P8 q' q7 A: a modelActions.createActionTo$message
7 \+ U/ P7 D+ d: l; K: c7 q. p (heat, new Selector (heat.getClass (), "updateLattice", false));
' U" g' C% \8 \: v } catch (Exception e) {7 L3 D) s/ R8 P7 R- j, S {
System.err.println("Exception updateLattice: " + e.getMessage ());
! G# n) F# c/ L! ~; |9 n }1 G* U6 [8 u) {* v4 X) H8 Z/ z9 l
& ?: @3 x8 Z% c6 D) d% R // Then we create a schedule that executes the& G! ?# N& t) y# N! O# A, ?
// modelActions. modelActions is an ActionGroup, by itself it
; Y) {/ X* J5 i4 \/ b // has no notion of time. In order to have it executed in4 Z y2 [5 ~$ y8 I: k
// time, we create a Schedule that says to use the& G. t, U6 r ?& d
// modelActions ActionGroup at particular times. This
/ |' D; ]7 v8 b* `- x |* F! ` // schedule has a repeat interval of 1, it will loop every! y! H; z. _6 ]5 M
// time step. The action is executed at time 0 relative to" }( @+ M# B3 {1 h4 R4 \
// the beginning of the loop.
8 K/ w( b/ u- W! U. Y
7 \2 \$ a& R2 j, {" {2 O4 f! ` @4 L // This is a simple schedule, with only one action that is
& C* c8 y) h* f# a2 O7 ]8 { // just repeated every time. See jmousetrap for more
1 U% ^$ Q# p2 r3 |+ N // complicated schedules.
$ J- D6 y% v+ P6 G [2 |. R' r
5 y8 u. U; a, B+ ]4 B" ^4 ?7 T modelSchedule = new ScheduleImpl (getZone (), 1);+ t5 \- v, V4 G$ [. l) _% g, j
modelSchedule.at$createAction (0, modelActions);
/ @, Z* t2 s, f
# E$ j0 g" h- o$ b4 ? return this;1 w. Y- I. e$ V- o) y1 |- D
} |