HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 [9 A' i( X: r9 E
8 M: D* o* _- ?7 I" T" p/ N: c
public Object buildActions () {
& P0 a$ J* t; o9 [ super.buildActions();7 D' O5 J! q$ J8 i: v; \
: B# _6 t( i, O
// Create the list of simulation actions. We put these in
9 }- S" w1 y9 S9 b# G7 K, w // an action group, because we want these actions to be; U# i2 S! q) z9 v1 l! D8 B: |5 I0 u W
// executed in a specific order, but these steps should
' R0 u+ ?% g$ w; |% [: v // take no (simulated) time. The M(foo) means "The message; n& K1 @5 T( o! ?( H
// called <foo>". You can send a message To a particular
4 {6 n# R: J$ W( J2 J // object, or ForEach object in a collection.' [1 T# G" o% f- |
. c; m( n. Z, j' p2 D; a // Note we update the heatspace in two phases: first run
. F! ]0 k3 L8 B3 G* A( A) Q // diffusion, then run "updateWorld" to actually enact the
! f" Q- m, e, Q- S8 ` // changes the heatbugs have made. The ordering here is8 v0 H, m+ X. z5 q2 ^( m D5 g
// significant!
; R. f, J- J8 b- Y8 T + k0 W7 e) r8 u7 O
// Note also, that with the additional( d1 _9 f" o- U' n$ {
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' K, R A: f' F$ B; s- A // randomize the order in which the bugs actually run
. ?% z( f+ M/ M/ s& p. k // their step rule. This has the effect of removing any
1 w$ ]& z# Y) r5 e1 o' o% h // systematic bias in the iteration throught the heatbug& ], _2 @+ ?: P+ a! m5 x2 e# g9 m
// list from timestep to timestep8 P$ p E) M+ Y: z0 Y
^" U- g: C0 f- Q- ^5 l* Y) i // By default, all `createActionForEach' modelActions have" c, @7 a# H7 Y
// a default order of `Sequential', which means that the
. _/ ?5 ]( s4 s: Y/ d6 c& ?& Y // order of iteration through the `heatbugList' will be
- ?( a9 b- A' _$ g4 @# |. `+ S // identical (assuming the list order is not changed
6 V* P9 P# j1 W, L+ a // indirectly by some other process).
! C! |0 V6 X; A. {4 m
8 m. _- Q" x* n' |0 n' D modelActions = new ActionGroupImpl (getZone ());
( I: _) n$ r3 x' L: q4 z7 s/ I% _4 f& F1 L; c+ ~5 g/ q7 z
try {
3 N3 K, C" n. x3 I5 H modelActions.createActionTo$message% O; u* l2 C/ h4 A" B
(heat, new Selector (heat.getClass (), "stepRule", false));
" x! i; s0 B: C. V) H% i } catch (Exception e) {
) C% S' i" b, J- S9 U, k0 Q; h$ H( \ System.err.println ("Exception stepRule: " + e.getMessage ());& g$ L1 c$ u: B7 C
}1 f8 ~; F' w( j) b; r+ c
1 E8 i( m7 \* M! E try {
. o) q0 E- Y3 @4 S) S* }( z& z8 V Heatbug proto = (Heatbug) heatbugList.get (0);
4 t; T {. U6 i/ ^3 W7 v) M1 g+ E Selector sel =
3 t0 ?; ?& v( L4 U' V+ ~ new Selector (proto.getClass (), "heatbugStep", false);# ~9 ~' I+ V! Y
actionForEach =
, H+ f' e! N4 T. `6 h' k8 @ modelActions.createFActionForEachHomogeneous$call0 L( v3 y6 Y- i" e- ^
(heatbugList,+ K( a9 ^: f/ T3 S5 W# O' L
new FCallImpl (this, proto, sel,9 G' _1 c; N% s' O
new FArgumentsImpl (this, sel)));
/ D8 @( N4 M5 }, W6 }: | } catch (Exception e) {0 e; R2 \0 D% y( p
e.printStackTrace (System.err);: ?, O' R* ?' ?
}
1 `) G: I7 e$ l; W$ X : v0 Q n6 T+ ^1 H9 R- p
syncUpdateOrder ();- V1 W( V/ q9 \/ K: n! ?0 V0 {: B! A
; \% W9 B0 C" p) d- S; ^5 b6 H& P6 C
try {7 D1 v- Q* L2 R) q, w
modelActions.createActionTo$message & `' R" Q1 L4 f) \6 D; N6 i/ M
(heat, new Selector (heat.getClass (), "updateLattice", false));: a! M. X0 F, h7 C
} catch (Exception e) {
. i y9 J6 u) u, E) { System.err.println("Exception updateLattice: " + e.getMessage ());
9 A D5 _( ^( t, F+ F1 u( i& X }3 l: Z+ {* l4 b6 l' ]
3 Y( V0 J5 K" y6 [ // Then we create a schedule that executes the
* Y' s+ a; G& u k2 {; N // modelActions. modelActions is an ActionGroup, by itself it
' `. X3 ?8 @$ J) {9 O // has no notion of time. In order to have it executed in/ m- Z" y/ ?& G" L* ]' X# X
// time, we create a Schedule that says to use the
. L& X! s, }+ o/ r // modelActions ActionGroup at particular times. This |& m& @8 N( K7 P8 o/ B
// schedule has a repeat interval of 1, it will loop every7 b+ D" S* n' l9 {6 F u7 {
// time step. The action is executed at time 0 relative to$ X2 i: m( ] J' q( a
// the beginning of the loop." H. W6 k/ l$ [8 j% `
6 Y# }: w" P- ~: I: j @5 s
// This is a simple schedule, with only one action that is
( e( @1 u& [$ l0 n- q4 [8 K // just repeated every time. See jmousetrap for more* n! J0 B3 P$ j# R2 e/ g1 n
// complicated schedules.+ ]: A6 \0 S1 R$ y1 `
4 Y# Z, r' h& @! V! w modelSchedule = new ScheduleImpl (getZone (), 1);
2 k( O$ e5 H9 U modelSchedule.at$createAction (0, modelActions);( n( R. t( h \6 ]- `
% Q4 U2 p( s; r( x! m2 S, p9 c return this;
9 w1 i- L5 }4 r& _ } |