HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! [0 g3 F8 C! w6 H3 N) C
( D, m# n6 r% c public Object buildActions () {
* S% p0 c, Z& B super.buildActions();
- b4 Q3 f( s' d2 ?# s7 D. r" |
% e8 y7 m: w+ j0 K // Create the list of simulation actions. We put these in
2 ?; w% { O5 _+ h // an action group, because we want these actions to be
0 d# b& A& N- U9 c1 r" q- X // executed in a specific order, but these steps should
7 B) u5 q6 i0 n: D0 W6 h // take no (simulated) time. The M(foo) means "The message* b9 f6 L( o' m; }
// called <foo>". You can send a message To a particular
% H+ k& z- v3 n& S; w$ k# S) m. Z // object, or ForEach object in a collection.
6 R$ l) B0 o) D
. i Y6 M( U5 ` // Note we update the heatspace in two phases: first run
7 L B$ X7 A% f$ e, g // diffusion, then run "updateWorld" to actually enact the
( Y9 q$ p- [) \, n/ v( N; D // changes the heatbugs have made. The ordering here is
( L- p! ^% C4 o; ]7 ~: e // significant!
3 l) I7 [8 @$ K- l& q. {% R5 Q
# B7 J/ h+ E2 s // Note also, that with the additional A) M; C. L9 e* H$ A
// `randomizeHeatbugUpdateOrder' Boolean flag we can, M5 N+ S* X* p6 Y! _& W0 K; Y
// randomize the order in which the bugs actually run! d! f/ ~4 M* K$ V
// their step rule. This has the effect of removing any; `6 f: P" @/ z- P0 _ v8 \; P
// systematic bias in the iteration throught the heatbug
, ~4 b$ g w$ p! K( y. \ // list from timestep to timestep
; P! _- N5 O, ?0 [3 ?
$ n' m8 ^! F, E& d // By default, all `createActionForEach' modelActions have: _0 B$ O0 O4 b- u0 P; d; d' V
// a default order of `Sequential', which means that the. W: W& l) `1 ?2 E1 l( P
// order of iteration through the `heatbugList' will be
8 H2 f, Y3 y: K // identical (assuming the list order is not changed7 {8 V! P6 V! R1 }/ a& K+ H" c
// indirectly by some other process).$ m& n- R* S( X) o
; s; a8 f1 Z% l8 A/ M7 E modelActions = new ActionGroupImpl (getZone ());
4 I* y: a; p0 c* v
/ }) y6 k* G$ F4 n9 H) W/ [ try { [, D3 M; N. j3 K# [6 n7 t
modelActions.createActionTo$message) L* p4 G* j8 _* f5 `$ v: l
(heat, new Selector (heat.getClass (), "stepRule", false));
7 T" @9 |$ P; ]2 t( v: g } catch (Exception e) {
" u2 U' Q! ?4 S. D& T, f0 v System.err.println ("Exception stepRule: " + e.getMessage ());" u& f4 {* a9 M# j
}
/ e+ E% z: r, ^9 ^) j* x, m/ F: f t8 N6 Y3 G
try {4 o0 L) V2 ]* ~) }9 a! i0 \+ s
Heatbug proto = (Heatbug) heatbugList.get (0);
1 c* p7 s i# h7 X Selector sel = % X9 l9 i) z4 n7 w2 V3 z, D
new Selector (proto.getClass (), "heatbugStep", false);
5 }) Q% g% m9 @. v actionForEach =* h! r% Y* _8 N; e& [8 k2 D
modelActions.createFActionForEachHomogeneous$call
! Q1 ?2 ]% [: A/ W! S" R (heatbugList,/ y0 b- K4 B. P% c# l$ O
new FCallImpl (this, proto, sel,' d$ x- Q1 _# ]
new FArgumentsImpl (this, sel)));
M" j) g- a- g } catch (Exception e) {
7 C$ W$ v- ^3 Q e.printStackTrace (System.err);( R! C0 K% c. z- H9 Q
}# U. ?; T: R+ a" U, d
. D' A/ d4 h3 q1 h& B4 n
syncUpdateOrder ();" [$ U" I# |# A" B9 Z: j
* W2 A/ Y$ @/ I8 p try {
3 C- j, z1 _: E modelActions.createActionTo$message F9 H7 X% i A. a* d o P4 ]
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 i) C( }+ l9 m3 F1 ]: A8 L* c } catch (Exception e) {' h: u1 Q0 p) Y3 {
System.err.println("Exception updateLattice: " + e.getMessage ());
* R: z E' t' n% ^, ] }* q* K+ ?- A/ D# d2 `. L% K
& N" U% X7 U2 m3 U. n
// Then we create a schedule that executes the
) M) Q. l& ~& e" R // modelActions. modelActions is an ActionGroup, by itself it6 g# |+ D3 _4 g+ B! y% n
// has no notion of time. In order to have it executed in
1 x ^& z# F6 m( I' S# ^- @. N- _1 p9 f // time, we create a Schedule that says to use the
, [! u2 t6 s* z+ K // modelActions ActionGroup at particular times. This
0 u" J2 [% |3 c3 j // schedule has a repeat interval of 1, it will loop every
. h( E7 E! O6 `# ]( A // time step. The action is executed at time 0 relative to9 \( {$ ?1 V+ h8 Y
// the beginning of the loop.
. Y" y$ r0 @& `8 T5 k4 i2 J! u, o; _/ J+ b D4 b
// This is a simple schedule, with only one action that is9 Q; j F9 t" L* C/ x
// just repeated every time. See jmousetrap for more' C- X# v1 k& t6 V/ J5 O0 J9 s {
// complicated schedules.
$ ^5 U% M4 n2 F" r5 g
8 n$ j; D, {+ L5 _( {: {5 h3 h modelSchedule = new ScheduleImpl (getZone (), 1);6 h2 e) w3 Q/ ^& ^- @% M/ P
modelSchedule.at$createAction (0, modelActions);$ ^, \. N4 {+ w6 P' @; X+ D+ k3 N
+ l; ~7 q/ O e2 q return this;7 U B2 P* W: b* g2 R, f
} |