HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 ~/ i/ Q' f) t* X" }
; u4 s7 V. n! u# [8 N
public Object buildActions () {
4 ?! u9 T( J$ Q0 J5 v super.buildActions();
, x- c' z/ t' Q# i: ^2 ^! S- I 7 n) p. d- Z2 v5 f9 H F# T" _
// Create the list of simulation actions. We put these in
- J+ G, q; D- p2 `' k // an action group, because we want these actions to be" M. O. n" W" }! Q9 W+ Z* u9 q
// executed in a specific order, but these steps should$ ~/ @' ?& c- R4 V
// take no (simulated) time. The M(foo) means "The message
( ]1 { ^; l/ A* j9 V // called <foo>". You can send a message To a particular
( @# i$ R- g& p$ n* O' s // object, or ForEach object in a collection.2 D/ N3 ^4 _+ E I
" U% u' R( I$ n" M$ ~0 U/ ]& |) F
// Note we update the heatspace in two phases: first run
8 v- q1 _, y2 y3 b2 t* Y& T // diffusion, then run "updateWorld" to actually enact the
$ H- |9 i7 g/ C e5 C) o0 Y% Z& s: l // changes the heatbugs have made. The ordering here is! ~9 C+ H1 f; F/ C6 t6 g& y
// significant!3 S- A3 t$ H$ m4 U
3 E' G% Y' \/ o% u6 p5 | // Note also, that with the additional
: x, b+ ^( t! O9 Y# T4 A // `randomizeHeatbugUpdateOrder' Boolean flag we can8 K! [& m: y) j" x2 P' Y* S& r
// randomize the order in which the bugs actually run
! _& p" @7 m0 i0 F/ B$ J // their step rule. This has the effect of removing any
) d1 z5 f! A' i) y // systematic bias in the iteration throught the heatbug5 I8 f& ^* U$ J* `( \# _
// list from timestep to timestep
+ r+ _: h* S7 ~3 s7 ?, p
8 j. o2 V# P7 Z1 n2 g6 ^. Z' E // By default, all `createActionForEach' modelActions have
4 t$ L9 a- K. {$ s // a default order of `Sequential', which means that the
: |2 U: @- ~, O3 b& y" j // order of iteration through the `heatbugList' will be
8 |9 }% M( Y" V$ C // identical (assuming the list order is not changed
( x; R+ S P. }# p e/ F0 c# y0 R // indirectly by some other process).
% C o" K% z% z c1 c& ~
g6 N6 t; o& y' C0 E modelActions = new ActionGroupImpl (getZone ());/ E4 v+ }$ ?" n$ r
# ~! i! b" a5 S7 T. ?: Z8 s9 q
try {
- P) e& {. D4 O8 c2 i9 V6 ^, z7 B modelActions.createActionTo$message
* r+ q% P1 D; i (heat, new Selector (heat.getClass (), "stepRule", false));- q+ b# G" [. ?( [. ~; k6 l
} catch (Exception e) {
% c! V4 v+ ^' ^% n/ y: m System.err.println ("Exception stepRule: " + e.getMessage ());
) |. x4 C: ?' b1 T }" F5 d, I& L A( g3 \6 ?
: ~& f# q2 J* z8 p# O5 r8 S5 ^6 U try {
, ?$ I8 p; X$ r9 W Heatbug proto = (Heatbug) heatbugList.get (0);
2 r6 @7 m: Z0 W: o8 `/ p/ I$ g Selector sel = & L" ?% q# b' p }4 Q% u
new Selector (proto.getClass (), "heatbugStep", false);
* s2 E5 J- S. p' D( |- I( J actionForEach =* L, G$ h2 \/ d3 E3 f# q
modelActions.createFActionForEachHomogeneous$call
; H0 _+ C% G5 W9 y4 a! ~( K1 | (heatbugList,7 u& \6 b! ~- j- h& H
new FCallImpl (this, proto, sel,/ [7 e& F1 _- }2 m
new FArgumentsImpl (this, sel)));( q& ]: K" s. @
} catch (Exception e) {' G; P6 c7 M% d% @! F7 z
e.printStackTrace (System.err);
' W/ p. X9 K4 [" u }
" q. p% N, h7 \ ) d1 H8 k0 q! P$ c; S' p& _
syncUpdateOrder ();" k: h6 L! v# W0 ]" g, I
) a F1 I8 k- o& M3 x
try {
7 l3 \- \' s; v3 U& V" U0 r, v modelActions.createActionTo$message
: ]! c6 ]7 _, o: Q0 L- j V+ _& P (heat, new Selector (heat.getClass (), "updateLattice", false));4 u1 }: @) E/ L
} catch (Exception e) {* w. D4 `! V6 @/ C. R
System.err.println("Exception updateLattice: " + e.getMessage ());3 m* ?( P. c) V8 c6 r
}& y4 m" m0 w2 A1 ?2 g' I/ u5 D
; [; d, O/ g6 |% }+ [) G# x // Then we create a schedule that executes the2 O9 G" {2 n* c8 O
// modelActions. modelActions is an ActionGroup, by itself it
7 Y9 k" {, ]# {2 t7 [. O- S( v // has no notion of time. In order to have it executed in
. n7 U: r8 N" u @ // time, we create a Schedule that says to use the/ S. X& l2 \5 c; S m1 {# z/ p4 W
// modelActions ActionGroup at particular times. This
, {1 I1 }" b: |' n3 i // schedule has a repeat interval of 1, it will loop every
+ K( C4 g, k3 b# [# C" Z: ^; c$ f // time step. The action is executed at time 0 relative to
* Q% t1 X) V Q6 \5 I: E // the beginning of the loop.
I+ s0 m2 `6 {" V; m) }4 x' \$ h' v. m- C' ?
// This is a simple schedule, with only one action that is
8 H* `7 \ d$ t- R& Q- W$ q- [ // just repeated every time. See jmousetrap for more& [2 V6 r2 X7 f1 c, [
// complicated schedules.
* ^& Z' h- P7 r; w% M
) N/ t: ^' H8 \ modelSchedule = new ScheduleImpl (getZone (), 1);9 O) p% s0 @, {! }
modelSchedule.at$createAction (0, modelActions);- m$ Q/ v/ T: L
7 U% C- o3 _2 c ]
return this;3 @1 v' _$ y9 j- C: O1 e5 ^ e
} |