HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' ]2 U7 l3 |7 ~ K# U
8 B/ `) A* d' f: I) `3 b public Object buildActions () {
9 j% }$ ~) y& A! O super.buildActions();
0 F3 o, E2 J& P
8 d1 h2 v) Z) j/ a5 O" \; A // Create the list of simulation actions. We put these in, Y/ X% M" g0 w. U
// an action group, because we want these actions to be+ |3 F4 y: B* X. ?7 [- I
// executed in a specific order, but these steps should- F* Z9 i6 j( E" K, e
// take no (simulated) time. The M(foo) means "The message
0 g& d9 |$ A3 V& v6 F // called <foo>". You can send a message To a particular+ }) s2 N5 w0 v1 p! B; [
// object, or ForEach object in a collection.
; |- Z! C; t3 I) n2 |
+ r& [/ N ?- K* x! y" f // Note we update the heatspace in two phases: first run
# A" Z. L5 B9 G! k# |% o" M // diffusion, then run "updateWorld" to actually enact the w' P6 t/ h5 A/ P- \) P# s
// changes the heatbugs have made. The ordering here is% `/ {# b1 \1 ~, B1 Y4 s
// significant!+ Y A+ N7 Q' L8 H* O2 g& {- ?1 M
3 J1 z1 e, ^( ^1 a // Note also, that with the additional8 {, Z1 S4 D8 `9 t$ ~ C# H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 P9 u; s) l6 q& O1 t; b0 ~5 e% _ // randomize the order in which the bugs actually run G# Y9 L4 O& I$ i, Z- T% q
// their step rule. This has the effect of removing any2 \+ t$ M8 Q6 B2 U1 N# G
// systematic bias in the iteration throught the heatbug, F8 w* Q% H2 k; }* Q8 O
// list from timestep to timestep/ r, [3 j+ ~" ~, g- H
* U" }1 `. d% _+ c; I // By default, all `createActionForEach' modelActions have3 F! r8 f# x' m# }
// a default order of `Sequential', which means that the' \4 G: X) H6 \8 g/ I
// order of iteration through the `heatbugList' will be. y# V- F* Q; \* _
// identical (assuming the list order is not changed
. @' c0 M. D& A" u // indirectly by some other process).
- g/ n. s% [% f5 ` L 2 e M; @8 Z& E
modelActions = new ActionGroupImpl (getZone ());
7 X; o! x3 k" c3 Q H& t
( {3 F6 }( U: c try {2 [) ]/ \5 L9 R; A$ U9 d* L Q
modelActions.createActionTo$message
# f' G t2 r7 P7 ~/ J5 ~7 X (heat, new Selector (heat.getClass (), "stepRule", false));/ ^: s- g- I- K
} catch (Exception e) {, R! w0 F ^6 ~: x
System.err.println ("Exception stepRule: " + e.getMessage ());
! r1 A8 w7 I+ s" w }
: E. U+ G$ p3 T5 z4 W9 ^
2 d9 D8 u9 D. k! ]' u9 | try {
' p9 D+ X: ~/ {$ n* L$ A9 I2 x4 p Heatbug proto = (Heatbug) heatbugList.get (0);
# }0 \0 ]1 _! w3 } Selector sel = - y6 v' @+ j% u* L- Q! }
new Selector (proto.getClass (), "heatbugStep", false);
7 {7 D1 Y& q, S actionForEach =5 m; w3 N' W& n, Q
modelActions.createFActionForEachHomogeneous$call" K% ~4 J/ f, S T
(heatbugList,. S+ C0 f% D8 p: T. h1 r @
new FCallImpl (this, proto, sel,
& E, O2 O5 p! [! L new FArgumentsImpl (this, sel))); a% P, W- d7 h
} catch (Exception e) {
( L; b- T, @2 X+ L7 c& r# W e.printStackTrace (System.err);
5 t# {8 o: b8 \* @* ? }
0 }7 _) N2 W" F" [( J# z' S
# C% g. \7 B+ S) R* m syncUpdateOrder ();
) I2 P7 l1 H8 z6 ]
: A4 P6 r6 m* t try {& ~8 a8 v. ^2 o6 H# l, Q
modelActions.createActionTo$message 8 c/ [! M+ N/ C: Q4 a6 V
(heat, new Selector (heat.getClass (), "updateLattice", false));' n! D8 D Q# i7 J0 ^0 e
} catch (Exception e) {0 v4 Y) c0 C) j$ d% z4 b, k! ` f
System.err.println("Exception updateLattice: " + e.getMessage ());, N3 N' a" u0 ^& K
}0 P2 ?$ z" b; _0 H0 h" X4 c
( K' _' `+ V% N% P
// Then we create a schedule that executes the
( i$ ?; K, M& p& g! r0 X; _ // modelActions. modelActions is an ActionGroup, by itself it
3 R: j8 ]# l# X+ E& @1 g' B // has no notion of time. In order to have it executed in. C7 F: |* B1 t1 i, }- _* `3 b
// time, we create a Schedule that says to use the
) M# G# D* M" C- c; v/ z; i // modelActions ActionGroup at particular times. This, f1 I: D( J! @2 ^$ ?' P D
// schedule has a repeat interval of 1, it will loop every, }" ^! l' l3 }6 E- ^
// time step. The action is executed at time 0 relative to$ O' E% {6 a" O* z. ~: A5 [
// the beginning of the loop.
6 T4 R( }% i( _" h4 U
. x; c3 W$ n$ Q, S2 o4 D // This is a simple schedule, with only one action that is! u: s/ C9 j1 O, F) U1 x( l% W7 J6 X
// just repeated every time. See jmousetrap for more
( m2 E9 [; p* Q J, | // complicated schedules.
9 z7 W) u! M3 B+ Y
& Z% j2 {2 A. |) ~+ {3 w/ u modelSchedule = new ScheduleImpl (getZone (), 1);# V+ j. @5 I# _" _& O* h7 @, ~
modelSchedule.at$createAction (0, modelActions);% [6 [5 P) g+ Z4 u1 e3 F
& a d4 L0 c- v0 t, E+ [
return this;' @; w9 y6 K, h* y# L' w7 `
} |