HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ e' W3 p1 t% i- u( X
% f) G* Q. W# B ` public Object buildActions () {
& T# N: @! E. I5 V& j: p super.buildActions();
$ f7 [; w, p+ b' h9 B$ k: D
; S9 C0 `3 z3 Q3 d+ a // Create the list of simulation actions. We put these in9 n1 o6 a- _; P
// an action group, because we want these actions to be
6 ~1 m* Y% U7 \: P" Q // executed in a specific order, but these steps should. ]9 q- R; ?( D$ n1 G7 M. O
// take no (simulated) time. The M(foo) means "The message% J3 ~3 a6 l% R; M& E7 _) R
// called <foo>". You can send a message To a particular
# z# J: @( B5 s0 p t, L R' B // object, or ForEach object in a collection.) m! V9 |$ s8 {6 o+ p
/ w( p- ~2 T( R, a' @ // Note we update the heatspace in two phases: first run
0 D4 b. C" D# B4 S5 l2 @& B3 E // diffusion, then run "updateWorld" to actually enact the. s$ b! R' V5 B H) B# O
// changes the heatbugs have made. The ordering here is, `( ^" k" A7 l' O% a1 X6 m
// significant!0 B" a- O+ ` q& E5 D1 B, U* K
6 k- L( c, A# _* @
// Note also, that with the additional
' R* i* n3 t, `" f6 [ // `randomizeHeatbugUpdateOrder' Boolean flag we can
: {5 r& x9 O9 r; S( @ // randomize the order in which the bugs actually run
& L/ \. \6 p1 B$ t t% D // their step rule. This has the effect of removing any
2 |2 u/ D: V+ W3 ]# ?; X4 E // systematic bias in the iteration throught the heatbug
5 n, P# m) p! W8 L# H5 ^ // list from timestep to timestep
8 t, J( m. {3 W( ~; d& ^5 J( P8 F
; E# Q# D0 [9 D$ w. Z& s% _ // By default, all `createActionForEach' modelActions have5 X7 u) r; |& m; g: R
// a default order of `Sequential', which means that the7 R. P$ P" m [! ~2 K. w1 e2 v' q4 X2 _
// order of iteration through the `heatbugList' will be9 _* p4 ?4 [" r- m
// identical (assuming the list order is not changed* w7 C' y/ B" P! E- j/ z) w
// indirectly by some other process).* N/ z+ g3 u1 ~2 X, r% c
0 r$ c9 |: g$ z i% v! z1 |. r modelActions = new ActionGroupImpl (getZone ());
- D( R* p9 `% ?% p$ N% u- D4 Z& F: k! i7 b7 g. n/ V
try {
- m2 C: H: R: H' X* }6 v; } modelActions.createActionTo$message, s. K8 M2 {! N0 s$ r# c9 v( n
(heat, new Selector (heat.getClass (), "stepRule", false));
* \* A! o/ w8 r$ ` } catch (Exception e) {1 _/ T! D. m7 o+ g: ~2 V) i- B4 l j
System.err.println ("Exception stepRule: " + e.getMessage ());
# q: D/ j8 S! v }
5 R7 z' ?# L3 X, K7 G& R7 _: I! ~8 x' r. H! I, p
try {
: A; t/ I" B8 B& W" I1 R( M Heatbug proto = (Heatbug) heatbugList.get (0);+ B% u# E8 ?& `/ d( y8 {; H
Selector sel =
4 \( K! H/ o1 K- t* J new Selector (proto.getClass (), "heatbugStep", false);
3 H1 _ q. G4 X; I! r- a actionForEach =
" _$ Z, i: _0 K+ ]1 _ modelActions.createFActionForEachHomogeneous$call5 [" d) x# t/ k, K9 S( `
(heatbugList,4 K( d, I% k# y ~# K9 g
new FCallImpl (this, proto, sel,
$ I/ G* t2 v; R5 B0 l new FArgumentsImpl (this, sel)));
+ y [9 y% Y7 f( W } catch (Exception e) {( ]: p* O2 T. m
e.printStackTrace (System.err);
! L9 j1 ?& W( \$ |+ [* _0 I/ x3 N }
& R9 Z" A# t3 l7 S7 \3 a
2 L; ]( b) I' k4 u# Z) e1 l9 f: o( c syncUpdateOrder ();6 p4 @7 L$ D4 ~7 p
4 y1 b) P" J4 X7 z- U
try {) Q$ U- {& V+ R7 i+ n4 A+ a- Y
modelActions.createActionTo$message
5 \' L) S8 [ r" H1 w& `, r (heat, new Selector (heat.getClass (), "updateLattice", false));! m, O# S7 J; R6 g
} catch (Exception e) {, Z8 R" y" ?/ X" u% j8 K+ {3 a
System.err.println("Exception updateLattice: " + e.getMessage ());2 b2 r, C* y% e) Z9 K: K
}* E) [/ Y' Q |3 J' g1 M* _
; Q4 n- Z! j% I0 n4 P1 S# j // Then we create a schedule that executes the
' K( Y4 ], n. e5 H7 [* g* c1 E // modelActions. modelActions is an ActionGroup, by itself it
4 o( A% p5 j3 U7 l // has no notion of time. In order to have it executed in3 a4 X' N {3 ]- J
// time, we create a Schedule that says to use the& y( H7 J5 Z) f4 L9 R! V9 ~$ h
// modelActions ActionGroup at particular times. This4 v, x# Y- u" v6 N$ ]
// schedule has a repeat interval of 1, it will loop every; E1 \7 V s' K7 z f1 w
// time step. The action is executed at time 0 relative to3 e% j3 |/ Z. b& U4 Z# M
// the beginning of the loop.
+ {6 T# E& z6 e: S5 {# K" y3 Z I% F2 X
// This is a simple schedule, with only one action that is
; t% o3 Y" w/ A' L5 l // just repeated every time. See jmousetrap for more
% v" x) Y6 r% M // complicated schedules.
- \+ L# q4 B0 u1 v9 w9 R
- R. t8 q; }# W4 }- S modelSchedule = new ScheduleImpl (getZone (), 1);
, R/ U# f; r5 h; ?$ z8 R) D modelSchedule.at$createAction (0, modelActions);
$ E. r* Q+ q5 k X6 E& s N1 r9 \
+ H# X, ^; D& x3 y$ i! j0 a, w return this;5 o' Y; | Q7 V& b9 x6 K
} |