HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 O0 U E0 ]7 x; n6 n2 I( Z& ^1 w6 e0 q! z
public Object buildActions () {
9 z! s- H4 E' _, V$ M7 W& m2 |, a super.buildActions();! _" _2 t9 G/ \' B7 ^! M2 |
) T& _" e& v$ i // Create the list of simulation actions. We put these in
7 G: p) s; Q- T/ S6 R% | // an action group, because we want these actions to be3 N; }" J; v& ^% l! w2 Y4 b$ s7 Y
// executed in a specific order, but these steps should: v4 c2 Q4 t4 f5 m% x- a
// take no (simulated) time. The M(foo) means "The message
) K0 {) X" i; O) {4 Y# D! v% g$ C // called <foo>". You can send a message To a particular
$ B! h( g; p/ T7 \0 G8 I1 z+ _ // object, or ForEach object in a collection.- w0 }8 {3 A s6 K% G x
" j1 U9 W6 D5 G6 }9 n // Note we update the heatspace in two phases: first run3 [7 W* C) b9 q5 U( g0 f9 a" x
// diffusion, then run "updateWorld" to actually enact the
' l$ G, z& J8 T // changes the heatbugs have made. The ordering here is
J8 h7 J7 Z3 e& o; P( { // significant!
d& F2 y3 ~' @' B
4 f. [ @1 I7 ?; z // Note also, that with the additional7 l( N5 i0 }; P$ t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 u& n+ o+ y# M" `7 b // randomize the order in which the bugs actually run
0 o% h7 F7 e E# Z! F% b // their step rule. This has the effect of removing any
2 C" Y" S" M: t1 G6 j0 l // systematic bias in the iteration throught the heatbug
7 L* f, x2 u) T. Y% | // list from timestep to timestep
7 b( Y1 {0 \ E, P% l- k
& l. p, W3 V6 } Y1 F9 a P1 G1 F // By default, all `createActionForEach' modelActions have
, ]& h( m! v: Z2 I7 L! D6 ] // a default order of `Sequential', which means that the
+ o' }3 \" C% @ // order of iteration through the `heatbugList' will be
9 x7 R" m7 n% N$ j // identical (assuming the list order is not changed
5 z, i5 Z& W K% Z // indirectly by some other process).
1 s. v2 D3 D# g+ ^- h
) V3 ~% E, r& M% m modelActions = new ActionGroupImpl (getZone ());
' a- e1 A0 V, }/ b
2 E& q% P3 d+ ~" _2 o try {
# K& j; x& W* v2 D. [3 Y modelActions.createActionTo$message9 ?1 f9 s+ q2 w
(heat, new Selector (heat.getClass (), "stepRule", false));
0 ?% b A/ H5 h; b3 n# L9 d6 C! }4 _ } catch (Exception e) {! m# Z& D1 X( |) p0 }# a/ t
System.err.println ("Exception stepRule: " + e.getMessage ());
/ v+ P. Y) O' u0 E$ Y }
4 N$ s' s @! v9 s2 _$ h! z5 ~3 ^( A( M8 l y* ^1 [. `) _, d
try {/ g1 ?" S( |) s4 S5 S3 y
Heatbug proto = (Heatbug) heatbugList.get (0);* ]. S \5 x O! `+ E7 L ?/ ^
Selector sel = 0 |( c# X$ l; p: U' ]0 U7 ?
new Selector (proto.getClass (), "heatbugStep", false);
- S7 G, t0 l/ |% H7 N actionForEach =
. e7 S4 F9 g `) s Y modelActions.createFActionForEachHomogeneous$call
* ^- K0 R6 ^7 ?+ T5 w (heatbugList,$ B; `0 l" H* N! n, h: x+ }
new FCallImpl (this, proto, sel,4 Z3 l' l3 W, r! v0 X V5 P0 J
new FArgumentsImpl (this, sel)));
5 Y$ g) \: `& S/ \3 K- ` } catch (Exception e) {
1 v' U1 s+ Y+ z1 y5 m% X1 l' S e.printStackTrace (System.err);: |* ^, Q6 _) O$ R5 \5 l: L
}
p6 _& E$ o2 ?3 N. e + z, ~9 {# F3 e
syncUpdateOrder ();
8 ?8 k3 h8 s1 Z% Y' O$ S0 h5 C$ b' S& s! C- W/ `. ?
try {% R0 b% X. X: E7 s) x& ]
modelActions.createActionTo$message - @; p5 k" A( ]2 \: `
(heat, new Selector (heat.getClass (), "updateLattice", false));" b) }( f( E H! [
} catch (Exception e) {
* h. A+ N8 y! ?3 n- A) ^, E, w System.err.println("Exception updateLattice: " + e.getMessage ());, {' u* K2 }# u. U! U' q/ [
}3 |( `, x: z+ a- p" @! ]! Z7 N/ s# v
6 A8 V3 r0 u( z, p5 Q4 |
// Then we create a schedule that executes the
# s/ x. p9 `! [& j3 b // modelActions. modelActions is an ActionGroup, by itself it
, }: V8 G; G2 c) I // has no notion of time. In order to have it executed in+ g/ z2 w7 @2 A
// time, we create a Schedule that says to use the6 K {) B, p+ V9 t _+ [, W' B; G% X
// modelActions ActionGroup at particular times. This6 Q, [; A L+ C: L& p. _
// schedule has a repeat interval of 1, it will loop every
% I# i- N( C# H) |/ y // time step. The action is executed at time 0 relative to: i' h& a7 S9 c' F+ ^
// the beginning of the loop.4 t9 G a! b8 O* v/ o
9 A0 Q9 Y& q" @3 a' X
// This is a simple schedule, with only one action that is7 h$ H7 H$ L8 E+ M
// just repeated every time. See jmousetrap for more
, D P8 E& N; w! ^7 F) r // complicated schedules.# s3 i4 u9 P- B7 V! z: G0 B) q
+ Q$ j2 t3 K+ V/ Y1 [
modelSchedule = new ScheduleImpl (getZone (), 1);
0 N3 w9 f W% G+ M: o2 ?5 X modelSchedule.at$createAction (0, modelActions);
4 l: R5 r1 z/ N$ c" O4 h $ w3 n1 @; D; z2 I
return this;
- R% ^* \" X7 Q } |