HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; M( C5 V% ?5 t( ~3 ` E. E
! F, b* c4 E: o [# f8 i6 I public Object buildActions () {) @. J$ B: c7 N2 a* N5 b. o% `
super.buildActions();# R1 l( n2 B9 ^- G3 X* C) W+ \
3 S3 X" {! p9 x& e9 d // Create the list of simulation actions. We put these in# }0 k) j: ]4 I
// an action group, because we want these actions to be
0 H: t1 N; k) _5 D5 ` // executed in a specific order, but these steps should, J# T3 {. a9 D. V o
// take no (simulated) time. The M(foo) means "The message, J5 ?' O0 q5 N! _
// called <foo>". You can send a message To a particular
; o1 O* [' {: S // object, or ForEach object in a collection.
- L1 g) X( S, U- ]; l6 A- N, V + q# r; }) R# |
// Note we update the heatspace in two phases: first run6 x: j% h5 `& V
// diffusion, then run "updateWorld" to actually enact the
2 ?" W6 v |+ ]/ [$ F$ u! w // changes the heatbugs have made. The ordering here is
& @- x: m& v+ O5 b // significant!, y; Q6 L8 x& ~0 H6 ^
! [; q1 J6 e. L8 Q // Note also, that with the additional- S" F' f) ^0 p; W# v3 {7 `
// `randomizeHeatbugUpdateOrder' Boolean flag we can p1 b/ F/ w! ?4 H
// randomize the order in which the bugs actually run
4 W& Q8 y( y6 g" H3 L) _ // their step rule. This has the effect of removing any5 M! f7 f" m4 G. o$ S8 Z. {
// systematic bias in the iteration throught the heatbug
% c+ E/ ^' A! k // list from timestep to timestep
1 m$ P- I* |0 _ ) ~3 R: ?$ M( i6 F' Q: J* W
// By default, all `createActionForEach' modelActions have7 ^, _5 \: p/ I# r5 J) u, V
// a default order of `Sequential', which means that the
, Y( C3 T+ y" r4 } // order of iteration through the `heatbugList' will be' h5 I) w+ r- q0 x, j
// identical (assuming the list order is not changed# C N/ @! E: c8 f3 h
// indirectly by some other process). F* l9 C4 A% s3 H4 X+ |0 |
8 i9 I# \9 c# j% Q: q# ]# C
modelActions = new ActionGroupImpl (getZone ());
$ a! ?/ Q: d% v1 e* r& N- p, `; q- Y1 U6 a* X& M
try {
1 H+ m( L9 j* o+ F modelActions.createActionTo$message
9 A! _8 T: r, q- W, c (heat, new Selector (heat.getClass (), "stepRule", false));
* v ^3 u2 J2 r } catch (Exception e) {& P% }( e4 n8 [+ _$ r6 Y1 C1 q
System.err.println ("Exception stepRule: " + e.getMessage ());3 v" P- d5 ^0 c+ `$ G6 Y$ S3 r
}
S/ t; f! p* O% I
6 U( t b j4 i9 t$ c4 X try {0 p- E: g; ^2 i9 J; {+ d1 h, I" f( r
Heatbug proto = (Heatbug) heatbugList.get (0);
" G S: C' W% l/ A# v4 ^8 ^ Selector sel = 7 Q2 ^( r" b2 s+ W: n
new Selector (proto.getClass (), "heatbugStep", false);
5 n* H8 Z7 L4 F! ?+ v; q actionForEach =& H+ R5 w z7 N7 Q$ A; y1 V
modelActions.createFActionForEachHomogeneous$call
) O4 g/ |' L0 u$ R) G (heatbugList,4 o$ l1 t" t7 a( q7 [) O+ A, Q" u% ]
new FCallImpl (this, proto, sel,9 s7 W- d# P. L/ x5 A) b8 U# a
new FArgumentsImpl (this, sel)));7 J' E# y) }, A: s
} catch (Exception e) {& R9 l# L: g8 g+ Q3 c. k
e.printStackTrace (System.err);
0 `8 Z( t- W- \+ \9 N9 v" K }
9 {, ]" Q! D' n- y9 w
! I$ o0 z! E8 q% |0 v* _4 N" R syncUpdateOrder ();" e0 e: s7 N7 T! L, m
0 U3 h) e, J, d# }! F try {
, v0 k( z- F3 A modelActions.createActionTo$message / `3 D; p" {+ V" u# Q. I
(heat, new Selector (heat.getClass (), "updateLattice", false));! Q& O- r1 G1 ]# p, v+ s
} catch (Exception e) {9 S. X- Z+ I6 |6 a. v" H
System.err.println("Exception updateLattice: " + e.getMessage ());
6 U2 ]& p! F1 B0 U h% `4 t }& `; \, T/ {; h
) {- g; D; N U6 b# J // Then we create a schedule that executes the
1 p: o( A7 ~9 ^$ U* N" c4 D // modelActions. modelActions is an ActionGroup, by itself it
p! Z9 ?! I3 x* t& N r // has no notion of time. In order to have it executed in$ s6 O7 v" Y' O+ P* s$ n8 x0 q9 ~5 P
// time, we create a Schedule that says to use the
6 l: a5 s+ ~5 C2 Z5 z p // modelActions ActionGroup at particular times. This3 N! g( d' `6 Y2 n, A4 T
// schedule has a repeat interval of 1, it will loop every
/ c' K9 D6 [7 O4 P+ g$ b5 L! d2 z // time step. The action is executed at time 0 relative to _# [+ N: y9 u6 y
// the beginning of the loop.
3 T' l+ R+ Q: f% U# L; k
- q5 w4 @3 y M) K1 G# x2 { // This is a simple schedule, with only one action that is2 k" Q, \- E+ z8 o& g) ^, I
// just repeated every time. See jmousetrap for more6 \4 f8 P! b3 v x2 |
// complicated schedules.
( I9 G8 z, n; W5 X( `$ d
0 m- f9 U) e, z modelSchedule = new ScheduleImpl (getZone (), 1);
1 u X* [6 k: |+ y! w! B modelSchedule.at$createAction (0, modelActions);
# V/ ]0 M6 O5 `$ p " x& m! V) m# ~! {4 {' d u
return this; C0 F8 F+ Y! k. b1 t! A+ [
} |