HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- T# L% V- |2 F; z
- Z* V. q6 c; f public Object buildActions () {3 q+ a/ c+ `! N2 P1 B
super.buildActions();2 m2 _* {9 f- U
5 U! f0 ]6 L- x7 C
// Create the list of simulation actions. We put these in
1 g+ ?6 c& {5 f, C" F8 d6 |/ N // an action group, because we want these actions to be
3 {; N# G' K/ q9 P7 b // executed in a specific order, but these steps should" M) j. r: y( Y& Z# j( L
// take no (simulated) time. The M(foo) means "The message1 v; E8 r9 y! U5 J
// called <foo>". You can send a message To a particular3 D& T" z% F) }, D/ Y! C
// object, or ForEach object in a collection.2 m: e S5 P5 Z
5 p3 ~1 E+ P/ a: j5 K; K/ x( h7 D4 o
// Note we update the heatspace in two phases: first run9 ^% |% o5 ?+ f5 W
// diffusion, then run "updateWorld" to actually enact the( \5 Y' o8 H& b/ a
// changes the heatbugs have made. The ordering here is
1 q N0 _) W8 M3 W) l4 k3 v/ K // significant!
4 e4 R; i5 T5 R' T# w: \) \ ) y* y) S" [$ k" H
// Note also, that with the additional4 U. V0 p8 G5 n. P0 B, v
// `randomizeHeatbugUpdateOrder' Boolean flag we can( V0 f4 m1 P* b1 Z# n! s3 d
// randomize the order in which the bugs actually run# U/ f" w* U8 b. O
// their step rule. This has the effect of removing any7 X, @; u: q5 m' W2 l% w
// systematic bias in the iteration throught the heatbug7 ]! P2 A! Q5 Q' h5 ~2 L$ K
// list from timestep to timestep
2 R6 ~: M A4 f/ V# A: q( [/ e
- i2 a- `2 ^* C9 }6 `% R; t# e // By default, all `createActionForEach' modelActions have5 E' n3 M" o* M* _" Q# c, O% O
// a default order of `Sequential', which means that the' W; X- ^ T) C0 a9 }+ B7 o' b
// order of iteration through the `heatbugList' will be- D( [5 |( Q7 G( i0 ~# k) l
// identical (assuming the list order is not changed+ q. H* H% M. U- |+ L* {7 A, \
// indirectly by some other process).
; H; }$ B- B9 U7 c! w$ p9 w: V
" d A: M& D% P( {3 U modelActions = new ActionGroupImpl (getZone ());! ]% y v/ E$ N- C6 L6 q
# B, o- v$ \( g; m, K try {
0 a N" T- [: M2 q. g, C modelActions.createActionTo$message
: K& [) x- {2 s (heat, new Selector (heat.getClass (), "stepRule", false));: m+ S+ H. o7 s7 }
} catch (Exception e) {
4 n* k; A6 H( u& }/ o System.err.println ("Exception stepRule: " + e.getMessage ());
1 v, Q$ m @9 g7 w) c d }7 ]- F ^( q9 A% U' q: V8 Q
& s1 i& H" G) r3 y& y' ? try {
% f+ l/ ]3 O- V" k( g& w5 u Heatbug proto = (Heatbug) heatbugList.get (0);! z* b$ b/ S- A0 H X
Selector sel = % [( {# `" R: L( z i# p4 w
new Selector (proto.getClass (), "heatbugStep", false);
' ], I% T4 K* |- B; t5 T actionForEach =
$ m& u* u; C, p7 l* p) Y modelActions.createFActionForEachHomogeneous$call
/ B* g5 c6 y) n8 `1 L: U0 c) o (heatbugList,
. Y! k ^% }, |' o9 ` new FCallImpl (this, proto, sel,
1 X& Z3 g3 V/ ^7 j- [- Z new FArgumentsImpl (this, sel)));0 I! Y" P+ ?; N+ F4 E
} catch (Exception e) {
4 I7 b' F* Z1 i; P1 c e.printStackTrace (System.err);4 \3 s! P R; i! A
}. Q' U9 `3 m* m1 t5 G$ P
0 N. V* V' S S, y syncUpdateOrder ();
' p$ {5 l" @3 O; d! }, K9 d- j6 b4 Q" T0 u9 u3 l
try {2 z' M2 }3 Q) o( ]. S) i# G0 P
modelActions.createActionTo$message 7 L( n+ t" a( ]( s: }1 x, `
(heat, new Selector (heat.getClass (), "updateLattice", false));9 H8 U w, q0 a9 u3 i* d& c
} catch (Exception e) {; h$ O4 ^1 v1 J& s8 g j0 F4 j* J7 o
System.err.println("Exception updateLattice: " + e.getMessage ());+ C, K0 ~3 p J/ Q6 J( v- f7 l* \
}
& I2 q; Q1 H% f8 X5 o; E * s% S9 n% T6 U- \; ^$ ^2 F9 R
// Then we create a schedule that executes the
9 X2 w9 f8 g! f; e // modelActions. modelActions is an ActionGroup, by itself it% D4 z5 \. j: D4 ]0 q) S. o' `
// has no notion of time. In order to have it executed in2 ~5 {0 R E- p
// time, we create a Schedule that says to use the
4 N. k; s) s0 P8 A, r2 y& g2 { // modelActions ActionGroup at particular times. This0 W, U6 E- p4 v5 O
// schedule has a repeat interval of 1, it will loop every1 ?( J& s( i* l$ K2 @) q/ l
// time step. The action is executed at time 0 relative to1 A2 _. X! Z8 n: n
// the beginning of the loop.6 N- ], M, @5 g) |
. t m2 |# h/ R) b
// This is a simple schedule, with only one action that is L+ e) y& L" Z$ {7 C1 f) a% ^9 a
// just repeated every time. See jmousetrap for more
$ e, M+ s4 ]- b4 x // complicated schedules." p+ I% b3 m5 J/ p( M0 Y
) k4 F6 B; o7 J& G/ w# k0 n3 S( ~; I
modelSchedule = new ScheduleImpl (getZone (), 1);
7 Z2 f' C+ s0 D$ ^3 e modelSchedule.at$createAction (0, modelActions);0 C# n/ e" _3 N1 w3 y
: s& X. p; W8 l A0 }
return this;
- d! o' p) w2 J) a } |