HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# Z8 ^: F4 x2 F/ L* ~5 ]/ ~
p3 I# Y# v! r. V) g( l public Object buildActions () {6 ]2 S) Z' J5 m/ e0 u
super.buildActions();* p& a: v2 s; k
, o7 k* E. K' A3 I- Y // Create the list of simulation actions. We put these in
* b9 I: t6 r4 S* ~! x& H; o+ g // an action group, because we want these actions to be
+ G8 e0 B: V- i9 J // executed in a specific order, but these steps should
/ V& ^. G4 y% A7 v, U8 y // take no (simulated) time. The M(foo) means "The message
4 G* J3 f3 b1 @ // called <foo>". You can send a message To a particular, B5 V/ {5 Z7 ~- F$ F5 H4 T- N
// object, or ForEach object in a collection.
# i, c* u1 M, z. b" t) P 0 G3 Q8 b% Y$ X) V0 ?
// Note we update the heatspace in two phases: first run- v& Q3 ?: R' O1 y4 _( k$ ^: W: a
// diffusion, then run "updateWorld" to actually enact the
. ?9 X0 T: P t1 J // changes the heatbugs have made. The ordering here is5 D6 f& q, N s' r# r/ @# L
// significant!
: ~8 D; J8 r7 z
' x" s! C! _# {+ \8 p) t // Note also, that with the additional
7 y9 y2 \' Q ~, r // `randomizeHeatbugUpdateOrder' Boolean flag we can+ f" b& z# f/ Q6 l& }9 X
// randomize the order in which the bugs actually run$ @; D3 p( C$ }) H
// their step rule. This has the effect of removing any3 K' @' b5 D0 d [6 |7 c- ?& @$ g
// systematic bias in the iteration throught the heatbug
5 N; P4 {$ a9 |5 x // list from timestep to timestep* w/ w) h' g. ~) F
8 J6 y4 Y# c. z' d8 h3 O
// By default, all `createActionForEach' modelActions have+ W9 d0 n& G4 t* [; ^9 x$ L
// a default order of `Sequential', which means that the2 Z% s2 {, x8 o! B6 p; F2 e& C
// order of iteration through the `heatbugList' will be3 c6 S- h- o2 _5 C4 o# X
// identical (assuming the list order is not changed5 ]9 E" s9 k- g. G
// indirectly by some other process)., W r5 [8 }# q# N+ j' v
7 E1 M3 o& B9 P3 J T modelActions = new ActionGroupImpl (getZone ());7 ?% ^/ O$ T- ^; I i
5 S: Q7 k# r* N) z1 P try {7 Z2 m$ d; T9 A+ O2 Y7 g
modelActions.createActionTo$message- M5 x9 c1 L( A( s* L: x
(heat, new Selector (heat.getClass (), "stepRule", false));: i" r* J; F& H% u
} catch (Exception e) {
+ ^+ {0 U6 h8 Z System.err.println ("Exception stepRule: " + e.getMessage ());( q6 D" G! ^6 |; d! N/ P
}
1 @% z( b5 @7 g6 U, S4 P# D
7 ~' i; w! I7 Y try {
+ d: c+ o% v4 I. Y0 E Heatbug proto = (Heatbug) heatbugList.get (0);4 C; @4 Z" l5 {4 v, r& g2 B; |; X: _ V
Selector sel =
z/ @! {1 j* j% d1 V; z0 u/ F( V new Selector (proto.getClass (), "heatbugStep", false);- y- r) J" `. z% g) Y
actionForEach =
( a Y; x. `# d" Q. [0 v modelActions.createFActionForEachHomogeneous$call) }. ^- R. C: _5 h' u# R7 W+ V8 W
(heatbugList,( Q! u: `( A1 D7 D" i) e& T/ y
new FCallImpl (this, proto, sel,5 a! Q3 t( t* U# ?* U! y* N1 _
new FArgumentsImpl (this, sel)));& [6 \0 ]3 ^4 q1 v9 g
} catch (Exception e) {
: i% b2 r( I! A3 r- G+ T; w e.printStackTrace (System.err);: C, z; D6 M0 |0 N
}$ I5 d2 @, q3 d! h) D/ ]# ~
/ [2 V+ j$ p! m% }4 G" g syncUpdateOrder ();7 d) x# T* C! [
* C7 r) }/ t: V/ p7 C6 Y
try {
5 C3 c! d) h. C1 ]/ t& N modelActions.createActionTo$message
$ A( Z3 J! [, P' m (heat, new Selector (heat.getClass (), "updateLattice", false));5 I! H& Q' I) r' u+ t4 B
} catch (Exception e) {
% Q; V/ O: {9 N% X System.err.println("Exception updateLattice: " + e.getMessage ());
) Y, {( q* m. { h* c; P+ _ }
3 V- H% q9 j+ ]) `8 R1 i + b% j/ u2 y# J- _* [
// Then we create a schedule that executes the
; l: F& |: l' l2 G+ }7 J' E" d // modelActions. modelActions is an ActionGroup, by itself it
6 o+ w$ N4 ], d // has no notion of time. In order to have it executed in& D0 E0 F+ O- h8 k2 O* a0 x
// time, we create a Schedule that says to use the
% U9 `3 i7 B2 B3 o // modelActions ActionGroup at particular times. This8 U3 v' M5 H/ k2 ^ h! u, W
// schedule has a repeat interval of 1, it will loop every( p0 @* H4 C+ d5 L M
// time step. The action is executed at time 0 relative to& [5 r! l- r6 b
// the beginning of the loop.+ f' m3 v. |' g1 p% X4 S% B1 k
, w8 r* l0 L6 b" O6 F
// This is a simple schedule, with only one action that is
! Y/ B* |7 R! \* `* v+ n$ f // just repeated every time. See jmousetrap for more; f o5 K5 R- `# G( C9 `
// complicated schedules.
: L" s% I; u* j; P
9 V3 ^+ E3 [* J* r# t6 T* D modelSchedule = new ScheduleImpl (getZone (), 1);
b% |; m4 ?1 O { B! F2 I9 H modelSchedule.at$createAction (0, modelActions);0 t5 d# }# }/ A$ L; c3 m
! z: o' D, l7 w
return this;/ _) j; P0 ]! P, H
} |