HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. X/ B, P/ D P: u9 `. g* S* O+ A0 l0 a, m
public Object buildActions () {
; `; E9 o2 L6 Z3 K super.buildActions();& U, q1 a U# U% t$ L1 g: |1 ]; q
- H- F7 I* Y; \4 z! z T7 N- E // Create the list of simulation actions. We put these in
, F# E. A. R# j" A" U% N3 a // an action group, because we want these actions to be J4 ]8 ^; |7 l& f) {% B
// executed in a specific order, but these steps should
* Q6 Q/ K) [# N // take no (simulated) time. The M(foo) means "The message
* e2 h+ ~( E+ J# E Y // called <foo>". You can send a message To a particular, {4 W7 | f* d
// object, or ForEach object in a collection.& C$ D4 g& S8 l% t! a: {5 b- Z
, p/ O# n% X8 J9 L- H // Note we update the heatspace in two phases: first run
$ R' y) G6 z- X, b4 K# y // diffusion, then run "updateWorld" to actually enact the- w, _6 l1 e3 X- a% u- d' x
// changes the heatbugs have made. The ordering here is
% N; Z/ Z; b: m8 F; A% K // significant!, ?$ N1 v% w7 e% |! i5 o
% d4 G! {8 l! p0 ^; L) p# n // Note also, that with the additional
. u8 f. F+ B) W9 b1 P2 A; Y. m // `randomizeHeatbugUpdateOrder' Boolean flag we can
- e) q2 J/ \; E // randomize the order in which the bugs actually run
p0 f0 X6 Q# g' }7 e% p+ ~, v // their step rule. This has the effect of removing any4 v* _$ ~- q, c" `! _! C6 ` J
// systematic bias in the iteration throught the heatbug6 w, P! K7 ~' E
// list from timestep to timestep4 x8 {8 ]* V& {9 U* H
2 k0 S# C1 N0 P, K9 `
// By default, all `createActionForEach' modelActions have8 W1 w! x# g& y3 c- b* V
// a default order of `Sequential', which means that the
' c' H$ c' p J" t) \ // order of iteration through the `heatbugList' will be' G5 e4 y- O# H6 u" `6 M
// identical (assuming the list order is not changed
# K, J4 e' j( q // indirectly by some other process).$ l% x6 A/ V p7 T, e; I
1 c0 b' Z, \ w, U
modelActions = new ActionGroupImpl (getZone ());6 k: W8 W6 V0 }
9 O* q& z4 b: t7 |; z6 r
try {
4 O; ^5 I# \8 s6 E7 A modelActions.createActionTo$message
& ^; L. k- ~# }8 L. ^+ ] (heat, new Selector (heat.getClass (), "stepRule", false));* x i9 J$ B ]0 Y7 V$ y6 L
} catch (Exception e) {8 q6 b) }, m5 K& x: h
System.err.println ("Exception stepRule: " + e.getMessage ());5 F& k6 U' ^' F
}
3 h3 m# T/ ~ N0 ?5 U; u/ U; W, ~ M- m& p6 n% T* x5 S1 S4 H/ d
try {3 V; v# f! n. S* c
Heatbug proto = (Heatbug) heatbugList.get (0);; a0 X% n- N/ n% B1 H/ N7 W, S
Selector sel = + ]4 z8 E2 \8 M/ p, ~' g+ A
new Selector (proto.getClass (), "heatbugStep", false);
: r9 N1 C2 E" x actionForEach =
/ g4 Z1 t$ c. E% @. M1 h6 w modelActions.createFActionForEachHomogeneous$call. V% c- `5 v8 i6 m, d$ J
(heatbugList,
2 W9 e4 O8 k: g1 Z new FCallImpl (this, proto, sel,# U' q' {# l1 b" |
new FArgumentsImpl (this, sel)));
5 ?+ X! c3 U% i C0 m8 { } catch (Exception e) {! ?$ I# B6 z+ m& E0 J% s% O
e.printStackTrace (System.err);
" W& u+ f- |; S9 v2 {+ R }6 l% D( g/ ^5 d( S& S$ L h3 _6 k
# J. B8 I a& h1 J' w syncUpdateOrder ();5 p6 Z2 D7 l, n8 p/ ]4 h
; V I$ v4 \3 R b try {7 L- c# r, X3 D& U
modelActions.createActionTo$message . @/ ? k1 Q0 M8 y: h0 d
(heat, new Selector (heat.getClass (), "updateLattice", false));: J" e$ N7 J. j8 w
} catch (Exception e) {* c# _! U" t7 e" A! i
System.err.println("Exception updateLattice: " + e.getMessage ());3 K9 T8 L b: Y. i
}, N3 I [1 s& t, X
7 A$ G& R# D( I1 Q- \
// Then we create a schedule that executes the4 O$ i; U; P7 o! F
// modelActions. modelActions is an ActionGroup, by itself it8 [$ `, s( F1 z4 k" l: L w
// has no notion of time. In order to have it executed in( ?9 i) i( @' o( ?9 h0 k
// time, we create a Schedule that says to use the
- d7 b/ l" o2 h4 j3 h // modelActions ActionGroup at particular times. This
! I* c# ?9 [" ]3 G$ B/ a( p // schedule has a repeat interval of 1, it will loop every; M4 W. C5 w; k2 B- ^& v4 j+ z
// time step. The action is executed at time 0 relative to1 G5 W7 H2 Y2 X
// the beginning of the loop.4 E; z- U9 a# v4 B
/ `6 D: X' E: X' u1 K' U // This is a simple schedule, with only one action that is( l: K: V- B% t! @( R. a. A
// just repeated every time. See jmousetrap for more
( f. N+ ~7 [6 L; u! Y& i3 u, j // complicated schedules.
u% \. |! z# C9 }7 ?- `& M
}% M7 ]3 K& h1 c7 e# E- ?7 } modelSchedule = new ScheduleImpl (getZone (), 1);0 `- f0 l7 U; E0 ~2 ^: A
modelSchedule.at$createAction (0, modelActions);
2 r4 c) o O+ k5 v0 [' L2 ~/ A- y- ~/ G
' |; `8 `2 m, b- o, n! _9 W return this;
' _5 I% T+ B, B* ?9 p+ a: a } |