HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( |% h4 Z, q: B# k
8 E, R6 J0 u" |+ [: i. d+ b public Object buildActions () {
: S+ G$ Z% \, n" ] Z! T) A super.buildActions();
0 l' i" B$ ?8 g3 u0 q ' E7 `! f% \% L0 P9 Z
// Create the list of simulation actions. We put these in/ \( G j2 q8 Q) \ K, m
// an action group, because we want these actions to be
' M' n9 H1 t* ] ^3 N/ U* I // executed in a specific order, but these steps should9 r0 a: W5 T0 s+ `
// take no (simulated) time. The M(foo) means "The message
. x# H S& |' `3 d& [ // called <foo>". You can send a message To a particular! d5 h# K/ H: M( M) u$ _0 F7 s
// object, or ForEach object in a collection.# q7 }( z6 L8 g" t# p
! [7 N, E* \% d+ Z! N: M
// Note we update the heatspace in two phases: first run0 m1 p( s0 r% e) F+ J
// diffusion, then run "updateWorld" to actually enact the j, h8 ]3 l ]8 a. j3 p; T
// changes the heatbugs have made. The ordering here is
6 r, P$ M' \6 Q6 f O- R // significant!8 | O# _$ N/ ]. D' d
. W/ L3 p1 J$ }9 a+ ]: A) i: ] // Note also, that with the additional: k! I6 R3 ?; D* {
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ x& K7 ^8 Z0 ?0 o
// randomize the order in which the bugs actually run
F7 Z: l3 F" I& `4 x `/ z! t l // their step rule. This has the effect of removing any
( { o( D T* X/ q% Q; ~ C* B // systematic bias in the iteration throught the heatbug
' H) o8 @+ I" G$ f // list from timestep to timestep# ?" Q9 y6 Y/ T7 m6 U4 W( Q
' W' k+ C$ e$ R% ]# e7 X0 x3 l
// By default, all `createActionForEach' modelActions have1 K: E/ U$ \4 g t) s* a+ [7 Q7 f
// a default order of `Sequential', which means that the
* Z7 w5 X: r: V // order of iteration through the `heatbugList' will be. i* E s9 ^6 y% }! k9 Z8 n4 o
// identical (assuming the list order is not changed& K9 l5 E, e, c2 ~) Q5 E
// indirectly by some other process).9 S3 f4 c, x1 c7 W- C
6 K7 N. i7 e2 ~) m+ R- E modelActions = new ActionGroupImpl (getZone ()); b+ w2 }& x- u8 v3 `/ c8 o
6 U5 F3 z. Q6 Q+ t
try {5 K) L" S: Q% t( r% g5 g" R
modelActions.createActionTo$message" ]; v$ j5 j8 p9 F
(heat, new Selector (heat.getClass (), "stepRule", false));
+ E' b" G0 G4 v7 N7 w4 r% m& Q } catch (Exception e) {: B: C Z9 T3 w3 m. n
System.err.println ("Exception stepRule: " + e.getMessage ());* ]* X, N4 F$ A( k/ y
}
3 d6 R2 Y" O/ r
( N& T( y4 q, S; G try {! ] v- X. P# V6 h2 }* J
Heatbug proto = (Heatbug) heatbugList.get (0);$ W9 z$ X" m; J
Selector sel = 2 R2 b5 a7 m+ X) d* d7 U5 o1 Z
new Selector (proto.getClass (), "heatbugStep", false);4 N9 w# k" g8 L" Q* B1 W8 C
actionForEach =
( ]' ~) D. {) G' H2 g modelActions.createFActionForEachHomogeneous$call5 \! I5 w) u: P. [5 L2 Z
(heatbugList,
/ V! y& O3 ]6 b& D1 u' `( p, X new FCallImpl (this, proto, sel,
# f$ M2 H8 d. X( y( U8 c new FArgumentsImpl (this, sel)));' F; W1 d6 P# V1 D- T4 Y
} catch (Exception e) {& H( y2 d5 ]1 K( D- i$ c$ ~
e.printStackTrace (System.err);6 V) P& t% j' F$ S8 [, v& S" G
}6 _$ m6 ^" n4 ~( o" [- w1 R; [
0 ~) p+ I7 u* \1 e! y
syncUpdateOrder ();2 S2 S) d2 g, p1 m' M
+ ]# a7 p" z- F5 V. _ try {; b$ P4 j. X) @
modelActions.createActionTo$message
9 Z2 |, Q8 K) A {5 j (heat, new Selector (heat.getClass (), "updateLattice", false));/ d, N/ D: _: K, j2 T
} catch (Exception e) {
! e" ^9 t n0 V' C System.err.println("Exception updateLattice: " + e.getMessage ());' K" W" o) k( c- z
}! t, }* [ D- a. z ~6 Z, W2 ]: g
; Y' o8 j5 f7 n l$ }1 A$ e // Then we create a schedule that executes the+ z! Y7 ^) V7 R3 x# D
// modelActions. modelActions is an ActionGroup, by itself it( a( l9 |+ V8 G6 P% c, Z
// has no notion of time. In order to have it executed in
: U& s/ A. { }: ?! \4 m // time, we create a Schedule that says to use the5 \/ S: x9 {/ ^5 \" g1 P/ Z
// modelActions ActionGroup at particular times. This- _0 H* T2 v2 P8 \4 S/ B
// schedule has a repeat interval of 1, it will loop every
1 m7 F9 Y4 n: U) D // time step. The action is executed at time 0 relative to" W. G8 ?( ^; l
// the beginning of the loop.
& k7 n! S) X1 K" q
' [; A* m4 h* r3 R( |2 m // This is a simple schedule, with only one action that is
& U, w7 ^* W0 N$ ]% {! K // just repeated every time. See jmousetrap for more/ G. z& A9 _) W0 }2 n! x3 P
// complicated schedules.
7 K& b& }% S2 f8 L1 s- M+ B C 8 J/ V1 Q. Z+ @/ S
modelSchedule = new ScheduleImpl (getZone (), 1);" ~8 B8 F- Z" ?& h, A& z* N
modelSchedule.at$createAction (0, modelActions);
9 C; S* T" A" g Q ( s0 h K* J2 W, G. j5 Y. j2 L, r2 Q
return this;
2 K, |4 q5 s; L, d5 {1 N } |