HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ @8 t" O+ ?+ f# X
+ r5 | d$ O7 J! Y( B
public Object buildActions () {
. y* b6 Z$ m1 c5 o super.buildActions();% l, _" Z u% o: N T; b8 o" n
& y1 a$ G! Z, [& R
// Create the list of simulation actions. We put these in6 S% g8 Y# f6 C) K9 L
// an action group, because we want these actions to be
' Y# h0 \2 l. t3 t: K; ~1 ? // executed in a specific order, but these steps should
0 v1 ]. ^8 H( F% v* d+ m // take no (simulated) time. The M(foo) means "The message
4 N0 n% ]" K7 }% @ // called <foo>". You can send a message To a particular C& N* p" h4 S$ T' U8 a {9 c
// object, or ForEach object in a collection.$ e9 K5 i9 @4 E: C
" z t! n/ h# R0 V // Note we update the heatspace in two phases: first run
/ z: N }0 o u // diffusion, then run "updateWorld" to actually enact the
$ q* \+ x( E* S7 Q& _ // changes the heatbugs have made. The ordering here is# V) l, \9 D9 F4 R7 `5 z' u
// significant!& @, h/ f+ W D
- @7 } M- N: k3 [, |3 a2 u8 _ // Note also, that with the additional" o0 U$ P! @/ x; c5 m6 z- R0 c
// `randomizeHeatbugUpdateOrder' Boolean flag we can, ^7 M5 U% g- A! w$ T9 f
// randomize the order in which the bugs actually run a( Q4 w! p! @) u8 O
// their step rule. This has the effect of removing any2 Q& v" z1 @& D D9 c9 V
// systematic bias in the iteration throught the heatbug* {/ h8 C$ ?3 c6 G: t/ c& M4 I
// list from timestep to timestep( U q7 ~& b# q
5 \- s7 w, p6 l( e( m; w6 i: I // By default, all `createActionForEach' modelActions have" O; u7 Y" `2 c) f; m, m" S
// a default order of `Sequential', which means that the
2 U7 G7 N) Y1 c // order of iteration through the `heatbugList' will be0 D9 R$ S5 X. B$ D
// identical (assuming the list order is not changed
8 B* _8 U: Q8 u! F1 p$ a1 s. z // indirectly by some other process).# V4 Q+ \1 o" O) a% `/ I) v; l
! C5 x5 g- Q( p H$ S6 z
modelActions = new ActionGroupImpl (getZone ());* e. E" m6 I% V `
; m) T; R9 h, j$ o5 A try {# \" o( M$ i s1 |
modelActions.createActionTo$message
5 h; Y1 O* u$ w! F4 H (heat, new Selector (heat.getClass (), "stepRule", false));" I. y* h( n. E; l
} catch (Exception e) {
& W# f! i# i( y' `; g: Z System.err.println ("Exception stepRule: " + e.getMessage ());
% u# s t3 ]1 k' E+ l& o! T }
( S$ o3 }; w# c8 ]9 o4 \
7 g' B* c. ]# D8 ] try {
|2 y. [! G8 u+ i$ f$ n7 P5 n Heatbug proto = (Heatbug) heatbugList.get (0);
7 Y/ {0 _ F3 y4 U$ a. S8 ? Selector sel =
! P& L, I8 x8 T5 V new Selector (proto.getClass (), "heatbugStep", false);
% X; ^2 Q' N2 o& M9 U! G actionForEach =" g0 t0 F& F3 A% t: O3 B- ~
modelActions.createFActionForEachHomogeneous$call
0 k) u- R# I& [ Y: Y9 y+ u) {' U (heatbugList,* H* ^/ j8 a0 z4 C- B! K! d, r/ y
new FCallImpl (this, proto, sel,
6 s9 H+ o/ i# n+ t* Z! S9 F7 r; C new FArgumentsImpl (this, sel)));% x& P* m( X7 M+ H9 e, O f; g( J
} catch (Exception e) {
3 L' r' Z/ v2 F7 \- b e.printStackTrace (System.err);) W7 u4 Y( h! E& V! u) C+ D
}# D4 E- J2 M- _
& c* m) I; _$ C2 k: h6 h
syncUpdateOrder ();
% U$ b. r7 }, x0 O+ h% D# v& e9 v* ^* N+ L2 R% [# y
try {
$ Y& ~. {4 C9 t- J; K. F8 E5 o modelActions.createActionTo$message 3 T! x8 |0 A. ^- W7 _# @
(heat, new Selector (heat.getClass (), "updateLattice", false));
* y( S5 D+ F K3 n } catch (Exception e) {
' P7 K' L! r2 A; V% a System.err.println("Exception updateLattice: " + e.getMessage ());
/ `6 E/ W u/ [* P* G& E }7 z2 E5 Z5 j& S0 D4 T, x" u
* b J5 d3 H+ z8 W ]0 H3 I // Then we create a schedule that executes the
% [2 y/ R. V* P8 A3 ~3 T // modelActions. modelActions is an ActionGroup, by itself it6 J3 G% w* g, H& }
// has no notion of time. In order to have it executed in
# c! u* U. ^9 @; q# E- ^* o. ] // time, we create a Schedule that says to use the
7 _2 x# v/ @7 C // modelActions ActionGroup at particular times. This! y! k4 D) V/ K& o; [, ~5 f! h
// schedule has a repeat interval of 1, it will loop every
U' v u6 O* _ ^8 A# U // time step. The action is executed at time 0 relative to
1 s" Y$ O: _) a // the beginning of the loop.2 F! j" ]" h+ _$ p2 O ]: t1 Y
. C$ ~4 C) d% H% J; i // This is a simple schedule, with only one action that is" K* j' b+ ^! E y1 s1 w! M6 G5 L' {
// just repeated every time. See jmousetrap for more" O! m. v# v* T& f! I
// complicated schedules.
+ u& C* z- x0 P; y$ k" }! ?
2 X! f, x+ Z! d: u modelSchedule = new ScheduleImpl (getZone (), 1);; c+ X3 O z% P+ ?+ ?! j
modelSchedule.at$createAction (0, modelActions);
9 A. q3 ^5 y) @! S8 @3 u % d# o) ?% \ h H: r- R
return this;. X9 ]6 j1 t8 ]* j
} |