HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* _: h1 U/ m9 w% G( N6 s3 w
1 R6 H5 w( g- _% T9 {
public Object buildActions () {
8 t! k$ B% t, L4 V& ^, t super.buildActions();
& v, _2 K- `* Q$ M8 \3 |3 x
+ @; d$ l; p) } // Create the list of simulation actions. We put these in
( U$ X: c$ k6 H7 \/ C0 K! N% y; _; T // an action group, because we want these actions to be
3 {0 c& W$ Q9 K1 l // executed in a specific order, but these steps should3 G# w- h) N3 |* n/ A8 X# M
// take no (simulated) time. The M(foo) means "The message0 Y/ E9 R. q5 }& v& C; S+ `/ P
// called <foo>". You can send a message To a particular
! E. d/ C8 F% b7 X ~9 @: c! l# @/ B+ y // object, or ForEach object in a collection.
7 F; d; g! \8 [& T( n0 F ' ^: s0 g! S: W/ w" |1 N% d" O
// Note we update the heatspace in two phases: first run1 |+ F, K/ p' i& i
// diffusion, then run "updateWorld" to actually enact the
& V% o Q3 D @ // changes the heatbugs have made. The ordering here is E; `$ B! Q* T7 ]' T' e5 ?& E0 O
// significant!
$ ]0 f* P9 m( z D " |$ C) v9 z$ A2 B; V
// Note also, that with the additional
* |: z7 n8 j: l( C; ^1 Z. g, m // `randomizeHeatbugUpdateOrder' Boolean flag we can: v# L% o+ N* J( k5 A; x2 }
// randomize the order in which the bugs actually run8 @/ f% S3 G. _- T
// their step rule. This has the effect of removing any
& n3 Z: E7 M+ Q i/ h- n! y; A // systematic bias in the iteration throught the heatbug4 U6 B2 N' ~0 Y Q
// list from timestep to timestep; B) M; S& `' L1 q$ n+ ^, D
3 V$ ^8 m+ r! V2 U: y2 E3 w+ _
// By default, all `createActionForEach' modelActions have/ h/ j4 @+ k- f2 |) Q
// a default order of `Sequential', which means that the
+ [' M" z5 e. P& I6 d // order of iteration through the `heatbugList' will be
+ b' q m+ I* J h9 i7 ^3 J" E // identical (assuming the list order is not changed9 Y+ A9 p# p# L9 A: p
// indirectly by some other process)." [( V) o& m. G+ M
9 _7 J5 P2 L* C/ N* }7 r' r
modelActions = new ActionGroupImpl (getZone ());
4 W S, K. k. {3 S' C# K t: |/ y
2 q H3 M( b1 U( R( V% L- @ try {
0 i' Y7 X* Q ~: [$ T' b4 s" _5 D modelActions.createActionTo$message
0 v) g" c0 o8 {/ W, Y! Z5 h (heat, new Selector (heat.getClass (), "stepRule", false));* i+ h4 P4 r# O! u' t* d
} catch (Exception e) {
$ c; a, c' s2 Z+ @. v2 \ System.err.println ("Exception stepRule: " + e.getMessage ());* o) c4 L/ a Z+ J; u3 @8 P
}
$ @- t, N* L, u5 m$ H8 d+ r* P8 T. h3 K( n2 X) [+ b! k
try {
, K ^" N# U& K- ]$ s Heatbug proto = (Heatbug) heatbugList.get (0);
$ K) h" N+ x8 |/ m Selector sel =
* u9 j4 s) k0 b9 @' W% `6 ~+ l7 Z new Selector (proto.getClass (), "heatbugStep", false);% w/ I& f4 B A# j1 ?
actionForEach =/ D" {# ^& T4 \
modelActions.createFActionForEachHomogeneous$call% ]8 L. H8 b3 l/ V! C' F
(heatbugList,
# B: h) c+ z- G new FCallImpl (this, proto, sel,
+ e2 b9 G n( H& E3 c% \ new FArgumentsImpl (this, sel)));
( J" w! z- {/ ~ } catch (Exception e) {7 \4 J; w2 F9 R+ J' t, S/ Z* ^
e.printStackTrace (System.err);. P. ?3 Q- g* ?3 f0 K
}
- B, {. p1 Z5 T/ _! |/ a - M$ I8 c6 {/ n' e8 z/ ^6 y
syncUpdateOrder ();
! N5 ~6 m! n, O; s$ R9 |5 u- @. o/ o- g* k9 L
try {
, X: I; q% U: {3 E& @$ {* B l+ M modelActions.createActionTo$message
' ]4 T/ m. I& A+ k; m (heat, new Selector (heat.getClass (), "updateLattice", false));% N- Y; M% m. O5 V
} catch (Exception e) {& l8 g1 w: W+ ^ J* f
System.err.println("Exception updateLattice: " + e.getMessage ()); W, T2 q2 D+ c! V
} u. f9 I) O+ W
- ?3 n" P" T' O" ~5 N4 m( ?
// Then we create a schedule that executes the' w$ v" } ] w) T% C" C; c& C
// modelActions. modelActions is an ActionGroup, by itself it& x; D- [( Q$ T0 Z+ P
// has no notion of time. In order to have it executed in3 z/ `# X) j6 @2 b( M
// time, we create a Schedule that says to use the" K1 k! }' g+ q0 O
// modelActions ActionGroup at particular times. This
( V: B# {1 E- ~1 o5 e: M# y // schedule has a repeat interval of 1, it will loop every
% C. g4 k. T: J1 S1 v `. C // time step. The action is executed at time 0 relative to: x* D E1 X3 r5 `8 |
// the beginning of the loop.
* V) ]) o1 v2 W( r% K u7 s" @8 y+ F% S
// This is a simple schedule, with only one action that is
2 F9 _, g1 z5 a: [; F- U; I, b // just repeated every time. See jmousetrap for more5 O' }6 |( ^. r* f* l2 D) Y
// complicated schedules.
5 }5 i$ U/ m7 t( _( o+ {) ^6 @% B
2 H! O* C- i! j# U1 I% X modelSchedule = new ScheduleImpl (getZone (), 1);" h8 v2 h, e2 e
modelSchedule.at$createAction (0, modelActions);
- ?; B5 x. y" n8 @1 M: _; W% J
, l: v6 U) t+ F return this;" O7 y8 V) `; M, U, w9 q* g0 o
} |