HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 t- n9 q- e" p! i6 u! A- p2 i# t' D7 Y, x" u: Z# m I
public Object buildActions () {
' v& i; U5 O7 X1 ^8 x super.buildActions();
1 _0 C5 F' \6 `5 G$ h
1 M2 ]6 V, Y, S4 O2 t // Create the list of simulation actions. We put these in
: v% h7 `7 r# m8 F0 } // an action group, because we want these actions to be R! X( ?; v$ K, F+ D- t/ W
// executed in a specific order, but these steps should
7 ]" J8 ~8 _0 F3 b5 b // take no (simulated) time. The M(foo) means "The message& D" N+ @" D% l3 t/ K, y" D: Y! C/ y7 J
// called <foo>". You can send a message To a particular
]- n; j" \( k3 B // object, or ForEach object in a collection.
* x E! j) b# Z6 }" ?9 g3 g ; c/ n) D" t7 R
// Note we update the heatspace in two phases: first run
' V9 k+ N: c P' i8 F m; i // diffusion, then run "updateWorld" to actually enact the, s1 K; S* W( @6 o$ P1 z) J7 A
// changes the heatbugs have made. The ordering here is8 A# A8 s$ g+ a- G6 p9 ?) W5 A
// significant!
* }* j/ m+ j+ O5 }4 q {! `
# i1 B) C9 S9 |5 P // Note also, that with the additional
- o& U* _( x F/ k: @/ C // `randomizeHeatbugUpdateOrder' Boolean flag we can
! s6 u- D ^6 Q0 R // randomize the order in which the bugs actually run- B4 O, |: Y: g3 m
// their step rule. This has the effect of removing any
" C$ Z- Q# Y" Z1 F6 ?. X. E // systematic bias in the iteration throught the heatbug
( ^$ P Q+ P$ m // list from timestep to timestep | o* s6 E1 m
, [ f5 T( p; K& k( b0 Y4 c1 w // By default, all `createActionForEach' modelActions have9 g& D4 v9 O9 m4 b% s3 k" C
// a default order of `Sequential', which means that the
! [6 \# h7 j' A# u2 J // order of iteration through the `heatbugList' will be1 q" }$ n# [: f9 a" q; [- Q# @
// identical (assuming the list order is not changed1 F. N `; e5 q: I2 k: {0 W! |& V
// indirectly by some other process).
0 E2 d1 Y9 A0 P" K* j; k! m
! J8 F; n; b+ h) p* D! ~ modelActions = new ActionGroupImpl (getZone ());
$ F: `4 J5 E2 b8 }7 o
, U% q3 O( e2 |9 A. [ L try {
/ S+ U9 t5 e( |2 Q7 r modelActions.createActionTo$message6 ?3 Y( S) s8 I+ y; B
(heat, new Selector (heat.getClass (), "stepRule", false));( l5 o0 ]8 y7 B* P1 V, {/ J
} catch (Exception e) {
! K3 I! i8 J% z System.err.println ("Exception stepRule: " + e.getMessage ());4 W4 D+ |7 u- E/ m4 m
}
$ }5 ~) I5 d1 o+ n7 R9 y- @
6 S1 l( u1 O2 i, W try {
5 k) p& c" f+ H P' @, A6 E Heatbug proto = (Heatbug) heatbugList.get (0);+ J7 d2 k3 ~; x( N/ t: w6 r
Selector sel = ! \- l2 n2 E0 C% v$ \
new Selector (proto.getClass (), "heatbugStep", false);
4 ~$ x' T! w# V, |, Q& R actionForEach =
" k2 f! T# q0 \7 G m modelActions.createFActionForEachHomogeneous$call0 j9 i% T4 b4 ]' I4 S9 O, x8 o; S9 c
(heatbugList,' B: H9 w/ A A, _! d) c2 {
new FCallImpl (this, proto, sel,- t7 R. a8 [3 V {9 ~
new FArgumentsImpl (this, sel)));% P) J" Y/ F' J/ n; b
} catch (Exception e) {2 n- D& ]) z T+ N3 a' M3 ^( w
e.printStackTrace (System.err);" G( y" d: B- R) g! Q
}& J+ z1 s/ V! I
' \4 g+ g$ j- H2 _4 A
syncUpdateOrder ();
" V: u1 ]1 ~+ O& L3 T
: ] L& N& |- l2 P try {$ ]; B9 y9 }. Q( a
modelActions.createActionTo$message ! u7 t- [; ?' \, r, ]% ?
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 t' H+ W2 U! @% M+ O } catch (Exception e) {
6 i& T6 q& R& P! p& T6 v- H System.err.println("Exception updateLattice: " + e.getMessage ());
- d8 I6 T4 O. D% D( q% f3 L7 H }
: ^* E9 q/ R- f
+ a g3 w! \! p k$ t# M v // Then we create a schedule that executes the" }6 ]5 f, M, I& H
// modelActions. modelActions is an ActionGroup, by itself it" |0 ]6 Q. v: U0 U2 \6 k5 R
// has no notion of time. In order to have it executed in
2 M0 k: ?* z7 ~0 r // time, we create a Schedule that says to use the
9 C7 }0 ?5 x @% s // modelActions ActionGroup at particular times. This3 s, @0 J b$ m9 F) X# a2 E4 e
// schedule has a repeat interval of 1, it will loop every
& T' q8 D! [5 _3 o% N // time step. The action is executed at time 0 relative to+ g9 z+ e$ }$ l2 u8 a' v! C
// the beginning of the loop.- x+ ?. R& C3 r, i
: o; j+ A& }' C1 Y // This is a simple schedule, with only one action that is
1 d1 o8 r+ ]! ?/ O& T4 r // just repeated every time. See jmousetrap for more
! o: i6 |2 S" t: u7 [ // complicated schedules.( Q4 J3 R1 Y/ H& @& q: b% Z
+ q2 T, I/ L$ S! X& i9 w modelSchedule = new ScheduleImpl (getZone (), 1);4 q7 E) E4 k5 H2 K* w5 [
modelSchedule.at$createAction (0, modelActions);
, Z4 e6 R* n8 u5 y5 W+ h+ V
/ C" K4 |. x+ u: ^- A$ k7 j return this;. ?! i1 c5 i3 `2 R
} |