HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. m/ | ]3 g/ @/ p" u0 M
& n M- a( ]- `: S# `7 U public Object buildActions () {
7 v- [) b0 E( m/ n$ ? super.buildActions();$ B* _: c" M# H- S6 ~. E% w
2 U3 M1 q' e0 C( F2 \3 N: I9 S8 p // Create the list of simulation actions. We put these in5 k7 _- R9 ? x3 u1 j0 K: Z1 G
// an action group, because we want these actions to be
% |) c$ f1 O R1 E( s, b4 M6 a5 D' ^ // executed in a specific order, but these steps should3 }) s8 [0 Y N' r& y7 ?' a
// take no (simulated) time. The M(foo) means "The message
* O7 a8 {8 B. e# g // called <foo>". You can send a message To a particular4 n& v. V. }. _8 M4 W
// object, or ForEach object in a collection.
. G8 W; [7 L- h$ V( G/ ] 5 o; j8 p: t5 n. i0 g2 o3 l/ a
// Note we update the heatspace in two phases: first run
$ n( N+ L$ H# a( B4 _ // diffusion, then run "updateWorld" to actually enact the
1 H/ M% f% X+ k c! A* p5 P3 A // changes the heatbugs have made. The ordering here is
) P* _! I) Q- I; P- T: h // significant!
3 \1 Q) F1 \7 c& j Y8 L7 I
) q9 e6 v5 G) y6 ^) ^. L // Note also, that with the additional
2 O6 P# ~+ S6 I- U7 t. m // `randomizeHeatbugUpdateOrder' Boolean flag we can: X/ j; B* [: x/ A1 {5 w. X/ H% s
// randomize the order in which the bugs actually run
/ \/ x8 @! R4 G* }. T" q // their step rule. This has the effect of removing any
: d3 k2 A5 |* |4 P( } // systematic bias in the iteration throught the heatbug
6 `7 c- l: a" E/ i/ z0 }* Q // list from timestep to timestep% Y& r9 P& v# ?$ ~
: j+ r) @( J d Y. \ // By default, all `createActionForEach' modelActions have) }( P# c$ s9 b& [& q5 ~
// a default order of `Sequential', which means that the
& [3 S9 x" H, E$ q! x! g5 T // order of iteration through the `heatbugList' will be) b5 O+ s, P! J6 y2 P' o) J8 y3 @
// identical (assuming the list order is not changed6 l0 g, v) B6 S' ~! A
// indirectly by some other process).
6 _9 S! R" d7 Q# X4 m6 p5 Y2 v
# d0 \5 h9 k, P modelActions = new ActionGroupImpl (getZone ());
$ b$ X# H& B: L( _2 e3 t" Z: J/ \3 {
try {
& S& t( E8 M& L( } modelActions.createActionTo$message) G& x8 r1 `8 [$ \% t
(heat, new Selector (heat.getClass (), "stepRule", false));* o5 Y0 S1 o* s3 K; a' R' q/ k
} catch (Exception e) {7 Z$ J4 U& f$ N
System.err.println ("Exception stepRule: " + e.getMessage ());0 Y, ]9 H4 {/ e$ f& T8 I0 h
}+ w# {/ Q" }, U& K) }3 p
4 K) \% Y; k( b6 E N. N
try {7 \% }) [- T* }# d f0 S+ O, w
Heatbug proto = (Heatbug) heatbugList.get (0);! R; M; z8 S6 X( A
Selector sel =
5 ]& r! c8 h- J+ f3 ~8 u% \ new Selector (proto.getClass (), "heatbugStep", false);$ J8 O8 n0 R s8 k4 G
actionForEach =. `& F: v7 F, b' b- j. L5 p( k9 L/ ^
modelActions.createFActionForEachHomogeneous$call
1 g3 j! @' a w' i( n$ ]1 v+ P (heatbugList,
/ p+ @$ l5 p: V; p* H* p: N0 z new FCallImpl (this, proto, sel,
. ~4 V- C) b: @! B! A/ ` new FArgumentsImpl (this, sel)));
1 X1 @" w0 R9 ]: R0 C } catch (Exception e) {: S' P8 B* }6 k/ \9 [
e.printStackTrace (System.err);5 @! O G0 r2 Y' q7 a8 B5 o8 j: w
}
$ y- T$ B1 a0 o' g. b# U9 O 6 T- s' k1 F: h+ e' X
syncUpdateOrder ();8 @# a& S: t5 T3 P3 }* v/ l6 o
+ i' L3 w' p+ v* u5 M
try {
. W% l! h' k! [! ^: D modelActions.createActionTo$message ; y9 g, q+ o$ M
(heat, new Selector (heat.getClass (), "updateLattice", false));+ `$ H" p- d- P- D2 o+ {9 h
} catch (Exception e) {; z+ z7 c4 f7 S0 M+ K& d8 N5 S; K
System.err.println("Exception updateLattice: " + e.getMessage ());0 q& R8 s: b/ B; ]2 H
}
4 M( k; P" `! n
. k) I; X9 ^1 E) z" J // Then we create a schedule that executes the7 G- T1 Y$ S1 Q) ~% ^
// modelActions. modelActions is an ActionGroup, by itself it- d1 E: s/ t$ t: S" V) v: B0 E
// has no notion of time. In order to have it executed in
. e# |/ L6 @6 }. p8 e( R // time, we create a Schedule that says to use the* u. [: Q1 L; D$ ^/ Z
// modelActions ActionGroup at particular times. This, z0 w# P+ V B' q. N4 Y9 ]; {3 M
// schedule has a repeat interval of 1, it will loop every4 s# {$ D4 j9 ^9 B# L2 c3 `* d
// time step. The action is executed at time 0 relative to# I L- \: G% n/ a& a; V
// the beginning of the loop.8 S5 G- N5 p& l$ ?( ~. k# F9 s3 Z, e6 f
/ z+ N+ \7 D5 g* g8 y a
// This is a simple schedule, with only one action that is
1 Y: x: b) s; z B1 ~+ ? // just repeated every time. See jmousetrap for more C, d1 l$ k( y. w& S" i, ]
// complicated schedules.8 w( s9 x' k' U% H8 }' `, u
2 b+ x: h% g, G0 { modelSchedule = new ScheduleImpl (getZone (), 1);
7 V7 R" `5 J2 T' @ modelSchedule.at$createAction (0, modelActions);
6 e1 ]) ]. U& {# I+ n4 p" d1 B6 n6 G
! C9 N; S$ \2 ` x return this;: P. S( a, Y0 J- e3 p
} |