HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ I, P6 d3 k( x+ f; A6 n) V, T- l4 b
public Object buildActions () {/ f! i5 Q. U4 S9 B" e' @: y/ [8 }
super.buildActions();
) `- [3 T: r& \4 X) ]
" H) s8 E9 n5 @ U2 ?2 T* { v$ Y: [ // Create the list of simulation actions. We put these in2 D; y. @% u+ J ]1 j2 ] u
// an action group, because we want these actions to be- n0 e$ b# d" K* u0 ]; q) K5 B1 T
// executed in a specific order, but these steps should
' D/ t1 A% Y5 C // take no (simulated) time. The M(foo) means "The message- l+ k, X5 n% a; W" P
// called <foo>". You can send a message To a particular5 R2 m( g' O! q- X- c) {7 ^! t
// object, or ForEach object in a collection." R2 o! U8 E9 S1 X$ l7 j! d
) V( j3 Y% p3 p! o( y4 Q
// Note we update the heatspace in two phases: first run
1 K; |. C# j' M: l // diffusion, then run "updateWorld" to actually enact the
) J+ h% B* _; i7 x; d8 A // changes the heatbugs have made. The ordering here is
# U! z) e* i: p6 V/ ^! o // significant!
, ~( T! X; P; x- t4 [ ) o, ^$ n2 o( i
// Note also, that with the additional
1 ^. M. o7 X$ A0 ^7 B8 g5 ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
# `' X" p: b) N% q // randomize the order in which the bugs actually run) K% E4 s& d+ c: B8 A D
// their step rule. This has the effect of removing any
, M8 g- t- X& U" m9 A, k+ T& q // systematic bias in the iteration throught the heatbug
2 u1 q) F) Z0 u+ F/ E* n- l // list from timestep to timestep* L. O2 P2 r2 j5 {- d5 w
- X" Q; H! N: @3 w* B: [ // By default, all `createActionForEach' modelActions have
; x; r2 s( q" o! o) f // a default order of `Sequential', which means that the
+ i( u( Z/ b+ }# ~" a* W // order of iteration through the `heatbugList' will be; v( F% J$ S" e% z# `3 ?8 R
// identical (assuming the list order is not changed
; n5 b) k2 d: r0 `+ u // indirectly by some other process).' _: \7 u9 m! q) ?" f3 C: Q7 N* |
( K# z- V' y% {3 X) s
modelActions = new ActionGroupImpl (getZone ());/ a- L5 k! D# f
1 j9 ?2 b' }" K4 L+ P# S M try {" ^2 H0 [' e: J% q) b+ M( r
modelActions.createActionTo$message( e( N6 u1 i) v7 r0 H
(heat, new Selector (heat.getClass (), "stepRule", false));1 M+ m V, ]5 r' I, p3 M
} catch (Exception e) {
9 U* ]' G+ a- Y. r1 s" @ System.err.println ("Exception stepRule: " + e.getMessage ());
% t% L) F7 `+ s* M# |1 s! n# k2 J7 y }
# [: V* x, b; T" _/ L# k
$ Q* d$ Q6 Y0 ]1 O. E try {$ m& c+ k0 d( t) J$ j: f
Heatbug proto = (Heatbug) heatbugList.get (0);
/ P* R0 x+ c9 b/ H {! D S+ s Selector sel =
$ T# V, s9 B' ?7 d' l' m& M new Selector (proto.getClass (), "heatbugStep", false);
' [- p: m. P) ~3 e6 ]4 V) N4 i) }# g' B actionForEach =; H3 P' E. M2 m. n
modelActions.createFActionForEachHomogeneous$call5 K- g. f8 B5 L, _
(heatbugList,, p( H) l( f0 h. F* k
new FCallImpl (this, proto, sel,/ B9 {' O3 P7 b& F
new FArgumentsImpl (this, sel)));' r$ \5 |% M. F- z2 b
} catch (Exception e) {
7 E6 j# N2 h- V e.printStackTrace (System.err);* j! [! x5 W; Z
}
5 V. ~) m( S7 ~# U3 K# ?( y
+ l# ~( @# j1 o syncUpdateOrder ();
1 {6 v$ R9 Z8 ]; P: \
$ {# a% |6 F! ?' V. z4 y- F8 t4 P try {
( E0 E: m3 F( Q" r modelActions.createActionTo$message
/ b. v _( Z5 @, s/ Q3 B `7 ? (heat, new Selector (heat.getClass (), "updateLattice", false));; o! T- Y. {9 |
} catch (Exception e) {6 m7 R. E% g8 E" B
System.err.println("Exception updateLattice: " + e.getMessage ());
* E, j9 i m! ]$ U }( o1 l- ^+ O5 H# w
$ }+ I3 }7 c: c3 U8 m
// Then we create a schedule that executes the6 x, b# [! I5 f# m% E2 ?! T! L
// modelActions. modelActions is an ActionGroup, by itself it
% e7 _' l* h. P2 a3 [: d // has no notion of time. In order to have it executed in
6 H- j8 ?+ t" c: Z! ] // time, we create a Schedule that says to use the, E* E4 e+ n/ d- b# Q1 S) }4 B7 x5 f
// modelActions ActionGroup at particular times. This
+ m4 c' j& X6 Z) I& c- {* C$ } // schedule has a repeat interval of 1, it will loop every
& k8 ]. n" n9 ~/ S( W4 o; @ // time step. The action is executed at time 0 relative to7 {9 Y2 T( U. s6 u% ]
// the beginning of the loop.
2 K/ D$ `2 M4 c% f/ V3 R' V1 ?
3 ?4 I, k8 K& H4 G // This is a simple schedule, with only one action that is2 P# _! I/ l- V8 p% X( k/ L' {9 @
// just repeated every time. See jmousetrap for more
0 P6 b( ]+ [8 |* U$ p // complicated schedules.& R( |2 h s8 E0 q! b
) N9 f, X ~5 l: u$ _# R, \
modelSchedule = new ScheduleImpl (getZone (), 1);. t7 j5 Q9 y4 Q5 o8 [
modelSchedule.at$createAction (0, modelActions);
; X- b. ~ f1 I5 E# z
3 Q5 L; x0 {! I return this;
5 x3 n3 V, K) h n } |