HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 b* a5 p7 ^& T) {/ {: N$ E: M% w9 K$ n! }/ g% p( i4 P3 J u
public Object buildActions () {
( ~# |9 ?4 I3 j/ B7 ]# [* v super.buildActions(); f& W' z$ ^+ `8 ~+ e
' J0 h$ U- s4 p& i // Create the list of simulation actions. We put these in
* r: g9 {: `8 v9 W, |. k // an action group, because we want these actions to be/ B( l8 Z3 v) H9 ]( o! x
// executed in a specific order, but these steps should
: C! }7 K" t+ P3 l // take no (simulated) time. The M(foo) means "The message5 f; K8 s. Z7 W
// called <foo>". You can send a message To a particular
' ^& j( c7 Z2 D! T7 J8 b // object, or ForEach object in a collection.
- [6 h& ?) R x$ S 4 s$ o9 ~- [! B, c0 W( }
// Note we update the heatspace in two phases: first run' V% f7 w P8 W# p1 N% Q3 b
// diffusion, then run "updateWorld" to actually enact the3 m7 F' X4 n& b; r, H& |6 |5 f
// changes the heatbugs have made. The ordering here is$ ]0 \4 b& `7 s
// significant!
- v/ l! V f" u0 i
( D0 S/ y; b3 ?) Q) ^" \ // Note also, that with the additional
% I8 h; u! _+ v9 F* y+ | // `randomizeHeatbugUpdateOrder' Boolean flag we can& h; }( H* y, r. i$ K* f
// randomize the order in which the bugs actually run
5 V6 ^/ H+ B! ~9 n6 L6 [ // their step rule. This has the effect of removing any
7 x3 S) i6 ]! T0 R6 _& f" [% S // systematic bias in the iteration throught the heatbug; a7 Q0 x" \$ M+ \7 l+ v5 l, D' G
// list from timestep to timestep
6 Q' l* u$ o1 P$ Z/ X; w( z0 V 2 P% N7 S r. `* A- a
// By default, all `createActionForEach' modelActions have
+ C6 x& [0 N4 D+ Q& S- r( Q // a default order of `Sequential', which means that the
+ G' p0 w) l* o2 A# k% Z // order of iteration through the `heatbugList' will be/ w$ ?' k8 [) }! p& P5 ]0 z
// identical (assuming the list order is not changed" X u9 d5 ]5 B, C4 F9 e
// indirectly by some other process).
8 S7 l! _( p7 T/ N# t+ L ^
- ?6 ^9 u0 l& |: I modelActions = new ActionGroupImpl (getZone ());
- R% K. G8 N6 Z( ^5 r1 }/ X: L4 T
& V4 I6 |+ m9 ^+ Y$ X! l! o try {" ^; e+ Z2 }9 k n) B4 u
modelActions.createActionTo$message9 y+ W Q& k4 ~2 X
(heat, new Selector (heat.getClass (), "stepRule", false));% I; Q8 j9 D4 Y; R
} catch (Exception e) {
" \4 k) M) h7 ^) ], l8 f System.err.println ("Exception stepRule: " + e.getMessage ());
5 c# c0 S6 {% z7 @* ~ }
! Q9 Z; r" K1 v2 b3 p! b! _1 T4 w O E% |1 R6 c
try {: O7 E' o0 ^' m, @7 m, U2 @
Heatbug proto = (Heatbug) heatbugList.get (0);/ H% U1 V3 r% N' |; H, j2 q
Selector sel = 9 g3 z+ ^+ o+ F* f# e( B* h
new Selector (proto.getClass (), "heatbugStep", false);
: _1 g% p8 f6 r2 z actionForEach =1 g% ?$ |4 H7 v& T9 S
modelActions.createFActionForEachHomogeneous$call. n4 U- d/ b' r* h Y
(heatbugList,
3 D9 J1 p1 M6 C! }# ~4 D) D new FCallImpl (this, proto, sel,
1 h! X8 Z$ `! B' o/ ~ new FArgumentsImpl (this, sel)));
1 W' N. d$ A. B: y1 k } catch (Exception e) {
: F! ~( A" H* a2 S6 G e.printStackTrace (System.err);
8 \; l2 O0 W$ `% n5 k% r }
9 S3 w& T% }4 S: k3 `# G/ @! c. A $ H- a7 R. k: i- N9 B. \8 R
syncUpdateOrder ();. x7 c5 }9 l; g6 Y/ J# I
( S5 E) o/ m; B' A: L5 c
try {
. f- D2 [: B) I8 C4 s- w modelActions.createActionTo$message
: [3 `% F, X, ]2 S9 b6 f* N% A (heat, new Selector (heat.getClass (), "updateLattice", false));
" c$ d& H7 U% R/ ~ } catch (Exception e) {" ~1 h0 B E& l1 w8 G
System.err.println("Exception updateLattice: " + e.getMessage ());( k8 P! R) c9 w
}
3 {! \ X7 |8 Y& Q' B' Y8 u1 ?7 G : k: o/ g- F& [2 y0 P
// Then we create a schedule that executes the) v. x" {8 n9 E% [
// modelActions. modelActions is an ActionGroup, by itself it- _+ F4 v# J. q- i
// has no notion of time. In order to have it executed in
6 } N) F+ ~8 X3 l // time, we create a Schedule that says to use the
! w5 a8 g# ~+ S( ]% `, ? // modelActions ActionGroup at particular times. This
4 u" q1 Y+ p/ S; G' G6 R // schedule has a repeat interval of 1, it will loop every
( y0 l6 s) i: i6 P' r // time step. The action is executed at time 0 relative to
0 B( d4 ^0 Z2 G' {5 P. X1 p, g // the beginning of the loop.
, ]4 X9 H( t) `: |7 U# _
/ m# @( Y6 ~/ Z' P! o // This is a simple schedule, with only one action that is/ ?9 G8 e& t) N0 I: \6 P/ D
// just repeated every time. See jmousetrap for more9 e3 |, J$ k5 I6 I
// complicated schedules.
- D0 d; J' l) Y5 f% ] ' [3 p" V, e6 a* G
modelSchedule = new ScheduleImpl (getZone (), 1);
0 A9 ^6 H/ R: `; u/ N* v6 [& P, u modelSchedule.at$createAction (0, modelActions);6 ?2 m1 D% K# F- N1 Q
8 m2 h2 z# f n1 l return this;
( |9 O# P( ^/ e% p' o! z# O/ |$ d } |