HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, B* x! o! u: ]. ^0 S/ F
5 H( P* {8 z8 f' t& k
public Object buildActions () {
! E" u# p$ L( o4 U7 r, H2 R! k. R super.buildActions();
3 y1 o$ j' e% k. |$ v/ K0 \
0 I: `# Z: h; k2 |, b4 |9 E. { // Create the list of simulation actions. We put these in$ z1 P: C8 ~, B# D8 U: L( ?1 r- u
// an action group, because we want these actions to be# r$ M# I+ w' I' S
// executed in a specific order, but these steps should
8 L0 b9 j8 E. h; @2 X+ J, `/ n // take no (simulated) time. The M(foo) means "The message0 Y2 f M, p/ ]$ _# x$ l5 c- B
// called <foo>". You can send a message To a particular
0 |! z3 Y2 d0 E, t5 x, W. U' O0 {' P // object, or ForEach object in a collection./ Q" H% o+ r( p3 Q7 v2 q; [
, c# Y9 C( }8 a' b$ z6 ^ Z
// Note we update the heatspace in two phases: first run8 J9 e0 O/ n/ `. I) M
// diffusion, then run "updateWorld" to actually enact the! l2 {! @ g' g: F- r/ U
// changes the heatbugs have made. The ordering here is
9 p& q# C- c7 c! _ // significant!$ N3 K% A2 D8 i$ i, Q
" m. T+ d4 F. U/ L# q
// Note also, that with the additional
/ r; ^+ V8 D% q/ ^4 j1 E+ A9 _% C // `randomizeHeatbugUpdateOrder' Boolean flag we can
! w2 m# o: }" J* |$ x) {4 C // randomize the order in which the bugs actually run. v$ M1 [. T- r
// their step rule. This has the effect of removing any
; U( R/ n8 _% t. f6 N6 X+ B( ` // systematic bias in the iteration throught the heatbug
8 ?1 T/ b( P+ J# a {, o // list from timestep to timestep
5 T# C/ \5 R+ A/ N2 i6 ?
6 Z0 A) r: G; J P+ j9 q! W0 y$ g" [ // By default, all `createActionForEach' modelActions have: T9 G2 M6 z) ]6 m5 ?+ B
// a default order of `Sequential', which means that the' j5 M3 W" A1 C/ d( y. [
// order of iteration through the `heatbugList' will be9 \9 _& L) R3 R1 R, x5 e- @
// identical (assuming the list order is not changed
5 o, ~" ~! A% i! j5 C( u // indirectly by some other process).$ V3 [8 B; n$ a2 A
7 d) c; T/ v0 w ] modelActions = new ActionGroupImpl (getZone ());9 P1 l( N6 o! B# G# y
# c: S! ^4 d3 {% g
try {' ?3 a" {& v$ q- \$ a
modelActions.createActionTo$message
4 D: G+ I0 q$ g4 y1 ~6 I (heat, new Selector (heat.getClass (), "stepRule", false));
W6 b5 j' i; n9 B# f* m3 x: y" [- r } catch (Exception e) {% g% F. |* S& G3 d( y2 Z4 `
System.err.println ("Exception stepRule: " + e.getMessage ());
& Y7 k+ `5 @# x! x: j' q1 X }- I( Y% D- z( `, l
* c6 }0 B7 N J3 h try {5 T3 P( _5 U# {! U6 S9 r6 F
Heatbug proto = (Heatbug) heatbugList.get (0);
7 M+ ? d6 }* m Selector sel = * w$ y, k2 q" P# c. w' B) ^- D
new Selector (proto.getClass (), "heatbugStep", false);
5 R- L4 `# ]! G- r2 w6 f actionForEach =
5 i8 o& `! E q5 @% n* n modelActions.createFActionForEachHomogeneous$call3 z; C1 u% A. Z0 H3 P
(heatbugList,# Q' P+ q+ Y6 |- u5 t9 m% p& d9 ~/ Y
new FCallImpl (this, proto, sel,/ q6 F& w& _: o7 [! f) L S& @
new FArgumentsImpl (this, sel)));
* {7 V, o- W1 a, U } catch (Exception e) {: W D7 R5 u, L2 S3 j
e.printStackTrace (System.err);; F& g& V+ T) p- B* Z* Z
}
% b0 K9 B* k7 f1 t- d ! l) G- H" l( s" w
syncUpdateOrder ();
1 s k) i5 Z6 a- I! G. N( Q" K1 V8 x A
try {
! p/ h8 i) h, x# N5 ]8 l( f: R modelActions.createActionTo$message ' H7 i2 g& V9 C
(heat, new Selector (heat.getClass (), "updateLattice", false));
! P/ i3 F% {3 R } catch (Exception e) {
* f' h( P! |( `/ U0 H System.err.println("Exception updateLattice: " + e.getMessage ());) g( P8 X9 Q d6 x
}
+ ?# z5 |" |" _$ l
6 E+ ?, n+ c% E6 g$ X- D0 L // Then we create a schedule that executes the9 t$ f' x7 C" a
// modelActions. modelActions is an ActionGroup, by itself it
5 N$ r. t6 p+ y0 r% }: g y // has no notion of time. In order to have it executed in! l1 {, Q9 G/ D0 L
// time, we create a Schedule that says to use the9 ?+ j& P+ [4 u1 M6 y8 _7 M
// modelActions ActionGroup at particular times. This; v' a! g! k" {
// schedule has a repeat interval of 1, it will loop every
% l2 a t) j6 S' ]( ~# `8 { F // time step. The action is executed at time 0 relative to
3 e- W' H& V/ k( b // the beginning of the loop.4 Z2 y' B! x# ]: h% P; ^
$ A' E$ P/ k X) Q // This is a simple schedule, with only one action that is7 X, f: g) P+ F( s% d
// just repeated every time. See jmousetrap for more! D# b# p6 p$ H* R" D3 Y
// complicated schedules.
3 Q+ p6 ]+ r% H2 M$ g5 h! ~) @ : \- \$ K( D% D" f! ]
modelSchedule = new ScheduleImpl (getZone (), 1);
1 L: a% S3 Z4 W! d, p4 h modelSchedule.at$createAction (0, modelActions);$ b8 Y% v5 d, b0 V2 Q
- x. r/ v5 A, Z7 N. }
return this;1 y* Q+ r, `6 ]9 B3 {
} |