HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ y: [' f! ~' s+ Y
$ J- t1 z$ a* z: U3 h. G* s! F public Object buildActions () {
* f R/ C6 o2 s$ S3 m/ x+ e0 v super.buildActions();
" m. J8 X1 r( ~2 m : E# X! k. M4 o+ N; q0 d6 j
// Create the list of simulation actions. We put these in% l5 ~% l- e) o! a0 J* B( s
// an action group, because we want these actions to be
0 D7 b0 e) W3 j, x. Y // executed in a specific order, but these steps should, B+ z' s* ?1 _& ~* l# T3 M
// take no (simulated) time. The M(foo) means "The message
}$ Q7 _3 T8 C- ^ // called <foo>". You can send a message To a particular
0 X6 V& x# ]' j' h4 B& w' a8 o // object, or ForEach object in a collection.- `+ r6 w2 O$ T/ B1 @ ^
( L- g# r# Q* q" V // Note we update the heatspace in two phases: first run
6 E" q7 s" f. G b, H1 e // diffusion, then run "updateWorld" to actually enact the
/ J+ x8 f3 u) x& q# M) K! b7 f) Y. ^ // changes the heatbugs have made. The ordering here is" @3 J. [, w! T% F1 j1 N
// significant!
8 }8 c( ~1 }: y2 \% ?8 J
: D9 [% W% x1 V9 L0 Z l6 a% V // Note also, that with the additional+ H; N5 I6 j6 j) X: H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ b0 V; @# S. P8 ]! w5 B5 G3 Q // randomize the order in which the bugs actually run
9 N! F3 \& C# j0 ^4 U$ ` // their step rule. This has the effect of removing any
' E+ B2 y+ k( C // systematic bias in the iteration throught the heatbug
1 z0 v/ J% H k: E5 C" r% o% Z // list from timestep to timestep( Z5 m+ O) ], W$ S
3 c5 V7 \5 F: v
// By default, all `createActionForEach' modelActions have# y8 I& m5 p8 X+ J2 _0 ^8 O) g w
// a default order of `Sequential', which means that the# x. p8 H7 ]7 j" ]
// order of iteration through the `heatbugList' will be
: c/ s: Q" i+ [& {3 t/ a // identical (assuming the list order is not changed$ R* q& }% r: M% q2 F
// indirectly by some other process).# }1 k3 l- R* P' _: u
. t. g( S- R N# I, i5 j! N4 ~ modelActions = new ActionGroupImpl (getZone ());
* c g% x1 t' m5 c; n
9 b6 e3 U- C: z, [ try {
) v; H0 J1 y& i9 k3 U modelActions.createActionTo$message. j0 t% o; S3 f6 G2 Y7 F( W
(heat, new Selector (heat.getClass (), "stepRule", false));5 m* q. N% w; V, o2 _' s5 E
} catch (Exception e) {
/ L% s( O1 v% \2 @ System.err.println ("Exception stepRule: " + e.getMessage ());
% i) c/ @2 H. Q; h# I$ S3 K }+ Q+ J2 S! _0 T/ H* o. O$ L
' X3 |6 m0 T0 ^+ H1 }9 V% h. ?
try {3 F, I8 \5 q' _$ [, V
Heatbug proto = (Heatbug) heatbugList.get (0);$ U. n+ _7 P2 p `
Selector sel =
( F) {0 k2 c! Q6 Q3 ~' k new Selector (proto.getClass (), "heatbugStep", false);: h2 | K a: c1 V3 o4 K
actionForEach =
% S1 n+ n3 c" \$ L |& Q modelActions.createFActionForEachHomogeneous$call
p+ V2 u( ?5 J; Z" P (heatbugList,4 z1 M+ ~# a' \: W
new FCallImpl (this, proto, sel,
; P$ T" c5 [3 B$ g new FArgumentsImpl (this, sel)));) ?' K- r. O5 X( n6 C
} catch (Exception e) {
7 P2 I/ w8 l) Y% E s e.printStackTrace (System.err);; m. P r4 V8 m5 R% B! N
}5 l( ^' b" `5 |4 k
4 v2 ~8 k: Y1 z syncUpdateOrder ();; {9 W# t( x/ ~" b \: m
' B4 J5 K9 z& U) l6 r1 F+ ~ try {
+ w! q( g! g/ @3 x' X- k- n; t modelActions.createActionTo$message
; B# ^) w z; E- {2 h: g1 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
k( B6 P$ ?) X- }1 P" d } catch (Exception e) {
! j# d7 {1 U& e1 R, x+ T0 c System.err.println("Exception updateLattice: " + e.getMessage ());+ v% K) M) \+ {+ j) r1 B+ D
}, M4 l1 e: [% t* ~' B
. ]5 F/ a# f n6 A2 U$ e // Then we create a schedule that executes the; A# x7 m8 a. ]* P3 r# C% R2 S
// modelActions. modelActions is an ActionGroup, by itself it* ^0 {/ r7 C1 D% p
// has no notion of time. In order to have it executed in% |9 b M% i; P2 j9 K) ?' Y
// time, we create a Schedule that says to use the) _+ O1 u: X2 m7 |& I
// modelActions ActionGroup at particular times. This2 T! K: G" c) p" k$ F# t% x
// schedule has a repeat interval of 1, it will loop every3 m& q$ C0 A0 u$ b
// time step. The action is executed at time 0 relative to
$ r7 T: O' o9 p4 D: n7 E+ X" F // the beginning of the loop./ S; z# a: o! J9 {7 H
8 l* p2 S% Y A+ H! p
// This is a simple schedule, with only one action that is
7 ?1 S/ E9 f' g m/ `9 F // just repeated every time. See jmousetrap for more. W! L8 @8 q9 Q+ p7 W
// complicated schedules.4 ~0 X2 T' Q0 f* p0 i2 j
M$ J! [! A+ a modelSchedule = new ScheduleImpl (getZone (), 1);
$ c% M N; q# W# A+ @+ J9 @: Y modelSchedule.at$createAction (0, modelActions);" T2 j; Y5 P* `+ N
# W3 a/ \, v/ ]3 t
return this;
* `4 X& w% Z) t) k } |