HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& B9 O: |3 s& o+ Z: Q& |" X% A F4 }/ o1 h
public Object buildActions () {
- p: {" P2 J3 y* Q super.buildActions();
+ ?! Z+ ^ X; `# m 0 {5 f& p, p9 y5 X) o+ t
// Create the list of simulation actions. We put these in
# ^% X. J" R6 @# B4 V( \' J // an action group, because we want these actions to be3 H9 g/ U. q% W/ q! L9 x- w
// executed in a specific order, but these steps should' z) l" f/ r* o7 ]6 W9 _( D, J8 \' I
// take no (simulated) time. The M(foo) means "The message
+ @; X7 j# w& N // called <foo>". You can send a message To a particular" H& H' i3 s- Y8 d
// object, or ForEach object in a collection.3 u2 S. C. i% v) U
4 P8 [- `$ D' ]2 ]& {9 x
// Note we update the heatspace in two phases: first run
. p4 G# K- Z2 }4 K // diffusion, then run "updateWorld" to actually enact the' B" d& m1 V) u3 D
// changes the heatbugs have made. The ordering here is* m3 e7 j4 [( _& e5 @- }) p
// significant!
# [/ ^8 @# w2 X, I0 n
" K, a! x- b+ b; B' p // Note also, that with the additional2 }; J S* U V1 V3 [7 D
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 E% H" c% | R6 A7 V
// randomize the order in which the bugs actually run6 K2 q6 F% X# b" q; E$ N, L5 `
// their step rule. This has the effect of removing any% G+ e9 g! x# v1 l7 e. C$ J2 X
// systematic bias in the iteration throught the heatbug6 i* S A# j5 X* B& p, j4 j% \; u% f
// list from timestep to timestep. u7 o! C7 B& c- V0 _
5 k" Y/ x* G0 i, ~7 c& w // By default, all `createActionForEach' modelActions have
- m8 h1 _9 X' Z. r. B* g- x // a default order of `Sequential', which means that the
# c, m( b# } P' I: s3 u // order of iteration through the `heatbugList' will be
6 ?* G5 ]# ?/ { // identical (assuming the list order is not changed/ E( ?0 T- }2 t- v5 B
// indirectly by some other process).* _& Z. e" a$ a9 I; s! w/ I
3 s7 _9 h5 d) K/ m1 J% \
modelActions = new ActionGroupImpl (getZone ());
. B0 ]# n2 x o, x6 Z! f, D; X3 n- L/ ~+ `9 E" Y6 `5 v# m& P- A
try {
, Y- ^0 V, L! `0 l( Q# X modelActions.createActionTo$message1 r/ V# O' \! R" x7 m) ~ r; M' T3 C
(heat, new Selector (heat.getClass (), "stepRule", false));
4 C$ o0 N$ }2 z9 X* C u } catch (Exception e) {
9 r% E1 U( h- r/ L# M2 {! T- a: a System.err.println ("Exception stepRule: " + e.getMessage ());) S) o J9 Z+ n& I2 e( l' R+ I
}
: G- {/ X& b' M5 r( _- b0 @ F) ^: c7 c" G$ w1 v; y- C% Q6 b
try {
5 l5 D" a5 S0 N1 y/ J Heatbug proto = (Heatbug) heatbugList.get (0);
, t9 Q5 K+ D$ r' p7 G; L Selector sel =
' g2 c, t$ F! a0 R8 { new Selector (proto.getClass (), "heatbugStep", false);
, K- g$ H9 z( F/ g$ O2 Y. x5 s actionForEach =* B$ h' x4 a# X I
modelActions.createFActionForEachHomogeneous$call, D4 v8 o' D* Y0 K1 p3 ^# k7 @
(heatbugList,) C( N* L, Z1 |0 Q$ i
new FCallImpl (this, proto, sel,
; ?4 K7 z S1 L; f: k* e$ w new FArgumentsImpl (this, sel)));+ @8 W2 Z0 J4 \
} catch (Exception e) {/ q1 j; x' F' a. E8 Y6 Q% {/ O# U7 m
e.printStackTrace (System.err);
4 C9 [" v. Y, A$ M3 H7 C0 x4 A/ h }
! x! E9 a, d& }
: f' g) f( O' s7 R; B syncUpdateOrder ();
" a* a9 ?8 J! A5 \) u
- I0 p" T$ V/ F& Y$ G7 {$ d9 B try {9 b6 c: [" n4 p/ B, M
modelActions.createActionTo$message ! E6 l& _; k8 G+ N3 H
(heat, new Selector (heat.getClass (), "updateLattice", false));
! }8 L; j. a5 P3 T9 {+ O( M } catch (Exception e) {$ f$ v* w, K" H5 W
System.err.println("Exception updateLattice: " + e.getMessage ());, R0 ^3 D; Q5 n2 R: K
}
0 [$ H- r! n& I9 g! a
9 R8 J# O0 d5 t! S // Then we create a schedule that executes the
_, o2 ^$ K7 ?& c1 y$ m // modelActions. modelActions is an ActionGroup, by itself it
& d2 i$ s% c6 U( z // has no notion of time. In order to have it executed in) U" ~. m! m! `# g
// time, we create a Schedule that says to use the$ R7 [' G) ^; i5 j: z" H
// modelActions ActionGroup at particular times. This; `: E3 x$ Q- u5 O! L
// schedule has a repeat interval of 1, it will loop every
2 i2 |1 z0 P; K7 G* `- R7 l // time step. The action is executed at time 0 relative to
3 v& a% c+ O2 d! M/ L! o! ]3 g2 V% D // the beginning of the loop.
* [4 W# L) O7 Y/ O) W* Q" T8 _. S g3 u7 @3 Q5 a
// This is a simple schedule, with only one action that is, _+ s7 F, C* N$ x
// just repeated every time. See jmousetrap for more) R# s, ~7 g( Z" h
// complicated schedules.8 y$ B- h1 i% j/ X- _' V+ O2 x4 P
/ z; p: Z7 b K9 y7 H
modelSchedule = new ScheduleImpl (getZone (), 1);8 q, @8 i, C# D
modelSchedule.at$createAction (0, modelActions);
/ R1 x. u" E E' n
* b7 a4 r r4 i) P" {; w return this;
; ?2 k# f' E& q2 g, m } |