HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: B$ [& |0 Y' F8 @' S/ V7 d1 p
' P* W: Q, Z1 V' t. Q
public Object buildActions () {. r& j- f+ H# b- y1 Z9 j
super.buildActions();
6 Q8 K/ w+ ?. K6 t5 a8 ]4 { ; k- w6 w3 L+ ^5 w8 m: s2 O
// Create the list of simulation actions. We put these in
/ ]/ C$ `+ g# E* x) v // an action group, because we want these actions to be
& S% E$ b& b7 L // executed in a specific order, but these steps should
. m) p2 F. f4 Q. h, M! [/ C6 Q // take no (simulated) time. The M(foo) means "The message I1 R. |" `; {2 i1 A4 Y+ o) `
// called <foo>". You can send a message To a particular
$ x$ U u" ~) v c% ` // object, or ForEach object in a collection.
8 ^& B' A/ N/ [: v j0 @3 | % |" C; d4 X+ K1 E
// Note we update the heatspace in two phases: first run
; D- B1 g- E% w // diffusion, then run "updateWorld" to actually enact the
N2 ~# x% d% o, e* o: ]6 B* D4 a // changes the heatbugs have made. The ordering here is
3 Y9 g6 t8 h0 F) r$ S; K- } // significant! m4 v, x9 r# ]4 H: V- S m0 m
7 `; j" {7 L/ ~ @# A
// Note also, that with the additional
1 h2 c! [ z" K1 S, K, c7 [ // `randomizeHeatbugUpdateOrder' Boolean flag we can/ P7 ~8 o) Y* g
// randomize the order in which the bugs actually run6 _0 h* w! D0 B4 J7 L3 z3 J. b
// their step rule. This has the effect of removing any8 O, Z0 J! `/ a! ~* A& l
// systematic bias in the iteration throught the heatbug
q: H, U6 @; @- d // list from timestep to timestep& E" y! @* y5 O% f( g, N+ E
- B# Q1 y9 i2 f% M2 h( L) }
// By default, all `createActionForEach' modelActions have
3 w! u4 v* M- X) c // a default order of `Sequential', which means that the: h# W8 L+ Y3 G) @" |: X
// order of iteration through the `heatbugList' will be k; ?& j+ t* d# [2 d
// identical (assuming the list order is not changed
2 D( ?2 A0 `9 X/ X5 t, \. r7 T // indirectly by some other process).0 [. ?) g3 C# Z2 M* }, U6 [
+ V2 f1 g+ A1 S6 d
modelActions = new ActionGroupImpl (getZone ());
5 v( V. E/ j0 o. x2 _! ^" d, `6 C) X: g5 h% A1 `2 R
try {# ?$ ^* d$ |$ t! U: j
modelActions.createActionTo$message
. n! o' [; R5 q; \* B (heat, new Selector (heat.getClass (), "stepRule", false));
" M1 Y2 A+ Q. K } catch (Exception e) {5 l! ^3 X, P; p% f1 i6 p
System.err.println ("Exception stepRule: " + e.getMessage ());7 X% E# f' m2 A' F8 _
}; ?$ t% s3 w/ f( ? S s( a
4 M# [( ~+ I/ W- V" w- y2 ] a# Q
try {1 R- Z1 e' I, D6 p! r2 B. b% d
Heatbug proto = (Heatbug) heatbugList.get (0);
/ _6 E6 s3 |% b Selector sel =
, F# x2 |; N7 p' W! p; K8 w" i. j new Selector (proto.getClass (), "heatbugStep", false);$ T1 T0 u0 ?( v6 D8 V
actionForEach =+ J7 P) {9 r( w$ C" f
modelActions.createFActionForEachHomogeneous$call
# {! |0 b$ B1 u% D) ]2 m (heatbugList,! d8 b$ h6 T- @! {% G
new FCallImpl (this, proto, sel,- {. C8 N& P2 Q. R" d
new FArgumentsImpl (this, sel)));+ C7 x( g' [( b( U1 b3 `# g, q+ M% {
} catch (Exception e) {% n' O. H( ]/ ]
e.printStackTrace (System.err);
) [8 L; o( u9 A# D) g- g- H* I0 h }
1 a8 ~, m0 X/ B! ]. W 7 u6 o! u6 X4 r9 | J6 ~" q
syncUpdateOrder ();! w9 r4 w9 I9 v7 e
% N2 m) k* I( J
try {3 R1 |7 T7 {) b! ^2 ^7 N
modelActions.createActionTo$message
% p3 V# }8 _ W) G0 @* [ (heat, new Selector (heat.getClass (), "updateLattice", false));) f2 Y; e6 f% U" z1 Y
} catch (Exception e) {/ V: O$ g4 m/ h! R2 q8 x& [
System.err.println("Exception updateLattice: " + e.getMessage ());
+ f, d! n% |" ` i! [ }
4 C. @/ L0 @/ b. Y' {4 n; Z
9 F2 \ G8 Y4 g/ s7 ~ Z' }4 \ // Then we create a schedule that executes the$ ]/ l) N) V1 C& i6 P
// modelActions. modelActions is an ActionGroup, by itself it T* o* O: ~7 @* E
// has no notion of time. In order to have it executed in) [' s/ J9 L& Y0 c4 g
// time, we create a Schedule that says to use the; G/ M/ | L+ K' m- _% Y
// modelActions ActionGroup at particular times. This) U0 j% @( P/ W- ` z+ W
// schedule has a repeat interval of 1, it will loop every. P5 @2 q# P# ?% }0 X, x
// time step. The action is executed at time 0 relative to
6 G- J/ c' j9 h: S // the beginning of the loop.
& M( c2 b4 e/ I
( J' F7 V6 ?/ U2 z/ i% ^ // This is a simple schedule, with only one action that is2 V9 x& g |$ ~) Y- ?6 ?
// just repeated every time. See jmousetrap for more
& ]* x4 x2 t* S* b // complicated schedules.
( @! i: E8 l6 j" ]7 Y
7 ?; P) [9 w" m! y$ ]3 F0 N; p modelSchedule = new ScheduleImpl (getZone (), 1);
{1 P, d: K( X1 D$ R0 P modelSchedule.at$createAction (0, modelActions);
* `/ _. {) c/ p N* s7 B, \3 S' D
7 Z9 d: B" I: h: Z3 Y return this; M; G, Z5 z7 j- ~2 Y. _
} |