HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 p9 A6 B6 S8 F) _* v
- K6 ?) T/ B1 n j1 ^3 j4 | public Object buildActions () {
4 P; d' S4 z! ~5 w" } super.buildActions();
& o3 s* r; j0 p# M# r: Z; ]
( O, d/ |+ B& | F // Create the list of simulation actions. We put these in
9 P1 T; M* z! p N, Z" ? // an action group, because we want these actions to be
# w" u3 ^- ?% R$ z! y, l+ f& a6 L // executed in a specific order, but these steps should9 V8 g+ V, p1 v1 m i' k- ]% B
// take no (simulated) time. The M(foo) means "The message
4 u4 R5 Y4 R, A. F" u* N // called <foo>". You can send a message To a particular
3 F* ?4 X2 z0 n2 ]3 { // object, or ForEach object in a collection.
3 m/ d1 Q; E# N5 M/ q
1 v/ e7 Y* ^( b( b // Note we update the heatspace in two phases: first run4 P6 f9 K1 Q( O2 ~. I" {
// diffusion, then run "updateWorld" to actually enact the; E! [4 }0 q4 `! ?) l
// changes the heatbugs have made. The ordering here is: _, C" ]& Y$ D* {: X
// significant!
3 ?9 Z- N( n7 S J
, c6 }9 }! H/ d, ^; t- W; ]% x // Note also, that with the additional
3 u9 F5 G+ k8 z // `randomizeHeatbugUpdateOrder' Boolean flag we can: i [* L; H8 G) H& L7 N
// randomize the order in which the bugs actually run. r9 H3 k6 }# y' H _# I: x# O
// their step rule. This has the effect of removing any1 W: ^& ~9 f' T! G2 }: d0 L* y
// systematic bias in the iteration throught the heatbug
% y" F1 X( c) W! d4 ?3 ^! v/ Y // list from timestep to timestep& Z8 u/ \ F l8 T v: W- s: T( b
0 C" K! B7 f, E: {3 @* K. ~
// By default, all `createActionForEach' modelActions have; J- ?6 J' ~: B; L5 s7 y; {. I
// a default order of `Sequential', which means that the6 d, p0 u; M- o s
// order of iteration through the `heatbugList' will be1 Z! j! H6 \7 Q
// identical (assuming the list order is not changed
& t9 X( W/ }: m! n5 I' `4 T // indirectly by some other process).
0 F2 L0 g* `# u" m5 ]7 u7 P $ C2 k) A- X. e, b# B* ~1 Y% M: `. f
modelActions = new ActionGroupImpl (getZone ());
6 ~/ z; _3 H) v( K- O; s! I0 }) Z: D- Y" W+ i7 q
try {+ Z0 e4 l# u0 B) a1 B
modelActions.createActionTo$message
% a& V1 w ?6 o0 E/ m7 _ (heat, new Selector (heat.getClass (), "stepRule", false));# F# }' z8 s4 q1 |& Q! E/ y' y- T
} catch (Exception e) {3 o5 Q# l7 V0 J* L
System.err.println ("Exception stepRule: " + e.getMessage ());
# P# F0 S/ y9 x4 p& ` }" x/ q2 c6 p( N
* \: K4 V/ s+ Z/ u+ M/ r2 H
try {2 a4 l% Y6 i4 m* u" |% }
Heatbug proto = (Heatbug) heatbugList.get (0);8 t. s1 c% g, y5 C
Selector sel = 0 u; x( t$ f/ C+ b' k# U: ^ u
new Selector (proto.getClass (), "heatbugStep", false);
3 o& ~3 q/ n3 m5 p" F' _ k; V actionForEach =
2 U1 y* C: ~$ R7 ~& u modelActions.createFActionForEachHomogeneous$call
6 {: x3 p$ T6 J+ k3 e7 N (heatbugList,
/ R& d& T7 S H new FCallImpl (this, proto, sel,) N1 ?1 P% m- u& i
new FArgumentsImpl (this, sel))); ]% W& a3 m' N" B) V- A
} catch (Exception e) {; y4 @. U# Q: b& t H( n
e.printStackTrace (System.err);
% i3 K* I; w6 S7 J/ n }* b3 w& ]- L# p
3 x- ~7 z% ]! T# X$ a
syncUpdateOrder ();2 v; I, e% C' U6 \: H
3 j* C% j2 Y) I, J9 N& p$ A try {
0 s. e+ |$ W$ |& x( g) V modelActions.createActionTo$message + n0 g9 S* n M) {
(heat, new Selector (heat.getClass (), "updateLattice", false));0 Y% p) H. d2 Y h! n
} catch (Exception e) {9 [5 D/ z$ `& i% i0 `8 [* H7 w# \
System.err.println("Exception updateLattice: " + e.getMessage ());4 [3 v: y0 _7 z0 }
}
, N: D3 i2 Z4 \8 H' c3 T ! Z# A* a! k1 Z# D9 {4 c
// Then we create a schedule that executes the2 w. R& C6 d- y3 i% }1 Y
// modelActions. modelActions is an ActionGroup, by itself it
% m, I: h) R1 `4 r! Y. v // has no notion of time. In order to have it executed in# U; j- t+ a2 p5 d$ s5 e2 s
// time, we create a Schedule that says to use the( j3 z- n; n0 o7 W
// modelActions ActionGroup at particular times. This( B8 O2 x/ _ r/ s6 {) a) I
// schedule has a repeat interval of 1, it will loop every( H O! q7 D- \; t: Q, W3 e: w7 I
// time step. The action is executed at time 0 relative to9 l- d$ H) G9 ]4 I# U5 ?
// the beginning of the loop.
; c8 x% W/ h, Q; h+ C+ Y2 t
3 \" }$ r) Y& G% z0 c. r3 i // This is a simple schedule, with only one action that is F" K- q" u, f
// just repeated every time. See jmousetrap for more
* a" Y# p. s! g4 r N. }; h // complicated schedules.# u2 u" z+ T( W% r8 Q/ e* m
. e$ Q! A. u8 X' x O! H modelSchedule = new ScheduleImpl (getZone (), 1);
* M8 J* D+ H# r7 f; S: W modelSchedule.at$createAction (0, modelActions);
5 b' v4 o/ D4 N+ u, z$ V0 [5 C ) ?% M6 R0 M: B4 J+ B
return this;
: G: y: G7 @4 ]8 V# s' }: W } |