HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 M+ n$ e O" L; L6 `
& l7 R0 V; X; R) h: m* T public Object buildActions () {
3 `' x& \ I* J+ P9 a( M super.buildActions();- Y8 o7 e, I: T' b& j1 G
" q& J; M7 a; S4 ^; I9 o0 Z# F
// Create the list of simulation actions. We put these in
7 R F1 S# F+ `# `' Q, W, { // an action group, because we want these actions to be, l- W' B. ^& p, m5 c
// executed in a specific order, but these steps should3 }% f, `' p) m _; U. J
// take no (simulated) time. The M(foo) means "The message
8 {' q9 {. i' l W! y // called <foo>". You can send a message To a particular
* G, _! V4 {6 ~5 q" `) E // object, or ForEach object in a collection.
/ W: V- U0 V% z, z ! O1 f" P: H( w+ U3 `( I, h
// Note we update the heatspace in two phases: first run* H- {: L" U# k
// diffusion, then run "updateWorld" to actually enact the) u+ Q% \( t7 r0 Q* V
// changes the heatbugs have made. The ordering here is
" A+ g3 G3 W# y // significant!
: U3 I+ P4 Y" k 2 k/ e& T1 B; F9 h" n7 K/ T
// Note also, that with the additional
) T# p4 }# s+ g" P3 q M4 |0 e' z! U' ]9 W // `randomizeHeatbugUpdateOrder' Boolean flag we can9 \4 }& w) C& y) \6 Z2 R4 L! |
// randomize the order in which the bugs actually run
- o- ~+ U# \' Q- f. N8 { // their step rule. This has the effect of removing any
E( \( s# c8 J$ I% r: ^! v // systematic bias in the iteration throught the heatbug
+ [8 k% ~* v& C/ `- ? // list from timestep to timestep4 x q ~# U: \8 w; P
1 ]3 e$ s4 I$ z- C8 S+ U
// By default, all `createActionForEach' modelActions have) L3 v& `0 |4 j' J: [( m, p
// a default order of `Sequential', which means that the4 d, H; Q9 h, F" J; |
// order of iteration through the `heatbugList' will be
9 Q" ]- I. l0 ~3 y# f // identical (assuming the list order is not changed
: `/ ` f: s8 e0 [8 G( ~" x // indirectly by some other process).3 {/ ^6 B" }- W4 y8 S1 Q$ A0 w; |
$ g( }- f8 {- u, f3 F1 G3 y
modelActions = new ActionGroupImpl (getZone ());
; p5 U4 S: X& G( O0 f& `6 s. D% u h6 M
try {
* W+ p* J1 O S# V( S2 Z modelActions.createActionTo$message2 [0 C# z$ x$ z$ c7 r7 D
(heat, new Selector (heat.getClass (), "stepRule", false));
) e8 q# p& |9 @1 g2 B, H# V* ~) ? } catch (Exception e) {/ p0 Y* [" P0 c0 ?$ ` V5 e3 f
System.err.println ("Exception stepRule: " + e.getMessage ());
% A' B5 d& m) ~% ?, E) a }
6 K- ~7 m2 c! K N% A' f- A: b/ p t. a' p
try {
6 ?7 S, S6 v9 ~- w# X: E Heatbug proto = (Heatbug) heatbugList.get (0);0 I8 T9 f8 o5 p$ _1 }
Selector sel = ' d* l: Z2 Q9 I" W; ]5 h
new Selector (proto.getClass (), "heatbugStep", false);- M/ o7 [: ^: q, V) X' T! y4 \8 k
actionForEach =
* H- ?, W- |7 P2 I- P u modelActions.createFActionForEachHomogeneous$call% ?. a2 a% W3 B' J
(heatbugList,( _7 r- H) E6 I4 H( H
new FCallImpl (this, proto, sel,9 Y$ O$ }1 m X' d2 g
new FArgumentsImpl (this, sel)));
) I E# v" B u0 E8 f# }9 A } catch (Exception e) {/ ]7 J2 i, K0 F' Y0 X' X: W
e.printStackTrace (System.err);+ r/ f5 ~4 z2 \! ` U6 V% q! i
}
6 x: D$ V. K) k5 x
8 T+ Q( Z3 H7 G, w syncUpdateOrder ();
3 f6 z! S! g+ |# v8 F% w* X% E( t* x0 G" R
try {% K. g) U4 n. P7 K& C
modelActions.createActionTo$message
n2 m6 ?4 H1 Y (heat, new Selector (heat.getClass (), "updateLattice", false));2 A6 p9 @ I, J( [- U
} catch (Exception e) {
0 R7 K) _$ @6 P: d System.err.println("Exception updateLattice: " + e.getMessage ());& o; P& r) N! O& A/ [ w, c
}7 } l+ M9 c$ K. z3 ?! _
& V. X8 Y% u; w" m/ c
// Then we create a schedule that executes the
. V) w" t; Q& U3 c& ^! y" Q. s // modelActions. modelActions is an ActionGroup, by itself it1 Y( C2 h' T o& o0 O5 W: g+ J# s
// has no notion of time. In order to have it executed in: O9 F3 T$ \# P5 e; f
// time, we create a Schedule that says to use the, k$ ?0 W* {8 o; c# v+ n% [- [
// modelActions ActionGroup at particular times. This9 |. B" q" R: a" `, s
// schedule has a repeat interval of 1, it will loop every
- |/ V& w. [* G0 Z2 B/ R' [ // time step. The action is executed at time 0 relative to8 W; R! g% @0 I! h9 I% l1 P
// the beginning of the loop.
0 M: }$ z# v5 o" h8 B# g7 Y8 [8 r( E
// This is a simple schedule, with only one action that is
' ]) h% D- `& E R // just repeated every time. See jmousetrap for more
/ `+ B& W5 c- X9 v // complicated schedules.
" d" h/ T, i: g - M' M3 I+ F* b9 o" M5 L7 `$ F4 ]
modelSchedule = new ScheduleImpl (getZone (), 1);
^- R+ m6 R% V" Q S- D. j modelSchedule.at$createAction (0, modelActions);
! t6 p4 Y* K9 g2 h9 R
& b+ i M* h7 ~7 T& D return this;9 H q* H6 e3 P5 E5 v! B; O N
} |