HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 ?) _1 l+ t) r
$ t4 y' [' R q% h& @
public Object buildActions () {
, v/ ^( k! T0 N! X+ Q, N$ s super.buildActions();
& Q( g9 y9 E( m" w& R7 G
- P4 ]; m6 R6 u) ? // Create the list of simulation actions. We put these in7 y# a& [- }+ U! a
// an action group, because we want these actions to be
( ~% w5 h$ I; I+ R* X7 k // executed in a specific order, but these steps should
* _& i( M- Q# C9 k& i // take no (simulated) time. The M(foo) means "The message0 h% r, }9 O; P2 d& C
// called <foo>". You can send a message To a particular
+ ^: Y# z1 B$ ~* X6 e1 i // object, or ForEach object in a collection.
1 T( \. Q, h/ \/ N' F
3 B5 o% `: Z( K, R+ M: b // Note we update the heatspace in two phases: first run3 R& D. ~7 z0 Z; F5 c* _ Y
// diffusion, then run "updateWorld" to actually enact the a) F2 b. S! n$ E# c' K
// changes the heatbugs have made. The ordering here is
4 W+ B3 o' ?! O M. w; Y, r" @5 y: w // significant!7 J \/ r' X% M$ L) O
- [6 U! w" f! j9 U: y- W9 k
// Note also, that with the additional( M+ _0 L1 H! [, D9 K0 r, L4 g
// `randomizeHeatbugUpdateOrder' Boolean flag we can c" m& P# x, G, L
// randomize the order in which the bugs actually run
! S0 U5 ~7 ]/ \2 e! F' L* v) { // their step rule. This has the effect of removing any
4 i; G: o1 n8 O* Q ~7 G) p // systematic bias in the iteration throught the heatbug& }: m* ?' A: }: {2 Z- S
// list from timestep to timestep
$ {. K" w, y; E 8 f+ l8 P. Z4 ]4 Z1 s! K+ ]" P
// By default, all `createActionForEach' modelActions have5 Y3 R1 f' z& C+ I4 m6 G3 P' M
// a default order of `Sequential', which means that the+ `) v5 ?: f' D3 M A
// order of iteration through the `heatbugList' will be! E' H, g# u1 A+ k# X3 U
// identical (assuming the list order is not changed/ C3 \) c9 j# }) n' u% E. Z5 `, K
// indirectly by some other process).
: p; A3 d b: A$ B. h& { 6 h, E! c# z% V! \
modelActions = new ActionGroupImpl (getZone ()); A7 S* k6 i+ `: [' `% g
/ Y- Y6 ~2 |2 X1 v0 Z$ F try {
7 f0 M& y/ U, a modelActions.createActionTo$message
4 }+ j+ o2 p3 k5 @ (heat, new Selector (heat.getClass (), "stepRule", false));( v2 _" v! A& z6 V1 R
} catch (Exception e) {
" b5 U* p& k3 ?( M0 J System.err.println ("Exception stepRule: " + e.getMessage ()); \0 k, I0 z8 U6 U+ U2 X4 l6 J
}
' Y5 e8 t6 S6 v6 X6 f
: w) f4 y5 W2 A4 j8 Y F; b5 v try {' B4 k7 z5 {& C" b
Heatbug proto = (Heatbug) heatbugList.get (0);2 a, b: ?8 J9 Y1 r) u% X
Selector sel = * Q5 g- b. N$ I0 v
new Selector (proto.getClass (), "heatbugStep", false);- ]" o- @, e1 A7 y6 x& H
actionForEach =
. [9 K' k7 }1 O+ }* G. X modelActions.createFActionForEachHomogeneous$call
, G9 e6 k/ \$ c! k* m9 _ (heatbugList,% @7 S5 q9 q% @6 D. w5 K1 v: G
new FCallImpl (this, proto, sel,2 B) I+ k" S! K Q; F
new FArgumentsImpl (this, sel)));
. B+ N1 I6 P9 ^+ ~" O" w* m7 _2 I' h2 t } catch (Exception e) {: _7 O/ e* m d0 ]. B5 f5 u. l$ B7 k
e.printStackTrace (System.err);4 g; t: z1 f6 i0 y! P6 b5 f! h8 g
}
2 f, J( `5 z2 T 1 V' f) Y5 k6 c9 w7 G9 A3 B
syncUpdateOrder ();0 ] X4 i+ z' O/ o
9 I$ U* U1 y3 K try {
" Z! @4 J0 ?: z- C0 P modelActions.createActionTo$message
- T+ g9 ~& c& e, z0 l5 I; l* n (heat, new Selector (heat.getClass (), "updateLattice", false));
, I3 ?3 Z9 S$ J3 U1 b% J, f+ w } catch (Exception e) {, Q! q! \# N, E3 R& W9 t
System.err.println("Exception updateLattice: " + e.getMessage ());
* R8 b' O) u( ~5 V- H6 {4 Y4 H }
0 E& ?/ j9 m- G$ {8 Z; | ; ]$ @( P; b0 T$ e9 D* K) T
// Then we create a schedule that executes the! Q: j0 X) d2 P
// modelActions. modelActions is an ActionGroup, by itself it& r# p8 ^! Z$ B
// has no notion of time. In order to have it executed in/ e! U# [$ }* i0 s: i: h1 n" g6 j
// time, we create a Schedule that says to use the4 Z0 S0 b2 D) r
// modelActions ActionGroup at particular times. This
% i* @1 h' u+ R$ u) E5 ^3 t // schedule has a repeat interval of 1, it will loop every) |, P/ F! M4 n* [
// time step. The action is executed at time 0 relative to
- t: w# r3 k+ V& Y // the beginning of the loop.
2 ~# n* Y3 @3 {8 L/ D% z) A, Z' W% D& J8 t% a( b& ~* N. a- L8 U
// This is a simple schedule, with only one action that is5 Z! X* a5 t+ L
// just repeated every time. See jmousetrap for more
; ] G, Y0 G) X' j* D8 n2 s // complicated schedules.+ w Q {+ ]- ?1 o5 u
; g* N H, t' B9 A6 _4 q4 L2 b/ {) u
modelSchedule = new ScheduleImpl (getZone (), 1);
( Q- L; x6 y4 l- A; [ modelSchedule.at$createAction (0, modelActions);
8 G, C6 ^& y* H 1 B" i1 R$ d2 c" b4 }9 r# [
return this;' {/ R1 J: @; \: L
} |