HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) f$ s; h' v8 ~
$ E1 ` t& | j0 m: J" \ J' M, [ public Object buildActions () {- @5 _$ G6 U5 Q* E( N2 `
super.buildActions();
* e) j; f0 z; _8 \6 A* Q9 j
- u3 r% e8 h- S3 j/ ]" p M9 k; E9 ? // Create the list of simulation actions. We put these in
0 ]: |# i) A/ y) p // an action group, because we want these actions to be
3 y; R8 S G, @/ K // executed in a specific order, but these steps should
4 L) y6 s( o& w& s' n+ R: f // take no (simulated) time. The M(foo) means "The message# t% T. S( Q+ r% V. n$ g, [2 n7 k
// called <foo>". You can send a message To a particular
) \, O9 J" z# {7 n) D // object, or ForEach object in a collection.
6 a. H7 C5 f; n& w
9 t, K- f0 g; G: O8 I4 h // Note we update the heatspace in two phases: first run/ W% _% \3 c! X5 F
// diffusion, then run "updateWorld" to actually enact the
! a3 h f {1 o* i- A+ H1 I" g // changes the heatbugs have made. The ordering here is% l' t0 E2 ~8 M" |0 D
// significant!
$ G# H D# A: e- E% r& O
6 Z$ r2 b* m p, D( q" b, n7 X // Note also, that with the additional* V% n* `6 B+ U Z* ~# O& ~: f% ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can! l9 L* Y4 z4 _
// randomize the order in which the bugs actually run1 |. P6 u9 V; H% E: j$ ]+ Z4 W4 V. ~
// their step rule. This has the effect of removing any
8 U* r- [* @! j9 C // systematic bias in the iteration throught the heatbug- o7 |. l `! y" A. q/ |1 `
// list from timestep to timestep
/ w8 j+ }/ J9 f7 N2 v q
+ Z' t1 T" I! f$ a( J% Z- q // By default, all `createActionForEach' modelActions have
3 i9 Q2 N0 K% { // a default order of `Sequential', which means that the
& i, \; N0 y, y& k" V- m& r // order of iteration through the `heatbugList' will be' k1 C5 S+ O+ ?1 M5 [
// identical (assuming the list order is not changed" u Z' b. ~6 k, ]+ U W, @5 T5 T
// indirectly by some other process).
: |$ \3 t0 L, [# v) y
4 h5 C6 a, f3 w' N" w/ q q modelActions = new ActionGroupImpl (getZone ());( i9 p6 E* Y* ~2 b, K6 F; M
! V, h8 l$ @! b
try {
' X7 ]' _0 o" q modelActions.createActionTo$message' }/ `0 f) p3 \/ P9 T' g' H! j
(heat, new Selector (heat.getClass (), "stepRule", false));
+ n/ d, ?: M0 R) K- T0 e } catch (Exception e) {
0 b) \3 c2 N/ g2 T System.err.println ("Exception stepRule: " + e.getMessage ());# j/ g+ C7 ^: {* h' z1 ^2 A
}& M2 o1 M2 t, W) i) t
+ n- @' S) k. A5 w1 _* d4 m( a7 q try {
* E7 L& B) P8 j& }. X' W/ N. f Heatbug proto = (Heatbug) heatbugList.get (0);
4 W) L0 c9 t( Y( f Selector sel =
6 Q: g ]5 F! ]7 z/ E& y0 X6 z% n. r new Selector (proto.getClass (), "heatbugStep", false);
/ h) M/ A0 s# ?0 V actionForEach =
" r9 ^ W- `5 C3 V: B2 M9 p6 B modelActions.createFActionForEachHomogeneous$call Y+ U7 N/ |4 [3 P1 l% m. n% c
(heatbugList,& l3 Q2 n! l( H3 r; ^2 B. A4 I
new FCallImpl (this, proto, sel,0 X1 `- P: M' v6 t, Y. x# K H
new FArgumentsImpl (this, sel)));
/ M. [3 N, i/ J7 R3 H# G8 ?$ V7 T& { } catch (Exception e) {+ @& b& S. n% z* `
e.printStackTrace (System.err);% g2 C! N5 P: U. i3 H3 n% N. O2 }. c9 F
}
. a* @3 g3 c O. V& G2 O4 _ $ T5 V# T P0 D
syncUpdateOrder ();
0 ~! c. \) c+ S# [# J1 U: x4 S9 `0 ]" A, k3 V1 n2 P
try {
7 V7 v, j e+ @' t! I$ X1 S5 J modelActions.createActionTo$message
0 S `- a( }7 i; ?) u (heat, new Selector (heat.getClass (), "updateLattice", false));0 L) Y3 {5 [: r: r" n6 C/ A3 r
} catch (Exception e) {( s! z; Q/ [. n5 p$ I
System.err.println("Exception updateLattice: " + e.getMessage ());
0 \1 i# x7 ~! l }2 q3 g- s! e7 m* R" x
/ P$ U" ]( g8 e1 @6 K6 p // Then we create a schedule that executes the! b& D" {3 e a# u: C6 [; u( p
// modelActions. modelActions is an ActionGroup, by itself it% q" C v* b/ }! e' a9 A# }
// has no notion of time. In order to have it executed in
+ [) B" _4 b6 I% F7 d; r; H // time, we create a Schedule that says to use the w! f& S2 e) r R
// modelActions ActionGroup at particular times. This
6 R* `, g9 M& R // schedule has a repeat interval of 1, it will loop every
4 z5 Q1 u- d/ z/ z Z, S // time step. The action is executed at time 0 relative to
! T/ w) I5 R. s! s/ t // the beginning of the loop.
" n! n z; h& `% ]3 q& N1 `/ c }# o4 A! N L/ P* k
// This is a simple schedule, with only one action that is* Z+ {4 z3 G/ Q, U7 a
// just repeated every time. See jmousetrap for more
; j4 v. W( z: i) K0 C$ k // complicated schedules., ~* k/ v9 V, @
' N) Z& t. v( z# ~
modelSchedule = new ScheduleImpl (getZone (), 1);
6 A! _* o0 D1 r) m" ~ modelSchedule.at$createAction (0, modelActions);
# J+ r Z' J2 F 5 [ G: R+ {" w/ h7 S
return this;8 T- j0 g3 H* K5 _+ D( m! }
} |