HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( F" w+ W# }8 a2 T
. g6 `1 J+ V* a7 C public Object buildActions () {
: O* k. q: T% Z* d super.buildActions();; m! Q9 g# M+ X g2 K5 J+ b
! T% v) s# {4 D" z0 u, P // Create the list of simulation actions. We put these in8 U* h. q4 B4 K8 N; U/ ]/ D
// an action group, because we want these actions to be
4 B+ ]5 j7 }1 t* K; o q // executed in a specific order, but these steps should1 ]0 O, Q' y V! m# j
// take no (simulated) time. The M(foo) means "The message8 e9 v( a$ u2 W
// called <foo>". You can send a message To a particular% {- _: z9 R# ^- l
// object, or ForEach object in a collection.& y6 Y! n$ v) {. f: C1 @2 Z; t2 s
5 C2 p9 [5 W8 z // Note we update the heatspace in two phases: first run
+ B* r) F- z6 N // diffusion, then run "updateWorld" to actually enact the+ N- T# b1 \, P( u4 ]0 H
// changes the heatbugs have made. The ordering here is I" i0 `* {6 \ N3 ~* T/ X( ^& ~
// significant!; d. i! ^, W5 p' e/ q E: b. t) w
" _3 I/ }& @$ i5 ]
// Note also, that with the additional
7 G% m& s8 N0 ~, s7 Y# [& z // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 {; z9 w% e# x; \* Q2 [7 G // randomize the order in which the bugs actually run
; e* J/ ?/ C7 N- z% O2 [+ w // their step rule. This has the effect of removing any
: H" k4 A! X" ]0 T& g7 \7 M0 b% y // systematic bias in the iteration throught the heatbug
7 b9 ?) w3 f! U8 Q7 t // list from timestep to timestep
W5 e+ F7 V4 V. L
# b8 o+ k! q* ] // By default, all `createActionForEach' modelActions have
0 ~- i; N) o$ k; t) d8 Y/ H) r // a default order of `Sequential', which means that the: ^8 h# d( a9 c* r4 @0 X
// order of iteration through the `heatbugList' will be
$ R: J1 I$ E% j2 {0 ? // identical (assuming the list order is not changed
* A8 L- z- R2 j7 T7 h // indirectly by some other process). \# h" e6 y1 ]; c. J+ D: a' M$ L& R
# D& x- D( M h! f* l9 |/ B
modelActions = new ActionGroupImpl (getZone ());% F& z9 R+ S$ a4 M8 K3 g# ~/ H8 n4 J3 l
* V) i0 M( a, `0 J
try {
# J/ C& B" r# d; p5 c( l modelActions.createActionTo$message) b* X0 F( l+ h/ \9 E# k% E' n
(heat, new Selector (heat.getClass (), "stepRule", false));: C. z( q: R& y
} catch (Exception e) {
6 N& U* h, L2 \& z+ J System.err.println ("Exception stepRule: " + e.getMessage ());
' P% s" d: D5 \6 {7 U }
/ y# p% J4 N) T4 D
: ?2 Q. l5 V3 l! J try {/ P8 m( t4 z- J. p( E9 R/ c
Heatbug proto = (Heatbug) heatbugList.get (0);$ B; D% q! Z0 _0 E U! S
Selector sel = % d- `& l% w/ [7 }" m. R7 `2 ?* c
new Selector (proto.getClass (), "heatbugStep", false);! k8 J) t# j9 e. Z, N4 P2 \
actionForEach =; ^% d) H* x. N1 {2 C9 x
modelActions.createFActionForEachHomogeneous$call
" o: y. J: _3 U5 {0 s3 n" _ (heatbugList,- m2 F2 t/ L: S
new FCallImpl (this, proto, sel,
7 q8 b5 C9 [8 o2 Y new FArgumentsImpl (this, sel)));
2 A& A; ~5 r' F! R/ d, V* l# k9 c1 w } catch (Exception e) {
4 i2 X E$ D& @' _4 J! q e.printStackTrace (System.err);
9 N+ t& E' g, P& B, i1 b6 E }6 ~# P2 K, y# u. z3 m
* Z4 H. f0 |7 _7 f
syncUpdateOrder ();5 }3 C! R3 z, O/ X5 d. B" }
) ~, I3 k" G9 c+ Z" f* K try {; x, J) n6 a. l: @6 J5 _1 s; ^* t
modelActions.createActionTo$message
! ^. a$ P7 @. j (heat, new Selector (heat.getClass (), "updateLattice", false));. S5 \! ?( f z- B; b
} catch (Exception e) {" s- G" Q; Y1 r3 ~3 u& d
System.err.println("Exception updateLattice: " + e.getMessage ());5 n+ @ n% j4 G4 U- Z: V
}% _$ B+ L( n& o: V( J" m! [
5 F5 I9 S7 K/ J9 Z2 a! h. f) |
// Then we create a schedule that executes the
6 \1 p4 ~9 h, x# Q. j // modelActions. modelActions is an ActionGroup, by itself it
: z" k3 Z) q0 U // has no notion of time. In order to have it executed in# w9 \8 ?* h- q
// time, we create a Schedule that says to use the& N7 o7 o/ l3 {4 o1 w; g x
// modelActions ActionGroup at particular times. This
3 B' G+ s, n7 I // schedule has a repeat interval of 1, it will loop every
6 @& I" P L$ @6 |/ ], J+ J // time step. The action is executed at time 0 relative to0 q) O/ t7 l5 _* h4 E
// the beginning of the loop.9 u! j2 e* z9 B" v8 |8 |1 J7 E! [: M, M
7 O8 M4 g' n7 K* g8 X* I // This is a simple schedule, with only one action that is2 b- i( t( C, k) c
// just repeated every time. See jmousetrap for more
# \* n" i8 P" N _7 W- ] // complicated schedules.
+ L6 a) W4 i! _7 L- E 9 H3 P2 A1 E8 Y" _" z$ r) I! s8 b
modelSchedule = new ScheduleImpl (getZone (), 1);
$ k# s& A" T2 Z! W! J modelSchedule.at$createAction (0, modelActions);
8 t8 T- N1 O8 H" B
5 q6 _# q& c8 U- } return this;% B3 v4 Q& k( d" Y" r8 L/ b
} |