HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: p) B; o; A5 e% R4 _: T: x: U
6 e3 l$ L, V, k; `% p public Object buildActions () {
' ^3 O' ]/ e3 u) ~2 j& s super.buildActions();
7 n: \( O* I' j! }, M0 T
; ]6 u' W$ z& `; B! m$ A: | // Create the list of simulation actions. We put these in. e ~& S8 _- \! E8 X, H$ b
// an action group, because we want these actions to be l: L5 \) I, I) ]6 x( a
// executed in a specific order, but these steps should
5 L( _" h7 G2 V1 I' ]0 N# ] // take no (simulated) time. The M(foo) means "The message' N, v) O3 i! E
// called <foo>". You can send a message To a particular. k, x+ H( _4 E
// object, or ForEach object in a collection.
, W" O4 B& T+ v K9 U
: h& J3 A3 ~* ?* K, _1 ] // Note we update the heatspace in two phases: first run/ }; j9 Q \! F# K) r
// diffusion, then run "updateWorld" to actually enact the B3 k( s8 T2 A* _
// changes the heatbugs have made. The ordering here is2 B/ {8 g! l+ `+ G' O( b
// significant!
9 G) ?- F: |0 b c) y. G . P: m3 m* ~/ ?' G# t
// Note also, that with the additional
4 \! Z. @& A- e9 J // `randomizeHeatbugUpdateOrder' Boolean flag we can' r3 c0 P+ J, r
// randomize the order in which the bugs actually run
a, F; o3 [% a# H L) U& D1 E // their step rule. This has the effect of removing any- C( ~" `& {5 ]9 ?& z( A
// systematic bias in the iteration throught the heatbug
( x- L7 \; t; l7 E' `. f O // list from timestep to timestep
. u: G/ D# B) Q1 n$ {
: h! H% h( I6 f+ [6 ~% u6 ? // By default, all `createActionForEach' modelActions have9 Y! A- S8 X' n8 E
// a default order of `Sequential', which means that the
5 x7 L0 M. r+ `* r% }# T // order of iteration through the `heatbugList' will be3 ~6 Z' t, T: U. C: g6 j' f5 Z
// identical (assuming the list order is not changed! r v( S _5 u4 O+ L. O; F
// indirectly by some other process).) D- m z- b7 u
y: ]9 ~0 a. J A% o' c
modelActions = new ActionGroupImpl (getZone ());
% `4 }' S7 U" z4 I' b9 U8 P m1 s7 v1 P
try {
% h9 x3 D% x, @ modelActions.createActionTo$message( m" b/ d- C y, H
(heat, new Selector (heat.getClass (), "stepRule", false));- j+ C- S3 {& z# h
} catch (Exception e) {
/ x3 @8 c, U( A5 [, Q System.err.println ("Exception stepRule: " + e.getMessage ());* n( D2 e w4 r- n
}6 M7 c, I' F& a( Q
- M" q! i5 U3 t
try {# `2 g1 B8 j L! ?5 E4 O1 h" _
Heatbug proto = (Heatbug) heatbugList.get (0);
3 m: W3 C# {% i$ V4 p Selector sel =
/ Z( f# V6 Q) o5 d! d new Selector (proto.getClass (), "heatbugStep", false);2 x! o4 m- p9 u& a [
actionForEach =9 v0 T& ~8 J1 f. E: Z$ i# |% W
modelActions.createFActionForEachHomogeneous$call0 Q6 |- P! l; m- R% Y; M
(heatbugList,4 H, g3 ]" v% F: g, }
new FCallImpl (this, proto, sel,
3 H2 P/ n0 y) W1 W. A5 z/ N { new FArgumentsImpl (this, sel)));$ R0 I3 _. p$ w. i7 L
} catch (Exception e) {" m9 `# Z% z5 k; h: \$ u
e.printStackTrace (System.err);
( f" S+ R; c5 t3 N) J }
1 U% O9 N- E/ k% M& B
1 I4 i! w: C3 ]: X- f; [/ O syncUpdateOrder ();
; y! s) Q$ F c5 V1 o3 \
- j, }' W3 ]# i ]; H1 O8 c try {$ j; q: a O1 j* F4 z9 H& o" s
modelActions.createActionTo$message
2 F" S9 X* @6 Q (heat, new Selector (heat.getClass (), "updateLattice", false));
' ], x3 i( F' Q0 I } catch (Exception e) {( R, `% V% g5 s4 h2 \
System.err.println("Exception updateLattice: " + e.getMessage ());8 [6 R0 v' o2 i( P+ h: i2 G
}3 l! Q" b) {- S! N
7 U0 j5 ^1 Z8 W @: Y // Then we create a schedule that executes the
: b5 p" m+ x& O5 ?8 Z // modelActions. modelActions is an ActionGroup, by itself it
1 e- ?- O: U3 h) g% A* I // has no notion of time. In order to have it executed in G4 r3 Q$ _5 Z5 V1 k& L% D
// time, we create a Schedule that says to use the
+ K$ u' S$ }; ]! F' ^ // modelActions ActionGroup at particular times. This# g4 Y& t2 Y! u. i
// schedule has a repeat interval of 1, it will loop every; w7 I5 `, Z9 _9 J1 F
// time step. The action is executed at time 0 relative to
3 C) I& F1 d" Y // the beginning of the loop.
9 v. b% l) S3 b. a/ I
& L- ?9 a, Z0 r/ }5 b // This is a simple schedule, with only one action that is/ _5 ]+ c0 b0 N( U% s: w2 }% ^
// just repeated every time. See jmousetrap for more" @: @. ?* ]+ c2 K
// complicated schedules.
% {' a6 g8 \ ?9 _ x8 j! ~+ \- G3 O4 L* o2 z! v
modelSchedule = new ScheduleImpl (getZone (), 1);
2 ]" N4 {: o8 Z4 p modelSchedule.at$createAction (0, modelActions);
$ p4 ~$ r8 q/ I- ^% I
+ X& w# o5 }" q return this;5 d$ O4 X' H4 r
} |