HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) N0 r9 H4 T1 N/ d% m2 X
! E1 f: q# [$ d/ Y public Object buildActions () {
5 Z3 K% q$ o8 L/ g super.buildActions();
& R' F) X! H, S! ] : x3 {) R* z6 D! B2 I% A, _
// Create the list of simulation actions. We put these in
: V2 `1 C$ ~7 w9 P- g% s: J& Y; @ // an action group, because we want these actions to be
( \, u. i0 V7 x; f5 I" z1 b- f# P // executed in a specific order, but these steps should
% d4 Y- p2 \3 e: b, u/ {. F // take no (simulated) time. The M(foo) means "The message
6 Y! s% }* u% O! S, z& Q: y' P // called <foo>". You can send a message To a particular6 H' q, H3 @. x7 ]" g4 E9 R( d2 B+ b
// object, or ForEach object in a collection.4 y- R3 h7 Y7 S
) v: ~( \* _7 Y4 S // Note we update the heatspace in two phases: first run$ U) N* \8 m0 G6 E% r t
// diffusion, then run "updateWorld" to actually enact the; M5 W: j: H* |0 @; O8 C. D
// changes the heatbugs have made. The ordering here is/ G2 I) w) |! d. L) [
// significant!% H C3 l: d3 ?& p
! C, M5 k8 w( A$ S( K
// Note also, that with the additional
V. D& P& {9 S2 _, n+ v/ b* y // `randomizeHeatbugUpdateOrder' Boolean flag we can
' a+ a' N O0 R1 w1 P // randomize the order in which the bugs actually run
) z4 R1 \' V& d // their step rule. This has the effect of removing any3 W; o* \* p# Q
// systematic bias in the iteration throught the heatbug
: L4 r' a" r% x' ? // list from timestep to timestep$ P( S! o+ ]- p# p- S d
/ Q) Q1 ^' i1 A' } // By default, all `createActionForEach' modelActions have! s' n) \" Y. o8 V# L% q( l1 u
// a default order of `Sequential', which means that the3 g. X. t: g/ J& X
// order of iteration through the `heatbugList' will be
: B2 o; m3 f! M0 i$ Y // identical (assuming the list order is not changed
" o- `9 J, D/ @, P // indirectly by some other process).
+ A R- b5 |) g) `- Q, j 8 b/ o+ e6 b$ U0 Q' U5 G
modelActions = new ActionGroupImpl (getZone ());. ~/ A- p. A0 [; X( P
. @7 T: O3 T3 u8 o# m
try {
$ m% m% z: e' ~% @ modelActions.createActionTo$message
& o/ c8 w9 c) P. E; Z/ S) Z (heat, new Selector (heat.getClass (), "stepRule", false));/ L, V1 m$ [ ^& ~5 Y
} catch (Exception e) {5 q* R' I. h6 Q: E3 e
System.err.println ("Exception stepRule: " + e.getMessage ());
% i e- p8 s( p3 Q2 z6 |3 k }
! @6 [+ ^* r" ^; f9 \( N* K1 j6 b4 S4 l6 O
try {9 z) [1 F7 }" `
Heatbug proto = (Heatbug) heatbugList.get (0);
* e U- [3 Y, h. Q Selector sel = 7 [- g h/ P9 C4 {" l: ?
new Selector (proto.getClass (), "heatbugStep", false);
* v6 t! C1 p. N& u5 |- f4 ]8 u actionForEach =
- Q: N B( N& \# S$ ] modelActions.createFActionForEachHomogeneous$call2 x" Y) e# n& o+ F
(heatbugList,
3 A |1 x; }' h" z new FCallImpl (this, proto, sel, e7 Q6 b3 k2 C- t1 v
new FArgumentsImpl (this, sel)));
0 P6 U, O: P) s5 Z4 X& v" S: A$ O } catch (Exception e) {
; ^8 b- ?: R" J) _ e.printStackTrace (System.err);
. H+ r5 X+ p' O8 t }
; g$ T- e; R1 t0 Q) I( [/ Y$ a8 j 3 J, U* k* M6 a H
syncUpdateOrder ();
Z- `8 P. I# P- z# o/ O, {/ I z9 l& ^# Y" G- S( M. K
try { ]: M F+ \+ C3 I
modelActions.createActionTo$message
" O4 @4 h3 u" W; s6 v (heat, new Selector (heat.getClass (), "updateLattice", false));4 l: ? q! _" v2 b0 x7 \0 I7 P
} catch (Exception e) {
, F" `4 F. Z- h |# K5 m" q9 ? System.err.println("Exception updateLattice: " + e.getMessage ());, Z: s8 N* v( l; y3 m
}8 e& g. J9 \( r
H- j% U7 q1 w- ]: [3 w* {
// Then we create a schedule that executes the
& P# T- Y. x4 J" a8 t) V+ ^ // modelActions. modelActions is an ActionGroup, by itself it- f( V( W) t; J( C u
// has no notion of time. In order to have it executed in
U8 Y! \* p! C- I0 n // time, we create a Schedule that says to use the5 B( M3 g. k- T1 K8 ?& ^/ F
// modelActions ActionGroup at particular times. This- a5 \4 V5 {: b& p1 G1 o0 ?6 X
// schedule has a repeat interval of 1, it will loop every9 }( }/ w; P" h
// time step. The action is executed at time 0 relative to4 ^! i- f1 Z0 T) b9 M2 P" g
// the beginning of the loop.) h2 K _. ~8 B1 E
, `" s# P7 J; c7 R% ^$ f' c$ f9 F0 ]+ B0 ^ // This is a simple schedule, with only one action that is
$ [: l ]# d5 u1 ~2 a$ D Q- W& ^ // just repeated every time. See jmousetrap for more
4 W7 b; ^: o) Y1 f p' Y // complicated schedules.
- B* O- d" G# D, C* ]& v; I0 R3 F $ r9 O5 T D+ m. b' b
modelSchedule = new ScheduleImpl (getZone (), 1);
2 M5 s; m/ x8 ~1 } modelSchedule.at$createAction (0, modelActions);5 g% b( D% O- x8 ?
}+ Z: R& t& E4 D7 {7 L return this;
6 B3 u6 j; L2 R+ V8 s } |