HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% K+ {& a4 q; y' J$ A. y+ `8 f" d8 d6 }0 _( |
public Object buildActions () {* Y9 Y* c' a% `3 M
super.buildActions();
7 T& h9 ]: \1 I0 A1 }: J
: E: B) V, s# B& D, c; ]5 k1 j& Y // Create the list of simulation actions. We put these in. w7 E$ @ h+ J- H" y! E& g3 g3 p
// an action group, because we want these actions to be: e& ]4 x7 O4 j( E
// executed in a specific order, but these steps should! y, ~$ I/ p* R- C) H6 Q
// take no (simulated) time. The M(foo) means "The message
* [ D! |* }3 a // called <foo>". You can send a message To a particular& m" v3 d! ~2 R' n+ \
// object, or ForEach object in a collection.1 l* r+ J- o& j, z$ L9 N C
7 ^( U" p4 B) Z3 i. v
// Note we update the heatspace in two phases: first run
$ I" j( I6 B# y* } // diffusion, then run "updateWorld" to actually enact the, ]1 m. v$ j5 K" M- W3 C1 G3 ~ ]
// changes the heatbugs have made. The ordering here is
{2 ` s) b5 J& C7 S- M2 o // significant!
5 x: `8 e z" o4 h0 y+ ~ - X9 Y6 [' \; q# d: P
// Note also, that with the additional
( f! Q, a1 j1 S* E // `randomizeHeatbugUpdateOrder' Boolean flag we can7 U4 Q4 _0 [, b/ b
// randomize the order in which the bugs actually run) r/ `. q! S' }. u
// their step rule. This has the effect of removing any
. [6 u$ r* W' A4 V3 `/ x/ W // systematic bias in the iteration throught the heatbug: S- H5 R% }8 P
// list from timestep to timestep
* c7 h0 M1 H! ]( v& e5 K6 L1 E 5 K, W2 s0 G! K: p, ` I
// By default, all `createActionForEach' modelActions have7 d2 @$ L, h1 u5 {# I7 h2 m
// a default order of `Sequential', which means that the
5 v, v0 t( u( ]+ [ E3 F/ Y' o // order of iteration through the `heatbugList' will be
3 U+ H; q* U+ r3 u! V% |& c // identical (assuming the list order is not changed. d5 I* F4 h) j, c. [ f5 m7 x
// indirectly by some other process).
3 l- W1 [1 S- v, S* G# T
& G4 H* C& }6 q- F modelActions = new ActionGroupImpl (getZone ());. t/ M+ y$ i$ R
1 m4 @) U! A& ^* f/ [7 D try {$ j- a2 E ^. E' n6 Y7 G, b
modelActions.createActionTo$message
9 o' K+ C6 ]3 j( Y* M! h, a | (heat, new Selector (heat.getClass (), "stepRule", false));
2 E, E- z% G6 D- i1 C7 x } catch (Exception e) {
' d* U& }# o# I: }4 o/ P4 a6 b7 T System.err.println ("Exception stepRule: " + e.getMessage ());% }: d. D0 u6 O7 a6 \5 V, i
}5 Y. c+ H% t# W& m
5 f$ O- H, I7 T# [
try {1 c1 f$ M! s( Y, R/ O" f
Heatbug proto = (Heatbug) heatbugList.get (0);
1 i. c* ~- m; N! _2 Z9 `% B7 R4 o Selector sel =
1 i. I8 S- [; u+ C new Selector (proto.getClass (), "heatbugStep", false);
2 e9 Q0 e% n/ F6 |; }9 x$ b( E actionForEach =; V2 z7 C( D& E% H! K' g% F9 m2 d
modelActions.createFActionForEachHomogeneous$call. `6 c9 I. k, V: a% k: v
(heatbugList,
* Y ^4 K7 ^: l; S new FCallImpl (this, proto, sel,/ G, C! N/ N3 L5 W
new FArgumentsImpl (this, sel)));$ V8 I6 Y2 E) {7 T( J9 v
} catch (Exception e) {
6 i4 q. S9 l* \ e.printStackTrace (System.err);( @, b, Q; ]. }, a( c# H& F
}) H! M/ H0 I8 q% I. v. W
+ h# ~5 t2 L9 r* P
syncUpdateOrder ();
0 ~1 a* v$ N6 l2 a8 _$ D) A1 {3 m. u3 q0 b
try {
8 ~4 b8 o# s0 ~* w modelActions.createActionTo$message
3 X o% S% E3 O3 H (heat, new Selector (heat.getClass (), "updateLattice", false));
- K8 _; J- }) n+ n3 d } catch (Exception e) {9 a: l0 f3 K6 W4 k( |6 t
System.err.println("Exception updateLattice: " + e.getMessage ());! f4 K% I9 N8 w3 e7 I0 X; Q2 v
}9 O4 G$ D2 u0 M4 @
) g2 [/ {# x7 ~+ e // Then we create a schedule that executes the5 K/ E* {; }6 o; z
// modelActions. modelActions is an ActionGroup, by itself it
7 p: d' a! n m, l // has no notion of time. In order to have it executed in9 P+ x9 S; }2 _$ F( d
// time, we create a Schedule that says to use the
: v6 |; s/ r/ J' Q0 v // modelActions ActionGroup at particular times. This
N. L* s& U* b // schedule has a repeat interval of 1, it will loop every
2 W# j+ I0 v, _. i6 \! Y // time step. The action is executed at time 0 relative to
% `' S8 m8 r9 R- G% o // the beginning of the loop.3 a2 @7 L* c3 s ]: h6 v8 t
3 I( o% t% Y3 w. C; H( S7 O. f
// This is a simple schedule, with only one action that is
4 b6 W/ P0 l o T9 q // just repeated every time. See jmousetrap for more9 H1 k' }/ j$ F- k; a
// complicated schedules.
) F% W$ r( n' f- w0 W + q7 u1 J9 W5 L5 I( O& L8 F7 t
modelSchedule = new ScheduleImpl (getZone (), 1);
* c- \, z4 b7 p2 L8 y; e modelSchedule.at$createAction (0, modelActions); x! w1 Z) w0 @
2 s; ]! \5 x5 C+ h/ B return this;
1 S$ O5 U* B- L+ [1 s } |