HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- K5 M8 {9 D; k8 }7 L, }5 G
8 j P- ~6 Y2 ?, z5 X4 m# b public Object buildActions () {
3 H2 h3 t& X' x& Y4 G super.buildActions();* G( `. x$ T, o% ?, N- M
8 c8 b$ f) F. D% c // Create the list of simulation actions. We put these in0 [8 q/ e6 X6 I9 w6 `' I% y
// an action group, because we want these actions to be+ j& `% T+ U8 K8 ?4 Y7 ~
// executed in a specific order, but these steps should, S6 c1 q7 [9 r- Q( W
// take no (simulated) time. The M(foo) means "The message2 g) h+ X" t7 [9 x
// called <foo>". You can send a message To a particular
2 C* O/ O; D& l8 G z; K! V // object, or ForEach object in a collection.
; N# K- p4 }! f) Q
! n! E" z. d4 `6 B9 f) n // Note we update the heatspace in two phases: first run
* ]# e! D5 U" \: ` // diffusion, then run "updateWorld" to actually enact the& {) c9 I, j7 B& k3 T/ r8 y: w
// changes the heatbugs have made. The ordering here is7 a5 J6 O1 l# Y( c) X* ^' r. q
// significant!4 B2 L& q2 c5 m; J+ T
0 |$ t- | y1 r& s/ o4 D
// Note also, that with the additional
" D N; D, u, l7 e Z# L D // `randomizeHeatbugUpdateOrder' Boolean flag we can
% t* K/ G- W7 S# y1 S6 b // randomize the order in which the bugs actually run0 j# X: `3 D3 C* V% D
// their step rule. This has the effect of removing any) q5 W% g3 v3 u, ]1 I! c
// systematic bias in the iteration throught the heatbug
& F( M, b- X# H# X6 J // list from timestep to timestep
# S; o3 }0 P5 ^8 c' w% n% O2 Y
) J* a# Q; |/ }! U6 Y+ X1 }4 a" G // By default, all `createActionForEach' modelActions have3 \! \7 G' ~# z1 r9 F* |
// a default order of `Sequential', which means that the- w3 t9 c" {3 ^$ Y
// order of iteration through the `heatbugList' will be, z4 X6 e% F. W/ w
// identical (assuming the list order is not changed
9 P; r+ ~. s. w // indirectly by some other process). P* N( E6 [9 g2 |: `+ o! U- Q( G
8 E0 \% J1 j9 `
modelActions = new ActionGroupImpl (getZone ());
; t, i. q6 U% {( A8 M0 T2 l% U% l# r$ `
$ g! a2 a. l9 s try {! b" F$ Q+ `" r' Y
modelActions.createActionTo$message
* \1 E0 K7 \- ]' S (heat, new Selector (heat.getClass (), "stepRule", false));
4 t" ], C% D: y( }: B } catch (Exception e) {
$ ^2 i; @/ _% \3 I9 K System.err.println ("Exception stepRule: " + e.getMessage ());: ?- a: ]5 }) r# l( t( d% e
}
; O" C3 ^" V# ?* S& S6 j; l
6 G* F8 M7 @% {7 |, @$ ]# P try {
D D- A: d1 n" {3 c9 } Heatbug proto = (Heatbug) heatbugList.get (0);
2 s! s# i' w! o9 R! ^* _* C Selector sel = * l# v) X/ d. q. s* h
new Selector (proto.getClass (), "heatbugStep", false);
; R. C4 _' E: V2 ` actionForEach =' B$ j- O' B I$ o l1 V2 \
modelActions.createFActionForEachHomogeneous$call
3 X& T, r# K) ?- [) [5 J( e (heatbugList,+ i1 k6 Q2 m8 {* z* a7 d6 I/ x8 \
new FCallImpl (this, proto, sel,0 ~% a8 U0 \/ q+ G) u4 u
new FArgumentsImpl (this, sel)));
$ E8 f+ r3 D( r* f' ^/ F } catch (Exception e) {
0 K' f8 _4 f; U( n e.printStackTrace (System.err);
0 n$ ~+ N: U) n$ B }
/ ~' x4 [; A* `( }9 D9 `4 o
* y8 ? j/ G0 W1 W/ r4 ]# V syncUpdateOrder ();
2 ?6 Q- A( ~* M3 r: L) Y/ ~; F. U3 ~9 k7 n9 A3 E
try {- Q+ w& D; k( m9 r: I
modelActions.createActionTo$message # Q8 j2 Z! Q' ^/ ?/ i! h4 |
(heat, new Selector (heat.getClass (), "updateLattice", false));5 v: l O; W# R( X, V$ U E
} catch (Exception e) {
& d# X- j& M! `9 u! @ System.err.println("Exception updateLattice: " + e.getMessage ());& L+ D) z! ?, @: i7 d
} b3 K! c; a& h3 \' a- i" V
9 }& Z$ Y, v( h$ n s7 w // Then we create a schedule that executes the; z- e+ h9 C7 n( s+ F3 _
// modelActions. modelActions is an ActionGroup, by itself it/ f% g U F/ e/ W( f
// has no notion of time. In order to have it executed in
" m" e- A+ H7 \( ~( U // time, we create a Schedule that says to use the
4 G2 j9 ]% Z7 ~8 T // modelActions ActionGroup at particular times. This
1 P, m$ {- |# i* i% X // schedule has a repeat interval of 1, it will loop every
# E0 r4 }, P$ I // time step. The action is executed at time 0 relative to5 R( q0 E N0 h% p
// the beginning of the loop.
+ q2 X8 E, h$ n f9 R9 L) {: J ?" q- Q
// This is a simple schedule, with only one action that is
|# W1 a5 {. S9 M // just repeated every time. See jmousetrap for more
0 z1 y! T" U0 d$ d/ Z // complicated schedules.1 b7 n# K3 k' A
" ?9 S* |! _9 a- ^4 F6 Y modelSchedule = new ScheduleImpl (getZone (), 1);
& v( S+ ]: Q! B \ l2 z6 \ modelSchedule.at$createAction (0, modelActions);3 o7 I( }4 V; f z( s
) ^, C! O7 ?! S. _, q7 B1 t& A return this;
3 A# b6 p" d( g4 K9 Z, a1 \ v } |