HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 B# r: S4 R" B- Q
: K0 D1 r+ k9 H6 Q4 @& O
public Object buildActions () {" g3 _: w u3 [4 L, M- b
super.buildActions();
& a) S4 n- f/ V( I5 Z( u& K # d3 q+ n. M* R3 l4 S3 G
// Create the list of simulation actions. We put these in, S! ~* N% Q1 W6 M
// an action group, because we want these actions to be: F% E: K( d# `4 x0 r
// executed in a specific order, but these steps should
! I, o: q$ @5 u' s8 b/ e // take no (simulated) time. The M(foo) means "The message
' Z0 q- d" ^' t! A, N // called <foo>". You can send a message To a particular
% Q- }% z2 _# P7 s/ g& r // object, or ForEach object in a collection.3 D. Z& }5 i. {: c, k2 j" r
3 b# ]! s; ~& N" w' j9 z- z2 R // Note we update the heatspace in two phases: first run
; ]0 D. @' w* J( B6 ?$ W // diffusion, then run "updateWorld" to actually enact the; Q; t! p: i7 z6 r+ k
// changes the heatbugs have made. The ordering here is
6 k; j: k9 ]5 W4 K' N' Q' K9 v1 q // significant!
' Q. x( } O" b1 _6 Z 5 V$ w( ^& X9 S( o5 M6 ~
// Note also, that with the additional
3 N V! t* x) @ _: q // `randomizeHeatbugUpdateOrder' Boolean flag we can0 c% R7 `8 E: @- R0 J8 G
// randomize the order in which the bugs actually run! H# U+ Z6 k. q1 v: |
// their step rule. This has the effect of removing any, L/ \* k. Y) Q
// systematic bias in the iteration throught the heatbug5 D6 O# e: ^: C* \. V: B8 I
// list from timestep to timestep* Q) p, u5 m- c3 `
- B3 ~9 g% H Q% Z // By default, all `createActionForEach' modelActions have
. v) K/ A) f, B# H9 a // a default order of `Sequential', which means that the
0 M/ Q+ V$ s" P* Q4 W0 T! T% t // order of iteration through the `heatbugList' will be' K0 I% }! J4 K% }: a
// identical (assuming the list order is not changed9 t4 }5 Q3 H( L
// indirectly by some other process).
" h: n/ z6 b' ^! x9 I1 _ 3 m4 d R. H" U9 w, N
modelActions = new ActionGroupImpl (getZone ());
% G8 \# W V2 m4 Q
* J5 j! Z" m0 A) x try {1 {7 m# ?4 f7 ~$ z; M
modelActions.createActionTo$message
, \0 Z# Z( y# g9 G Q (heat, new Selector (heat.getClass (), "stepRule", false));
1 B* E% b$ Y( N& D } catch (Exception e) {
% ^, N$ D* e a& m3 r System.err.println ("Exception stepRule: " + e.getMessage ());
' N& y' G/ @$ x5 `% i" t }1 l1 P% L7 L4 e- [
" y0 K! ~4 d( l+ O: e( R# z, J2 y try {
, g& a& ~# g" S0 u% N* E2 b Heatbug proto = (Heatbug) heatbugList.get (0);
, v8 l C5 k% H) e( U+ ~ Selector sel =
$ g4 c. o8 D) g1 E* R1 r- i) I new Selector (proto.getClass (), "heatbugStep", false);1 J/ M1 P7 Q' q2 O' J4 Y
actionForEach =+ w( T" k5 P7 w" P
modelActions.createFActionForEachHomogeneous$call5 o( y. {* {- u: M, f
(heatbugList,7 C& I; B- C$ h( p, E
new FCallImpl (this, proto, sel,+ p( S8 p- w7 f; _7 m5 t
new FArgumentsImpl (this, sel)));! F/ |' y0 T8 l' {+ t
} catch (Exception e) {
, @8 v9 E! D5 i. } e.printStackTrace (System.err);/ u$ K5 o4 ^9 p* ] I z
}
?# [+ R. Z7 e* r
" ~& P5 F$ T3 Z: Q syncUpdateOrder ();. c- r" F0 f5 [# A J3 c+ ]
2 K8 @" ^4 c9 ?- a
try {
+ o& X& j# q, A& |8 q$ v* V% J modelActions.createActionTo$message
0 e' b$ W5 u5 {( R5 m" d4 Z (heat, new Selector (heat.getClass (), "updateLattice", false));
9 K% g3 ~& Y% G. W9 t1 i } catch (Exception e) {1 f3 V! m+ j! n
System.err.println("Exception updateLattice: " + e.getMessage ());. l! _" N" ~) I! s3 Q i
}
) h6 {8 {/ ?3 l' Z- S+ `, n; d ( [& `+ \! ?+ r
// Then we create a schedule that executes the
$ q. f) L( z' j# L // modelActions. modelActions is an ActionGroup, by itself it/ d1 D% }8 |, T5 `& l% E! k
// has no notion of time. In order to have it executed in, `- }8 j$ v, t- C! l, e% n0 h0 g
// time, we create a Schedule that says to use the* \, s3 e) W4 a p: {
// modelActions ActionGroup at particular times. This8 ^5 U$ c0 ]! [8 O( X
// schedule has a repeat interval of 1, it will loop every4 E A. ^& W$ C4 F- O
// time step. The action is executed at time 0 relative to5 D( o! ]4 y; L
// the beginning of the loop.
1 m1 |+ D. y0 b7 |5 r; ?# Z9 U: V) e" h
// This is a simple schedule, with only one action that is
/ A) ^3 Q- {! N8 R* f4 H; M+ U, }# F // just repeated every time. See jmousetrap for more
) f! [, w9 b" Y0 h. e" V, k // complicated schedules.
4 j1 S0 Y8 \3 _' f2 a. S- c % p* V* P8 N6 }7 z
modelSchedule = new ScheduleImpl (getZone (), 1);
( ?0 p" l7 d; G. b modelSchedule.at$createAction (0, modelActions);' R* A" _, K9 h
3 M8 N# B4 y: M; e8 R return this;
; u2 }( a5 E- m- G5 \) ~ } |