HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" A2 x! W' r; {' n& x4 t( F, j
+ z; ?5 J4 C% ?$ w& E: e2 z
public Object buildActions () {% |3 N9 s9 S* T+ m' k
super.buildActions();3 D9 ]' {5 ]) O! t U
! a' n/ G1 [3 I# T, G3 G7 y
// Create the list of simulation actions. We put these in- ~" h+ Y* W# X$ [$ ~9 h4 b9 Y
// an action group, because we want these actions to be Z8 `3 O' O& U4 ?7 U5 n/ z- F
// executed in a specific order, but these steps should7 I F; F# q J/ }
// take no (simulated) time. The M(foo) means "The message
( Q% l4 ^1 y1 {/ i // called <foo>". You can send a message To a particular
# y" V0 [1 A* F% M1 w; b0 ~% }* K // object, or ForEach object in a collection.
% @1 F& Q! P9 C# ?7 V, Z# ?0 B
7 |/ Y4 y, \, T# ]+ M& }1 l // Note we update the heatspace in two phases: first run
, ^/ d9 a) O( W' q& w' h // diffusion, then run "updateWorld" to actually enact the
. h7 ]& _" D) J& \) P( _7 q; R. R // changes the heatbugs have made. The ordering here is/ X* y3 \$ X m" g; Z1 Q8 y P8 y
// significant!
5 v. ^) h! q, d+ d- U7 j0 E( l
3 t( a# M4 P# U; e" v; K // Note also, that with the additional+ O/ ]4 U, J6 L$ [7 \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% T, M! `! z- D. ~5 \7 C // randomize the order in which the bugs actually run
6 B5 I+ o( y4 k. h; ^ // their step rule. This has the effect of removing any
9 C7 g7 ?& a' B! w- i // systematic bias in the iteration throught the heatbug
$ X9 A# j. h' ^# }4 p" f4 K // list from timestep to timestep |3 Q5 w) n U9 E0 g3 [" P8 ^/ H
( x6 I8 k2 e% t8 j // By default, all `createActionForEach' modelActions have
* v( [2 e* K- |' d+ A) u // a default order of `Sequential', which means that the+ K7 [( x( x6 X0 B5 ~$ L
// order of iteration through the `heatbugList' will be
0 }8 r' a: z2 e( a0 X // identical (assuming the list order is not changed
" Y# k3 ]& z2 a) E; n1 [ u9 I: R // indirectly by some other process).
0 p$ E2 O; \; S; L
8 y" h$ I, H) V( m/ ^ modelActions = new ActionGroupImpl (getZone ());
' d5 Y3 h' o2 Z3 u' p
; I; n8 b& R" A% i2 Y. b try {
' O c8 ~2 ]6 r8 q modelActions.createActionTo$message+ q$ o( ~% @ N: _. E5 q, g
(heat, new Selector (heat.getClass (), "stepRule", false));
5 k6 j; Z1 v- e' c: h } catch (Exception e) {& x5 c* h3 N% F. V; }
System.err.println ("Exception stepRule: " + e.getMessage ()); ]+ k, p4 l* V$ F3 @8 X, \
}
8 i! e/ b: u" O+ ~. ]" A' A3 `8 F$ E. R
try {
?# C5 N5 T2 K# c% w! c' g Heatbug proto = (Heatbug) heatbugList.get (0);
]" `2 J" j7 x, A7 O Selector sel =
/ S- G1 W: D- l, |6 E new Selector (proto.getClass (), "heatbugStep", false);
! L. S9 J* ~6 `5 a, Y8 N7 f6 R \ actionForEach =
9 v) j4 Y; |$ c: y modelActions.createFActionForEachHomogeneous$call) L9 {, u% s" h W3 k* w3 K
(heatbugList,
# {; z' j3 n9 h0 s" J new FCallImpl (this, proto, sel,
5 t1 E$ p# z3 R& @" i new FArgumentsImpl (this, sel)));" ^4 j/ u% q) n
} catch (Exception e) {1 C: L! D' I$ w# k# F
e.printStackTrace (System.err);
2 W* u* F8 g( P5 h) U/ A }, q6 D1 W" e/ `) _9 e$ \
* o( [3 T4 F0 r8 G: W( w( P+ ^ syncUpdateOrder ();
" }, L" L" z% q2 U; n' h! B+ F t; o$ i8 G
try {
- B: v, ]' V1 W- x# V& c modelActions.createActionTo$message
- c( o$ A i! q4 A7 n (heat, new Selector (heat.getClass (), "updateLattice", false));
; C! t3 x! D$ I9 Q4 ~ } catch (Exception e) {. D. n8 t! [- x" |" m
System.err.println("Exception updateLattice: " + e.getMessage ());
C" H3 G- G1 R) m+ l }" q2 D2 J- v6 O6 C6 a
" ~1 j+ |$ j/ S2 R) ]
// Then we create a schedule that executes the
: P* k: L# [! J( t // modelActions. modelActions is an ActionGroup, by itself it
- u u( N8 P7 J! a/ s // has no notion of time. In order to have it executed in* O! l. F* _+ Q& Q' Q9 E
// time, we create a Schedule that says to use the
3 S" B/ @' U- s( }6 l- k. y4 R // modelActions ActionGroup at particular times. This/ X( A9 N% T7 l. Y
// schedule has a repeat interval of 1, it will loop every" v7 i$ ?: g) c/ `4 {
// time step. The action is executed at time 0 relative to/ |+ l- u/ F, V" l4 X- I
// the beginning of the loop.
4 F3 B' A6 _: ^" d) H8 `. U: ^
* x) g. A6 x2 g8 H6 _ // This is a simple schedule, with only one action that is
$ \1 }$ l h; L f6 M. V; n7 c // just repeated every time. See jmousetrap for more( F0 T6 \* Q7 z- J- f3 M+ z
// complicated schedules., v7 v5 q0 X( E+ X. }0 }
7 S6 ]% n) x" `% l2 K. z modelSchedule = new ScheduleImpl (getZone (), 1);
0 P T6 T8 z7 ?4 R6 Y" c" c modelSchedule.at$createAction (0, modelActions);
" \3 p, H7 _$ i# _, m
: M$ Z2 T4 u* w return this; |* g( r V" W4 o# c7 v
} |