HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# I6 v4 Q6 e5 n& m0 y: i" \! u9 J. C; Y1 M
public Object buildActions () {' l/ a" m: c. e9 h# F; ]
super.buildActions();
5 m& m* p' N) j# `: h s8 X# q! v 0 o* b# i z! P6 _8 @! w
// Create the list of simulation actions. We put these in" D4 i1 `# z, G' b4 m
// an action group, because we want these actions to be& I9 N2 r$ c( B; |3 g( C
// executed in a specific order, but these steps should8 R: ~2 s0 V! Q+ W3 J
// take no (simulated) time. The M(foo) means "The message
, G$ E0 L8 Z6 P |; b2 z+ j3 T // called <foo>". You can send a message To a particular
+ m. K" {6 @4 {; V$ t% m // object, or ForEach object in a collection.; I( a2 S+ d* w
0 U1 ~! [# N. Z7 y
// Note we update the heatspace in two phases: first run
" \2 s9 L9 J2 Z2 m0 B8 t // diffusion, then run "updateWorld" to actually enact the8 [# j* p0 f2 s2 K' z7 D
// changes the heatbugs have made. The ordering here is2 k2 B4 \( ]2 C/ v
// significant!
5 k- Y* q1 o: \
1 V, M% l* j) M& k1 ]4 L# _3 F7 N // Note also, that with the additional" J; @4 L3 @% ?* H2 l# S) L
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, Z' `1 k. Y( ?; P/ [ // randomize the order in which the bugs actually run* }0 e2 }( |" o! R0 @/ ]/ ?5 w8 G
// their step rule. This has the effect of removing any7 g3 {- n$ I- L' W9 g
// systematic bias in the iteration throught the heatbug
2 J- H& l3 g1 }8 B o3 Y8 K' B // list from timestep to timestep# {1 T1 x) |+ w0 @5 f: h
) I. C8 o _* F // By default, all `createActionForEach' modelActions have
! r* ~+ ^/ f( J& u$ ] // a default order of `Sequential', which means that the$ ]7 @0 ~) H) S, R% ?) G
// order of iteration through the `heatbugList' will be
* \3 Y+ B3 C& r8 I* f5 s+ B; F // identical (assuming the list order is not changed
( O& N' T- C: a& b) ]7 c) k6 `' F // indirectly by some other process).
7 a& s2 T( C/ J . l9 G" Z5 U. t4 D
modelActions = new ActionGroupImpl (getZone ());, T7 H2 O+ }- T* b, O; i3 Q
' F; L1 I0 C7 {( V6 A try {' }9 z/ A! ^5 C/ ~8 X
modelActions.createActionTo$message
0 F* K* r5 T+ }4 s6 U (heat, new Selector (heat.getClass (), "stepRule", false));, w0 t* n0 w& _( {( h9 X
} catch (Exception e) { c; n; ]3 R6 T, {
System.err.println ("Exception stepRule: " + e.getMessage ());
4 J- J$ f+ |1 p- v! j. d7 d2 | }
; f. c9 {2 \7 x8 s
3 R7 N- Q) h. j& U$ x2 ~6 I try {( ~& \; d |* h$ C
Heatbug proto = (Heatbug) heatbugList.get (0);( X2 o3 d+ g- ^# W8 w; G
Selector sel =
& [' k) x# {9 @1 L) F new Selector (proto.getClass (), "heatbugStep", false);
- p2 U2 D8 a/ z0 ? actionForEach =) e, M; h& n3 m7 e
modelActions.createFActionForEachHomogeneous$call: C* | ~7 U* t# k
(heatbugList,/ F) d, d [+ W
new FCallImpl (this, proto, sel,! e9 U! n* I! x
new FArgumentsImpl (this, sel)));
: X9 w' u- j @7 ? } catch (Exception e) {
0 n. Z# L7 J. x2 [' ^0 j e.printStackTrace (System.err);
! H) V% H4 k z5 K }
6 l2 X- _1 h* y: T& E9 M* y$ S % |( S1 b7 j4 @0 P, I2 g
syncUpdateOrder ();5 U& l# v& W" x1 U8 ~1 H5 L
3 b. w- @6 B0 E: B5 H( I try {
$ }" z3 C8 K6 `# @: F modelActions.createActionTo$message ; ?" @( F8 e- K, E w
(heat, new Selector (heat.getClass (), "updateLattice", false));% u$ d! P; \+ K1 D
} catch (Exception e) {5 A9 M! c- ?. T
System.err.println("Exception updateLattice: " + e.getMessage ());3 S9 D6 ]( o# c, q5 G
}
- t) b, J5 K, U * a. h' D, O. _; n4 Y; Z
// Then we create a schedule that executes the
. C7 P; n& m. \) u- a" E- G // modelActions. modelActions is an ActionGroup, by itself it
+ [# H6 u; B8 s" m // has no notion of time. In order to have it executed in6 D8 u$ F0 Q2 _2 _5 [6 \! W7 i
// time, we create a Schedule that says to use the- a$ b* g: w/ b, n0 c3 W
// modelActions ActionGroup at particular times. This' z; |6 j& J B. {9 u5 l
// schedule has a repeat interval of 1, it will loop every
! c6 s. C. X% c // time step. The action is executed at time 0 relative to
$ y) {( u1 H' l5 z" x // the beginning of the loop.' x; M$ u0 N' o/ j# c8 b+ Q
0 y$ y# I% H1 G n9 K \ ^
// This is a simple schedule, with only one action that is I) Y/ [9 J7 W1 Y
// just repeated every time. See jmousetrap for more
( ?( q1 t1 F" o0 P* M // complicated schedules.
8 m, c" i l" T2 i * F' ~- i9 h& J0 M
modelSchedule = new ScheduleImpl (getZone (), 1);
5 n d4 [0 _# [! W2 E modelSchedule.at$createAction (0, modelActions);- B- f4 J! U8 K# }( f1 O& B3 S! S
0 S8 d1 j! v' y1 e) S" s4 f& T return this; p2 } g* n5 ^
} |