HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- [" Q8 O0 X$ p7 Z+ N" R: G2 i. K$ }' D$ H: t
public Object buildActions () {" x1 ^8 S6 z, l9 P' J) @
super.buildActions();8 }, h) U% U9 q- ?
4 m% g8 Z1 `4 q5 q- D# b! R2 Y
// Create the list of simulation actions. We put these in
. [! i5 K' G. M' R3 A- e5 I$ R // an action group, because we want these actions to be& U0 {. \6 O8 q5 v: {- C; G
// executed in a specific order, but these steps should% c1 W8 K- t0 B
// take no (simulated) time. The M(foo) means "The message, g. i1 B( H/ |: Y
// called <foo>". You can send a message To a particular
& T4 l; R5 |6 A, J, W( @" ^ // object, or ForEach object in a collection.# R% {- l& i' @2 s
/ n+ }9 c8 \3 t" n
// Note we update the heatspace in two phases: first run
8 f& C: q0 g; c W7 F4 v. q# E // diffusion, then run "updateWorld" to actually enact the
, }5 G' |# U5 Z- Y% A // changes the heatbugs have made. The ordering here is: U' v7 ]% B3 B; S! }" V& j! h
// significant!
0 C* ]- Y ?" I7 J/ z9 G7 _ ) o# a4 a# ^# o. J0 b
// Note also, that with the additional
8 I/ b4 p7 H. b6 n // `randomizeHeatbugUpdateOrder' Boolean flag we can2 [! [$ Z7 Z) d1 ]# Q
// randomize the order in which the bugs actually run
" f- X) [: E& X: U // their step rule. This has the effect of removing any
' \$ k0 o, `' l5 [ // systematic bias in the iteration throught the heatbug s2 M' I% E. o0 S- W
// list from timestep to timestep8 j: R, }: i) D7 i7 h, F, p) q2 C
: H( J4 J! `. `0 b
// By default, all `createActionForEach' modelActions have
. e& `6 ] Y9 V K7 j/ q // a default order of `Sequential', which means that the
: p0 h* A: c& B3 g. u0 A1 _ // order of iteration through the `heatbugList' will be) Y4 a7 |( P* |
// identical (assuming the list order is not changed
' I2 L3 C6 o8 d a4 y' d- f // indirectly by some other process)." Q7 B) }1 K7 M7 _
7 S3 f1 a! ^5 Q4 E2 D7 [' S5 w
modelActions = new ActionGroupImpl (getZone ());7 V1 ?" s5 J9 {" m+ S' p4 a
5 d' } X% v# `
try {
5 D; \4 U7 O* @- }" d; J$ A, O modelActions.createActionTo$message
+ c; t7 {: A. n! s( u* q" g* M/ k (heat, new Selector (heat.getClass (), "stepRule", false));& t& R, i& x3 m" M
} catch (Exception e) {7 O5 ?8 \/ T4 A# a
System.err.println ("Exception stepRule: " + e.getMessage ());
h$ {+ T# [: M& r3 n }9 |9 f0 ^! w8 b7 G! A7 m. D2 [
- e9 e! y$ b1 n' d+ z try {
4 o7 _" k* F$ y2 l' `& q! _ Heatbug proto = (Heatbug) heatbugList.get (0);% Q( E# o* W- X& l5 b7 u( H/ m
Selector sel = 2 l# [( P4 r, b6 Y4 e, G7 K
new Selector (proto.getClass (), "heatbugStep", false);" i1 x7 `$ I; K2 h, \- [3 D/ {
actionForEach =
* @; X7 x- \( A4 g5 x1 a modelActions.createFActionForEachHomogeneous$call5 G- u* Q6 A# l5 u+ V! d- C! @
(heatbugList,3 K" |& V( U- z }, D* f
new FCallImpl (this, proto, sel,
, k- G9 V1 u) k new FArgumentsImpl (this, sel)));3 h) x8 G6 f3 z; r9 g7 \ L# j$ @
} catch (Exception e) {
5 k0 j6 _! P3 K( K3 K7 _. F# h e.printStackTrace (System.err);8 i2 P/ j P7 c( G% i0 i: I5 `7 r
}/ l d$ `7 k: N% o* w, t4 [
* i) {5 P* k* q* e, _ syncUpdateOrder ();; b! F! Q7 G7 k* o$ K# v) Z
+ O6 m& `% I( b1 A
try {
* x1 ?% y, p# X3 C. B. n modelActions.createActionTo$message
8 x; N* O! X3 z& G2 G7 T (heat, new Selector (heat.getClass (), "updateLattice", false));
/ Y# L% k" G; R: l/ J% L. ~ } catch (Exception e) {
5 a8 @& Q( v* l" a% S9 m$ a System.err.println("Exception updateLattice: " + e.getMessage ());+ n4 W4 J( w* [) P
}
% m3 {+ ]* \2 @ y* f2 t 6 s h) X% w- x. Q! s1 {
// Then we create a schedule that executes the' V! }& E2 C* C9 o: Z2 m
// modelActions. modelActions is an ActionGroup, by itself it& g* S- I9 d4 m" u
// has no notion of time. In order to have it executed in
# w! T# J% p$ q! I; k( k // time, we create a Schedule that says to use the" F5 M- T! q G# {+ n
// modelActions ActionGroup at particular times. This
$ ~) f# j' I+ P# ?3 t, e // schedule has a repeat interval of 1, it will loop every! C" N [0 F0 Z0 c8 `
// time step. The action is executed at time 0 relative to& j: C9 E" o* k3 n! c
// the beginning of the loop.6 [; @' P2 w* e! Q9 N) C8 N" c
5 r% n" u, r/ D: P // This is a simple schedule, with only one action that is% d1 \) x$ _; p5 T& A) S
// just repeated every time. See jmousetrap for more
' [: `* d7 W, \ ` // complicated schedules.1 u6 j% R3 e' |6 n/ ?6 Y$ s
/ ]* U$ `. }4 i5 q2 x$ Z$ B. l
modelSchedule = new ScheduleImpl (getZone (), 1);9 l# _; `' {+ c% P& g4 ]: ?
modelSchedule.at$createAction (0, modelActions);
, T& {& Y" l/ e8 J$ m3 b
- i2 P. K3 G0 `$ b$ ? return this;
$ [( @2 F' ~) G7 v } |