HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ Z" Z. y- a0 W" N4 U( `# z2 k# K8 [( ~0 D- N( n2 K
public Object buildActions () {, G0 l1 X: V! n4 S# l/ Z
super.buildActions();: a( @% p# c" M8 e
8 W5 M2 Y* L& N9 s) ` // Create the list of simulation actions. We put these in& Z" C7 ?* E0 \' B
// an action group, because we want these actions to be
5 b) x* T) [6 B4 p: S2 ~ B1 V9 y // executed in a specific order, but these steps should
" ?1 z* o+ L$ I // take no (simulated) time. The M(foo) means "The message: T. Z, Y a9 n* t$ q5 s* e
// called <foo>". You can send a message To a particular
# [4 `* @, V* O& @ // object, or ForEach object in a collection., }( G0 N' D9 ~) F7 ?( l
7 X& Y; E$ E' n1 m( p9 ~7 s // Note we update the heatspace in two phases: first run
" x. q5 c0 ?) L) p // diffusion, then run "updateWorld" to actually enact the6 x3 H0 v& H8 V7 ?3 f1 K* L2 F: D
// changes the heatbugs have made. The ordering here is& c# K. t2 J2 x3 \
// significant!
1 P" Y& k# }* F# T1 d7 b( h1 U) T
; I' D4 P. j% A // Note also, that with the additional
0 l) r/ t2 c8 D5 w, q' z9 o& f // `randomizeHeatbugUpdateOrder' Boolean flag we can/ M. y( e# ^( Q+ X6 l" Z' E# t+ W
// randomize the order in which the bugs actually run, B/ k7 [/ i3 Y. Z7 y
// their step rule. This has the effect of removing any
0 m$ N2 I0 p. C, U9 Z% g2 n // systematic bias in the iteration throught the heatbug! O2 \% c3 j! M! F7 V# n4 I5 t1 M
// list from timestep to timestep
/ f) {8 R2 x/ ~( S
2 F5 }; x1 n" x- I+ P: N/ s1 _( L // By default, all `createActionForEach' modelActions have
: w9 ^: q% r9 L" ]; x; U% A // a default order of `Sequential', which means that the, b0 M. ?9 g2 R% O( j( t9 m9 ^
// order of iteration through the `heatbugList' will be
5 `) v4 L8 j0 o6 j( o0 Q* G7 N9 M // identical (assuming the list order is not changed( ^( C+ d; _0 b* f4 K s8 N8 ]
// indirectly by some other process).5 k8 L& m( x" v! b+ o B+ k
9 p9 F+ {9 J5 v+ W
modelActions = new ActionGroupImpl (getZone ());5 ]8 @5 C9 F+ F0 _* m( X
, X. \1 o% a3 U$ R* A% |
try {( _! G( L$ [; R% V3 Q7 T
modelActions.createActionTo$message
6 U) t& d8 a8 _: ^ (heat, new Selector (heat.getClass (), "stepRule", false));2 [, K) y! e5 R9 R! q& a
} catch (Exception e) {
. c5 h1 A* d, G3 Y5 A System.err.println ("Exception stepRule: " + e.getMessage ());
' S E! N+ ]) U# l) x% q0 i }
8 Q5 f( {- b3 p3 ~0 K+ h# u/ x! Q/ P" z
try {# e# N1 R: {# P( r: [2 Y
Heatbug proto = (Heatbug) heatbugList.get (0);
6 b, O& J4 z! Y" Y Selector sel =
% A+ O! [* ?1 P; N) T( t new Selector (proto.getClass (), "heatbugStep", false);, r( ?+ m; J9 f6 H7 Y5 m
actionForEach =( |! x) y- f+ { m" k2 L, `
modelActions.createFActionForEachHomogeneous$call
7 K/ f: F$ U1 w, ] v (heatbugList,. x A$ w+ t, c1 g; m1 T9 v
new FCallImpl (this, proto, sel,
6 A( r2 x7 n4 j0 m3 _, E% y new FArgumentsImpl (this, sel)));! X* U) n! {, ^9 c
} catch (Exception e) {
/ `: ?' D9 J! C. g, X: `; Z e.printStackTrace (System.err);& }# O# Q! n! Y; f m' _, P6 i
}
2 Q/ X! ]. K( k) J/ A
7 T7 `% l- S& {3 V8 R# h syncUpdateOrder ();
: N, V# L2 N8 x8 x9 B$ l5 M5 ?+ \! ?: j, Z I8 Q
try {7 E5 R$ I' f& V0 [1 U) G
modelActions.createActionTo$message ( {, K% o4 I" c3 d
(heat, new Selector (heat.getClass (), "updateLattice", false));
, f( P$ s# V1 |6 K1 ] } catch (Exception e) {; |6 D+ f9 w7 b( N1 r+ a9 b
System.err.println("Exception updateLattice: " + e.getMessage ());
+ I8 D+ u: x9 P& [ }
# S0 b" r, G; Y7 R/ m# z- Z 9 D- N |9 j( c8 j. I
// Then we create a schedule that executes the. h [8 n8 I+ Y( ^) p, V1 e
// modelActions. modelActions is an ActionGroup, by itself it' I" t" ]& i2 o3 q& D8 C" t
// has no notion of time. In order to have it executed in
. B: `$ g* ~5 q! Y; H! ? // time, we create a Schedule that says to use the& F' E3 `$ Q9 @
// modelActions ActionGroup at particular times. This
3 H2 r. s, R; N // schedule has a repeat interval of 1, it will loop every
4 v# k# [2 }. N+ \% T // time step. The action is executed at time 0 relative to- \" y9 I8 t1 e$ {& _
// the beginning of the loop.
R6 K7 H: o3 M( x* B+ k5 _$ V1 s0 m% s* U' \7 \# `9 W# W T
// This is a simple schedule, with only one action that is1 C# ^9 }# K: t4 J- a/ Q: \
// just repeated every time. See jmousetrap for more- j9 _; r1 Y, Q7 v
// complicated schedules.
4 w$ Y1 T$ k0 _$ ? 7 t- `) [0 {% L; q2 Q1 H
modelSchedule = new ScheduleImpl (getZone (), 1);+ p/ @0 N* X, i0 o4 Y3 R# K
modelSchedule.at$createAction (0, modelActions);
' T$ Y( Q1 y" T: l3 |. B/ m. ]
+ q# h/ k) ^( H7 R) y8 C+ \ return this;
+ n2 u* r! b7 Q6 u" V } |