HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, J+ z$ |- ]- ~: t. y5 g
8 i1 x2 p7 d3 b" S9 }, g0 n, y8 j4 B public Object buildActions () {
! D% B- b, M0 R. x( M3 _$ a$ i( c( E" \ super.buildActions();
- O' u6 P6 ^! c% q% k( r* ] 6 `8 ^) i( m: s2 ~* M/ \
// Create the list of simulation actions. We put these in
* g0 l; D7 w+ ]- W0 E* S0 H // an action group, because we want these actions to be
3 c! N- i- O, I9 ?* J" `! h! r! p" F // executed in a specific order, but these steps should9 j8 v+ X6 z. T1 U; d; ~
// take no (simulated) time. The M(foo) means "The message
, l- u3 O: @% N; K, C2 W ` // called <foo>". You can send a message To a particular$ Z% H; L6 u; p
// object, or ForEach object in a collection./ _) z. l r8 ], Z4 j& y
. J( _: v) e( }% k& \6 U
// Note we update the heatspace in two phases: first run0 u0 t- T% z0 P% f- c
// diffusion, then run "updateWorld" to actually enact the
' d$ u- \* |3 E h k; O) t // changes the heatbugs have made. The ordering here is
$ {" A& m7 I1 S8 R6 B9 G% } // significant!# B. Y( _* j. I
1 R o* y8 g; t
// Note also, that with the additional3 ~" f/ H- D: x/ o& W; ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' v* j$ S. G9 ^" t& i" T! f( w // randomize the order in which the bugs actually run0 `6 }3 m' U, N, c0 }$ U
// their step rule. This has the effect of removing any
$ D6 J& \7 ~& b* w* e, T // systematic bias in the iteration throught the heatbug% f# [3 Q4 m. S
// list from timestep to timestep
$ x$ O4 V( f1 ?- L1 H
) o0 ~+ {% n7 j9 ?* I" t // By default, all `createActionForEach' modelActions have
5 \5 w" W9 Y- t // a default order of `Sequential', which means that the) S# L3 A* y" M4 L7 x: l k7 t
// order of iteration through the `heatbugList' will be
$ h! m' A p. x' ]& L // identical (assuming the list order is not changed( t+ N. Z5 r6 w. i+ S6 F& m. }# d9 f
// indirectly by some other process).. ?, k3 @0 p% p- n4 x
|. V$ U8 Y8 v
modelActions = new ActionGroupImpl (getZone ());9 G$ @2 l8 U2 a1 i* k! N
9 A$ Y8 L. |& p& _( F try {
/ D. Q6 Z. X5 U3 q. j* _ [ modelActions.createActionTo$message5 u, \/ d, v2 {
(heat, new Selector (heat.getClass (), "stepRule", false));
3 j E# y7 B+ a0 A% {. b6 h } catch (Exception e) {
$ I; w, {* ?& s- q. t2 U/ ?( _- J( { System.err.println ("Exception stepRule: " + e.getMessage ());
8 }7 j" L7 Q9 N }- A) ~5 ~2 E+ o- X
% V* G8 `9 w* G, K/ S
try {
. j6 q2 Z0 _4 Y; O; X Heatbug proto = (Heatbug) heatbugList.get (0);1 N# @( A4 y& i# f
Selector sel = ' `8 V; |% ]7 A4 `
new Selector (proto.getClass (), "heatbugStep", false);
0 h: U2 Z" z) K7 f7 _ actionForEach =/ w' N, [, h2 i' A, C8 d r
modelActions.createFActionForEachHomogeneous$call
Q ` y0 s; o# x (heatbugList,: h( j8 T/ b" p1 U) l6 k1 ^
new FCallImpl (this, proto, sel,
+ i9 d% \. g! t new FArgumentsImpl (this, sel)));
# F% b/ y+ p- T/ I K( p) o } catch (Exception e) {) ?+ _" P- ]( ~/ B5 z
e.printStackTrace (System.err);
6 {9 b5 f! c+ L& X: |/ u7 T$ j: r }
/ Y7 T" b# N. W/ n6 ^/ ?- [ ( Y, O3 ?& i K+ P: l6 l$ J
syncUpdateOrder (); X2 `9 ?1 i3 a, T, I8 t1 [
* m0 X$ I9 F8 n8 U. y0 F" l try {
c- f$ o) b: T1 x. s9 q" ]/ X modelActions.createActionTo$message ' ]; E5 a- ^, k2 Z
(heat, new Selector (heat.getClass (), "updateLattice", false));
# { D- [( }* Y6 i) u: {0 p } catch (Exception e) {" O+ F' o V3 W, Q
System.err.println("Exception updateLattice: " + e.getMessage ());4 O! _" t& t2 @/ k* v8 }
}1 H; F" e, G; K j; ^
/ X, |* I' y' V6 m: A8 ?* X( X# V& [, v+ \$ @
// Then we create a schedule that executes the
/ ? m$ p9 H* {& n8 J, m // modelActions. modelActions is an ActionGroup, by itself it0 r p$ N1 P: |# K9 f
// has no notion of time. In order to have it executed in" l- W& z- `' |- u4 Y" f
// time, we create a Schedule that says to use the i5 Z5 L% r9 k# \4 a C
// modelActions ActionGroup at particular times. This" C, W1 b/ m8 P f1 `
// schedule has a repeat interval of 1, it will loop every
' a; ]% V: k8 r' h) k // time step. The action is executed at time 0 relative to
; f# f# p2 ?, {2 Z( Z // the beginning of the loop.
4 f4 x( y4 S9 W
P$ f9 ^5 _2 v // This is a simple schedule, with only one action that is
' t; z4 \. _+ s // just repeated every time. See jmousetrap for more4 t! c& k- o" {0 ?' l: R
// complicated schedules.
5 `9 j* x% y9 @6 ?
( t( d+ r3 G8 ]" K! L3 y( o0 g modelSchedule = new ScheduleImpl (getZone (), 1);1 m3 Q: O' r3 Q/ v
modelSchedule.at$createAction (0, modelActions);
" J( B! ^1 U. t4 R$ Z- d" B' @ 2 _; F9 [' ^& {+ j" c- f U" M3 z
return this; a/ r+ l9 u( k1 L
} |