HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) R5 j; W$ B; Q6 E
% w, J6 i- _8 A6 ^$ Y
public Object buildActions () {' l- }8 [* ]" ?
super.buildActions();0 g9 t% o/ J4 o. n
8 U0 }0 ~- J: P8 W
// Create the list of simulation actions. We put these in" [% @1 f5 b# J3 ]( O) K9 r: B
// an action group, because we want these actions to be! U4 A& s: {6 [" W
// executed in a specific order, but these steps should
+ a7 c' _! J: o- U5 y8 W2 ]" v* a // take no (simulated) time. The M(foo) means "The message$ u2 z, L g/ X1 G
// called <foo>". You can send a message To a particular
+ n( |5 x, ?2 {7 O" K7 Z+ q0 | // object, or ForEach object in a collection.0 d! p( F, u. k$ x6 R) y
- {, Q& K; t. ~0 U // Note we update the heatspace in two phases: first run
) L9 ]& J1 k, S1 h // diffusion, then run "updateWorld" to actually enact the0 i7 ~+ s& P( r: x* e" K
// changes the heatbugs have made. The ordering here is! j2 w% P4 P4 G
// significant!
! z( `3 X9 U" C0 Q- h7 r : W( W4 Y, \# s/ a! `
// Note also, that with the additional
. {, z7 W5 }# `6 S) s // `randomizeHeatbugUpdateOrder' Boolean flag we can9 U: d1 ~9 [& G
// randomize the order in which the bugs actually run! \3 i5 B+ G0 S" ?
// their step rule. This has the effect of removing any6 i8 T" x1 F" o3 D _
// systematic bias in the iteration throught the heatbug
! m( V" [7 ~5 |8 A8 s // list from timestep to timestep
4 k* ?7 b" o6 N# j! c! w+ f 8 h- v- b; _. K }( Y: I/ E
// By default, all `createActionForEach' modelActions have* T* O6 V7 J6 C9 v `. F
// a default order of `Sequential', which means that the: _! z0 z9 w2 @8 [' y% ?
// order of iteration through the `heatbugList' will be$ T! d7 h( @0 L+ ?3 g p1 _2 V0 w: d
// identical (assuming the list order is not changed
7 s6 V5 G2 `9 z" t; C // indirectly by some other process).
x+ g8 g+ M+ m# @% M # c, D/ j' F# X
modelActions = new ActionGroupImpl (getZone ());) z3 a: R' K3 v3 Y
4 r _' n0 W, n& ?, z2 M2 F1 _3 { w
try {
0 l# u. N9 n0 N! \# T8 q modelActions.createActionTo$message
/ c) H2 w" x( G5 K/ T }# i (heat, new Selector (heat.getClass (), "stepRule", false));
3 Y i8 V! J" i1 B3 X' T2 V } catch (Exception e) {
$ M+ \! S# @: o6 {+ a& N! O) f0 A+ S System.err.println ("Exception stepRule: " + e.getMessage ());9 g% v& j' b' `& ]$ v1 ]8 g8 w g
}
8 `) `2 o% [; |% d
`9 M# y) v9 m) }- w try {
. G/ }, }9 q9 R- ] Heatbug proto = (Heatbug) heatbugList.get (0);. I* U% j' A1 f; U2 q
Selector sel =
5 Z6 ~4 \+ I" @: w/ a# M; a new Selector (proto.getClass (), "heatbugStep", false);
$ M& T, u0 J- t. ~7 k$ V0 E3 B1 ^7 q _ actionForEach =
f9 x+ L" C6 o6 u# o( B+ B4 Z modelActions.createFActionForEachHomogeneous$call
& W# h/ g Y$ e% q0 K (heatbugList,
" J9 ?8 X: A; ?8 X) |1 k new FCallImpl (this, proto, sel,6 L" H" J* N0 B4 X7 a; E. I
new FArgumentsImpl (this, sel)));
' u$ M# D) X. V q } catch (Exception e) {! w% W- F! L0 j0 R; \0 z9 ^
e.printStackTrace (System.err);
* b& z) R7 e! I+ }) B }
! F! z' C. d% D$ n9 B$ K2 x 1 a" a1 L6 S2 `4 z. g) p
syncUpdateOrder ();
8 X# S- j- C) x+ i, m3 Y
; R9 A6 J" ?4 Y( W: x try {- |# P( H" v7 O3 c
modelActions.createActionTo$message
4 U; z8 ?% d' ^: u! G9 ~1 Y (heat, new Selector (heat.getClass (), "updateLattice", false));
, L$ H4 s& c+ k! z- F8 { } catch (Exception e) {
" _! B9 Y/ R" s# S) e0 N2 D System.err.println("Exception updateLattice: " + e.getMessage ());
( w9 P" s) I$ Z }# w: y, g' g! M! o6 K) C" {( d+ O
5 n! e Y5 H9 k# m' u0 Q' l
// Then we create a schedule that executes the
+ m& i* t4 @2 j // modelActions. modelActions is an ActionGroup, by itself it
3 l* Y7 j7 _8 @1 } // has no notion of time. In order to have it executed in g4 @2 V0 }% {% f: N5 {! K* F
// time, we create a Schedule that says to use the
- |; N5 U. |4 e& T+ G // modelActions ActionGroup at particular times. This8 A% I( P$ P3 W
// schedule has a repeat interval of 1, it will loop every1 V8 _- p( p T3 i6 A- w1 o
// time step. The action is executed at time 0 relative to0 i3 K! n6 ?: O( k' Q
// the beginning of the loop.
+ @- c6 q+ q7 t- r( N
+ _/ o; \, R1 G+ i* a // This is a simple schedule, with only one action that is/ f3 U* U* R. ]; g
// just repeated every time. See jmousetrap for more+ d$ F" s% g+ H, C$ z
// complicated schedules.( V9 a% S) e) o' o
( R0 l+ C: H' F8 o- G6 J modelSchedule = new ScheduleImpl (getZone (), 1);8 D# E+ K2 W2 C$ U* Q- D6 C
modelSchedule.at$createAction (0, modelActions);+ l" J# V% a# G$ j2 d+ f; F: n
, m- j+ i+ X7 s* Y0 M# l return this;* K' J9 |# Q3 s6 u9 X7 U7 o7 Y
} |