HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& Q3 b# F* X4 p1 p0 \ p
" K: d( ~) D0 Q7 D3 Z public Object buildActions () {
: i, K2 t* O$ t super.buildActions();7 D0 b$ y( j8 E& V
i2 L6 O2 U9 I( e' G0 Z( C
// Create the list of simulation actions. We put these in7 y: b& p- n' |5 ]2 L$ W
// an action group, because we want these actions to be; C) m9 b3 O G% y& ^' s# n, \
// executed in a specific order, but these steps should6 b) _) h" h& k! x" d" {
// take no (simulated) time. The M(foo) means "The message# w) A# H. m5 I/ a$ ?& A- `
// called <foo>". You can send a message To a particular
& d9 Y! P$ n8 g, g // object, or ForEach object in a collection.1 ?0 Q! M3 M# w% b0 J8 x
' @% M* o! W% u- K2 `2 o- H2 P
// Note we update the heatspace in two phases: first run# X1 M6 w, `9 H, t. p# b( i
// diffusion, then run "updateWorld" to actually enact the
/ d8 U+ U" {" Y' s( c // changes the heatbugs have made. The ordering here is
6 z$ W1 D$ k& S3 j! i // significant!
+ w: J W. o- Y5 J/ ?
5 x7 ~* X Q1 X7 e" {& w // Note also, that with the additional
' n8 q0 d7 e1 J& Q) Q# [ // `randomizeHeatbugUpdateOrder' Boolean flag we can% d. _# X% u( G0 i8 r
// randomize the order in which the bugs actually run
/ u" v8 l$ F/ x3 l: u // their step rule. This has the effect of removing any
+ E& Z5 |1 K( Z: W- z4 f // systematic bias in the iteration throught the heatbug
( t3 J8 d1 n; X3 [ // list from timestep to timestep
8 j- R, e0 f* {, u {$ q7 ]2 J - o# x) l! N6 m5 U3 J
// By default, all `createActionForEach' modelActions have; Z8 |( \' c( m6 X& E
// a default order of `Sequential', which means that the
' L0 _5 S2 Q& N K% p( s; a, i // order of iteration through the `heatbugList' will be) Y$ f7 n4 `: L! l' ?6 x
// identical (assuming the list order is not changed
/ G# m+ G0 Z/ p# ]* b$ x // indirectly by some other process).
~6 Z* H U' L1 d+ h6 A- \. x ( q2 Q/ q( W( V% {6 U: Z
modelActions = new ActionGroupImpl (getZone ());
, l3 }$ i/ p- w/ g$ r
, G! u$ w' |% F! f3 Y try {5 Y& X. U- d$ f6 Y5 A) J. s
modelActions.createActionTo$message* m- z* R6 u* e' ]& s+ V
(heat, new Selector (heat.getClass (), "stepRule", false));% k9 V8 Z8 Q9 n7 b
} catch (Exception e) {
0 u3 B( M/ ?5 ]1 a System.err.println ("Exception stepRule: " + e.getMessage ());' X, v; y2 h I% j: _% e+ o
}
\! P- N M% p7 C
) O5 V- Z* ]3 ` try {
* i% E, Y7 r- z( ] Heatbug proto = (Heatbug) heatbugList.get (0);
* B: v# }8 @; z5 u. d Selector sel = 0 F9 G, R) d' r6 G
new Selector (proto.getClass (), "heatbugStep", false);
, ^9 e S& d" t) Q( F$ C5 ^ actionForEach =0 E8 @0 I7 o! f
modelActions.createFActionForEachHomogeneous$call
0 v) U& [3 s9 |$ o+ g3 Q (heatbugList,
. s1 O# F" A7 L" }% B/ {" ~5 K new FCallImpl (this, proto, sel,7 @7 h8 L, Y4 b0 M
new FArgumentsImpl (this, sel)));' f/ u( D7 `6 V& Y) |
} catch (Exception e) {9 o) ?7 e' M b: C& U: U
e.printStackTrace (System.err);
& M2 }5 B% Y# J" p3 D" T }9 J* |+ S8 a) f$ w) l8 l
( j0 Y& t; m) R3 \! d% Q# w syncUpdateOrder ();
$ _; t, o, A# _" e D! @- n
3 S7 w( t; S! b. |* [$ Q try {
% l# e0 Z3 K# q' M x! J) O! d modelActions.createActionTo$message
. i1 U. p9 o) J/ y7 ~. j9 ~$ L6 b (heat, new Selector (heat.getClass (), "updateLattice", false));8 |1 a6 d( `+ o/ P* e' E
} catch (Exception e) {+ Z! |; L2 Z0 f
System.err.println("Exception updateLattice: " + e.getMessage ());1 X' l# _7 O, g3 l' C" c# t" B% Z
}6 e; I. S6 r+ B5 e0 G
, O( A1 X+ K7 B) s8 a( z5 E // Then we create a schedule that executes the% y/ Z, ^6 q" d' v+ W1 K
// modelActions. modelActions is an ActionGroup, by itself it9 {* Q% w1 \3 \$ C" W1 l3 n
// has no notion of time. In order to have it executed in$ H! {* m: ~! f& L# x$ i7 U: ?6 l& x
// time, we create a Schedule that says to use the7 f8 T; o1 \- B! T
// modelActions ActionGroup at particular times. This
: I) {# ]2 ]$ `. } // schedule has a repeat interval of 1, it will loop every: c3 o/ Z0 J. E r! b
// time step. The action is executed at time 0 relative to& u9 b( L2 U( ]
// the beginning of the loop.
7 D- X2 n( X& @0 N5 V, ^# T( h$ d; D; R# c( y
// This is a simple schedule, with only one action that is: V, Z* n: E) @, ?/ Q
// just repeated every time. See jmousetrap for more4 \ M- m/ E5 [* p# S
// complicated schedules.
$ o h$ {1 b2 }/ L% ^) h0 r+ ~( a) r ! w$ d, M# K! ?' @
modelSchedule = new ScheduleImpl (getZone (), 1);( K0 i* X/ X0 L7 a1 o$ |7 {7 M5 T
modelSchedule.at$createAction (0, modelActions);3 I' Q+ p: r, u( ^ E$ y
7 ~& i, j R7 S" D7 k2 V( c return this;7 \4 K6 x5 H1 g. U, }8 b
} |