HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: E/ }$ u5 G5 \; h
. B Q/ S. B' i1 i+ \+ r& g
public Object buildActions () {" {& E6 ^1 I* C: c* j
super.buildActions();, k4 j! R% B6 H5 X0 p- p% V# l
7 H" L2 t, c: d* E! R
// Create the list of simulation actions. We put these in
& b# A2 H9 k0 F5 v. Q, I // an action group, because we want these actions to be; j: u2 u5 p1 `5 O
// executed in a specific order, but these steps should
9 z, g8 F* v2 J# y( @" e // take no (simulated) time. The M(foo) means "The message
8 r) p6 Z" [0 d+ V // called <foo>". You can send a message To a particular
, {% ?1 n% q7 Q3 t // object, or ForEach object in a collection.7 B& B+ `( o, P+ c5 b0 z
3 B9 W+ ?$ I$ q- A2 F9 F // Note we update the heatspace in two phases: first run
* g0 q8 W3 H, h$ \. ?) ] Q // diffusion, then run "updateWorld" to actually enact the
% X7 a2 K' o4 t8 s$ g; ` // changes the heatbugs have made. The ordering here is
# g" R( v, |8 h' x) } // significant!3 Q5 n# W0 @9 B7 H% `) `& c
& |+ v* w2 i) O
// Note also, that with the additional
8 C. {4 i- U( d9 G# j( l // `randomizeHeatbugUpdateOrder' Boolean flag we can& h0 \& R$ r; b F) l2 \
// randomize the order in which the bugs actually run5 t/ `( r0 p/ u- v) l
// their step rule. This has the effect of removing any/ y3 J9 b% a% U; B9 [
// systematic bias in the iteration throught the heatbug
% r$ M c: g' q4 p // list from timestep to timestep
1 t! ^$ B) x u$ { , \: r$ J9 A. H' L- t- O: Y
// By default, all `createActionForEach' modelActions have
2 p$ y# q2 h/ g$ }; h- _* Z& N1 @ // a default order of `Sequential', which means that the
& j3 c% N0 D* s7 N G+ w) x+ c // order of iteration through the `heatbugList' will be3 r+ W0 O: ?' h* X# B9 I- ~/ T
// identical (assuming the list order is not changed
) X0 T* F1 o% [. R // indirectly by some other process).
( u O5 c; \9 r# `) ^
+ k5 h9 i: F @ modelActions = new ActionGroupImpl (getZone ());
9 O% c3 O% |6 X/ W, k
" F! e' U8 l( \+ C" W5 ^. P try {/ B% B; r+ L# x4 a4 D( D: h
modelActions.createActionTo$message# T& h: s+ o% n% S1 @! ?9 F) U
(heat, new Selector (heat.getClass (), "stepRule", false));" ~" B9 u0 v# b6 G2 H! I
} catch (Exception e) {- s4 Z5 n+ x, H- J3 L8 ^
System.err.println ("Exception stepRule: " + e.getMessage ());
+ T# ~; e/ w; c, F% T \ }# G& D% D; o" R' a7 E
' O& ?/ Y$ k; Y! T1 r8 P- G
try {2 ]* B) N$ C& _9 J; t
Heatbug proto = (Heatbug) heatbugList.get (0);. C/ D! q% K6 B: W7 ^+ q
Selector sel = ; _$ J' n' _4 V1 y( V! i: w
new Selector (proto.getClass (), "heatbugStep", false);& W% e/ g8 m( W/ Z) t% S, W
actionForEach =2 \6 L; p5 t" p$ q
modelActions.createFActionForEachHomogeneous$call8 Z# D5 A. j8 _9 b6 u; {
(heatbugList,# W) `- a1 w) i6 |9 ]2 f7 v7 Y
new FCallImpl (this, proto, sel,; D" O' B1 e4 E2 I% N1 ]
new FArgumentsImpl (this, sel)));
- I' c/ y9 Y- B+ m } catch (Exception e) {) o( a' L3 @" |! l; s$ S
e.printStackTrace (System.err);
; B/ |4 F. @( A' Q9 T- h }
# T4 c1 u& T1 \2 F 7 D* j D0 K3 R- q3 ~+ R6 H9 g' I
syncUpdateOrder ();
- E; g/ t( U: ~
( H# G0 ]* R' L n' V2 W' R0 t try {. J& w, _% A9 P l5 Z+ g% m1 d
modelActions.createActionTo$message
) |" M: Q0 l$ [- ~3 l (heat, new Selector (heat.getClass (), "updateLattice", false));9 p. J6 g* K c- O7 t* [
} catch (Exception e) {
/ O# E- M1 l" |8 J5 Z System.err.println("Exception updateLattice: " + e.getMessage ());7 i2 d" l* u$ X7 R" B
}! B' A, {: M E) E$ j
$ Q5 [0 S# D% x/ W // Then we create a schedule that executes the
" I0 V! r( t4 P. B; `! i- b* L: Q // modelActions. modelActions is an ActionGroup, by itself it1 G' u, w9 y0 |& |/ V: k7 O
// has no notion of time. In order to have it executed in
; D! A: w. h0 I) {( a // time, we create a Schedule that says to use the
- A4 l6 E t2 T# x // modelActions ActionGroup at particular times. This
1 e( W. O: \% V$ n2 o" C // schedule has a repeat interval of 1, it will loop every# c+ Q' `) ~6 o% r
// time step. The action is executed at time 0 relative to
% c1 j; Y/ N! q2 \5 O! m // the beginning of the loop.
6 L" l" e6 g+ b0 C) A1 O
6 W& m) A& ~& y1 x2 E4 [ // This is a simple schedule, with only one action that is3 c* Q4 ]8 m6 \, M- k$ X1 u. P/ Z
// just repeated every time. See jmousetrap for more
F1 j, _/ J1 U0 x- G // complicated schedules.
* Z( q, `1 x' P2 u5 k
3 E1 n2 y: f: s$ ]' Q7 T8 E modelSchedule = new ScheduleImpl (getZone (), 1);
9 G& z2 f& k: D1 r& R modelSchedule.at$createAction (0, modelActions);
0 K# o8 r4 {8 Z8 `
, k9 I' e8 {, e) H+ X0 x- W- K- u return this;( ]9 D3 @) J) G; \5 m, g
} |