HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% c8 u B* ^ |% Z0 V
: C' ~) f& P9 V/ d
public Object buildActions () {
3 D" s0 c# E/ F# z; c# i super.buildActions();
* ?1 C2 b* ?& d; Y+ w! g 2 y) Q+ \4 ^. [$ w5 s4 E( F
// Create the list of simulation actions. We put these in
/ H" Q3 `9 \7 C. j& Z O // an action group, because we want these actions to be8 t' I" L5 |, H! Y- m: z
// executed in a specific order, but these steps should
* x4 [: ~7 c8 Q5 { G$ H$ U; I% d // take no (simulated) time. The M(foo) means "The message
" W4 C* K0 ]1 |1 V: H2 G- ] // called <foo>". You can send a message To a particular* K, a) r! n P1 {) E
// object, or ForEach object in a collection.. Q, @1 `- K: n) e
# z9 B$ b" B A; v& O) u // Note we update the heatspace in two phases: first run% W7 E! o- T1 V2 P
// diffusion, then run "updateWorld" to actually enact the3 ~+ s" r7 k& h
// changes the heatbugs have made. The ordering here is
% a# p! q# h3 R0 M: X // significant!
+ Y. B- ]0 L: I' ^& p! v7 O; A! i
( ?1 \" x! \! k, l8 q) X- ^ // Note also, that with the additional
$ o R7 A& V" D# D2 }: m5 k% ^3 e // `randomizeHeatbugUpdateOrder' Boolean flag we can* d+ W G3 U' L6 M7 h2 K
// randomize the order in which the bugs actually run
# A. A# B: t/ O // their step rule. This has the effect of removing any
' b) a- Y- B7 B3 H( J // systematic bias in the iteration throught the heatbug
! U* l2 z" Y, }4 p0 z+ ] // list from timestep to timestep* {$ `+ d" `' @( A$ J
7 c9 E$ r; K k) N
// By default, all `createActionForEach' modelActions have
3 k7 U* ~! s! q5 l/ D // a default order of `Sequential', which means that the6 e. Z, T+ k5 ^/ _$ W
// order of iteration through the `heatbugList' will be! r2 `8 j C; Z& X
// identical (assuming the list order is not changed8 r" B2 s# b$ F- d
// indirectly by some other process).
# w1 C- h7 ^! l4 D8 d 2 l" h8 X; p6 {
modelActions = new ActionGroupImpl (getZone ());. ^( L; H$ o m0 r$ t
/ ~- Z6 U6 {+ z2 v Q
try {# w( [/ ~# I( R4 X7 Z3 h$ R
modelActions.createActionTo$message' B" u& _9 Y: c2 r
(heat, new Selector (heat.getClass (), "stepRule", false));
2 h# t. G2 D# y+ C% z } catch (Exception e) {+ O4 Q. ^2 M! e8 I
System.err.println ("Exception stepRule: " + e.getMessage ());7 u) y; V$ u$ r# n, x! e
}
9 R4 X# m; b$ {* b3 g0 T' a4 U% n7 j) o& h
try {& J& b! h. T1 {, K7 q) m4 C
Heatbug proto = (Heatbug) heatbugList.get (0);
+ I a8 F9 \& o2 i0 r9 b0 B+ d0 I Selector sel =
+ Q: Z" i' r/ q+ Z new Selector (proto.getClass (), "heatbugStep", false);) d9 r w$ t% d% T
actionForEach =
. \8 s; ^0 t8 d( ]# P+ L9 P modelActions.createFActionForEachHomogeneous$call
3 i9 b& `3 a8 B; B& s (heatbugList,; \( S. ^) K8 J: @: L. \* A
new FCallImpl (this, proto, sel,
2 B1 l" W9 t/ Z5 f new FArgumentsImpl (this, sel)));; v6 R7 W+ L+ p( g
} catch (Exception e) {
& ], O3 s+ l: b4 b/ _$ q. m4 Y e.printStackTrace (System.err);# B; _+ q2 f! c4 ]/ \
}
& t* M0 Z- h( ^( E$ a8 Z
+ W5 a1 }- ?. [5 D4 }1 A; _ syncUpdateOrder ();( D3 h0 n' Q _: X: r
4 d7 S4 D: v" {" z5 m" e try {
5 ~' v C3 d" H modelActions.createActionTo$message ' N/ ?# r9 o: v8 d: o9 \- D
(heat, new Selector (heat.getClass (), "updateLattice", false));
% u! x0 t0 k# n* s } catch (Exception e) {
) K& S+ ~7 |2 D/ T- v& ? System.err.println("Exception updateLattice: " + e.getMessage ());( {0 \/ c7 M& D2 P; s* W
}+ |7 ^- Y; O- X+ n
5 j. K) F' k+ \3 O* E0 c M! i
// Then we create a schedule that executes the% \! I, Z O: N- J. i: s# e) z; b
// modelActions. modelActions is an ActionGroup, by itself it
+ A! L. k8 e5 s5 }( ~' k // has no notion of time. In order to have it executed in! P* M0 ^/ d2 ~. J: d+ l( }* W
// time, we create a Schedule that says to use the# A) G. r) H2 [6 o
// modelActions ActionGroup at particular times. This
6 p& s6 O6 D& S0 [& m$ Y // schedule has a repeat interval of 1, it will loop every" z% O6 L0 E9 b! d% m
// time step. The action is executed at time 0 relative to
7 P0 L8 Q) g/ i1 p5 }- _ // the beginning of the loop.
/ O: M' a% L5 B( r1 W+ j; y6 b
7 E( [& H' f+ @' }# l // This is a simple schedule, with only one action that is+ n: N. H3 `' h9 y& S0 m0 x
// just repeated every time. See jmousetrap for more
! ]+ c( ~1 `4 [ _. l // complicated schedules.
9 f# r' h$ B5 u. h6 g: Z" J1 ]
9 M* a3 ]$ N, `& t; y5 s modelSchedule = new ScheduleImpl (getZone (), 1);, v5 d `+ k/ E# W# F5 e! g
modelSchedule.at$createAction (0, modelActions);/ x5 }& L% ^ D+ F. r8 A& s- f0 p: L
1 s! M. o3 w, a* x: Z8 d( _ return this;
0 O0 h. T( a1 M% Q } |