HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
a/ V5 a1 b1 T2 L1 l, |' U; ^- d0 ]2 x2 ~; _: Z
public Object buildActions () {" m! Z' i$ Z( K2 G8 s
super.buildActions();
' l- [' e0 I( `2 c % n; n& F% B; ~9 M
// Create the list of simulation actions. We put these in
7 W i/ j9 i2 c" K7 P* E8 U // an action group, because we want these actions to be1 d1 h* Q6 p) E
// executed in a specific order, but these steps should7 X# B: Q: U7 d4 m4 y
// take no (simulated) time. The M(foo) means "The message
: @2 X1 s) b S // called <foo>". You can send a message To a particular
& c2 V: g3 p u# C // object, or ForEach object in a collection.& ~+ a$ H. M# N0 w7 L
J5 u& @" h9 K3 i5 h" ]. [
// Note we update the heatspace in two phases: first run
- @# e! w& t' C // diffusion, then run "updateWorld" to actually enact the
4 ^/ S7 d- U$ ?3 [/ t // changes the heatbugs have made. The ordering here is
n: b. r. u. ?; L% m3 f$ v, ^ // significant!/ w+ G- T. p# f
" [; c! F5 k5 z, w // Note also, that with the additional
1 G) F/ U, V. U! _/ H // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 _& p) z+ |8 } // randomize the order in which the bugs actually run
( o7 r7 L0 q G' Q // their step rule. This has the effect of removing any
/ R& E4 \8 n! f+ m // systematic bias in the iteration throught the heatbug
8 G1 ` g6 A. E! d8 o7 W# f // list from timestep to timestep. m$ O. A4 u: W
; O" O5 T: @ x {/ q" _
// By default, all `createActionForEach' modelActions have+ z3 [ q% T9 [
// a default order of `Sequential', which means that the* g F8 i9 U+ u Z# w, w( h
// order of iteration through the `heatbugList' will be: {: K8 n( T. r O$ A) `
// identical (assuming the list order is not changed
7 h6 v# G$ P& i7 I/ I& K // indirectly by some other process).1 P$ l* \& v5 O. h
3 P/ F7 w1 l' G4 j1 K6 n modelActions = new ActionGroupImpl (getZone ());
8 }' m q4 ?" `/ D( E+ W4 T
1 n6 N8 o" s) E' z& m try {9 u. h2 c; `. F5 i
modelActions.createActionTo$message
* @$ @- @% i2 u (heat, new Selector (heat.getClass (), "stepRule", false));0 e. W2 ]$ G) i- `: X) y' C
} catch (Exception e) {3 F7 N: n$ ]& }, e# @4 c _5 W
System.err.println ("Exception stepRule: " + e.getMessage ());7 o- [1 @; I6 s/ v* O) x
}8 N( i" h0 e# }$ Q% x1 f
: s/ X& D0 t/ e4 e+ r
try {
6 R8 z9 s+ j/ G" h Heatbug proto = (Heatbug) heatbugList.get (0);! `! L" q0 g4 M. q& v
Selector sel = " R0 n) z5 o- R1 z6 k" }6 s
new Selector (proto.getClass (), "heatbugStep", false);! u' t# a L1 k6 l
actionForEach =
" C! | u* C1 F- p modelActions.createFActionForEachHomogeneous$call) L+ n$ Y' v* `6 J9 I
(heatbugList,: V" } s" z) |) C5 m+ m
new FCallImpl (this, proto, sel,+ `5 M* H% U" ~: V: s$ K3 h! B
new FArgumentsImpl (this, sel)));
1 P1 k' p+ F: f: r9 S' I } catch (Exception e) {
( N" p' ~. W' L5 E e.printStackTrace (System.err);
, i8 X8 E( N" `/ I5 l+ H" U }
/ `2 H0 R; ~$ W1 z1 j3 v P$ ? ! S6 n, O4 m7 B8 U# x& D
syncUpdateOrder ();
( T9 @& L+ W5 b. _4 W3 Y5 z$ h6 V8 T( `" R) q
try {
" T/ O1 g% z" I modelActions.createActionTo$message
; A! ^1 J' ^$ Q (heat, new Selector (heat.getClass (), "updateLattice", false));9 w3 i- U, Y5 t4 j
} catch (Exception e) {
! K" J! n+ K8 s8 `) S' W2 y6 o1 c System.err.println("Exception updateLattice: " + e.getMessage ());7 W" q" w2 E: b0 R. u. q
}
, p5 d4 @, H7 K1 W. }( p! @% O2 t
1 \$ R# T0 V" n: g: `; | // Then we create a schedule that executes the
$ b9 @! g4 H3 b# Q$ q& X, h7 S, ] // modelActions. modelActions is an ActionGroup, by itself it
9 z/ G% U4 {. g$ W, x0 w // has no notion of time. In order to have it executed in$ Y$ S5 e9 L2 Q, ~
// time, we create a Schedule that says to use the6 o- H, r! m: e$ O( \
// modelActions ActionGroup at particular times. This
" W# P0 p; ]6 P0 B5 G1 z- z // schedule has a repeat interval of 1, it will loop every
( w- n+ F3 N- e // time step. The action is executed at time 0 relative to
0 j" N& l0 @8 r // the beginning of the loop.8 F0 H: I+ e! |: U8 M
: x3 M8 P- J" _" }6 U; l3 I3 M% B
// This is a simple schedule, with only one action that is
4 R3 ]6 j5 L' o# R5 h // just repeated every time. See jmousetrap for more/ A, |6 m3 o# w4 R) W) m
// complicated schedules." S6 Q: f: S i. C$ t7 p
+ C* {# V% Y5 Z( \8 Q
modelSchedule = new ScheduleImpl (getZone (), 1);
1 g5 P. P6 W( D; ^" C5 v* U modelSchedule.at$createAction (0, modelActions);
: ]. A; ~1 X2 g/ V( _
: T& |5 Y7 Z) X9 E" |5 f2 ]. ? return this;1 i; o" J1 n8 f3 G. U
} |