HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ y+ S" Y" j1 c
+ w/ b, I. I8 I( y) I% w B/ Z public Object buildActions () {, Q6 J& \* G4 f$ G5 P
super.buildActions();0 N, P+ t& v1 p, v6 D4 B
" Y n& o: [3 f // Create the list of simulation actions. We put these in+ y: m. P8 T6 D) Y
// an action group, because we want these actions to be; S) z) P0 c8 v* i& o1 B1 r
// executed in a specific order, but these steps should9 f( U8 L+ N, I2 s! R
// take no (simulated) time. The M(foo) means "The message
6 p; @6 `5 O/ J, @$ E% u // called <foo>". You can send a message To a particular, w) w* M0 f+ V8 y2 {1 h s/ e
// object, or ForEach object in a collection.
7 k7 y3 v1 s) T
( K- I9 t$ g" V6 G6 `1 W // Note we update the heatspace in two phases: first run
4 ~- G( }5 w$ U9 [$ z2 ] // diffusion, then run "updateWorld" to actually enact the, R% V8 u- G3 G- y ]7 C7 T
// changes the heatbugs have made. The ordering here is
/ O) P3 H/ ^; t: d" x- _ // significant!% r$ |/ y' D m- t
/ l- I$ l( S! z( M2 r4 U: j
// Note also, that with the additional
1 F4 U4 L% d! W2 O6 f* Q } // `randomizeHeatbugUpdateOrder' Boolean flag we can
; l3 y3 u% H% O$ U7 u7 k( ?6 q // randomize the order in which the bugs actually run
5 t5 L0 c* k3 l0 ]5 m% | // their step rule. This has the effect of removing any
; h% `' b. g( E |. w // systematic bias in the iteration throught the heatbug
9 G* _1 @6 g; k6 @3 U // list from timestep to timestep
8 H9 l9 ?, C7 i8 ]9 _2 s0 x # ? u; E2 g/ B- j. m) P- x) e
// By default, all `createActionForEach' modelActions have" f* Z4 O0 [6 Y- [2 e# e7 W
// a default order of `Sequential', which means that the% n o& _$ w0 @% j: \- ^
// order of iteration through the `heatbugList' will be# e' m% G8 ]2 g3 A" v
// identical (assuming the list order is not changed6 l- L/ L& K* f- K
// indirectly by some other process).
- Q1 K# r) A$ b/ z2 ^ & F V* `9 X* i( G8 A5 z
modelActions = new ActionGroupImpl (getZone ());0 _2 r$ T) k" M: C% W
' O& S5 G! C: ~6 e' N% Y try {
1 P7 s0 G. J/ ^& r modelActions.createActionTo$message
* V* Y- l# V* F% ~ (heat, new Selector (heat.getClass (), "stepRule", false));
' e Q* \2 H' Q8 F7 A I8 Y& q } catch (Exception e) {
0 M, u- X' g. _8 ?+ Z/ `6 @ System.err.println ("Exception stepRule: " + e.getMessage ());
4 k9 \+ [ f5 V% q: ] }
: K p: X9 J# B# D& F+ M/ T5 H
* y, j& ?( {4 `: w9 X% A3 t try {
! ?* j& i+ Y) m0 R; w: h8 c Heatbug proto = (Heatbug) heatbugList.get (0);, w4 p$ \+ k% ]
Selector sel =
/ N; s/ R1 U( z0 K' e( e new Selector (proto.getClass (), "heatbugStep", false);
; Y3 T7 e! S" l" g actionForEach =# r T& d% x4 M" |6 c# Z
modelActions.createFActionForEachHomogeneous$call
) s* c" ~8 t3 y. d$ [ (heatbugList,
# ^9 k8 m0 T8 f, }7 [, m0 ?! W4 a new FCallImpl (this, proto, sel,
, @# F2 f3 T4 c. e) W. n1 z5 h new FArgumentsImpl (this, sel)));7 F/ y" `+ e _# w4 A
} catch (Exception e) {4 _- a2 B6 ~# q- H
e.printStackTrace (System.err);. }9 u# b' H8 d5 g
}% o1 K5 ^' N! s2 A# a
) W+ _0 t2 F& c$ K% N) i syncUpdateOrder ();
% x# q( U4 s, G6 f4 ?% Z& _
% B! i0 t' S8 s Y- y try {
: A, l1 D$ F; S* f modelActions.createActionTo$message # _$ [! x' ]' _, t5 r3 n
(heat, new Selector (heat.getClass (), "updateLattice", false));" M# M" w3 _- x8 ]6 e& g5 v& R
} catch (Exception e) { x0 P5 w3 {) ^5 m9 V
System.err.println("Exception updateLattice: " + e.getMessage ());
- n/ `$ f) [5 Z' ` }) N: y. r. k" W
) b% ~4 p- t0 f, ^0 G // Then we create a schedule that executes the
" X) i, l; x* H7 c // modelActions. modelActions is an ActionGroup, by itself it
n* L% b* m! B y* r1 k // has no notion of time. In order to have it executed in* D6 D) p o3 R6 F4 C* W- \
// time, we create a Schedule that says to use the
' T7 f" @. r5 t x1 m7 N; L* w0 P& t! P // modelActions ActionGroup at particular times. This& H3 w! a. Q. v& M% r
// schedule has a repeat interval of 1, it will loop every) ^. F- b: Z" l
// time step. The action is executed at time 0 relative to0 c" F' o5 Y$ s* K4 r/ D {
// the beginning of the loop.
9 t) h/ ~3 ?8 ?$ o1 ~# y6 T
6 }, M: Z) D. d // This is a simple schedule, with only one action that is, `8 L0 ]; a' k* S
// just repeated every time. See jmousetrap for more
1 C# f# I% N' y9 h& \/ x7 R5 p // complicated schedules.
* d" ^; \/ S( c! D3 a8 m# A$ n
3 D% V* e3 G. _2 S modelSchedule = new ScheduleImpl (getZone (), 1);
# @7 {# r, r! s3 \. U9 i& c+ l modelSchedule.at$createAction (0, modelActions);
( L: d. v$ I, S; b# [% j
) r+ ?) w$ g2 @' M! v, v: c4 b: x6 K9 S return this;7 Y, H2 x p7 u2 M) |7 H
} |