HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) x) O( q m. l! I2 d5 e- ?# b
* W! y+ o6 J/ n+ m
public Object buildActions () {
% O/ o( b+ F# L9 h0 h+ Q" w7 n' Y super.buildActions();, e' D% t4 y9 p. ?3 @# `3 L- L4 G
; F. d4 U/ S% u/ I3 d( E // Create the list of simulation actions. We put these in
; R/ K+ A' k% a( K- G8 z // an action group, because we want these actions to be
5 \0 H" x% R$ K( A' O // executed in a specific order, but these steps should
: f. Y' P( A& F3 M! w9 \1 Q // take no (simulated) time. The M(foo) means "The message, ^# V V% a9 U: x
// called <foo>". You can send a message To a particular( V) o1 `4 u! [- D# e
// object, or ForEach object in a collection.0 E% u/ t9 j0 g$ h, ^5 \" z! v% D
, s e4 l# h4 {: a // Note we update the heatspace in two phases: first run/ g8 x Y) k v' z
// diffusion, then run "updateWorld" to actually enact the
5 Z4 S8 k3 v, [2 [$ g3 c9 d( { // changes the heatbugs have made. The ordering here is5 d7 E) N4 U p0 T( v' Q
// significant!
! u7 a" ], @1 A
$ R* }0 [+ ^" ?7 \+ g // Note also, that with the additional
% B0 z' [ q/ Y3 k0 N // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 y) X; |' Q, E% s3 M# S, @ // randomize the order in which the bugs actually run
. q7 P+ U T1 p! s) V; V: i // their step rule. This has the effect of removing any( q9 @4 O2 H; o1 @9 `+ _9 |
// systematic bias in the iteration throught the heatbug
2 h9 l# A, n- C2 P- I; B // list from timestep to timestep
i3 o) P e9 y 6 b# H5 n" [! W+ V1 Q) V+ Z0 {. @
// By default, all `createActionForEach' modelActions have
0 b8 _4 x! ~8 [* H9 t // a default order of `Sequential', which means that the: p% {6 N/ D) _$ z
// order of iteration through the `heatbugList' will be
9 |# T% a" x1 c+ ?0 n8 |1 n // identical (assuming the list order is not changed
6 B5 v, B) M4 {* S. e$ N) M // indirectly by some other process).0 T( j, u7 t8 _2 V; k6 N. d
9 ]( t1 f7 z2 l+ k1 l9 {
modelActions = new ActionGroupImpl (getZone ());* f) l( z. y4 k* N+ X/ {3 L
% j+ G0 F. d5 Q7 C3 E
try {
{) I* K& I, ~4 `2 B/ a modelActions.createActionTo$message) o8 j" a! `- b# v% ~
(heat, new Selector (heat.getClass (), "stepRule", false));
, [4 U6 _2 q4 h9 z3 { } catch (Exception e) {
7 v" ]% d$ g) g4 Q System.err.println ("Exception stepRule: " + e.getMessage ());
4 I4 |. f6 l! R+ [+ g2 E }: V, U# B- v+ O8 J. D
) w ^' T& \1 y4 m6 V9 Z
try {
. F+ B6 {( J9 t Heatbug proto = (Heatbug) heatbugList.get (0);; I" S; B/ {+ W
Selector sel = * @( i9 m( ]( A' f+ }$ ^! X
new Selector (proto.getClass (), "heatbugStep", false);
5 b: O b* W$ k9 j+ s) ` actionForEach =
1 G5 V' c/ z/ L" V- D modelActions.createFActionForEachHomogeneous$call
+ u2 J8 }: _6 ?; ^ (heatbugList,
1 J) q% p: U8 F new FCallImpl (this, proto, sel,
. W# i/ m3 N5 K. v0 @ new FArgumentsImpl (this, sel)));
) v8 a2 H! k% @7 S } catch (Exception e) { N0 D9 p7 S* l* j
e.printStackTrace (System.err);
/ M9 q8 l% q& ^! p: l' P }
8 \: i/ w% @+ k1 m [% ^; H+ Y
1 e$ Y! ~5 Q: j1 Q5 m& a* X syncUpdateOrder ();
! ^8 `4 i& w! X0 ~/ k% i, J1 x/ q/ ~ K4 I% W. J: b3 J
try {$ w3 t* i" r X) k
modelActions.createActionTo$message 1 k8 Z- u! W1 ^$ M
(heat, new Selector (heat.getClass (), "updateLattice", false));
# a" M* n/ u# w0 n' C& j# v } catch (Exception e) {
% c4 e. r5 c/ z1 b+ h System.err.println("Exception updateLattice: " + e.getMessage ());) i4 A* h- X# D; E) S: o
}" ~7 `( g3 s; I- s! E
7 U% f1 l" g5 g( e0 c
// Then we create a schedule that executes the
$ ]* H5 X1 S3 ]9 E+ H9 L' e3 L // modelActions. modelActions is an ActionGroup, by itself it
( i1 ^+ f% y7 t: S7 ^, x // has no notion of time. In order to have it executed in- i( k! E9 l( l. K
// time, we create a Schedule that says to use the9 k5 }& v0 \6 I8 Y
// modelActions ActionGroup at particular times. This: _, J0 y* H2 y5 I% S
// schedule has a repeat interval of 1, it will loop every
: `+ m6 o* R! k* i+ h // time step. The action is executed at time 0 relative to; O, [8 g! U3 Y$ c& |
// the beginning of the loop.
7 i) x1 Z% k: r! q! V! O+ E7 }
# [ M% R6 F% H5 V3 B // This is a simple schedule, with only one action that is6 }' o: ^6 Z- Y {6 O' {/ i
// just repeated every time. See jmousetrap for more+ n" U' s6 p. k6 Z2 m4 r
// complicated schedules.7 e3 \6 p- n$ o8 c
/ q8 K; p3 ^! s: Z2 I modelSchedule = new ScheduleImpl (getZone (), 1);
6 H9 X+ ~+ o& `9 j, b2 k modelSchedule.at$createAction (0, modelActions);
?$ Z: U K- Z, y9 u! i , r" ~5 J& B( \. p# P" K/ J
return this;
' a9 T" x0 g4 a* L } |