HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ G) T4 [2 D D% |# a2 ~
9 z# m4 c+ z( m' ] q# F+ W
public Object buildActions () {
$ Y2 }4 P- `$ m, A$ ] l0 X super.buildActions();
5 j0 J6 w) t1 M: I. J" i& z ! U* {+ v0 b4 L; h# m0 E& A
// Create the list of simulation actions. We put these in2 a: ?% s0 M$ F6 b: l
// an action group, because we want these actions to be( V- m7 G: Z& l4 ?; t/ I
// executed in a specific order, but these steps should
W2 _3 q3 |. Z3 {% w g% a ^ // take no (simulated) time. The M(foo) means "The message2 `# e' _* U v
// called <foo>". You can send a message To a particular
( y2 l, u& w2 N6 h // object, or ForEach object in a collection.
u/ B! ~/ Z( Y/ k7 B b& j0 c
- h1 L) b- O4 F, E# F( A // Note we update the heatspace in two phases: first run
3 i9 C' ^5 v0 `9 B. Z1 m; E // diffusion, then run "updateWorld" to actually enact the7 I# }$ m3 y& S$ x+ K, `; s
// changes the heatbugs have made. The ordering here is* F" r. ?) U/ { o0 i
// significant!
/ ^; o- h" [ A7 G" M& y ' @* U. K, p, t( R
// Note also, that with the additional$ J8 a/ t7 m5 C# P9 P, j2 U* H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& r& A$ W9 q: ~, ~' H // randomize the order in which the bugs actually run* ~9 L% l4 n5 D6 r1 p, x4 ]
// their step rule. This has the effect of removing any. Y2 s# W2 B* e& X, d
// systematic bias in the iteration throught the heatbug
& U! n! k' y' ~" H' M // list from timestep to timestep! t m. _# O4 N, f' o* c+ F
1 D7 g m% `4 U! K* y // By default, all `createActionForEach' modelActions have. G8 L8 I& o/ ~" U; f. u! e3 S
// a default order of `Sequential', which means that the4 \" Y$ W. S% o2 n
// order of iteration through the `heatbugList' will be" b) w) N* O" @- B
// identical (assuming the list order is not changed
. ^. V% o. m7 D# {3 _ // indirectly by some other process).8 |% M0 ?9 F! ~& ?8 E9 @
' G4 d1 Y$ |! @' h: h8 K2 L6 n
modelActions = new ActionGroupImpl (getZone ());" }' U% v+ ~1 @! e2 b
9 T; k1 r; D. l" r( ^* f: |& a try {2 ]! N. e4 n8 e- o! t# o- Y5 @" \
modelActions.createActionTo$message3 L' {$ z6 P4 R! n H5 C9 N. e
(heat, new Selector (heat.getClass (), "stepRule", false));5 O6 N. E# _' N) a
} catch (Exception e) {
+ _ k0 z: R2 h/ c+ b6 K: S System.err.println ("Exception stepRule: " + e.getMessage ());5 }7 V6 M5 v8 u" G
}
" q- z* }" o; P6 B; b7 r0 i) y) {) n3 B/ `( G3 ^9 v7 B% s8 @9 q% l
try {
8 `( K% g. j6 w* x e Heatbug proto = (Heatbug) heatbugList.get (0);( j8 b8 \5 c1 s0 Q& {5 ]
Selector sel =
+ ` G" [! }1 r) l. e" `: V3 r new Selector (proto.getClass (), "heatbugStep", false);) N% n1 z2 Z& w/ n# m; C1 F
actionForEach =
$ F- M6 C* E' i( u modelActions.createFActionForEachHomogeneous$call
9 h2 a$ \7 r) R$ I) { (heatbugList,
1 `. h9 X/ P1 f: {+ m new FCallImpl (this, proto, sel,
0 \$ {( I2 p/ O+ z new FArgumentsImpl (this, sel)));
E7 B# U! O0 Y* e0 f6 ] } catch (Exception e) {
! z1 A7 `5 w! }# h5 U$ c e.printStackTrace (System.err);
: D; ?: q; l7 y, e/ x3 R } n$ \, j% j" B b* k z
]: S4 M$ _' ~/ c0 i! Z% [ syncUpdateOrder ();3 x2 L8 A# g: R' G
* u- S/ ?, v6 E2 g6 }$ Z$ G; I7 | try {* q" Q# R6 P w. m
modelActions.createActionTo$message # {2 V. q8 K1 D& `: z/ T: U+ ~
(heat, new Selector (heat.getClass (), "updateLattice", false));2 D2 |1 g! G1 Q8 O' e
} catch (Exception e) {
; I/ N" a' Y3 f+ L System.err.println("Exception updateLattice: " + e.getMessage ());$ \9 l H; E. G2 m! y
}
J8 r( d* L# l2 q
* I5 N1 m7 X% G/ a // Then we create a schedule that executes the
/ \/ B% g9 D, W3 x( X // modelActions. modelActions is an ActionGroup, by itself it1 _' j& e+ N; F& q! p
// has no notion of time. In order to have it executed in/ j S- j! F7 z1 y
// time, we create a Schedule that says to use the& w, d% G: Q; P/ g
// modelActions ActionGroup at particular times. This
6 C+ i# V$ X4 Y3 g' t // schedule has a repeat interval of 1, it will loop every
0 F+ J/ A* h: u$ G( s // time step. The action is executed at time 0 relative to( A0 Z7 z. |7 i7 P0 ]6 m' H
// the beginning of the loop. o$ M* j+ [ N
1 i1 D# m {7 D, k |. x // This is a simple schedule, with only one action that is
8 p& C! _! l/ ^% I" C // just repeated every time. See jmousetrap for more7 z7 R* _4 }, |$ F1 _
// complicated schedules. \/ U9 n& u6 n* |0 H
. C( Y/ [7 W- B: `
modelSchedule = new ScheduleImpl (getZone (), 1);' H$ ^0 Z5 ^+ B4 P
modelSchedule.at$createAction (0, modelActions);
0 w, d/ C0 _- ~8 b( d7 p
) A* G9 \, D0 E+ O% [8 L return this;
1 a$ a# t4 {+ W3 W } |