HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 k. `: K) W( l. j8 B
' V% p# C7 N) z. ~- } public Object buildActions () {) f: R' v) s1 x" _- V* }
super.buildActions();
/ Y4 \6 f5 I Q; l
9 z0 @/ F3 A! o' H Z3 }; G& h+ P // Create the list of simulation actions. We put these in/ R9 ?* k2 j& y4 A
// an action group, because we want these actions to be) h* O+ O8 l( Y" K+ M: R2 K
// executed in a specific order, but these steps should
+ `$ ], s# I( W0 L7 L+ @' U# { // take no (simulated) time. The M(foo) means "The message
. C# I, j) ]# ? // called <foo>". You can send a message To a particular4 ^8 n- r1 h8 B& E, y
// object, or ForEach object in a collection.
1 E; f M( k b6 k/ {
! X, ?4 i6 Q" } z, R // Note we update the heatspace in two phases: first run* t0 _& {2 D i0 s. Q
// diffusion, then run "updateWorld" to actually enact the% x; ?' U( @" S: g; B
// changes the heatbugs have made. The ordering here is
; d6 Q; Y8 C2 H s9 n5 A // significant!( v2 R! I* Y+ m4 |: B! `3 @
5 L: T+ H& q3 ?. K$ V( k- b
// Note also, that with the additional
i( y3 A/ b7 B% | J. g // `randomizeHeatbugUpdateOrder' Boolean flag we can$ b' `, {. C+ K- L: y5 g9 ~
// randomize the order in which the bugs actually run
3 ^; |: K& s/ f1 Y+ X+ B$ E4 I // their step rule. This has the effect of removing any
/ h( a: j6 U: u, P- `8 e8 O7 P' Q // systematic bias in the iteration throught the heatbug
# C- ~9 Z! E) {( W/ S1 n" G6 }; E // list from timestep to timestep# |6 b0 L9 }7 M) V: w v% F" ~
! x* v2 d' c& r
// By default, all `createActionForEach' modelActions have/ I4 r2 W. i+ S# i
// a default order of `Sequential', which means that the% X( U* B3 i% Y: n7 j! }
// order of iteration through the `heatbugList' will be7 p3 m* W [& v J% D6 E+ g r
// identical (assuming the list order is not changed
: g$ ?% w$ I6 L+ ^6 ~ // indirectly by some other process).# }' l0 L1 f9 H B4 f
3 R \+ a" O, e$ C4 C" A: O. ^& E modelActions = new ActionGroupImpl (getZone ());% {6 ^9 S0 U* x- ^! k( i- @
/ X5 ]4 d N# @ try {
( E5 z9 G6 O* l2 r9 u# ~: o# k- a modelActions.createActionTo$message% b5 ^$ x- N# O) a' B! c2 }- I
(heat, new Selector (heat.getClass (), "stepRule", false));# l+ M* \4 l2 x- u j# p3 Q. ~- x! h
} catch (Exception e) {7 {6 I$ {/ I C9 V
System.err.println ("Exception stepRule: " + e.getMessage ());
1 r: q) S) A8 ?% N8 Q5 z6 B }0 Q" {" \8 M' z: z) i- s
' O+ ?! X6 Y8 I. F" w
try {
+ o5 J3 C5 i/ b$ z' K1 O5 n" Q, {) d Heatbug proto = (Heatbug) heatbugList.get (0);( a& ]4 z0 l! _) p* [
Selector sel =
7 W& w. _* d/ T s3 j( e new Selector (proto.getClass (), "heatbugStep", false);# w! \$ ]- Z N0 [' Y
actionForEach =
. s' l$ z2 g# s+ v2 t modelActions.createFActionForEachHomogeneous$call" e# y) J6 @7 n! }6 N; j( Y5 u/ o
(heatbugList,
2 y( j( a. O: V* Y5 Z/ l+ K new FCallImpl (this, proto, sel,* }1 B9 F- c a' T+ ]3 W
new FArgumentsImpl (this, sel)));
' S$ G. m( L' D( V8 F } catch (Exception e) {
, P2 b, B# Q4 j' Q: r e.printStackTrace (System.err);
% \5 j0 ?3 O8 f { }
/ y4 r3 i; v- q" t" N, |, ~
+ D' g; {/ ?: D, W6 z Z9 J syncUpdateOrder ();' Z5 h/ U: e* B3 i) Z$ ?
1 O+ x2 p4 _1 }7 }) |$ x! c
try {/ Z7 u, R! \8 Z. [% O" W
modelActions.createActionTo$message
, j7 m) Z" Z M' [% N& K (heat, new Selector (heat.getClass (), "updateLattice", false));; @& f; O! | {0 u5 g$ N5 X$ i
} catch (Exception e) {3 A' T& U/ \. d' n' Q+ M4 l* M
System.err.println("Exception updateLattice: " + e.getMessage ());. x7 i1 z. k3 ?
}
, Q! y1 e* M3 Z * |4 O5 k$ M) n) \. u& I. M5 m
// Then we create a schedule that executes the
' A3 n1 k. Y$ E$ K$ J; ?2 W // modelActions. modelActions is an ActionGroup, by itself it/ U% Z1 d6 b9 b* t* P U2 k+ u
// has no notion of time. In order to have it executed in2 ?! [" N4 O# _5 G( c, J
// time, we create a Schedule that says to use the6 ], ^; [. A0 R# b4 W2 h" U2 a% q
// modelActions ActionGroup at particular times. This4 h; |# ~* {; L+ U8 p- u
// schedule has a repeat interval of 1, it will loop every
- ^6 b2 n3 r& ]. J; g // time step. The action is executed at time 0 relative to- O! F( @1 I J7 ~9 b! x' b; B0 Z, @
// the beginning of the loop.
9 i6 n; d: k* i+ }6 f6 g1 B& O# t. Q
// This is a simple schedule, with only one action that is o l/ l: G* g- h$ w2 g2 g
// just repeated every time. See jmousetrap for more) I2 P% o! a! F. i( \, m$ l. P
// complicated schedules.
3 h. B6 [" I: W) \5 B$ ?) W
" K* O& d% s+ c modelSchedule = new ScheduleImpl (getZone (), 1);0 S, p6 g( V: v' H( [
modelSchedule.at$createAction (0, modelActions);) C/ A1 l$ o$ K/ ]7 P! ]) o7 f
6 b0 @# J9 R& ~8 |# a% ?! N, r* i return this;
" u$ t4 v5 q/ U } |