HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ q2 p1 h- x: L0 e$ S- v! L8 `) Z; V
public Object buildActions () {
% l, m5 }4 L$ K- E! I7 o super.buildActions();( D$ E: P" @% C& y
% f- e) x9 A! A) d1 k1 [ // Create the list of simulation actions. We put these in# a; p# E$ k! Y- T+ h: ^9 t2 ^* O
// an action group, because we want these actions to be
) y% q2 h/ S c& [7 |- l // executed in a specific order, but these steps should. S/ g: P: `; l/ b! C
// take no (simulated) time. The M(foo) means "The message
: ^+ K$ y8 S( u' i+ l( }9 g. E0 P // called <foo>". You can send a message To a particular- N( |; A: o5 d) Z; @
// object, or ForEach object in a collection.7 s1 U! Y. g- s
+ }2 |- r/ `2 R9 j t+ Q
// Note we update the heatspace in two phases: first run
" M# |+ }: S1 a: T# r' | // diffusion, then run "updateWorld" to actually enact the
) Q+ X0 [2 y" l) o# l // changes the heatbugs have made. The ordering here is7 R" S1 o! H% l% ^$ L: L" m% {
// significant!# Z# ^: \3 b9 F( ^2 ~( N% \, G
0 _) W& t4 h% ]: _ // Note also, that with the additional
7 t& K% v( q' [; n* S // `randomizeHeatbugUpdateOrder' Boolean flag we can/ c6 N* V' X5 f( l5 ]
// randomize the order in which the bugs actually run; E; m* X' V* u; b' O) m8 U* A
// their step rule. This has the effect of removing any# o* n: b5 D& N8 Q
// systematic bias in the iteration throught the heatbug
. U# K5 R$ i% v% N- S/ F" e // list from timestep to timestep: _, P: B+ Z. v! [ {
( q" l- e, ]/ x9 \ // By default, all `createActionForEach' modelActions have
7 h6 \6 S4 _+ o // a default order of `Sequential', which means that the0 }2 g2 A6 w7 ]4 w
// order of iteration through the `heatbugList' will be
7 p' E- G/ e2 U // identical (assuming the list order is not changed
" W1 k9 \; P4 N- M7 Y // indirectly by some other process).
: U7 d4 _; c' G f( _
! _! X6 C# m0 J O8 N# ~+ @ modelActions = new ActionGroupImpl (getZone ());
. c* H# a" s; v+ }+ k) u. i' l
2 j9 b& |3 M9 \0 |# Q* d% ^" b* T3 ] try {
1 W J* s V5 p) ]8 M, d modelActions.createActionTo$message$ X$ v& U u0 p, y( C+ h2 y, G% Y
(heat, new Selector (heat.getClass (), "stepRule", false));
' p9 {& N: } P+ m0 J0 x } catch (Exception e) {
l8 G: Z! X+ t9 { System.err.println ("Exception stepRule: " + e.getMessage ());
: {9 V6 x# }' A5 g' N3 I# o" n! g( c }
& d3 J. L( Q/ V% ?% a! o$ f- s8 {+ U v# U/ J9 M, v e
try {, p, p0 p4 U. \5 \' M
Heatbug proto = (Heatbug) heatbugList.get (0);& M# f# t q" P7 _
Selector sel = . n+ V5 q4 Y2 v) t( F
new Selector (proto.getClass (), "heatbugStep", false);9 t" X% z, g. b# x+ J
actionForEach =2 l- M) ^& W& t8 g
modelActions.createFActionForEachHomogeneous$call
0 ~! h! W8 d- `7 A- [" p (heatbugList,
' w9 W8 x: ~1 k new FCallImpl (this, proto, sel,
. x6 }. g: N; J new FArgumentsImpl (this, sel)));0 Q3 {; \/ s+ p' W
} catch (Exception e) {
# A; A l- {- z. x0 t; I e.printStackTrace (System.err);6 u7 n3 j7 L+ s. ]
}) m Z2 p- D4 H% f
7 l9 A: a% `9 u8 [9 m9 G
syncUpdateOrder ();
* c' s7 X/ Q+ I, y
* w8 ?3 P) i+ A1 p1 ]. T" B* g* n try {, @( b4 h- Y, @; f1 [$ `. J
modelActions.createActionTo$message 7 b3 w/ E1 t, m5 k0 g
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ r0 r* q7 k( s Z3 ~* i } catch (Exception e) {
! Y* t. {! H, X) q7 q% X System.err.println("Exception updateLattice: " + e.getMessage ());5 |4 P. l6 E: R8 U
}
' c7 w# g0 E4 O! W# J: A- j* F 3 j) m5 }8 P2 \6 }$ n
// Then we create a schedule that executes the
' P H% k! Z/ q( H // modelActions. modelActions is an ActionGroup, by itself it. J. G5 r" L' S( ?' x
// has no notion of time. In order to have it executed in$ M6 ?8 {9 ~' D9 D# j4 {
// time, we create a Schedule that says to use the9 u' M' v" Q* r% ~# K. ?
// modelActions ActionGroup at particular times. This% g. C6 a* G( ?. |$ ^2 R( I
// schedule has a repeat interval of 1, it will loop every
+ j5 |2 C# G6 r, ^ // time step. The action is executed at time 0 relative to
/ s4 Q. R6 ` n) U$ R // the beginning of the loop.! I3 j4 I+ M( {3 H5 e5 o
9 U# G. b* z) a+ h // This is a simple schedule, with only one action that is8 k# q: U/ I/ A) f+ b0 G/ M
// just repeated every time. See jmousetrap for more
& H! C! e6 Z5 }9 ~4 B3 l) S // complicated schedules.4 R6 a" T$ Z6 m" x; E
9 a0 f3 [& d+ S modelSchedule = new ScheduleImpl (getZone (), 1);& m/ U+ Q& s+ s$ s, J6 T9 u- L
modelSchedule.at$createAction (0, modelActions);
% Y u7 i! T+ D2 Q5 \0 X( F - ~: z: d, U/ i
return this;$ |/ @) U* d6 Z8 q* q
} |