HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 `8 a) h o5 p, t+ `3 c; ]0 e& W: O
public Object buildActions () {: {8 ~' k3 u" @- Z3 t" D
super.buildActions();" v, }# e t( h1 c2 _3 q- E
- o; {* }/ b s! u
// Create the list of simulation actions. We put these in- y5 c* R0 Y; v. v$ O! J2 u) M
// an action group, because we want these actions to be5 Z+ [/ B' H* e X' L* Z) x- _$ n
// executed in a specific order, but these steps should8 D8 p2 M2 R4 |0 {& O8 @0 A
// take no (simulated) time. The M(foo) means "The message
" p' R5 n; S' T2 K* x/ n9 E9 s // called <foo>". You can send a message To a particular
1 p n0 x4 C8 E* o* { { // object, or ForEach object in a collection.
- y* E% O+ ~* b% `# M
. C' p v3 F9 L D; e // Note we update the heatspace in two phases: first run
+ h$ i* [. h) o5 v. l2 V ? g2 Q // diffusion, then run "updateWorld" to actually enact the
% R2 ?1 R- I: b0 H' V // changes the heatbugs have made. The ordering here is
- z& K, F0 {7 ?+ j, l& V# F( c* T // significant!
/ o/ f# a- t# ?" \1 \, ]9 U
X) H4 w" f( z: p1 A // Note also, that with the additional& P6 o! `- U& F. l A: H
// `randomizeHeatbugUpdateOrder' Boolean flag we can, ?& ^" ^9 K. e7 ?/ S
// randomize the order in which the bugs actually run9 Z5 B7 J) @, }% S! T" ?
// their step rule. This has the effect of removing any% m# v6 f6 m" X, N) F
// systematic bias in the iteration throught the heatbug; c3 y% U2 K0 {0 ~' f
// list from timestep to timestep( X5 I0 A; K& i, `6 W. \
4 ^" v @8 d' n* z0 Q1 d // By default, all `createActionForEach' modelActions have
) q; @7 I% u* G3 V! `% s3 J1 A // a default order of `Sequential', which means that the
; M: }$ J7 }3 [ // order of iteration through the `heatbugList' will be
! k" c3 {: u/ |! s // identical (assuming the list order is not changed
8 ~6 ^; n W" Q8 B // indirectly by some other process).
% `0 R6 I( O5 O9 J/ k ~
3 W- |3 n ?4 n6 G) i3 u modelActions = new ActionGroupImpl (getZone ());
5 o8 T) k; B# B9 Q- y6 K/ A: J9 p' C' l5 e1 l1 k# o1 i
try {2 t! ~0 Z. b5 |+ I
modelActions.createActionTo$message
% Y+ G# h- P2 j* b (heat, new Selector (heat.getClass (), "stepRule", false));
0 e2 t$ F8 O; x; D } catch (Exception e) {
: ?* o1 U3 Z3 X System.err.println ("Exception stepRule: " + e.getMessage ());( ]2 F$ ?% r; `$ \
}* R6 k: x3 s% ^& Z P" w
; p) y$ p F5 j% e. \5 B* H `
try {
: _ ]; i: b: W* u1 F% e Heatbug proto = (Heatbug) heatbugList.get (0);+ u/ a/ H/ }/ s
Selector sel =
* J9 z: @! A5 J: q; ], K: ` new Selector (proto.getClass (), "heatbugStep", false);
E6 L2 Y3 E3 l! p$ Q: e) ` actionForEach =
! P! I0 O1 N7 E! o# f* l0 t modelActions.createFActionForEachHomogeneous$call
: y( j( q H1 u& j" ~ (heatbugList,
6 f K0 C! C$ U9 j# C+ N g! x. n9 j new FCallImpl (this, proto, sel,1 T+ T% z" ^ | O7 v5 [
new FArgumentsImpl (this, sel)));
! i! |% s$ k- D5 o' L7 |9 r7 J8 S } catch (Exception e) {
% ^! Q! F3 a3 A e.printStackTrace (System.err);
0 d1 f6 P4 v) _7 u0 a4 p& l }$ F# y. H3 A; A
3 J B! l# p! I1 I$ e: D7 A
syncUpdateOrder ();) q. w$ J4 T. S: K5 R
: e' H0 u% z% O. |4 r5 z try {
2 A& r0 r# U9 u$ G. ~ modelActions.createActionTo$message
8 C1 {$ t0 B x) v/ U5 m! U (heat, new Selector (heat.getClass (), "updateLattice", false));
! n% G, b' k! ?2 _1 C } catch (Exception e) {
+ r" i- q( \6 ^3 }2 i System.err.println("Exception updateLattice: " + e.getMessage ());3 e6 ?9 h6 O8 M+ m3 Q( w2 U) W5 b6 E' r; A
}
- X8 v, G. u' x8 M% `: Q
( M: l/ s# Q/ }+ m // Then we create a schedule that executes the+ M2 x$ y2 X/ ]- m. \
// modelActions. modelActions is an ActionGroup, by itself it
, N% S: R8 @4 y. _; U, e5 b/ ` // has no notion of time. In order to have it executed in
- K$ J! N0 J" g. M // time, we create a Schedule that says to use the5 Z7 N' G3 d; S& j5 I
// modelActions ActionGroup at particular times. This% o6 T$ r( y5 k" O/ Q& M/ }
// schedule has a repeat interval of 1, it will loop every3 _2 e/ d9 E& ?) p6 N0 d
// time step. The action is executed at time 0 relative to3 o+ p. P0 {, _ j/ S5 E$ v0 @8 U
// the beginning of the loop.
* k5 t8 _3 f7 ]+ u0 D2 m) C7 `3 z6 M: _: m5 u# a
// This is a simple schedule, with only one action that is) H, ~6 U* f: q* x4 c: E" y8 L* B
// just repeated every time. See jmousetrap for more
/ b! R7 P# ~. _) M7 k // complicated schedules.
: v4 i3 Q0 f7 t% X* p% H# Y7 P6 ^ 3 y6 {0 Y4 v& g* J
modelSchedule = new ScheduleImpl (getZone (), 1);# X0 l0 l7 M" x* u7 W
modelSchedule.at$createAction (0, modelActions);$ }7 } {, E. b, M. U
( i; T; n' Y1 M" O7 H# O8 Z; l return this;' u* q0 L) g! F) |$ {
} |