HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# i9 d, n) i/ n8 k
& Z, p8 L* {; v; m: g public Object buildActions () {
" b9 S$ h2 f- {" R" I \, Q super.buildActions();
" m+ E0 Y7 [$ S) p
1 \3 {8 Y) o' F+ w$ ]8 o! V1 K! H // Create the list of simulation actions. We put these in
' u9 K0 E( `9 T z // an action group, because we want these actions to be& P" i# [4 t5 S) K; [; C; r" v: \! X' {
// executed in a specific order, but these steps should
* h4 u) l1 J! A# G // take no (simulated) time. The M(foo) means "The message! j$ U9 P3 E6 e( ?
// called <foo>". You can send a message To a particular
$ N, z& `: T# i9 b @4 E! q! Q, z // object, or ForEach object in a collection.
* |$ \! c3 v; w/ \ # S7 ]% H- p& W' c/ r
// Note we update the heatspace in two phases: first run
1 |2 I3 [* w) o W/ N" N `# e // diffusion, then run "updateWorld" to actually enact the! o# U$ P/ V* `, c9 B- `7 ?6 n
// changes the heatbugs have made. The ordering here is
# D+ u; v9 I4 P) H // significant!
$ G4 O/ |' p$ n3 h, t
& `" B& y8 f" ]. k& k // Note also, that with the additional5 j4 `: M. _" L, w ~" Z$ R2 Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" O3 n n3 \* |, Q8 J, ^9 C5 v // randomize the order in which the bugs actually run
7 f% M' R5 k; X' l/ O# O; e# _$ ^- ] // their step rule. This has the effect of removing any6 S! D; ?9 m' E9 f- a
// systematic bias in the iteration throught the heatbug' y; e9 Q1 @7 V! q
// list from timestep to timestep
6 O- e; |$ |+ G" W3 n& F9 u( `3 M% t 3 v; j5 w4 O) S& w5 c3 c# i
// By default, all `createActionForEach' modelActions have
$ U( o" N2 U9 n+ ] // a default order of `Sequential', which means that the+ T" A& E6 {1 E& B& |
// order of iteration through the `heatbugList' will be
! x% |6 l9 u" |, x, y // identical (assuming the list order is not changed
! X e7 y2 a+ Q3 o+ g6 j. ~ // indirectly by some other process).2 ~* u u; w7 F6 v8 ]4 w2 @4 I% z" X
! W) d2 k1 [% J9 R modelActions = new ActionGroupImpl (getZone ());7 V0 s2 I U f0 E
- J$ \ L# E! J$ z$ b; ^ try {. y0 ]- a* [5 m7 Y: c) f
modelActions.createActionTo$message
- y) J+ T. B0 `) k: o! ` (heat, new Selector (heat.getClass (), "stepRule", false));0 V8 N% s" o' W5 h+ C
} catch (Exception e) {! Q1 U2 I* M/ ]
System.err.println ("Exception stepRule: " + e.getMessage ());
8 ?6 j8 h9 `- }, T2 F2 ` }- ?7 y, w9 n, v+ N* u
9 G; ?1 D3 N% `0 B9 X
try {
& J1 f3 ?1 ^- L, ~ Heatbug proto = (Heatbug) heatbugList.get (0);
L F5 B+ e! X- T9 Z, z0 b. y U% N Selector sel =
9 [2 k2 U& v2 i' g6 P new Selector (proto.getClass (), "heatbugStep", false);5 r* Z+ [! X M+ A+ B+ J. P0 i
actionForEach =8 _* ~0 t" Q [9 u: |, S! K' @: {. D, b
modelActions.createFActionForEachHomogeneous$call% R- ?4 i9 I3 E
(heatbugList,) Y( K) v r' Z. s+ F/ G1 R q
new FCallImpl (this, proto, sel,
, h0 {6 X- M& n7 o; h3 G new FArgumentsImpl (this, sel)));
: d# O: A" i1 g } catch (Exception e) {
6 V& c' U c- F6 o, _0 K e.printStackTrace (System.err);1 x0 N) i2 ?7 Z2 G/ ^
}
/ A" ^, p" k- U . E+ ~5 }) e- o0 x
syncUpdateOrder ();
- d! W* N% L2 G! f/ O/ S8 o2 ~$ Y: `3 \! k/ [" I
try {
/ u, y( X8 ?7 } A; r modelActions.createActionTo$message 3 X2 C# R# b* ?7 P, r
(heat, new Selector (heat.getClass (), "updateLattice", false));' q. v3 Q. M( v& x
} catch (Exception e) {% k' s$ a { ?" Y1 Y0 y3 n
System.err.println("Exception updateLattice: " + e.getMessage ());
& l* d5 H8 z' }# x8 F {5 x# ~; C6 h }; R3 k6 L3 y! [* a
/ Z( @; [% o2 n
// Then we create a schedule that executes the; k. T' d# M: |* @! E4 A
// modelActions. modelActions is an ActionGroup, by itself it2 X* x/ C m# z
// has no notion of time. In order to have it executed in8 g4 v m$ ~8 V: l8 I
// time, we create a Schedule that says to use the+ N/ U9 s2 w- ?, a& r
// modelActions ActionGroup at particular times. This
" X! i6 k8 f+ n' T2 _4 P& W( D // schedule has a repeat interval of 1, it will loop every
: m6 w, m |6 R+ A" ~ // time step. The action is executed at time 0 relative to
Q z5 g$ a! c5 k, J7 s // the beginning of the loop.
+ q3 r' S% Q' w1 ^' G1 Z0 W6 K3 I; P3 V) \/ r: K& h) ~8 K
// This is a simple schedule, with only one action that is
$ h# O) f& u q8 o; E // just repeated every time. See jmousetrap for more5 l* c1 m8 V0 j
// complicated schedules.
, n C$ }2 o6 [& O , X9 R- _( K% }! H
modelSchedule = new ScheduleImpl (getZone (), 1); Y) P: b# l1 y6 I8 Y6 {- S' R' A
modelSchedule.at$createAction (0, modelActions);
8 x( H n: @8 B- S$ W& B- B1 j
' @2 |; I) g. D8 V1 e return this;
! ~6 A' X2 k( H2 S } |