HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' K* v) }8 K( Z7 |( a5 d2 ?0 J/ ]
public Object buildActions () {2 c1 f0 k M: T" [0 c0 O2 I
super.buildActions();( Y8 [1 N# E, v$ w
% d& [5 V/ C6 T' s1 j4 B' @, N! M
// Create the list of simulation actions. We put these in
6 ^' u* |2 N) ^) V // an action group, because we want these actions to be9 W2 v/ p- j! L3 X# T" R' C
// executed in a specific order, but these steps should
7 b7 a/ M4 n) l, P7 b* r* Y3 X // take no (simulated) time. The M(foo) means "The message
& {# ^% W+ E1 e // called <foo>". You can send a message To a particular
N- \# C" x% \ W- t3 N& S9 p // object, or ForEach object in a collection.
1 X1 i3 C( b7 L6 o2 o ) P" g! X+ ~7 x e
// Note we update the heatspace in two phases: first run/ [$ ~8 d3 a" r' { U9 W" E
// diffusion, then run "updateWorld" to actually enact the
X$ o+ W& Q) g3 a, v // changes the heatbugs have made. The ordering here is: {! g* c- ]' b9 M$ T: a
// significant!1 E& Y U( z1 R- L& I7 a9 @, {
* i: V& S/ W& u8 v // Note also, that with the additional a4 K; G K2 r) A4 z8 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can* X1 U' t5 S) h- l1 W4 d4 ^
// randomize the order in which the bugs actually run
7 X2 Y0 O( q) ]$ `: a // their step rule. This has the effect of removing any4 t- j2 z9 U! V' D A) ~
// systematic bias in the iteration throught the heatbug
1 @4 r/ j$ b0 V* \, I0 b% g // list from timestep to timestep
; B4 L7 f. ^6 X$ J/ o ( S3 S( D1 n$ r) ^
// By default, all `createActionForEach' modelActions have
+ L3 Z: n: a6 a% Y; x; ?) [ // a default order of `Sequential', which means that the
. n" h) @& e f/ ~$ E: r4 u // order of iteration through the `heatbugList' will be6 W! I% h$ b$ c, K$ b$ ?
// identical (assuming the list order is not changed
+ A, F) V v9 N. W5 q* { // indirectly by some other process).
( J l2 i& ^' R - v4 E/ [2 B7 n8 J3 `
modelActions = new ActionGroupImpl (getZone ());% B4 P4 `0 I( v4 ]
0 _8 U7 }. S n- I. m/ o
try {
& w+ a. R% w' _! }# R, A& E modelActions.createActionTo$message5 x3 n7 i7 ]- [+ E% Z3 }+ ^0 E( i
(heat, new Selector (heat.getClass (), "stepRule", false));8 G9 j: s5 K+ u/ T/ Z; U& M- {& a
} catch (Exception e) {) H, Y/ }/ e5 `% }* ?. C* ]3 X
System.err.println ("Exception stepRule: " + e.getMessage ());
2 E7 U4 Q! p. v" e }
8 P4 |8 t8 T$ p" C! q, N4 U% T, t0 [; y5 H
try {
" I* q5 D1 P5 v; [ Heatbug proto = (Heatbug) heatbugList.get (0);% p% c- Z \9 \. y
Selector sel =
" i, ~6 W- p" |2 [) Z% O new Selector (proto.getClass (), "heatbugStep", false);
4 F; z" y# R9 t6 a( |1 J5 p) g- B actionForEach =2 |1 {/ b: @# Q4 @9 N2 Y* U8 h# A
modelActions.createFActionForEachHomogeneous$call
9 O' k8 `$ b1 M) r/ d& P$ ]9 g (heatbugList,6 h1 h% r2 n3 H: G2 a3 J1 f. b
new FCallImpl (this, proto, sel,
+ G9 }0 j0 U7 ?& }: h new FArgumentsImpl (this, sel)));
# i! M3 G' B( ^ } catch (Exception e) {
( W6 v7 C+ t5 u5 C2 o/ R4 K e.printStackTrace (System.err);0 x$ A3 a6 t. W- J) X, b) V
}
, N- g- U% N2 `7 z9 Z& ?8 m , U. H9 u1 {$ X2 V( f; E
syncUpdateOrder ();
+ I, H( A% G, G& B9 D% [
9 R9 `( q2 }0 X9 | try {# ~/ V* U3 B. P7 I
modelActions.createActionTo$message
r/ ~! }! W( Y- B. r4 z) g7 I" d (heat, new Selector (heat.getClass (), "updateLattice", false));
m; s% @& w* [/ D# Z } catch (Exception e) {/ e5 v, Q0 i1 c7 K
System.err.println("Exception updateLattice: " + e.getMessage ());
* u6 w$ F5 Q9 P }
* I4 ^& O% K1 R4 t" P
' \: {3 x' w& E/ Y; ` // Then we create a schedule that executes the5 [% \* f4 H: `: @
// modelActions. modelActions is an ActionGroup, by itself it# d2 U( G9 s2 c3 q8 X6 ~7 c
// has no notion of time. In order to have it executed in
! D5 e+ Y' X. P6 a+ ^ // time, we create a Schedule that says to use the
: `3 t- o' `: `6 _. f( y T) I; n // modelActions ActionGroup at particular times. This
* }, I# C/ v. ~' }' }: e // schedule has a repeat interval of 1, it will loop every# h" U! P# ]! f1 U* N
// time step. The action is executed at time 0 relative to3 W* Y0 h1 a2 w) b3 A8 T
// the beginning of the loop.. ?( `: c: v" A2 c
U# D; H% _0 {" o5 _1 }* t7 N, S
// This is a simple schedule, with only one action that is
9 Z& n% Q( K: Q$ |0 R // just repeated every time. See jmousetrap for more* a" ]. `% x& v0 y9 m- g" H
// complicated schedules.8 K l% k5 `, a" i6 Z4 h
8 p) _8 b3 L9 j! X
modelSchedule = new ScheduleImpl (getZone (), 1);4 _) Q- P3 `0 |/ t+ {! W9 ]) @
modelSchedule.at$createAction (0, modelActions);9 }: M7 ^% q5 ?( \$ @ [
: o* C2 B4 w. p0 @" U$ z! X
return this;5 p1 e% Q* y8 a% I, J( y
} |