HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% m- n2 a2 I4 Q3 Q5 q
6 \4 _3 B8 k; } public Object buildActions () {
' i4 J( L8 O6 W/ V* }- v1 @ super.buildActions();/ C! J. u1 i! {7 l$ Z: \' `, i9 e$ [ C
6 t4 O: ]/ N" K
// Create the list of simulation actions. We put these in
! W( ^$ g c4 t' s. k' ?; C // an action group, because we want these actions to be) [! ~& d# g8 v9 _
// executed in a specific order, but these steps should
" R3 ^4 r0 ^& z8 M' e' P // take no (simulated) time. The M(foo) means "The message. p+ q* }* D; D" V2 Y4 }* ^+ l
// called <foo>". You can send a message To a particular
0 @* r6 S9 q( o5 X6 x // object, or ForEach object in a collection.7 ?% D: {" R. i! C7 |
9 M1 ~8 M9 R" A0 \1 [" s& s
// Note we update the heatspace in two phases: first run
$ M2 s: d2 v5 T8 {9 O // diffusion, then run "updateWorld" to actually enact the9 C# y; P8 Q$ m3 ]# ]5 g* d$ O
// changes the heatbugs have made. The ordering here is
9 e4 \& _+ }# r/ j // significant!* c1 Q4 H; {. v2 Y
( \8 e& c3 q( F5 Q1 ]
// Note also, that with the additional
5 I+ ~3 T5 I$ u5 a$ H // `randomizeHeatbugUpdateOrder' Boolean flag we can4 I; T7 V7 i& O3 l b
// randomize the order in which the bugs actually run, w0 j% t. X6 `# d$ o
// their step rule. This has the effect of removing any4 Z' Y' m; N, Z. b. z h" w
// systematic bias in the iteration throught the heatbug- p: h( Y7 [; J: m/ @4 g6 I
// list from timestep to timestep
. |( u& H7 j& m6 K
& `0 k: E3 [& p, X( ^ // By default, all `createActionForEach' modelActions have: r. o/ [8 |- t* M8 G( T
// a default order of `Sequential', which means that the' i: F4 L+ X, g
// order of iteration through the `heatbugList' will be
5 i3 }8 R, j) [: S @" x // identical (assuming the list order is not changed0 u* c5 c* j( J1 |
// indirectly by some other process).
& D6 b; N. c1 x$ ` 5 G9 {/ f: K% ?" K* U. ?
modelActions = new ActionGroupImpl (getZone ());# a$ ~* V; v1 g8 E3 ?1 [7 j6 S
3 Z2 Z/ f2 o0 w9 h( }: [
try {
( g% b- r1 Q% u+ g modelActions.createActionTo$message1 m% Z: o( }2 u% Y2 \, l
(heat, new Selector (heat.getClass (), "stepRule", false));2 _6 A+ a$ o4 X# V: C! v9 f
} catch (Exception e) {
: U( f' N/ c( Z; n/ n System.err.println ("Exception stepRule: " + e.getMessage ());
# L7 q% q5 @ B7 ~& N0 k }# J0 f2 x: e. _& v+ y8 |
* Z8 @0 T7 H" V7 Q/ Z" d try {
B" r6 _/ d$ D8 \7 x* ?" u Heatbug proto = (Heatbug) heatbugList.get (0);% i8 k0 t& ~7 v. U" e) A$ g
Selector sel =
0 E6 f J& q& [ T; u new Selector (proto.getClass (), "heatbugStep", false);" h. @, `% L l4 ^+ Q) K% o2 k
actionForEach =
: y. i1 G3 z" n! v2 t modelActions.createFActionForEachHomogeneous$call S) G5 T& P: i2 }1 _( K$ E' ^$ }! S6 Y
(heatbugList,) j* [' H V* O4 x
new FCallImpl (this, proto, sel,5 Z; s# O2 Y; B" h1 A
new FArgumentsImpl (this, sel)));$ a# t( t) c- l2 _/ A, T5 q
} catch (Exception e) {1 C3 @5 y& k6 `( B7 P
e.printStackTrace (System.err);9 l+ y2 L" K% u
}5 F) T$ `! X- ^3 F( q
8 t) k$ W' M6 E) ]3 H' {) C- X syncUpdateOrder ();2 E! q, M, _) ~& M3 v1 r9 G
, d1 Z5 H: E9 ]) ^2 m( C try {
- s y' u% ]( T; k" s9 v/ M modelActions.createActionTo$message
# W7 v. v6 p/ V. |3 p (heat, new Selector (heat.getClass (), "updateLattice", false));" ^0 c5 X( D( d0 m% L: G4 |
} catch (Exception e) {7 q7 A# X& q9 ~' P% Q! c0 E1 {
System.err.println("Exception updateLattice: " + e.getMessage ());7 d" W2 t- o2 F4 x- C
}
% q& z/ J. k. y4 ?. R. U) X
) W, v3 }' b4 Y& ^ // Then we create a schedule that executes the
; k2 p2 ]. z0 p$ J4 Q. @ // modelActions. modelActions is an ActionGroup, by itself it
9 o r" E, ]" v {. a/ W1 H; |9 E // has no notion of time. In order to have it executed in( ]9 s5 H) [- ~9 @- s
// time, we create a Schedule that says to use the
1 W% W+ G9 q( R) X, k // modelActions ActionGroup at particular times. This) K' l: ~% x( a! B+ g9 Z
// schedule has a repeat interval of 1, it will loop every
8 Z9 [9 L' |8 G+ B3 L4 u9 x // time step. The action is executed at time 0 relative to
& _4 S& b: f1 x$ S5 n, q; [$ z4 J G // the beginning of the loop.9 y/ T3 I; n7 b' |" c; O. ]# ~8 `
0 n, Q9 x9 B4 Z( F // This is a simple schedule, with only one action that is" v* F/ L+ ~- K# @" @" e
// just repeated every time. See jmousetrap for more
$ K% |! B j( j8 T' u // complicated schedules.$ f9 B5 R: ^7 i, o4 e) p
. k# T! H, T1 ~
modelSchedule = new ScheduleImpl (getZone (), 1);
9 i$ X, Y* e# p1 d modelSchedule.at$createAction (0, modelActions);
8 c% R9 s+ z2 {# F. [ {" e 5 F: T. [" L- P
return this;+ s b4 j) m4 k3 t
} |