HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 j/ E4 X3 e; t6 H/ j& |
@7 E Q0 m/ F
public Object buildActions () {, p; u: F1 m% a3 [
super.buildActions();0 V0 e5 m4 L/ c M, b- \. Z
; ?( |% @, A v. j0 Y; p0 n // Create the list of simulation actions. We put these in u5 J% B5 j3 f) R# i
// an action group, because we want these actions to be
( w' X' @ V2 ~) L8 Z/ R# H // executed in a specific order, but these steps should
7 J" h" y$ w9 d // take no (simulated) time. The M(foo) means "The message3 f8 O1 x- W& T6 b+ `! p
// called <foo>". You can send a message To a particular; N4 ?1 Q; G2 }% G4 P
// object, or ForEach object in a collection.
3 N [- ~# e8 Z: P
+ R, Q2 P |6 N4 P7 C$ n$ }4 L, S // Note we update the heatspace in two phases: first run
; ^" r- b/ U8 F# s // diffusion, then run "updateWorld" to actually enact the9 i6 ~0 ^; ~. }. c% } i
// changes the heatbugs have made. The ordering here is1 ]5 E; _3 n# W! {+ m8 _* D% C" J
// significant!
9 E( I. p4 I- D8 b) t+ u+ p# @ 8 B7 f; d& I7 G5 M/ L; Q
// Note also, that with the additional
; \0 Q( T2 t9 I) S // `randomizeHeatbugUpdateOrder' Boolean flag we can
) e+ d7 p' t' V% t; S& v' c // randomize the order in which the bugs actually run
9 _, _2 _, \9 A# J) [" `$ X* t // their step rule. This has the effect of removing any
6 P1 S6 Z- K. q // systematic bias in the iteration throught the heatbug
8 x; d: x- Q, j- j8 L // list from timestep to timestep2 I2 `. }" X9 p M* u$ V
5 x' s2 Z4 j8 w0 ^/ s3 ?$ { x! q // By default, all `createActionForEach' modelActions have
$ ^% b, C/ c2 S) ~7 T5 K // a default order of `Sequential', which means that the" d, S2 h+ t" ]# L2 Q# ]
// order of iteration through the `heatbugList' will be: u6 r" D2 c$ L9 L+ t) [
// identical (assuming the list order is not changed
. T- X+ X8 n. [: ]3 p // indirectly by some other process).8 [; s7 h9 i9 m8 _) h s6 X
3 n% o1 o" ]" m& w
modelActions = new ActionGroupImpl (getZone ());+ K# D7 M: d! r6 R- v
0 ]6 [$ M7 _7 W8 F. ~# n try {
- ~% q/ G, O, ^" ~6 C4 D/ O modelActions.createActionTo$message
% g' i) w8 f8 W% l# p8 c; H5 ^1 C (heat, new Selector (heat.getClass (), "stepRule", false));
' y8 ^+ G) h2 j% K6 ^, A: T% j0 F } catch (Exception e) {
( O. ` h: f( A2 e8 ~: K System.err.println ("Exception stepRule: " + e.getMessage ());
# W. s7 W3 _# h$ q1 U }7 R; j4 a; T' G6 B& B
0 A) G, q$ F8 e# \ try {' J! E# p$ m! G& p6 }, ]0 f( x/ ?$ E6 p
Heatbug proto = (Heatbug) heatbugList.get (0);
9 {( G4 x, d5 c( D! u3 L6 r Selector sel = ! T; r( f2 i7 N# h$ k
new Selector (proto.getClass (), "heatbugStep", false);
7 M, ?4 o' x/ m* t* m, b actionForEach =
+ ~/ L: ~4 p3 y P/ n$ b6 D modelActions.createFActionForEachHomogeneous$call" I s0 x( J4 X9 X+ k6 r8 i
(heatbugList,) v5 q, G( R! Y% r
new FCallImpl (this, proto, sel,3 I/ m; v. n) d
new FArgumentsImpl (this, sel)));) b% O* `2 b* X4 a! `& e9 N
} catch (Exception e) {
- ]7 ]% ^' E; z8 {' B" V+ ~1 w$ x e.printStackTrace (System.err);, m% [, L* C# K \& m
}2 t9 i z/ b% b% \; a9 w
7 |1 ~ }! p2 H/ Y& a syncUpdateOrder ();
: F2 b+ z( ]8 o: [/ B6 O
; p/ m0 h. P+ u% O, N) U try {1 x) K$ N- u% P+ @% |
modelActions.createActionTo$message ! N5 Y0 I9 w- d$ u& z2 W
(heat, new Selector (heat.getClass (), "updateLattice", false));
& x( m, K5 d) i5 l# T0 ~8 Z } catch (Exception e) {
" e/ P6 k/ [1 C System.err.println("Exception updateLattice: " + e.getMessage ());5 z* ~1 F5 r* O. i# u" i! J+ \/ T$ L
}7 S, V0 \# d9 N; \
. O, e/ G& @! f' [ v+ S
// Then we create a schedule that executes the) M# a9 O) ]% h) C) i
// modelActions. modelActions is an ActionGroup, by itself it: l7 ?% d% M. Y7 O; ~8 T
// has no notion of time. In order to have it executed in
2 s8 I+ [* T1 F7 R8 E // time, we create a Schedule that says to use the
; e4 W9 ?* m: P: U. o; c // modelActions ActionGroup at particular times. This2 X4 y# J8 ?) ^, B
// schedule has a repeat interval of 1, it will loop every
2 ]+ b" o; W: d/ ?$ H; K // time step. The action is executed at time 0 relative to
! V2 _2 q. F7 ] // the beginning of the loop.2 t: H M' |+ C4 r# m \
% t1 ^# a1 J" Y3 L5 W // This is a simple schedule, with only one action that is
; G( S5 @8 R8 f5 X5 |/ H% f // just repeated every time. See jmousetrap for more
- B1 G# e1 a4 p b0 ]; Z // complicated schedules.
. @ `# S3 }/ k5 i) e
2 Y6 j. w! A4 U modelSchedule = new ScheduleImpl (getZone (), 1);# L' Z) [ g4 {7 C+ F
modelSchedule.at$createAction (0, modelActions);
0 k. G( e6 I3 Q+ j- j 7 d# ]2 {* s, V3 Y3 _9 i3 S' S
return this;$ ?9 H( @; t* s8 r6 T9 `* u' k
} |