HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! O8 B$ m' ?6 s- e J0 S) J) L0 e2 G6 X
public Object buildActions () {, z- Z+ f1 ^/ H, p: H- g' X! k
super.buildActions();
( x# F f7 k. N' j% Z9 q
% O& j# g# {" j3 K _4 o [4 u // Create the list of simulation actions. We put these in
$ s, L2 X, m- V7 S8 m0 q! |9 j // an action group, because we want these actions to be) S% g, K; w8 H" i# a; T
// executed in a specific order, but these steps should
0 k5 L7 G' q. E2 r6 A // take no (simulated) time. The M(foo) means "The message. m V% v4 `! p4 M1 ~$ j( ?/ o' E/ _
// called <foo>". You can send a message To a particular# U9 D. h3 y+ H/ o
// object, or ForEach object in a collection.
1 [) b: [9 m* r4 ^% M U
% K! k/ Z+ y3 c; O // Note we update the heatspace in two phases: first run
9 W! P. w. P+ ]1 h+ E' W // diffusion, then run "updateWorld" to actually enact the
/ r/ \/ V3 q' T5 |8 P, {5 l2 \ // changes the heatbugs have made. The ordering here is' N6 a4 i. ?1 r& k* g" O- H
// significant!* \9 T5 n- s, X4 a) |
! }) s) p$ u( f Z- C // Note also, that with the additional
2 d! Q* J: j% a4 |# E // `randomizeHeatbugUpdateOrder' Boolean flag we can4 t5 g, R x3 p4 _
// randomize the order in which the bugs actually run
) _3 a# e* f. ]$ x% y // their step rule. This has the effect of removing any# `$ ~4 ~" J, k" L
// systematic bias in the iteration throught the heatbug/ A5 a5 x9 T7 s% \; \ P- h
// list from timestep to timestep' L& v& f* a8 i6 S0 k' H
" S+ m2 X* u6 b! e( j1 K, I
// By default, all `createActionForEach' modelActions have1 _1 E( n. ] u3 d- D/ D& X/ `
// a default order of `Sequential', which means that the
! D: P0 H+ E9 v) S // order of iteration through the `heatbugList' will be
: U$ P: }* R, B% k7 O // identical (assuming the list order is not changed
* ?7 O' w6 t' p9 |8 q$ Z // indirectly by some other process).6 }8 B* l! t5 b$ I9 t$ l; l( J
* Q* B2 B% V0 e! Y modelActions = new ActionGroupImpl (getZone ());
& i7 g, W) Y! O6 i9 g0 K8 q1 X
3 [- J5 O* N* h! m try {
' e2 J+ R! ~0 M. S( s3 K modelActions.createActionTo$message9 Y3 @! A% z b
(heat, new Selector (heat.getClass (), "stepRule", false));
/ y1 m! r+ v3 z" `) v } catch (Exception e) {
% a# X6 B' _! M System.err.println ("Exception stepRule: " + e.getMessage ());
- A& B) B+ E$ Q7 S8 L% C }
/ A3 Q J8 [8 i8 m4 s$ B2 `7 X2 W, n4 S; x2 F' \, |# n5 G5 _4 {% W
try {
' @$ i3 N3 C2 |; Q1 {% |9 [: k2 e Heatbug proto = (Heatbug) heatbugList.get (0);
K& p0 W: o, ^: [( p Selector sel = : W+ k. R* Z/ j( R
new Selector (proto.getClass (), "heatbugStep", false);
A: a! J& ?* v5 a" g% _3 i6 g! Y actionForEach =
: o* k( \) J$ x4 H modelActions.createFActionForEachHomogeneous$call
7 i! Q" A1 a: |" l, B: v (heatbugList,3 b2 B$ K% B6 k7 _
new FCallImpl (this, proto, sel,# Z# f, Q3 ~: W# ^: m
new FArgumentsImpl (this, sel)));3 k2 d9 r' ?- `/ k8 d2 r
} catch (Exception e) {' j; f' ?8 Q7 O: E
e.printStackTrace (System.err);) t- c* R# F4 }
}! Y2 l4 \ y2 n- P Z/ B8 `& R
* L' u, J& j$ _ syncUpdateOrder ();
5 c! x: J* z$ P' O' Y! L4 ~' V% J8 _. u L
try {
6 P' U' B6 p& g$ x+ }: w. S modelActions.createActionTo$message
3 w0 J H. T( ]$ ~* M. R; T (heat, new Selector (heat.getClass (), "updateLattice", false));3 P I3 X3 q) j. h5 g; g4 i
} catch (Exception e) {
* }* Y0 ^% m, V# x% _1 ? System.err.println("Exception updateLattice: " + e.getMessage ());
+ D4 o& n. M9 O/ l }( C* _1 A: Q8 G' ]0 n
* g5 C$ w' V+ b" y* j // Then we create a schedule that executes the
( J6 ^6 v# S( U- D, G+ x' M$ W // modelActions. modelActions is an ActionGroup, by itself it
: Z" w# X! \2 Z, R( B: a" e2 ]* ` // has no notion of time. In order to have it executed in
; G; A) n9 Z4 a // time, we create a Schedule that says to use the+ K4 t9 x9 S; Z2 e/ C+ ^
// modelActions ActionGroup at particular times. This
1 O0 h6 D1 x( {- {, L // schedule has a repeat interval of 1, it will loop every, l% m( e' ~! b8 Q/ Z- Y8 c K
// time step. The action is executed at time 0 relative to- K# g9 y/ [# n0 ~4 ?. i
// the beginning of the loop.
/ A: A9 s. E$ g9 B0 t1 ~4 \2 u$ a. H. i* y( {! f
// This is a simple schedule, with only one action that is
( d' V8 r8 G( D2 z0 Z // just repeated every time. See jmousetrap for more. ~ f. S; Z& V( j- H2 i4 H
// complicated schedules.
) W# `- M' A1 _5 h " H4 U( U, J4 E. \5 @2 U
modelSchedule = new ScheduleImpl (getZone (), 1);
" j% R+ M H* z' g% a( n* O modelSchedule.at$createAction (0, modelActions);
- ~; k% k M) V1 l4 Q t1 W8 h " e& `4 K* `- d+ h0 M
return this;
) S% w: C( z8 }3 ]/ L } |