HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 C$ b# [( r1 w; o
) h8 N2 b% n5 M public Object buildActions () {
$ c, u# p, e2 s( s2 |% V$ } super.buildActions();- `$ o% o; m3 O+ p$ D1 F
& v0 W" {3 `& M5 y* ^
// Create the list of simulation actions. We put these in# Y; n5 F0 D7 Q/ g; O2 Z
// an action group, because we want these actions to be* i' m* G! A! ] T
// executed in a specific order, but these steps should
0 K; Y! e/ [* T- {& r+ X: E6 J6 J // take no (simulated) time. The M(foo) means "The message+ h W+ a1 e9 N( ?; J
// called <foo>". You can send a message To a particular) D) L- c6 e* G; }4 Q y- Y0 w) l
// object, or ForEach object in a collection.9 Z) W* A. b7 ]" j
& J: z2 p, y5 H5 c, e
// Note we update the heatspace in two phases: first run
) K* V# b1 g; L1 K9 O3 _- M: I% f // diffusion, then run "updateWorld" to actually enact the
}* U& c/ X5 T- M; M/ n, ?! ] // changes the heatbugs have made. The ordering here is% U1 l/ C1 D: P9 R
// significant!2 L! D5 {" C2 ?, v9 o- V* Z
& z. o* z' ]2 V& r // Note also, that with the additional: T7 N8 w% y- K) P; m/ x' |
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; l( \% ?8 _" s/ l& x$ C A) d // randomize the order in which the bugs actually run
' o' g; Q$ o' g* q, D' I" x' @ // their step rule. This has the effect of removing any2 {# v" C* _* q+ t5 s
// systematic bias in the iteration throught the heatbug" y3 f2 r2 |" z u( c a% q2 r
// list from timestep to timestep( W6 g- r* R- y
0 ^- V% q# d$ r7 \ // By default, all `createActionForEach' modelActions have" q G- e# S& g* D
// a default order of `Sequential', which means that the* z& ^4 \! e( e# O% T* E' ~
// order of iteration through the `heatbugList' will be
8 Z1 q2 C# l7 Z; _+ H9 j; m // identical (assuming the list order is not changed( x( x9 {+ h( t" B
// indirectly by some other process).: K- j) u: Y3 l: j
T. A. H9 \/ p& C+ @" Y. D
modelActions = new ActionGroupImpl (getZone ());
7 a" e2 A) h( f1 Q+ y. s. T6 _" q" Q. M' Y3 I% o! P
try {# N( t' {6 Y6 B
modelActions.createActionTo$message$ A, ]) d' Q/ W$ W* m9 Z
(heat, new Selector (heat.getClass (), "stepRule", false));
0 F! ?6 }% `: V% D" s" L9 i. \ } catch (Exception e) {
+ j. U' R/ T6 s' b' y$ j! }: o1 Q: [ System.err.println ("Exception stepRule: " + e.getMessage ());
2 ^8 I, E& y, { }0 o( n# t1 \7 ?5 G/ r$ R2 c
. ?4 g7 Q( |9 ?" j! z: d try {
+ y! k# q2 l! v3 C# Z: [ Heatbug proto = (Heatbug) heatbugList.get (0);
# O0 g3 t% X& x7 e Selector sel =
- w$ k, v# I: d$ D0 _ new Selector (proto.getClass (), "heatbugStep", false);
6 D6 C& m7 }& o- Y actionForEach =1 h$ O% m* \. Q+ x+ M6 h6 l0 T
modelActions.createFActionForEachHomogeneous$call+ h- i5 h7 Q) W- d
(heatbugList,
5 {& `6 ~, n) ^2 i new FCallImpl (this, proto, sel,8 r1 H8 y) ]; S+ h s* r
new FArgumentsImpl (this, sel)));
0 c* T0 N1 w0 P( l" Z } catch (Exception e) {) q- X9 U; k% z' a/ ?) y
e.printStackTrace (System.err);
( g1 P- V: w' P ? }% P6 \9 E2 T6 @
5 y! d6 T( L: X6 ~$ E6 ^2 u
syncUpdateOrder ();
1 f4 c/ g. _& z5 J# o$ @
* N4 A" {0 M. j; @6 \ try {
/ C: _( R/ d5 p: Q3 C modelActions.createActionTo$message
1 r/ N$ B% D! G3 K: w$ H' r (heat, new Selector (heat.getClass (), "updateLattice", false));
! r, P1 s& u4 f8 ^ } catch (Exception e) {1 `5 R5 W$ h" E4 W. Y6 j
System.err.println("Exception updateLattice: " + e.getMessage ());
2 q' C q# F7 M2 R! b }
; `- T+ b! a8 G! H ' r; @3 Q7 }8 ^8 {$ y
// Then we create a schedule that executes the& [4 c" U. N) m) w* |6 M# k7 m
// modelActions. modelActions is an ActionGroup, by itself it
, h9 F7 _, m( \% I // has no notion of time. In order to have it executed in- E# [: q) I2 n" j* R
// time, we create a Schedule that says to use the1 R% J; o7 f: u4 w4 v4 n# T
// modelActions ActionGroup at particular times. This' f0 _' m' U9 W d* q# [5 l6 D) Y
// schedule has a repeat interval of 1, it will loop every# @* U+ ~; X: s; U) s' j$ C& Q
// time step. The action is executed at time 0 relative to% f9 |! t( }9 b6 a$ T6 P( B
// the beginning of the loop.$ i; z( T2 m1 u7 H
0 h6 L+ S- V4 |! |$ y4 N
// This is a simple schedule, with only one action that is3 N! z$ _5 Y, A& D$ Z
// just repeated every time. See jmousetrap for more
$ T; Y" \' Y0 u: \ // complicated schedules.& ^5 M3 ?* j* ~! U/ G) G8 y
% H1 @: Z7 H) s9 k6 u7 v
modelSchedule = new ScheduleImpl (getZone (), 1);- ?* ~5 A2 q: u/ y- |
modelSchedule.at$createAction (0, modelActions);
8 K# Y! | j7 {9 d" G; g - _# \" o: y7 `
return this;
+ f6 x9 t& b" y } |