HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! D' E& @: E- s& I1 Q" f
; N7 g. J/ G$ [* b
public Object buildActions () {6 X; w# y% ~2 ~! r G$ b k
super.buildActions();
$ @' A4 f1 d% D: u3 M, | d% C' ^ o# U& g# @: r
// Create the list of simulation actions. We put these in
$ P; E, I6 _( M ? K% c // an action group, because we want these actions to be' N' ^# T% o" ?0 @
// executed in a specific order, but these steps should
# c0 |. E0 k B* V // take no (simulated) time. The M(foo) means "The message: n- M, m2 x; f: K
// called <foo>". You can send a message To a particular
* C `6 ?/ L' }1 Z9 p0 k // object, or ForEach object in a collection.
7 U! x* [/ [6 u
" A3 F. c9 S R8 Y // Note we update the heatspace in two phases: first run
% r4 h3 S) y" m4 a3 @8 T* { // diffusion, then run "updateWorld" to actually enact the$ w# D" R& Z! i1 f: I. @) p
// changes the heatbugs have made. The ordering here is# q8 X/ @% n! g# v, {
// significant!
2 I" }9 k' y- Y8 X; \ E
! c) {" `$ p$ N/ E$ ?) T' J; l- ~ // Note also, that with the additional' x0 @" i; [4 T9 [' ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can
8 N( H6 A) ~( B // randomize the order in which the bugs actually run1 ?" t/ b4 E: G' ^" ^- A. C! h& I
// their step rule. This has the effect of removing any/ `5 Q# P- _6 {( W2 j: ]
// systematic bias in the iteration throught the heatbug# n7 i, k9 V' C: `
// list from timestep to timestep
# K3 b0 ]9 @3 w- X: \/ L
+ Z- q4 _+ e0 d$ O // By default, all `createActionForEach' modelActions have' m8 b% M6 ~: r. ?1 t7 L
// a default order of `Sequential', which means that the0 h6 y8 \" m8 h
// order of iteration through the `heatbugList' will be5 m9 x# u$ f7 B2 v3 v+ }9 r8 x) T+ I
// identical (assuming the list order is not changed4 K4 {7 m* }( b
// indirectly by some other process).6 `, L5 O" H6 H2 ~1 Q
+ c) Y/ x7 x$ Z" f5 w* L modelActions = new ActionGroupImpl (getZone ());
! U1 I* h1 H7 v2 ~9 e) M+ Z( \
+ R! C& f+ J' _6 \4 y. f" ] try {
0 P. X+ E. e$ D6 f, ?* F+ n+ {) k modelActions.createActionTo$message
) H4 w# {8 w( S* s8 @ (heat, new Selector (heat.getClass (), "stepRule", false));
! n5 g9 t9 t/ u& M+ x' x } catch (Exception e) {
' o; E1 r0 Q0 k/ N7 [ System.err.println ("Exception stepRule: " + e.getMessage ());2 ]0 A3 h& t& u* t* ]" W9 _
}
& x7 a0 k ^. o: V Q
8 ]# y) J: _+ W4 \: a try {' C0 E* x3 M' d
Heatbug proto = (Heatbug) heatbugList.get (0);
: m8 X" C9 A$ V( c' T) Q3 D& r Selector sel =
0 w' v- W7 k$ t* x+ m9 n5 U new Selector (proto.getClass (), "heatbugStep", false);
: @7 G; s/ [- t) I Z7 q/ |( T& F actionForEach =
8 x* T7 o: o) Z9 e& D" \ modelActions.createFActionForEachHomogeneous$call! a0 |0 x5 v" B2 \$ ]; w
(heatbugList,
) ]7 {. h" ?" i8 P new FCallImpl (this, proto, sel,
6 Z: \; I4 z6 e/ K6 W new FArgumentsImpl (this, sel)));4 f- R* \. `7 w0 a3 C0 J! g
} catch (Exception e) { }0 o9 S$ x- a
e.printStackTrace (System.err);
% U1 k) ]; ~0 A8 D* H" b }
% W! K& h8 Z; h9 e' \6 a) V% g5 O
# M, z2 Y/ E" E9 P. O9 @+ j, H syncUpdateOrder ();
9 C2 M) q0 v0 F$ }3 x6 p o( N- |6 w# u# F
try {
, v4 i( g s a. d+ c$ x modelActions.createActionTo$message
v& H* W3 \$ A (heat, new Selector (heat.getClass (), "updateLattice", false));- G, f8 Y- m3 O. q$ V
} catch (Exception e) {
+ C* }! y# j! C System.err.println("Exception updateLattice: " + e.getMessage ());
w3 p0 x3 n6 ^8 [3 j- F }
* d( [+ A* p4 H+ Z' n) d. I
0 J, i" M& T* Q; }4 E- a+ Y) \& {" R // Then we create a schedule that executes the
6 i" p6 S# X' I9 G/ C // modelActions. modelActions is an ActionGroup, by itself it8 L5 n2 ~ d3 |
// has no notion of time. In order to have it executed in8 l/ Y X' R% y
// time, we create a Schedule that says to use the4 Z$ Q! Z$ C! r
// modelActions ActionGroup at particular times. This4 e( N& a# Z( e9 |
// schedule has a repeat interval of 1, it will loop every
! Z" Z- i% M( { // time step. The action is executed at time 0 relative to
, O; L5 }0 r+ n2 f: P // the beginning of the loop.6 @1 Q( F$ L. m5 }) r" h
( [" A X: d# `6 ]- L // This is a simple schedule, with only one action that is d# ~ F, M0 P. ]( `2 d& n: M7 P
// just repeated every time. See jmousetrap for more: f" f2 g/ h4 ^4 v7 ?
// complicated schedules.+ ^+ C v" R8 W" U8 v& P$ i; D
6 d {6 R2 t7 `3 w: K' P) d modelSchedule = new ScheduleImpl (getZone (), 1);, ~( ^% N$ P- j- C2 A$ ]: R: p
modelSchedule.at$createAction (0, modelActions);$ z7 t/ k9 e0 E: t
0 A2 [! ^3 r2 |' n- I. B+ I1 ~
return this;
4 K) ^2 N! \" D7 a } |