HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 o' B' j) J0 _2 p5 d& h$ S ?
public Object buildActions () {
9 X; _( {! v6 v4 i5 \! b super.buildActions();
' L3 i. b) J8 D9 A8 ^ ! p: v R$ _: z- m; X6 n+ H
// Create the list of simulation actions. We put these in
+ G6 v6 U) j4 a, j* S // an action group, because we want these actions to be
+ [( Y& G7 g& j% U* j // executed in a specific order, but these steps should, M4 i% e# m, ?! \
// take no (simulated) time. The M(foo) means "The message
* K, w0 {: I2 a |/ a // called <foo>". You can send a message To a particular( b# k+ s0 M3 X5 u5 V
// object, or ForEach object in a collection.
& u' B% }: s7 Q6 Y
1 O& a3 Y7 E# M9 p+ F // Note we update the heatspace in two phases: first run- n4 T1 Q* U# P) x; I% U `9 D
// diffusion, then run "updateWorld" to actually enact the
' t3 Z* X( _8 N // changes the heatbugs have made. The ordering here is
/ `1 j9 {. q6 K6 M4 [* [& ? // significant!. J) ? G, z5 R! H
5 r2 U) \5 X/ m0 [5 G
// Note also, that with the additional! Z# A$ X+ i; h
// `randomizeHeatbugUpdateOrder' Boolean flag we can; ]0 G. `3 U) X( E
// randomize the order in which the bugs actually run
3 D# G& o* k' j+ H( Q% s! ? // their step rule. This has the effect of removing any9 c+ k7 z) x0 }; y7 L' m6 \6 [
// systematic bias in the iteration throught the heatbug5 {: a# ]$ }2 c+ N
// list from timestep to timestep
; ?& h' @ k1 Q0 N & u$ y/ e- Y m) u. m/ `- `
// By default, all `createActionForEach' modelActions have
0 d- y2 f* z2 L& I9 Y1 K7 V- c, R, k // a default order of `Sequential', which means that the
+ m- [' q% V* ]5 Z u# N // order of iteration through the `heatbugList' will be
( f- }* }& q% w$ S // identical (assuming the list order is not changed# z' S7 u% K, Y3 R& b( f
// indirectly by some other process).
( E8 }1 t7 E( R" l7 X ' Q/ n v" d/ S
modelActions = new ActionGroupImpl (getZone ());8 \! J5 V; L; e( G
6 q( f+ A0 N/ Y; M7 ` u
try {) ?$ M0 A. X4 j% U. E' M
modelActions.createActionTo$message( U# o. \; {% g7 U4 m- Q1 c! l+ c
(heat, new Selector (heat.getClass (), "stepRule", false));. x1 u: U" a Y" V7 }# K7 F
} catch (Exception e) {
" N2 W/ v% \" D System.err.println ("Exception stepRule: " + e.getMessage ());
8 j& }) u2 o3 O/ |" Q+ x }
+ _1 z( z$ Q j( E
3 l% `9 M5 o! B! f9 c y* h( L try {. U# P6 l" Q) g+ k: C3 ~5 I
Heatbug proto = (Heatbug) heatbugList.get (0);
9 j, M! n7 U w: S' M1 d9 L$ }& t Selector sel = 0 ?2 [4 @/ M' c. l, H1 H. B
new Selector (proto.getClass (), "heatbugStep", false);
' a" h" q& P8 @5 }' r actionForEach =
7 a1 G7 X) U7 y$ ~! V3 w8 H9 U( ^0 \ modelActions.createFActionForEachHomogeneous$call3 R: Q: U' ^4 y% u) L; e
(heatbugList,
8 a2 R- O7 u9 u! Y new FCallImpl (this, proto, sel,# ~9 E( f6 i8 M2 n, D$ g
new FArgumentsImpl (this, sel)));
; c5 g7 e& {5 x5 o } catch (Exception e) {
2 d' f% U8 o0 U( d% |* d: M7 D e.printStackTrace (System.err);
! ], ~( h, Z! |% `$ N% I+ h2 c }
# E o. L- K/ `; U
9 R7 }. e1 g2 u; O syncUpdateOrder ();
( F! M! Z/ G U4 e f- f1 ]: Y8 a5 W% F7 a+ Z
try {0 D: B9 k4 \1 v1 W# T6 }3 F6 i
modelActions.createActionTo$message
# Q; P P/ H0 t" K (heat, new Selector (heat.getClass (), "updateLattice", false));
t5 l- T1 Z5 w( T6 [% i) U& t, r8 { } catch (Exception e) {
0 Z% M7 J( J* c' k; q3 x) O System.err.println("Exception updateLattice: " + e.getMessage ());
5 d& L* Z) K( d1 S( y# S; \ }) E$ A0 ? w) D' a) J, D2 t q0 Y
- x/ V% Q, U8 ^' e- i7 i: N // Then we create a schedule that executes the
, c" l! N+ v7 @, g" z/ g // modelActions. modelActions is an ActionGroup, by itself it
' O/ A: H2 Z' U; B& f // has no notion of time. In order to have it executed in+ O1 W4 a% }' b" l6 u( K, n! p+ `# K
// time, we create a Schedule that says to use the
& `6 B+ u2 ?" B2 | // modelActions ActionGroup at particular times. This }9 O% V+ V1 o6 D$ S
// schedule has a repeat interval of 1, it will loop every
l$ W0 z, S( p1 I/ s // time step. The action is executed at time 0 relative to- P( r( T' V& M4 d D2 S7 V
// the beginning of the loop.
5 X* c' p4 I( V+ ^, X
# v' N: w8 U$ q // This is a simple schedule, with only one action that is- v# d3 W i0 y% ~! R8 u' R8 V' Y& E5 F
// just repeated every time. See jmousetrap for more4 t V( G# j5 C$ s) F. @% _2 d. a
// complicated schedules.
6 [6 H; V& W2 O; q/ u6 a3 S2 | * s) t* O/ i0 o; S2 p& l
modelSchedule = new ScheduleImpl (getZone (), 1);( W1 w# w7 _, \5 X! [
modelSchedule.at$createAction (0, modelActions);
1 D5 N5 _3 {! E , U+ \% Z/ h/ k# m' v9 y6 T8 C
return this;
" b4 |' M8 Y& I; U3 @8 }0 m } |