HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& Z) \& k1 J" c; }: G& f! }! R/ e: R
public Object buildActions () {
" `6 D* N& x4 j/ I8 x" L7 ^ super.buildActions();
6 M" r9 N) b& f }4 L U2 D) S 2 G: @- A0 s3 I1 Z9 J. C1 Z, E
// Create the list of simulation actions. We put these in
( N2 g$ C' V! W! O' i; o* B" N // an action group, because we want these actions to be1 [8 A; U* r9 t
// executed in a specific order, but these steps should
* ]8 h) e. l* P; E // take no (simulated) time. The M(foo) means "The message
: z: [- k- y$ g0 o6 z // called <foo>". You can send a message To a particular
3 I5 z+ ~% Z( F- }1 W // object, or ForEach object in a collection." \8 L) z% [5 c+ W! ^" W9 H. l8 x
( v9 d8 ^4 Q- M6 n5 c, I // Note we update the heatspace in two phases: first run3 V( u4 N. q/ o4 h; v7 I+ D0 a% X0 ~0 Z
// diffusion, then run "updateWorld" to actually enact the
" j* w8 e1 x$ \$ m- \6 k4 w // changes the heatbugs have made. The ordering here is
2 ^3 { a, T, X' q2 M: ?3 A // significant!/ ~3 ?- K4 }) i+ U7 {
2 z2 b- T5 H2 _- d& k
// Note also, that with the additional1 q/ E& ^* { p6 o0 K
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ a+ ]% P# e5 u/ R+ `0 } // randomize the order in which the bugs actually run
5 O' ~$ \' N$ z# W // their step rule. This has the effect of removing any
1 S M) Y4 ~# k9 u% L // systematic bias in the iteration throught the heatbug
4 o. j5 ^% I- D" z3 b$ d/ C5 W- [ // list from timestep to timestep+ |/ o3 _* l. p7 m$ g- v
" i# E& ]$ a7 o9 t( u7 y3 B% U // By default, all `createActionForEach' modelActions have
4 b3 W* L7 M5 z5 c+ P: F0 L // a default order of `Sequential', which means that the
4 r6 U* |( f9 d // order of iteration through the `heatbugList' will be5 I" S. r& O9 _3 B
// identical (assuming the list order is not changed/ E7 s0 ?7 v- z7 D9 Y. p! [
// indirectly by some other process).8 H; m4 A. R' J9 K" K
/ c) x" L4 R W! J; }$ ?8 Q modelActions = new ActionGroupImpl (getZone ());8 P4 m9 k% |! a) W& x
1 v* M3 ~ }' G7 f& h try {
4 E& b$ X) y3 j% F5 _! A modelActions.createActionTo$message
- O/ P; o/ v- r- T. O (heat, new Selector (heat.getClass (), "stepRule", false));/ A- W7 E5 x: `" |( U7 p
} catch (Exception e) {$ ]+ L9 v' y$ n, b' M" [: b) U `
System.err.println ("Exception stepRule: " + e.getMessage ()); t! M1 q" m1 \! q, z" r
}8 G; @4 A! y/ K% b3 K! P
0 @: S: V C' h* d8 u8 q$ W
try {7 p( `; P$ L. }, f
Heatbug proto = (Heatbug) heatbugList.get (0);- N2 J& U& u( o" \- ~) s5 }6 \3 X
Selector sel = 2 \5 V, w( b3 I) |/ ?% K/ t
new Selector (proto.getClass (), "heatbugStep", false);& Q0 X; M O: _0 d( A E% t! }! I
actionForEach =# D; |% N. g6 R3 g0 C, p" L
modelActions.createFActionForEachHomogeneous$call
" A& u" \' ^: Z" {! v (heatbugList,1 `: W/ U% w2 S" _' V
new FCallImpl (this, proto, sel,/ U k5 J6 S! t
new FArgumentsImpl (this, sel)));
0 B! X9 P; X4 r' D } catch (Exception e) {
; |; W/ n8 v* Q- ]1 o8 K" l4 t e.printStackTrace (System.err);
& @% }0 u) Q! {/ s }
2 `& j; ]( D) N& }. e
3 I: T8 @9 J5 Y& ?9 c3 G# {- D syncUpdateOrder ();
4 G, A6 E3 j$ P/ T/ V
5 } O/ \- Q" W r4 ]" k$ n try {
7 P; K, M& b* D0 s+ V modelActions.createActionTo$message
6 C2 m0 O7 f7 K; U% N' v. W (heat, new Selector (heat.getClass (), "updateLattice", false));9 \7 T- E% ]/ O- r
} catch (Exception e) {$ b" v" U, M$ G1 u2 {
System.err.println("Exception updateLattice: " + e.getMessage ());7 t& s; U: u7 }! e2 C I6 I
}. s. r4 B/ I) B& v8 K. E
3 C8 l1 C7 ~2 F" N; X
// Then we create a schedule that executes the
+ C$ |. S, x8 r$ Q6 }3 I4 t // modelActions. modelActions is an ActionGroup, by itself it8 M5 d6 Y2 o) ~* Z2 Z
// has no notion of time. In order to have it executed in2 _ c0 T- x$ g4 U0 h* O
// time, we create a Schedule that says to use the8 W$ x4 Z- A4 V p+ M
// modelActions ActionGroup at particular times. This2 c2 d; a8 R/ p) y9 n
// schedule has a repeat interval of 1, it will loop every8 G3 j/ Z3 w) m1 i) n* K
// time step. The action is executed at time 0 relative to
; F q& [, b. l // the beginning of the loop.
6 n+ L/ s2 B% m+ ~2 o# d7 o P5 t& e9 G8 {9 m. |
// This is a simple schedule, with only one action that is8 L7 i- L! F; M! [/ E5 s
// just repeated every time. See jmousetrap for more+ |, L6 N: M7 W4 J2 H' c# O* r
// complicated schedules.% Y. j2 }' e, U; a% }
/ X; J% E/ _! d& k
modelSchedule = new ScheduleImpl (getZone (), 1);0 _. }6 a( @; e/ i* O7 n
modelSchedule.at$createAction (0, modelActions); O7 N6 j4 J2 n5 e& T
$ f% l1 R/ Z- i+ p6 J8 m; u
return this;
$ i, s: b$ A. e/ G } |