HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 ], Z4 H# u+ E$ q
! C' B8 z" z2 {1 `& ]" R; w public Object buildActions () {7 i9 e D7 {: D
super.buildActions();
9 k* G! ^7 m8 I6 y) M0 X* a' p0 {
& |& ]0 c5 [1 W$ C) L // Create the list of simulation actions. We put these in
3 ~% w1 `$ H4 E // an action group, because we want these actions to be* Y6 w$ \2 s2 T, K! q0 K& |
// executed in a specific order, but these steps should
+ I z4 K5 t r& F( {: D; d // take no (simulated) time. The M(foo) means "The message0 r: h9 T# {0 s, x7 {* V3 s: b4 I
// called <foo>". You can send a message To a particular: q4 ~9 a3 F4 k# p1 j
// object, or ForEach object in a collection.4 Y" y" x; `+ e! c% ]
* }! K3 y3 A, o! u# I" R- G // Note we update the heatspace in two phases: first run
0 N1 o" R: E3 u; \- P // diffusion, then run "updateWorld" to actually enact the
+ n" M! L- d K2 K" J // changes the heatbugs have made. The ordering here is7 x$ l$ S4 ~! Q3 T' [8 a
// significant!
/ [5 ~( x5 H5 f1 [+ q; F
5 c- I$ r3 T+ P! O // Note also, that with the additional
' Y6 }! u- e& t, T+ F6 k // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 x, ]! ?' S# Z- m! D // randomize the order in which the bugs actually run! b0 z x' v4 s9 e) {# W2 Q* p# J+ m7 F
// their step rule. This has the effect of removing any
4 y; R5 V3 N5 V. Q7 ?9 [ // systematic bias in the iteration throught the heatbug$ s+ w, V1 u/ n( |1 w- h: K
// list from timestep to timestep' }6 b9 f1 O5 c: H/ q
3 q0 K: m4 ~3 ~3 f // By default, all `createActionForEach' modelActions have
' e& Q" @( ]* H1 e' J$ g9 }1 ?3 S // a default order of `Sequential', which means that the h# w6 l; H+ I; J! `
// order of iteration through the `heatbugList' will be. a- M8 O" p& S
// identical (assuming the list order is not changed/ i4 U7 ?* C+ B0 x
// indirectly by some other process).
X6 y \3 G9 y
1 y9 O D9 @& d1 S' j6 W modelActions = new ActionGroupImpl (getZone ());. i Q- i8 a/ A% ^+ L( }" R$ @
% B! z7 ]# F6 t# Z- A
try {- ^* m4 s( f* s: g, `5 n/ F
modelActions.createActionTo$message
1 q, P$ F( X% u! I3 p (heat, new Selector (heat.getClass (), "stepRule", false));3 C& H. ?9 R+ D6 v- v, }* R% B5 u
} catch (Exception e) {
3 P1 T8 K8 B; I; q1 ^; L System.err.println ("Exception stepRule: " + e.getMessage ());
& U: w6 s9 o+ b+ i, X" ? }- V$ Z7 v, x( p& Y1 G- ]
5 ? ^' l* T/ s, @: S
try {
& U5 I8 u6 S2 v1 G6 w9 N0 v Heatbug proto = (Heatbug) heatbugList.get (0);! T2 d- K/ Z' s2 X
Selector sel =
" ~3 V- a1 a7 |8 H1 ]) X) Q6 T) F new Selector (proto.getClass (), "heatbugStep", false);1 }+ V5 G' q1 _2 k% P: X" f9 T Z
actionForEach =' S; |- V) q, ^- D; G* n! R
modelActions.createFActionForEachHomogeneous$call1 ~9 p. g9 P, b1 M
(heatbugList,
9 I1 s, |. p: i) t; W new FCallImpl (this, proto, sel,; k7 Q$ \: G+ Q W6 p0 E
new FArgumentsImpl (this, sel)));) {$ o" |$ M$ b; K
} catch (Exception e) {. F3 p6 d" V2 g8 C
e.printStackTrace (System.err);
2 v A- B p% @6 U6 I' |8 t' K }' \5 e( i6 I" L+ {0 h7 p, E
. w5 z8 }; |- Q2 C& D% {/ a+ U syncUpdateOrder ();4 x6 |2 e: x' d7 c2 n8 H6 }
# c4 x! m1 @& u8 w# s try {3 A5 j: F5 E* R: k
modelActions.createActionTo$message
7 k; j, S# G# X2 K8 U# f1 p* U3 { (heat, new Selector (heat.getClass (), "updateLattice", false));
" R0 n. O. r5 O( f } catch (Exception e) {8 z6 p. K7 Q4 a" Y8 k" n
System.err.println("Exception updateLattice: " + e.getMessage ());
7 T9 u8 T8 Q, ]& ^' f0 ~ }
2 d( o4 b- x1 f# H6 {# W* f - S1 R( G4 X) r+ ?1 p
// Then we create a schedule that executes the
. s% }" N) i* D3 ]! l u* p // modelActions. modelActions is an ActionGroup, by itself it- g5 i4 `% D4 F3 j! O, r
// has no notion of time. In order to have it executed in$ H: J/ z: k' \' J
// time, we create a Schedule that says to use the) T+ Q* }, d8 P2 @- T
// modelActions ActionGroup at particular times. This6 e9 {; |" B/ B! ~1 d" e2 n
// schedule has a repeat interval of 1, it will loop every
. @; k! S5 N/ i4 @* ~' p // time step. The action is executed at time 0 relative to; s/ Z& \5 C) W# f8 ]( z
// the beginning of the loop.0 i9 t) C+ S6 q7 ]; t; ^, D
1 V8 L- D4 V I/ b7 W6 `
// This is a simple schedule, with only one action that is
" b5 O8 p! k" L( G8 @; {" j* _ // just repeated every time. See jmousetrap for more
& H: |0 d: |- ?2 Q T. B // complicated schedules.2 @( Q" F0 U5 K: T, S, B
8 J5 W c9 \! k3 M2 C% T6 D modelSchedule = new ScheduleImpl (getZone (), 1);
2 C$ _, D" c; A& j c' t modelSchedule.at$createAction (0, modelActions);8 C5 i' t8 U# g1 S. M
( ]% @7 x3 m8 ?: f5 d# u
return this;
) R* F. C! q! v! Q S! j2 ]& o } |