HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 x/ w" {. g. r- Y8 z: Z7 J4 f- P3 Y- @
public Object buildActions () {6 h- A/ b6 _) K B) v
super.buildActions();
8 _# p3 l+ u- l& q l3 L
# `1 m3 N7 Y- {# d C* h // Create the list of simulation actions. We put these in
, ~8 ^) H5 a( P // an action group, because we want these actions to be
. Y4 o! G0 k: ^8 O# K0 M // executed in a specific order, but these steps should( B+ e7 J% B) }% a8 Q" y
// take no (simulated) time. The M(foo) means "The message" ~9 G0 s3 z$ e# u
// called <foo>". You can send a message To a particular
/ i) F o4 z/ L, a' I0 M // object, or ForEach object in a collection.
# A* f) Q9 \- j/ r N) \ ' j4 V, ?$ P3 ~) C* A5 N
// Note we update the heatspace in two phases: first run" R, ^; A" E1 c3 f5 J
// diffusion, then run "updateWorld" to actually enact the4 U6 n! j4 Q% \, ]) B2 N& [
// changes the heatbugs have made. The ordering here is
2 k( j- z0 ~/ y9 b- Y // significant!
' h6 S5 g4 o {& }8 T
# a* `& y' k; U // Note also, that with the additional4 @/ Y: a% e: |5 W; C4 v9 H+ ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ X( U2 i* D! k5 E1 y# h& {. j7 s) ~ // randomize the order in which the bugs actually run+ e+ O0 Q) h0 @ c# C: ^
// their step rule. This has the effect of removing any. l5 O7 l; }, |% t$ ~
// systematic bias in the iteration throught the heatbug% |: E, q* i- M4 m8 m7 L* Y+ z% c
// list from timestep to timestep
- i. H2 H' g' Z. C# C/ b
7 P# Q1 L# [7 ~0 G8 E& B2 t // By default, all `createActionForEach' modelActions have5 S4 y4 Q$ N1 @4 a! ~
// a default order of `Sequential', which means that the9 p% K5 p# R) y; |
// order of iteration through the `heatbugList' will be
$ j" }" @' _7 k! z // identical (assuming the list order is not changed
# y' d* e: }: Y7 G; ~ // indirectly by some other process).
: S2 A, N0 c' _5 c 1 F; _6 L q8 ?' s
modelActions = new ActionGroupImpl (getZone ());
) {4 a- Z6 ?5 L+ H* W
- ]8 A% B# \0 N5 U# x1 _0 j try {$ g2 X* [, z, e' v) y& f; S9 j
modelActions.createActionTo$message
# W; g( X T3 F- n6 I% j; s1 X0 J (heat, new Selector (heat.getClass (), "stepRule", false));( Z! `8 a/ B" a) E6 h6 B7 N
} catch (Exception e) {9 j% F0 N" ~- n0 w2 M2 n
System.err.println ("Exception stepRule: " + e.getMessage ());
, b& t9 `1 O1 C0 I: } }' f; e- n U1 R% u! U D
9 q! Y) |- n; y& b try {& m- }, x1 i: g1 H0 I. y
Heatbug proto = (Heatbug) heatbugList.get (0);
4 l* H/ c+ n* p/ c0 J Selector sel = 5 }; @- |, i7 |1 E/ i1 w$ q, d
new Selector (proto.getClass (), "heatbugStep", false);8 h7 S( Z8 w6 z3 g% ]2 I( A
actionForEach =
7 [; i1 E- j, s6 U' X modelActions.createFActionForEachHomogeneous$call
7 X# W1 B& W. c8 Q (heatbugList,; ] h$ _& {7 J$ n
new FCallImpl (this, proto, sel,
' v, s! Z+ u# Q% K/ H new FArgumentsImpl (this, sel)));* Q, {, y: z! ]/ o7 |' P
} catch (Exception e) {! m, ^& |, X: ~( M- D
e.printStackTrace (System.err);
, |* w6 p( E3 g+ C }
( C6 Q( ^3 e, P9 C- E
/ T& ?& c p, d7 v9 \ syncUpdateOrder ();3 \8 i0 G) z3 p6 V
6 Y3 X, `; C# X% f9 F2 n1 Q try {/ ~" @" @2 i6 [ @6 K
modelActions.createActionTo$message 4 r/ b6 e n- B8 j5 R. N& O% U
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 x1 \, Y6 L& m } catch (Exception e) {9 _* W/ \' Q3 i3 s$ A
System.err.println("Exception updateLattice: " + e.getMessage ());4 K1 o) l7 C$ L+ S
} @8 L9 l/ q# Q2 i2 _9 t
! |, m0 A9 e- Y3 z3 W$ \" B, R4 A
// Then we create a schedule that executes the. U6 Q6 V/ f1 P0 }4 w" o) j2 M
// modelActions. modelActions is an ActionGroup, by itself it7 N- M. ]" j3 [3 Y
// has no notion of time. In order to have it executed in% q7 i6 K2 Y# ^1 l) b7 w5 H l
// time, we create a Schedule that says to use the% _8 {4 F$ {2 A+ _
// modelActions ActionGroup at particular times. This; {) l8 A& o4 h* U& X7 J
// schedule has a repeat interval of 1, it will loop every
5 l7 J' P) L+ {7 U7 h& ` // time step. The action is executed at time 0 relative to" P0 q9 w1 t c9 C- B a2 y
// the beginning of the loop.
7 H& o5 i3 b8 B/ j) a1 Y) H3 @3 z. J; |$ H
// This is a simple schedule, with only one action that is3 d- B- g: m r3 G8 V1 R
// just repeated every time. See jmousetrap for more
# A* k. w; e# b/ o* L. N // complicated schedules. q4 w `9 X8 U! C, O, `
. W/ Z C. E$ T) N1 E1 q
modelSchedule = new ScheduleImpl (getZone (), 1);
2 l# N: }) `( O n0 ~' } modelSchedule.at$createAction (0, modelActions);: h2 w% U1 m6 D7 A9 K
! d1 [( p# t* Z+ Z" F return this;. \6 B L8 r, o- p* u: b. O
} |