HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ C4 @0 a! X7 u# T% f
/ r. i# h& X5 j* O2 k
public Object buildActions () {
2 z" b/ j9 \' G3 U super.buildActions();
$ @* W+ S/ ?3 s8 v7 w9 h8 C: f 5 d) Q: q8 Q8 d! I; W8 s
// Create the list of simulation actions. We put these in
1 M4 O# p! M( [ // an action group, because we want these actions to be
) }+ u, b# P7 n/ h3 k // executed in a specific order, but these steps should
9 u7 ~7 d, h# F+ C( M$ z& S9 f // take no (simulated) time. The M(foo) means "The message
j0 Q0 r' _+ h6 a! H // called <foo>". You can send a message To a particular, \% ]6 L" c! Q* M+ x, u
// object, or ForEach object in a collection.
3 r0 {. Z+ r/ a) j3 t3 Z5 [5 R
5 a0 L B! k2 X // Note we update the heatspace in two phases: first run
; H' A8 W" H o/ r/ S9 q5 {& B // diffusion, then run "updateWorld" to actually enact the/ x, H. S4 b: A, Z8 G4 e& [
// changes the heatbugs have made. The ordering here is
! w" V! G( I4 J# @' i // significant!* d; S& T7 E. x/ C h' \7 D. a
2 L$ `7 H! C+ r3 F% w // Note also, that with the additional" M; T( R7 S9 Y' O
// `randomizeHeatbugUpdateOrder' Boolean flag we can c6 B6 n7 G4 w* L7 F4 x$ }
// randomize the order in which the bugs actually run$ t/ ]9 l: S) w+ `# K3 _
// their step rule. This has the effect of removing any! U, r O2 k# b2 S
// systematic bias in the iteration throught the heatbug
5 S/ X2 b( |. N% i( v // list from timestep to timestep
4 {* K7 O: E( u5 `% s% S 5 G9 H4 M) u+ A
// By default, all `createActionForEach' modelActions have( |6 G4 e# U* Q5 m
// a default order of `Sequential', which means that the$ ?, I/ B7 _! t8 B
// order of iteration through the `heatbugList' will be
" b5 p+ n/ M2 ] // identical (assuming the list order is not changed
( _. H" j+ Z h( w( l // indirectly by some other process).
& c% }3 l3 h8 ^9 W; J' O& K5 V) W
7 C6 `! d9 E% U. x modelActions = new ActionGroupImpl (getZone ()); t' D/ K3 J+ f2 p9 t' j
3 [% p# _; z j4 T7 y
try {& H5 W, a6 A" U3 o6 K8 G
modelActions.createActionTo$message
3 ?' b5 g: _* u (heat, new Selector (heat.getClass (), "stepRule", false));
?: g( _* q) N6 f$ P } catch (Exception e) {+ _3 t7 D9 L& G F. s" _$ J) Z* v
System.err.println ("Exception stepRule: " + e.getMessage ());" c. p# v, r. V5 S" Z
}
( w4 ^" k2 V+ M" V5 ]7 E( i" F
try {3 Z! {$ S4 R+ e* `1 f2 x# Q
Heatbug proto = (Heatbug) heatbugList.get (0);# V# m4 F5 J3 y6 r9 ?: v
Selector sel = 4 f1 L0 U d# D: d8 f
new Selector (proto.getClass (), "heatbugStep", false);' H) k" Q5 u" X
actionForEach =' r ?* Y+ ^! A
modelActions.createFActionForEachHomogeneous$call
5 Q% M4 G% K" u2 A9 N" x. | (heatbugList,& @* |, W9 U- I4 s
new FCallImpl (this, proto, sel,
9 [9 s4 Z. A' ~- G. r new FArgumentsImpl (this, sel)));
8 N3 B( n* r/ T% W, W A } catch (Exception e) {) j. `4 |6 Z0 v, g; o: r% ^% B8 Q
e.printStackTrace (System.err);
7 W& G4 {& G) G$ e$ J7 K# T }1 [7 u9 w" f7 a& x7 \5 h
; N2 u# C' \- a syncUpdateOrder ();
* D+ T) W9 v# {2 w( p/ M" F4 J. p1 O" W, x3 K/ [0 a/ ], X
try {
6 M1 w9 j1 S- N g: K; `) u$ \ modelActions.createActionTo$message 3 }0 ?8 N& L0 Q, O# J( M T" u
(heat, new Selector (heat.getClass (), "updateLattice", false));
# b6 w7 g' {7 a1 B. C } catch (Exception e) {
9 e' a/ {* c. \) n' |! f System.err.println("Exception updateLattice: " + e.getMessage ());& d( ?9 z( H4 N, C# @
}
) {6 ^ W. T( d: a3 [ 4 e+ i: r1 t) z3 ]! g- H
// Then we create a schedule that executes the/ e* j4 G1 v2 x* T/ k
// modelActions. modelActions is an ActionGroup, by itself it; |" V4 t) x- M r5 T- l
// has no notion of time. In order to have it executed in7 G4 \1 d- [+ ]
// time, we create a Schedule that says to use the1 w# Z6 ]! Y; W# [) S k/ M5 H3 l$ [
// modelActions ActionGroup at particular times. This. t+ o# j8 l: H
// schedule has a repeat interval of 1, it will loop every( I& @- B. j U
// time step. The action is executed at time 0 relative to8 Z% \8 c" V* U+ Y
// the beginning of the loop.* f! t& i3 L* L) K$ S% W
+ i% u2 e/ z/ J8 e, ^7 h
// This is a simple schedule, with only one action that is- r \0 K1 h$ G' x. ` ^
// just repeated every time. See jmousetrap for more. K- z0 J3 y1 _4 {* n
// complicated schedules.
1 b4 Q! A" Q0 H' l. \ 0 e5 P5 n1 s! K) B4 |
modelSchedule = new ScheduleImpl (getZone (), 1);- O; n" V/ n8 L& w2 f
modelSchedule.at$createAction (0, modelActions);
, y7 v% @% [" e1 F8 | 8 J- E- U" a5 w3 q5 i
return this;; O) C' x4 g. H1 O7 ?' x8 S6 I
} |