HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& \' U' ]+ j" t1 U1 b1 L i/ a& D
# u/ |' w3 `7 Y9 H) J public Object buildActions () {
+ F5 o* S) H J7 x7 p super.buildActions();
8 q' U, n6 t1 a: s7 G+ O o1 k 3 { v% V0 {( h) e
// Create the list of simulation actions. We put these in
$ p- m( L8 u: G3 {* J // an action group, because we want these actions to be
8 r3 W, ]* g5 _& u' C // executed in a specific order, but these steps should. v; k% O) ]$ {' M
// take no (simulated) time. The M(foo) means "The message
' e8 K4 }- @4 l4 F) R! h // called <foo>". You can send a message To a particular& U e7 \8 @9 M' Q6 A0 h
// object, or ForEach object in a collection.
8 _. G( Q% Q9 H3 r& T* Z3 K
1 q) V( _& x% E/ D7 r6 S // Note we update the heatspace in two phases: first run( Y) _2 q' I e! @5 | r1 ~
// diffusion, then run "updateWorld" to actually enact the7 U3 }. ~- l3 V- W* C3 F& W c
// changes the heatbugs have made. The ordering here is
1 o4 {/ s$ {+ ^ // significant!
/ m6 @% L" a9 L4 g2 ~! P
5 A& s" L1 h) k1 N // Note also, that with the additional
' S5 U9 i6 V; E, L) d // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 K3 F+ k, I; g! I/ h4 H5 f // randomize the order in which the bugs actually run8 ~5 V9 a5 y6 T. C5 b
// their step rule. This has the effect of removing any! Z0 v, ~) k$ T4 B2 I. E3 M
// systematic bias in the iteration throught the heatbug& Z" M3 }* e+ S8 t4 R
// list from timestep to timestep. I ?# J) {- Q2 l6 u; H# k
9 S9 J' ~9 {9 m // By default, all `createActionForEach' modelActions have
! L+ N2 |0 s/ F1 N9 b7 u6 I F* { // a default order of `Sequential', which means that the
. K" f2 g2 M# x% ?# D: l( r // order of iteration through the `heatbugList' will be
& b, K+ E( C" z! p& }& t$ e // identical (assuming the list order is not changed" I9 [6 q9 k8 n1 E" a7 R- x0 L
// indirectly by some other process).- R1 ~4 _$ q4 s9 B- v6 \: i( n
. U$ v" W: ~! A M" j: x modelActions = new ActionGroupImpl (getZone ());
. w) D- f$ O$ f/ d, C: [% P0 Z2 h, x/ v' L; ^$ E# \# p% Q$ l4 M% z
try {
# l5 J) L8 ~' f. O+ b modelActions.createActionTo$message
Y2 N" l2 j5 n9 h (heat, new Selector (heat.getClass (), "stepRule", false));
+ L& a7 A. @6 s& F. b; t, l. U! i } catch (Exception e) {
, X0 s T/ S, H8 b- w7 G* p4 u System.err.println ("Exception stepRule: " + e.getMessage ());
! l, s* K# c2 e) ~ }
& k1 \$ [7 G3 A
( r* N- Z `3 u# p" o try {
) f8 R- T' a3 p3 D) U Heatbug proto = (Heatbug) heatbugList.get (0);- @, W) s1 H2 V2 e' T1 G/ S. l
Selector sel = . q4 Q0 ?- u! B. ~3 m
new Selector (proto.getClass (), "heatbugStep", false);/ y* F" G$ T5 q9 d- u/ d: c
actionForEach =
" c. X; U0 L1 Y. T% L% j modelActions.createFActionForEachHomogeneous$call: z2 k/ |$ V' \
(heatbugList,
4 D' M, k+ a" R5 I7 h7 g new FCallImpl (this, proto, sel,
$ x! Q4 u6 {$ P7 m ], S; M new FArgumentsImpl (this, sel)));
1 _8 H5 y4 @, P8 T# \4 ` } catch (Exception e) {
& s& O& Q( K7 I/ J* j, U2 L* L# k9 } e.printStackTrace (System.err);7 e, J: S9 Z6 ^: }' M W
}, V0 h& g! L7 h6 `
' B5 T8 Y- Y2 d1 F syncUpdateOrder ();
8 Q1 N8 P& N, ^! `8 \# N! o$ F& R& s, d2 C
try {
' D& m; I/ O+ ` r$ x; d& M8 n modelActions.createActionTo$message
* y0 a: z) W, `* M5 K8 H- \ (heat, new Selector (heat.getClass (), "updateLattice", false));: C* H4 n3 y8 V4 q3 y K7 z# U
} catch (Exception e) {* u4 {, V/ W- {$ g7 \5 n, k' d$ J/ |
System.err.println("Exception updateLattice: " + e.getMessage ());5 N2 o% H2 O' s2 ^1 b. K' }
}
: {' `- r: P* ^ H% j
: }; X8 M {1 _! W2 |8 r0 v6 ` // Then we create a schedule that executes the
7 m$ x' L, R0 ~* S& S' c // modelActions. modelActions is an ActionGroup, by itself it) `% p" k R3 q! o0 R. m
// has no notion of time. In order to have it executed in
$ f3 A# M) T2 K% l; } // time, we create a Schedule that says to use the
, Z% J% i) h- n9 w( l( s // modelActions ActionGroup at particular times. This& t$ \4 n/ n7 f2 Z+ N9 ?% t
// schedule has a repeat interval of 1, it will loop every' z: H7 t: x- k* _8 @/ r1 i
// time step. The action is executed at time 0 relative to9 B! m+ V) R# g$ E; V+ a
// the beginning of the loop.
$ l- y5 D |' a% Z
4 {& F& S9 ` B% Y" ?7 H, Z // This is a simple schedule, with only one action that is
! k# v8 N! l }7 s1 v% g // just repeated every time. See jmousetrap for more
. Q0 C. P6 r) q8 B // complicated schedules.
+ [4 W; K( ~) k# _) J0 F
) ^; A0 p7 H- X& N( Y modelSchedule = new ScheduleImpl (getZone (), 1);
( C3 \1 i- [$ v modelSchedule.at$createAction (0, modelActions);
- z$ f- w1 ?7 K# k, a& y
9 O, M7 j8 ^8 u7 R9 S3 Z return this;
: J/ \# x$ [0 L% { } |