HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" L% ]( [& Y; b$ h% K \
8 _, H1 T, F- I z( r. T7 S public Object buildActions () {* @' b- p0 {: H5 e! F
super.buildActions();
$ }) l: Q. G( Q2 W) |* r& o
) B# I8 }# E" T1 n! B4 d( J# v // Create the list of simulation actions. We put these in1 P; f9 m' y1 N& W, e% D
// an action group, because we want these actions to be! l+ x7 @3 B1 G6 \
// executed in a specific order, but these steps should4 D5 J; y. L8 O, f3 f
// take no (simulated) time. The M(foo) means "The message
* x; [/ L& a/ K9 x+ J" u1 a // called <foo>". You can send a message To a particular3 a) o' _3 R/ e) G3 b4 l
// object, or ForEach object in a collection.2 l: L' o8 I. E, ?( `9 b
( S( ~. b% h& ?
// Note we update the heatspace in two phases: first run
X+ q) X2 r$ B! D2 R // diffusion, then run "updateWorld" to actually enact the7 P" U' {( G* S* O% c9 y R: `2 d
// changes the heatbugs have made. The ordering here is. V3 D8 X# i% ]) _1 l( b
// significant!" S: U1 i$ P+ w$ H ]/ B$ x
; }: a& A0 r8 q/ E) a
// Note also, that with the additional- h$ b3 C+ e. J
// `randomizeHeatbugUpdateOrder' Boolean flag we can" o/ Y& `" g4 e6 `
// randomize the order in which the bugs actually run
+ t7 H+ w. m3 b! X9 | // their step rule. This has the effect of removing any! D, s, W* ^" W7 I& {
// systematic bias in the iteration throught the heatbug
% a8 {; b1 J5 v _2 c/ o // list from timestep to timestep
- A* K0 ? A% ^5 V
- E/ C0 C. r, h j. q; z // By default, all `createActionForEach' modelActions have
. c* W9 J7 ^6 u8 W* u // a default order of `Sequential', which means that the
- _$ l4 l' T0 C% [+ i // order of iteration through the `heatbugList' will be4 u: ]2 {5 X% F7 N
// identical (assuming the list order is not changed
- z7 v2 c) T5 P# n7 N$ |, w7 M0 j i // indirectly by some other process).
3 @' U; ]& Y% a% o+ S; k0 Y0 D1 u + r& k- H$ u1 o: ^: Z4 @) ^* O/ q1 D
modelActions = new ActionGroupImpl (getZone ());! } c `9 j$ g/ L0 r; T
" n/ Z1 a' d1 n5 Q* z try { j- w$ |9 V# G$ e( i; @( D3 k2 G
modelActions.createActionTo$message5 u% C0 ~$ g- q8 K, g" @. A5 ~# Y) _3 s
(heat, new Selector (heat.getClass (), "stepRule", false));' J! A& A$ ]. B& k6 {, s# v
} catch (Exception e) {
6 R! M. `5 F, J. ^9 V5 M System.err.println ("Exception stepRule: " + e.getMessage ());9 Y7 p2 A$ w, f1 P; n0 X- {) A: ~ ?
} Z7 R6 g$ P. i( _% R
7 Q3 C* Y1 `$ a try {) |6 T; D' j3 j1 x( g- X. a3 r; u
Heatbug proto = (Heatbug) heatbugList.get (0);: P7 n4 O+ {4 Y6 e9 A- T
Selector sel = 2 r- V9 L0 [ a3 O/ {5 g/ L7 @. k
new Selector (proto.getClass (), "heatbugStep", false);
$ R) w% b: b; `, R0 ^ actionForEach =3 r8 r5 s! o- s3 q( j7 P4 m
modelActions.createFActionForEachHomogeneous$call/ t1 M. B5 r3 u- G- k5 z
(heatbugList,
' ~2 }* g1 ]+ P& p new FCallImpl (this, proto, sel,
# L8 G3 A n! ?$ S2 H6 h new FArgumentsImpl (this, sel)));
, S* ]# W8 f2 e! t } catch (Exception e) { W+ m5 W g4 x9 d/ Q) G
e.printStackTrace (System.err);; Q. c- l" {; D3 V4 A
} V! J1 @& {9 e0 h
: R, f- f& q- @& ]$ F syncUpdateOrder ();
1 f' `( X; f. r* Z4 C1 G
" \* a& `. x& a" |: a try {
/ z1 x0 U+ ?, {( \, h modelActions.createActionTo$message ( o5 w" N' c7 i- t# h
(heat, new Selector (heat.getClass (), "updateLattice", false));
( I* w# w; l7 z6 K" p6 o# c } catch (Exception e) {! Q- X; N; b4 P; x
System.err.println("Exception updateLattice: " + e.getMessage ());
' k7 o% F5 V6 Y }
$ d/ q" H7 U7 Y% H9 N) I) f
, w! s$ d$ X* U# k9 G; T& T // Then we create a schedule that executes the7 Q6 J. q$ U, O' b% @. E
// modelActions. modelActions is an ActionGroup, by itself it" n% f. x( M! }) Z/ w
// has no notion of time. In order to have it executed in
) L5 c i7 \* P" ?( ^ // time, we create a Schedule that says to use the
0 A, x9 n, {# o) _+ X, k+ d // modelActions ActionGroup at particular times. This
( K5 Y& C& D9 v- u. r // schedule has a repeat interval of 1, it will loop every2 Y9 o$ N+ J6 |1 y
// time step. The action is executed at time 0 relative to
* j0 A( w% A* u0 l9 | // the beginning of the loop.
% D J% F: ?9 R/ E; `8 @
P1 U; S2 C' _9 D4 y // This is a simple schedule, with only one action that is3 `7 \2 O2 w+ L, O% K9 t4 {, t
// just repeated every time. See jmousetrap for more
U! r$ s a3 o+ d2 n8 |" m! i5 a // complicated schedules.
+ y' h7 I; i3 B! G3 k: `+ t + Q5 S! c) o: I& U; j
modelSchedule = new ScheduleImpl (getZone (), 1);
, z; r& t6 c/ B' n0 ^% b% ]9 ~ modelSchedule.at$createAction (0, modelActions);
# Z# M7 ?0 C, l5 U9 _ / J" s" L( x7 G+ ^; @
return this;7 d. L; B2 q, T# ^/ W' P6 t
} |