HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# Z' p& g7 m: s7 R2 i; U, v! S
) k/ K. V& }4 u$ V' I" y2 l public Object buildActions () {4 u% j/ {8 ^, K4 ]$ ~8 B
super.buildActions();4 r3 E) z) q8 D0 }; I Y
2 P+ r4 K# [$ Q T0 K4 Y4 g: d // Create the list of simulation actions. We put these in
g. c' `# e9 q" o- K, c // an action group, because we want these actions to be
" i1 D) d3 B- Z! {/ f$ E // executed in a specific order, but these steps should0 U8 x8 L |. T/ j
// take no (simulated) time. The M(foo) means "The message" B, p L' y" |- Z/ Q& s. l* C
// called <foo>". You can send a message To a particular" H& \) N( A% I9 a {
// object, or ForEach object in a collection.6 E; `: m) C7 C e3 h
! a" H+ x3 V) Q) p // Note we update the heatspace in two phases: first run/ P& N0 |/ n/ t
// diffusion, then run "updateWorld" to actually enact the
i7 B! N" e1 P( q# O; e // changes the heatbugs have made. The ordering here is
/ @/ k/ z) b7 ]: m8 R& F% _ // significant!: H I+ ]$ q5 F; o( }; ^
B \2 H. ~* N9 V2 W7 [ // Note also, that with the additional( S& f/ Y6 }: W$ b4 D7 b
// `randomizeHeatbugUpdateOrder' Boolean flag we can; @8 H0 p3 _5 E+ b/ r6 Q* V' W, L
// randomize the order in which the bugs actually run
3 j$ _- w4 j% N9 Y, Y% C. P // their step rule. This has the effect of removing any1 b. x d7 E8 @
// systematic bias in the iteration throught the heatbug4 T. ^0 g2 `; D, U8 V1 H5 ~) P A
// list from timestep to timestep
9 B6 \! G6 v' z7 e/ H 0 f: o# `$ j u5 q- A7 v
// By default, all `createActionForEach' modelActions have7 W5 V3 g3 n% W8 u
// a default order of `Sequential', which means that the7 G$ n" N2 z7 g6 s; k% N
// order of iteration through the `heatbugList' will be1 t% d1 ?3 o5 H: k8 `4 Y# i
// identical (assuming the list order is not changed8 |( V) A! A! _5 B! c8 o: W3 B
// indirectly by some other process).
# l5 `/ y, X( q7 B0 ]* Q
3 ?; c- X4 e; ]" f3 x) e6 @- [ modelActions = new ActionGroupImpl (getZone ());5 u/ C z; o0 K& \% {( o. p$ |
4 ]" V6 y3 A$ A* C) Z1 i7 T
try {
1 C7 R$ E$ w7 H+ A modelActions.createActionTo$message! O) V4 O6 b& r. V4 [
(heat, new Selector (heat.getClass (), "stepRule", false));
% f3 h8 F C) X$ L } catch (Exception e) {$ i& d( V" y$ v3 u2 V7 w* ~4 O
System.err.println ("Exception stepRule: " + e.getMessage ());% M: |1 e, M8 c4 L
}
7 _: A3 _# C' U& _) V! }$ c# V: L: ~7 V: e
try {
: o: u* b) p4 w3 ~+ `: A Heatbug proto = (Heatbug) heatbugList.get (0);" p7 `; ]& j7 S7 M9 }, I
Selector sel =
4 y5 D, L& ^& J4 T" {9 T2 ]( _1 u new Selector (proto.getClass (), "heatbugStep", false);9 ?9 w4 @& [! @2 u7 s
actionForEach =
7 b4 j/ D, u" o. i) a modelActions.createFActionForEachHomogeneous$call
9 J' U: X' u$ Y (heatbugList,: `# [$ p! S' g) e# W
new FCallImpl (this, proto, sel,9 @, J8 j4 ^- S1 x1 a9 a. X9 j W
new FArgumentsImpl (this, sel)));
8 w2 D1 K" H8 y, m5 S3 F- ~ } catch (Exception e) {
5 ]$ i# u# ?* D. n1 z5 { e.printStackTrace (System.err);
, r- w0 Z- {5 L' [6 z* K }% p' v+ N, L; B
7 W5 m. I0 S6 ]4 |' R, ~ syncUpdateOrder ();
8 x \8 ]* o, @* ?! S2 y- F. m
9 O* A# X& k: U4 d, g0 ? try {, z% S+ j& R5 A6 A
modelActions.createActionTo$message W9 I9 E5 O/ X
(heat, new Selector (heat.getClass (), "updateLattice", false));9 B1 U9 ^+ L/ M% ]7 P3 f8 }" Q
} catch (Exception e) {" E6 _. f( q3 v; I- k3 x) B
System.err.println("Exception updateLattice: " + e.getMessage ());
$ H. }4 f! B- s. w, K! B- C }0 v0 a) n5 ~9 Z7 m
. g# x M( X* q! }0 A
// Then we create a schedule that executes the# u2 I% @* Q2 K- g1 t$ i0 S* P0 ^
// modelActions. modelActions is an ActionGroup, by itself it$ g% b7 D8 q3 u, A0 {* u
// has no notion of time. In order to have it executed in
5 a2 F$ X. E P$ O2 S // time, we create a Schedule that says to use the( w0 `* `! V5 N6 K4 ]
// modelActions ActionGroup at particular times. This
( l/ p) j' E5 U // schedule has a repeat interval of 1, it will loop every% m3 `: K6 X- P% a ?- ]/ P5 a
// time step. The action is executed at time 0 relative to
# a0 @" K3 r% ^( P // the beginning of the loop.
( f! I$ P* s$ X2 r; e! ? U( d; n5 ~. K0 q0 h3 @
// This is a simple schedule, with only one action that is
3 H$ r4 H$ y0 Y* n7 O+ M9 M // just repeated every time. See jmousetrap for more$ b2 T8 m/ G: E6 d& M' N4 O9 |
// complicated schedules.
2 l; S, O* O- o h" G% E6 ^
8 e7 L( v4 [ ^! e modelSchedule = new ScheduleImpl (getZone (), 1);; B; e& d: p4 s
modelSchedule.at$createAction (0, modelActions);
- v7 i( j( h" c: m' ? ' ]+ K v" W( Z F' L
return this;
" f. n& |/ u6 I# M6 q } |