HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 K8 `$ a5 K% i- }( J3 r3 u2 h
3 I! \, D* F# ]- M! e
public Object buildActions () {
0 O" t, ~( g: V o, g4 r super.buildActions();. c5 N- k9 l7 U
7 K% _+ n+ R) f# R // Create the list of simulation actions. We put these in! b: {; y* A5 S' h
// an action group, because we want these actions to be
6 c* x& ~" r6 g+ H$ M* p& c! o // executed in a specific order, but these steps should Z4 P0 Q5 [; ]0 Y0 Y7 D. P
// take no (simulated) time. The M(foo) means "The message
: e. Y2 c: ?4 y // called <foo>". You can send a message To a particular( ?% y; H. ]. B J# ?) y/ d1 k
// object, or ForEach object in a collection.4 z) z* Z" \0 v: K6 E3 p
& ?! M/ Z, G* R2 n0 K // Note we update the heatspace in two phases: first run
$ V8 k X6 D' Y! j6 I; L+ W // diffusion, then run "updateWorld" to actually enact the
: o; Y0 _1 j6 F- u9 Y& N // changes the heatbugs have made. The ordering here is2 P, e& N7 p& k7 c$ ~- a+ R- f9 U6 x
// significant!- `4 M% I" r* w$ N# O& m3 c
b$ e: A9 z* n/ B
// Note also, that with the additional7 ^$ R+ S) F" K& O) r2 }
// `randomizeHeatbugUpdateOrder' Boolean flag we can; i1 d' m- P: i8 v- {& I
// randomize the order in which the bugs actually run
9 ~# I! F5 n# X- Y/ H" J7 D // their step rule. This has the effect of removing any, U1 T- B9 k! w8 t
// systematic bias in the iteration throught the heatbug7 F; e1 p, u$ V: _
// list from timestep to timestep
5 v4 n+ _3 S { $ O, y. F |- d) M2 Z
// By default, all `createActionForEach' modelActions have( \9 ^* a( |% j+ \
// a default order of `Sequential', which means that the8 y y: D5 D- m8 n6 E! b
// order of iteration through the `heatbugList' will be& N1 h4 ]0 Q# \6 D* X( r
// identical (assuming the list order is not changed
; t. V0 G3 k) C# A) N ~: E" T // indirectly by some other process). z* ]6 ]6 `2 x2 r. b
" z' V. B3 W7 _0 x6 R# Z7 Y" W modelActions = new ActionGroupImpl (getZone ());
" G- E3 o* z, Q3 j% g
$ W3 Q: Y. j ^# U0 N3 [. x- m try {0 ?- K5 X Y$ s& A0 s, X. L
modelActions.createActionTo$message+ G* V: U ~9 P7 Z# i
(heat, new Selector (heat.getClass (), "stepRule", false));
$ i) D; J0 `1 C8 o, ] } catch (Exception e) {; \, m1 z$ q. w1 w
System.err.println ("Exception stepRule: " + e.getMessage ());
) N8 V6 `( Y5 A. N- G }
6 l! } d% d6 H3 T! X% c6 V5 s8 q. K+ P4 N0 h
try {
. O8 ]' i9 }3 M1 ^* I Heatbug proto = (Heatbug) heatbugList.get (0);
2 |8 N2 Q1 |( Q* g3 V4 D$ A Selector sel = 5 k# }* z( R& e0 t3 m) L1 O
new Selector (proto.getClass (), "heatbugStep", false);8 W! p3 }5 o1 [. F+ p* h( q- [" e
actionForEach =# }& Y% F, Z, P/ t. q! ~
modelActions.createFActionForEachHomogeneous$call# d9 B) R- z3 V7 j/ ~! C
(heatbugList," U4 b4 ]4 |9 `
new FCallImpl (this, proto, sel,
* f* m6 T1 N# G2 n/ ` new FArgumentsImpl (this, sel)));
! N' W# X% s0 ~6 H8 H- c } catch (Exception e) {
* q; Z3 J- I) | e.printStackTrace (System.err);) E) f: ~! q' ^0 Y% p
}$ G7 {4 w0 ?! V* j1 V
. R3 ^" g$ n) c' x$ _ syncUpdateOrder ();
1 z, z* K( D% ^; e1 S) X! Z" I" R& z: c$ i
try {- T: |% g" c8 ]/ _: V6 m
modelActions.createActionTo$message
7 E! t# F7 ?6 K- _% h# A, M (heat, new Selector (heat.getClass (), "updateLattice", false));
' _" {) y+ R0 B4 X5 m } catch (Exception e) {% F: S9 A" Z W! S
System.err.println("Exception updateLattice: " + e.getMessage ());
4 Y5 M9 J, s0 I/ @! x$ ` }
' `6 {+ j. y7 Q0 w+ j K% X 6 [& ~3 I2 H. A A$ i3 ?
// Then we create a schedule that executes the
6 J3 J' W0 N+ e/ Y: P+ H) [' w // modelActions. modelActions is an ActionGroup, by itself it& J! v7 E6 Z2 U( p C( L
// has no notion of time. In order to have it executed in, D+ v. \8 `( K M+ S b
// time, we create a Schedule that says to use the/ b. ?0 T: Q3 j& }
// modelActions ActionGroup at particular times. This
c0 j" _- Z8 U // schedule has a repeat interval of 1, it will loop every2 h' k# n# t7 I4 E% _# O
// time step. The action is executed at time 0 relative to/ {6 z5 m& D+ v$ f8 K: E
// the beginning of the loop.
6 N7 T3 U: \3 e" g
& n7 G. r% w* i6 l; D7 P: ~# ^ // This is a simple schedule, with only one action that is
$ M3 y9 T# y& G1 B // just repeated every time. See jmousetrap for more
3 ~, A5 s6 {! X/ J! r4 E // complicated schedules.% \4 U$ N5 S' G4 x
' D3 N, s! h' d+ _$ `# J u
modelSchedule = new ScheduleImpl (getZone (), 1);% o( e5 F4 ]5 t5 V& w9 H( b
modelSchedule.at$createAction (0, modelActions);
% d5 |$ ]% Z5 T . t1 F$ r! \) i' Q" u4 D
return this;: E. ~* v* ]& Y
} |