HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! l" O Q+ [1 f/ M2 p- |" }! c# I$ M; H* T, F
public Object buildActions () {( Q% a: p4 {8 ^3 A' U% _3 U
super.buildActions();
6 M6 B2 z& v, }6 | o# p3 A
- A' f+ i$ Y6 p9 d // Create the list of simulation actions. We put these in
9 n O6 Z) u$ ~ // an action group, because we want these actions to be4 ?# l( A& O" k& U' \6 Y/ _, q
// executed in a specific order, but these steps should/ ^% R- y# g+ j$ B9 b3 L8 F
// take no (simulated) time. The M(foo) means "The message7 _, s3 S' {3 m' |3 M) z* A
// called <foo>". You can send a message To a particular, t$ O( H$ D0 z
// object, or ForEach object in a collection.
0 R/ z9 g# C& a. j5 G" O : k% W7 {7 \4 }- _6 R
// Note we update the heatspace in two phases: first run# B- V$ U* e2 b
// diffusion, then run "updateWorld" to actually enact the
: w5 N5 F. v5 Q3 @, U1 i // changes the heatbugs have made. The ordering here is
* N! \6 U& X( \/ z) Q* K" P3 m/ } // significant!
0 {6 Q* j2 ?/ f* e# c: G7 f G 0 B$ b, u* s& T3 a0 D' c) H
// Note also, that with the additional
& a9 F H; U! t8 t# z4 }$ Y // `randomizeHeatbugUpdateOrder' Boolean flag we can9 |8 y7 b# _ ~
// randomize the order in which the bugs actually run
" {9 \; g: u# g3 @# k% _9 h' B2 } // their step rule. This has the effect of removing any7 J& t7 g7 ?* e8 {& R2 i
// systematic bias in the iteration throught the heatbug0 u7 x" r# I, p* k) @' J
// list from timestep to timestep
4 A- _& K! l0 ^: J6 O+ T8 ^ / c! \$ N+ s+ R( k" l& W2 c
// By default, all `createActionForEach' modelActions have
7 f3 P c9 z" v2 K P4 a' V // a default order of `Sequential', which means that the
6 [* F6 f. d6 U7 ?9 O: H/ g // order of iteration through the `heatbugList' will be
4 O2 p; a; k0 b // identical (assuming the list order is not changed
: w! n" G9 U3 v // indirectly by some other process).: t# ~$ n% h. K8 ~) Y3 v7 O/ }; [
k0 Z3 v: ]' t! m; b modelActions = new ActionGroupImpl (getZone ());+ X+ F" X( I1 h& u& k
, T! O4 v) P: v% i1 M" o try {5 S3 |, j; K" Z! C, f
modelActions.createActionTo$message! _+ y+ }' [$ A; ?
(heat, new Selector (heat.getClass (), "stepRule", false));& l& U2 i$ q! ]: D/ ?. I8 z$ T
} catch (Exception e) {# a* k& f& H# ]7 f9 u6 d8 o; \) z) U
System.err.println ("Exception stepRule: " + e.getMessage ());
# |) A. B8 b6 Y }
: ` I( i5 r; _' N/ ]$ q+ X( M
# J1 i3 J" m7 O% y, V try {
8 K+ W, g3 Y& U/ a+ k b' x Heatbug proto = (Heatbug) heatbugList.get (0); x$ n* ?7 T: ?- E) v+ I
Selector sel =
/ |6 N q8 w+ c- u new Selector (proto.getClass (), "heatbugStep", false);
1 L+ B" }$ S* A# p6 D- ]5 L actionForEach =
1 `: a/ D2 E7 q- ^ j9 L0 S9 ] modelActions.createFActionForEachHomogeneous$call
1 p9 \0 k" ?6 Y1 J! q6 q9 w" z (heatbugList,' n0 I/ N& e: L, P! J0 T$ {
new FCallImpl (this, proto, sel,5 }# r8 V8 ?+ M3 [& c
new FArgumentsImpl (this, sel)));; G4 `$ u; S2 O3 T& H
} catch (Exception e) {2 G& q- i: s% x# v/ q
e.printStackTrace (System.err);* g# ]- e: a* g- r5 D' j, J- S" w
}, `# D2 t; D* L- Y% P- T. |
' b" G3 d+ ~. w- R% V" J syncUpdateOrder ();
- ~! ?3 P4 T% g& s- H9 M( }
+ D+ P, A1 C* d7 R8 p- s" ? try {
# Q/ I2 u) i- t4 I/ `. M3 e( d. M2 @ modelActions.createActionTo$message & M/ n$ M6 t- P: V; w. k! y
(heat, new Selector (heat.getClass (), "updateLattice", false));. ^2 \9 K: l$ T( P
} catch (Exception e) {/ G8 E* Q7 X/ A% F1 Y4 I; t3 k7 Q; u
System.err.println("Exception updateLattice: " + e.getMessage ());
* K8 U3 n( @ t7 I& O1 k }) S0 @7 M/ K5 z7 _
9 z+ i/ c1 q" [ ?5 s1 w
// Then we create a schedule that executes the6 V; ?. O4 G/ u: V1 ^, x, s
// modelActions. modelActions is an ActionGroup, by itself it! y3 t7 T. ~6 ~0 X
// has no notion of time. In order to have it executed in
5 x3 c' {' W' Z5 c // time, we create a Schedule that says to use the
$ Q8 Y' b0 S4 g& c7 t" u // modelActions ActionGroup at particular times. This
& N: O$ V% q6 c8 z4 Q: f- [, Y2 m // schedule has a repeat interval of 1, it will loop every [$ u/ a9 A6 k/ X
// time step. The action is executed at time 0 relative to" z9 i* E0 S s$ Q4 J: z- t
// the beginning of the loop.0 W5 T; ^3 S* l8 W; u. S
! ~! [8 C/ C0 F0 o // This is a simple schedule, with only one action that is
3 ^2 i1 f4 X" o8 `7 Y+ @ // just repeated every time. See jmousetrap for more: ]4 X8 @, a- N2 y1 m
// complicated schedules.
% T& j" v! X* |: r b* T" ]8 i' ?* z3 t
modelSchedule = new ScheduleImpl (getZone (), 1);
9 M n1 [# c& j! ?. h# ^2 D+ ? modelSchedule.at$createAction (0, modelActions);& R* d. d( f/ J0 U; [4 M- j
9 c8 }! V5 N% A, O; J2 J
return this;
+ \' k; p( U# L# r7 c } |