HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 t; { R' s0 |7 k. n8 j9 \( b8 ?/ s) V# M1 i
public Object buildActions () {- U5 i2 W+ k% _% b
super.buildActions();
% b3 m5 x2 F! |% l( z% F" v% S 4 H# v. G$ v: J- G" K% W
// Create the list of simulation actions. We put these in
+ b: d Y: @9 e5 Z& {) }% f, a7 E // an action group, because we want these actions to be
& W: y; I; c, S/ r3 p // executed in a specific order, but these steps should
; s$ k" J& p# e9 H // take no (simulated) time. The M(foo) means "The message) G! H9 v, o9 ?" w, p
// called <foo>". You can send a message To a particular
2 D5 K) A% c0 x+ A. B0 X: V // object, or ForEach object in a collection.
/ B% K$ i% U, [7 s) L 3 U, T- @) F1 z1 T! s7 @2 f
// Note we update the heatspace in two phases: first run
* k+ Y5 |/ ^% o) n // diffusion, then run "updateWorld" to actually enact the# E6 O; M, [( T% u+ W j5 ?2 L
// changes the heatbugs have made. The ordering here is
( d* ]$ o9 ^; i+ I0 j# R // significant!
! _* t$ r( G7 I" a) q8 C& s5 }0 Y: I 0 R1 {; d3 x% F
// Note also, that with the additional
. a" q5 j5 ?. B/ M$ Z! { // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ J5 R& |1 `& x; J( S // randomize the order in which the bugs actually run$ H, _4 m7 `) t) Z
// their step rule. This has the effect of removing any
0 _& R" ?0 l- D( L, V1 s+ l$ l8 ? // systematic bias in the iteration throught the heatbug
! ~. p0 d) U+ G: J/ k0 H) z // list from timestep to timestep
9 u5 F0 _% y( u- o2 _ 3 G' t( _4 q0 h. |" h
// By default, all `createActionForEach' modelActions have3 d5 A, y* e: N2 c
// a default order of `Sequential', which means that the# @" n1 S" R! F. j- m+ p# A
// order of iteration through the `heatbugList' will be
6 x( p, I% L2 u. \) |8 ^ // identical (assuming the list order is not changed
( k o) }& |# ]1 j // indirectly by some other process).
+ N7 i3 N+ v0 g* m) ], a1 W
5 |) ?6 U' z y modelActions = new ActionGroupImpl (getZone ());
# C5 K( y' |' V' E, C$ b8 U6 ]9 W- `, o C: F8 q
try {
1 o1 G* o3 f3 q' z+ Q modelActions.createActionTo$message
- c9 L5 m, D: G$ A" N (heat, new Selector (heat.getClass (), "stepRule", false));
9 o k! _4 e! y } catch (Exception e) {
7 `) J( D7 S( P% k( ] System.err.println ("Exception stepRule: " + e.getMessage ());
' n3 s" `/ ?5 q6 p8 U8 d5 J }
9 A# [! f: D$ c6 x; q y# J! s$ Z- D/ j3 w4 M1 W7 J# {
try {. G7 _ @) s! V6 U& G) `7 I& u9 |
Heatbug proto = (Heatbug) heatbugList.get (0); u W9 n4 o% |
Selector sel =
2 R. Q6 ?* v s: x new Selector (proto.getClass (), "heatbugStep", false);' s- X$ F8 j/ c3 D3 P
actionForEach =
' ?2 E. i9 L- G: V [& D4 r: m modelActions.createFActionForEachHomogeneous$call( u/ q7 e, D$ V: Q4 a
(heatbugList,
. }+ W0 @( J( b! D# } new FCallImpl (this, proto, sel,- g6 B1 y; ^8 I# n
new FArgumentsImpl (this, sel)));3 p% D1 W3 d! g
} catch (Exception e) {+ X6 `8 j! ~. Y/ D* V# r2 E
e.printStackTrace (System.err);
8 @1 t. Q! u2 }3 @ }* e5 j0 Y8 o1 m1 {% i
2 u( v3 c) P$ ?; r; |4 N6 N syncUpdateOrder ();
9 K: m" y7 S5 G9 ]; W; O2 K6 E4 F9 @3 v+ j
try {
% {7 ?, n1 k: w6 S modelActions.createActionTo$message 7 q0 \4 B3 K2 A1 t% c8 {4 x
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 x/ j! x) u+ g5 t# D1 r- ` } catch (Exception e) {) O. O/ c; i' l+ D5 T
System.err.println("Exception updateLattice: " + e.getMessage ());4 @& ]+ @ H7 |7 Q
}
/ r+ b4 B* c& a: n3 U6 Y2 x) y ) z9 h3 R. T1 X2 T
// Then we create a schedule that executes the" w9 B! f. C: F; K3 U
// modelActions. modelActions is an ActionGroup, by itself it S+ k2 C0 [5 z- a2 o
// has no notion of time. In order to have it executed in/ S' Q& U$ I: b& \+ J D
// time, we create a Schedule that says to use the
$ E T1 W6 Y$ J: E1 a- P6 n // modelActions ActionGroup at particular times. This
- c2 q# p0 L7 Z% U. h // schedule has a repeat interval of 1, it will loop every
: W+ g% W, X2 W/ O+ ^ // time step. The action is executed at time 0 relative to
) N) i( K; i8 ^2 A7 d6 p8 z // the beginning of the loop.' C) a8 u$ Y7 a+ ~# B
% O9 O; O! N' q // This is a simple schedule, with only one action that is
% o$ w; d8 [% \( `0 }6 w // just repeated every time. See jmousetrap for more& E( d8 t# z1 x- D
// complicated schedules.' m/ \& H6 I, p& {1 ~
* R* ] X2 U8 o- t9 M
modelSchedule = new ScheduleImpl (getZone (), 1);
8 c1 u5 T6 ]" U& F modelSchedule.at$createAction (0, modelActions);/ [/ c) @( Q0 s
8 T' K! ?! E) f2 e0 ?- l return this;
# W1 E7 \# Y8 c6 c } |