HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: ^ n- f' L! I- Q' t8 P7 V* X
+ Z. @: W& l6 B, n
public Object buildActions () {: c' S d$ J: k5 F4 y) C0 s
super.buildActions();2 k7 v5 {' U. ^- J9 _* \
+ o2 K, P/ Q/ I* S: F: y3 e // Create the list of simulation actions. We put these in9 H9 E3 N( m3 G3 |
// an action group, because we want these actions to be
8 G# u* n/ A. D, G* T6 i6 L' x. U // executed in a specific order, but these steps should
) |/ X) g, I+ V" ^; Z, ? // take no (simulated) time. The M(foo) means "The message- G( x" W2 v3 {3 N. v0 @
// called <foo>". You can send a message To a particular) U4 Q0 B% U2 N6 F
// object, or ForEach object in a collection.
! I |( ^+ H1 e, Y3 O3 w 3 X } _ n& V) N
// Note we update the heatspace in two phases: first run1 W( Y( x# x* [
// diffusion, then run "updateWorld" to actually enact the
7 x' o8 X* n; b- W7 Y/ v! o // changes the heatbugs have made. The ordering here is
( y: ~" D! w% P5 o6 E1 x" n // significant!1 g1 ?5 B. W: B5 Z
+ s; o' g- j+ L' e @7 q
// Note also, that with the additional# F% X. g! p0 A4 ^5 N2 G
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ e$ T) s8 Y$ z% ]* L& ~
// randomize the order in which the bugs actually run Q0 {3 k4 |. c, o$ A7 J5 M- }) p
// their step rule. This has the effect of removing any
! f& D; `" n6 S2 X // systematic bias in the iteration throught the heatbug
5 {( T- I& d0 _' Q* e // list from timestep to timestep
9 F: K8 M6 Q3 \, z& P
( I# x. B- C/ s. H: y // By default, all `createActionForEach' modelActions have) X9 s6 w: _! e O2 ]) U9 t
// a default order of `Sequential', which means that the/ c; q+ c; M; n @/ D- y; z
// order of iteration through the `heatbugList' will be# Z. C4 M! p7 _3 j/ F6 X$ R8 v! ?& U
// identical (assuming the list order is not changed' B0 X8 n5 r. D+ \, G
// indirectly by some other process).2 z8 i8 N0 H. W9 k
$ [5 w9 Q P1 x+ h9 z
modelActions = new ActionGroupImpl (getZone ());
1 i4 Y7 z1 Y6 Z- t- Y5 I
1 H& I) m* N7 Y) | try {& d# [9 F. ^1 B$ |0 n" t4 R; L
modelActions.createActionTo$message
" ?+ k" h' D5 I7 T: { (heat, new Selector (heat.getClass (), "stepRule", false));
% L+ q) r4 f- ? } catch (Exception e) {
/ A- q# {6 \; n1 A$ u* ~ System.err.println ("Exception stepRule: " + e.getMessage ());
& p6 m" r4 n# _3 e4 _- \* G3 h( K! | }
* ~ }* z. C+ B* y9 y x* [* r( `; t" M# ^' O8 o
try {& Z8 M" W( y9 N# x% l1 L" u" B
Heatbug proto = (Heatbug) heatbugList.get (0);" b' {0 H+ _, c% D& E$ H5 B
Selector sel =
. c7 \6 t9 h2 \1 W! a W new Selector (proto.getClass (), "heatbugStep", false);4 D' B( l/ ~2 V# Y! G- t7 I
actionForEach =
! ?4 l( ?" I0 H O modelActions.createFActionForEachHomogeneous$call
, E/ f7 P6 v0 C) Y4 s( i# r+ j9 L (heatbugList,
0 M0 D8 y$ B) c" O8 ]" B5 S" j new FCallImpl (this, proto, sel,
2 ?! T2 W0 n, Z, i new FArgumentsImpl (this, sel)));* r5 ^7 M& Y# ~5 r. ]; H3 A* v2 k- i
} catch (Exception e) {2 U/ W6 x, x1 v) F! N3 I
e.printStackTrace (System.err);
* e- c" x$ \. @ o" v }
9 F m- l, y4 f 9 s5 Y7 @/ I: N' v& Y* p
syncUpdateOrder ();8 o: t, j: S: w
2 ^5 ]: q6 z+ m/ r try {
/ F+ @" g+ A; M- D/ [0 H4 M modelActions.createActionTo$message
0 F4 m y& v+ _ (heat, new Selector (heat.getClass (), "updateLattice", false));
. s( f8 l0 I! V2 I } catch (Exception e) {
" }% z, p' ^$ V2 ` System.err.println("Exception updateLattice: " + e.getMessage ());+ Y5 W5 |0 G6 P+ u
}
3 E; L0 V! v4 Z " @7 v; B3 K( M# e6 t! U: s- {1 ]
// Then we create a schedule that executes the: }4 V, c. v# t
// modelActions. modelActions is an ActionGroup, by itself it
; p! E, I7 C# i1 S4 K9 x // has no notion of time. In order to have it executed in
2 \1 z! L1 T3 h0 y+ s // time, we create a Schedule that says to use the
9 i5 j; h0 b7 c, f1 E; H // modelActions ActionGroup at particular times. This
, q3 J) O; Q6 \* A // schedule has a repeat interval of 1, it will loop every( ]. s1 c B& H
// time step. The action is executed at time 0 relative to: x3 P% N6 ]7 h* u( w) I6 ], d
// the beginning of the loop.4 w8 M7 c. D4 _; p/ Z, p
9 j1 `0 z" o; r9 C+ n* `) U O K7 X // This is a simple schedule, with only one action that is- j# P. m: C6 F8 |# d
// just repeated every time. See jmousetrap for more. T; a/ Y1 i; E! |6 T& F0 \
// complicated schedules." X& z* x. T6 J1 }7 _- m+ m
" L! X: V) [. `" z% H modelSchedule = new ScheduleImpl (getZone (), 1);( d. w! a8 ^# I3 h
modelSchedule.at$createAction (0, modelActions);
D0 I; G/ h2 u- X' v* U 5 T4 B$ F$ ]2 j3 {! W& g) c
return this;
: v8 {/ B$ g8 `% \2 o! q5 @ } |