HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( f% |& G6 o0 k; x( P0 J
: V" b! r8 H$ e# V8 w1 [- | public Object buildActions () {
: r) |8 u$ x! G2 @& L) ^2 a" Y+ }6 p super.buildActions();4 e1 U% k& k0 K# O! E. ?# R8 W
2 n5 k) e4 ^7 \! d' m
// Create the list of simulation actions. We put these in
0 E6 Y( c, j, u- X z- X // an action group, because we want these actions to be" _4 h* T- o1 q; V) E
// executed in a specific order, but these steps should y# e8 L/ j. b: V, {
// take no (simulated) time. The M(foo) means "The message% p+ j, m. s) B! `
// called <foo>". You can send a message To a particular, u8 H* v# X C# w+ r$ j1 `
// object, or ForEach object in a collection.1 [% m; S6 m6 L& [- e4 Y+ r
6 O$ c- `6 S3 h [+ A& ^! O, s
// Note we update the heatspace in two phases: first run
" @1 ~0 @2 u& U8 Y+ ~# |! k5 b* L // diffusion, then run "updateWorld" to actually enact the" F6 z h* a' s- U$ |
// changes the heatbugs have made. The ordering here is
/ ]6 w( b. Z, J* D // significant!) P& H% g& ?9 x) x2 j
1 j3 z4 o1 o( A0 p
// Note also, that with the additional
% ?. V# R) \. ^- p // `randomizeHeatbugUpdateOrder' Boolean flag we can( }+ u+ F3 v2 t$ q: ?: V! A3 X
// randomize the order in which the bugs actually run
: Q; ^. `; c& N0 E // their step rule. This has the effect of removing any- B$ b7 b* |4 h: }9 d* r
// systematic bias in the iteration throught the heatbug
2 ]- K% b- s. X! y- Z' e // list from timestep to timestep7 v$ ~5 _* E) f4 Q& p) C) A0 b
5 Y0 C/ y$ O4 S7 e4 y- q
// By default, all `createActionForEach' modelActions have
$ d# \& {- G* \. p7 j // a default order of `Sequential', which means that the; J R! Z: n4 s: a6 C! R1 W
// order of iteration through the `heatbugList' will be, S6 T, S4 U8 o( k8 }& i$ R: K
// identical (assuming the list order is not changed
" y' j/ ^+ Y2 }! V# @7 L/ D* U // indirectly by some other process).
1 o/ Q9 W2 A5 k; p) {8 }
( C7 F z# ?& |/ H3 N modelActions = new ActionGroupImpl (getZone ());, o0 {' Q' I4 p( G- W
5 Z+ @: ~) q* u1 |3 f! I
try {7 L. R7 _+ }5 D+ n+ n
modelActions.createActionTo$message
6 d+ i6 h. M- ?/ h; K" ]8 \ (heat, new Selector (heat.getClass (), "stepRule", false));# s. U- r/ A# @" q5 S
} catch (Exception e) {" b: H6 A( {* j6 c( Q
System.err.println ("Exception stepRule: " + e.getMessage ());# |. n/ q! O7 t
}
! K2 c" @- B! m6 W5 W9 d4 y A" ]0 M
try {
1 I! \0 U( ^1 i" M5 d8 t Heatbug proto = (Heatbug) heatbugList.get (0);$ y p; M9 [! F' M2 y* K
Selector sel =
1 @7 ~; p% @8 I new Selector (proto.getClass (), "heatbugStep", false);
- H% }5 Z9 D) j) x actionForEach =' }+ K4 e( n5 b4 d' u/ P Q
modelActions.createFActionForEachHomogeneous$call
" k+ Z! u9 H! J$ U6 H (heatbugList,
X$ L* v- y4 f3 \+ Y; E. E7 R new FCallImpl (this, proto, sel,) _2 O! v) ]2 ?% q
new FArgumentsImpl (this, sel)));
0 r ]9 v" L5 J/ [ } catch (Exception e) {! L- Q/ d8 g' m1 E0 I* w8 l
e.printStackTrace (System.err);0 D1 @# n% Q1 Q) c1 }. _
}
U1 ]. [! d- x8 r y0 L J. G
6 A- M* B& N* i/ ~/ E- l+ e syncUpdateOrder ();$ }0 M/ U1 d; G5 e0 }; ]4 `
" p) U) S8 _/ f7 M1 x$ W* J. _, ~ try {
* v. K- Q0 V) M3 U- X modelActions.createActionTo$message
/ m! V& \% j6 o' m8 a2 U' Z (heat, new Selector (heat.getClass (), "updateLattice", false));
4 z3 A$ h2 X# ^3 O7 s; d( A } catch (Exception e) {4 @, O( |& @8 p( ^* G* T
System.err.println("Exception updateLattice: " + e.getMessage ());
8 _- h7 t' t# H4 a. k" D' v ? }# q+ i" w9 e" b% A/ ?% c
# L* P( [1 V% ~& G5 y // Then we create a schedule that executes the* u) g2 n6 S* R" \* x
// modelActions. modelActions is an ActionGroup, by itself it2 e5 r# h C' i8 S+ |- R4 X( ~
// has no notion of time. In order to have it executed in- V( |, O! t1 {- P5 l: P0 l
// time, we create a Schedule that says to use the
8 ^) d" J) q) O // modelActions ActionGroup at particular times. This
) D) c$ H# z9 H6 r2 m. X' X" m- t$ } // schedule has a repeat interval of 1, it will loop every- c/ C3 I4 D5 x. `% f! h& u
// time step. The action is executed at time 0 relative to0 S# T# B5 z7 W- z8 z' F. n
// the beginning of the loop.2 i6 `1 F" B8 n9 j2 q) H
. n# E; t5 G, D$ |; w8 q$ J) j // This is a simple schedule, with only one action that is/ T0 M0 A% B4 l" c
// just repeated every time. See jmousetrap for more
: o. S. d# z9 m9 T, T2 L; y // complicated schedules.) L& Y8 Z1 o: v) J" A
5 M; D2 o, o: Z+ b* D& K modelSchedule = new ScheduleImpl (getZone (), 1);; @; g, g( A4 `
modelSchedule.at$createAction (0, modelActions);9 u* [* q% A: h! P' w
; b8 l6 a) H: a: @: F* A return this;! F9 _- V- f" P# h' M, j
} |