HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. T u1 b9 @# ?8 `* b& R* j6 S# l5 L) L& ~* g/ X
public Object buildActions () {
" \ S" b, o5 @6 n super.buildActions();
4 z" [6 @ e/ q! [. m. T
" }% j5 V6 c8 o' j* T9 o' _" ^ // Create the list of simulation actions. We put these in# F! X, V! U2 q
// an action group, because we want these actions to be
6 E3 f0 X7 S g1 b8 Y // executed in a specific order, but these steps should
% g x. W# M( u S // take no (simulated) time. The M(foo) means "The message9 a8 u$ @6 y- C6 i+ ^4 _& g
// called <foo>". You can send a message To a particular
! S! Z. j& V( f4 Z( L- G+ e // object, or ForEach object in a collection.8 n. C4 p) S4 u
, }# v- U7 {; \ // Note we update the heatspace in two phases: first run
" q- C- E2 i" s* R // diffusion, then run "updateWorld" to actually enact the
1 i; |7 P6 W" b9 {# v9 ] // changes the heatbugs have made. The ordering here is
/ r# X* j* N9 ]$ Q. G // significant!
6 U3 ?* `# c7 D' S" q/ A$ G8 G # d1 ?& s7 a. n$ S( p9 b% q
// Note also, that with the additional& P3 Z( b5 A6 V9 S5 u
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ W3 ?; D# S: k& y) {; R/ h) O // randomize the order in which the bugs actually run
( l6 c* v6 O. E4 ]2 z/ c // their step rule. This has the effect of removing any4 u6 O7 u1 @( \
// systematic bias in the iteration throught the heatbug
- X3 j* D$ e9 f5 q s // list from timestep to timestep
2 K3 q4 O! M$ Y, D/ ?! Z- I
$ Q% z' b4 [6 W/ b* p |; L // By default, all `createActionForEach' modelActions have
' ^, L7 M7 z, O1 @! w // a default order of `Sequential', which means that the
( Q) T4 x: P; L7 X* n9 r // order of iteration through the `heatbugList' will be- K+ e [$ }& B# {- r( r
// identical (assuming the list order is not changed
1 G0 d& T# h8 ~ // indirectly by some other process).: v1 a; F3 v8 s7 w8 n
8 ?- i/ I' |9 [* q: o' o modelActions = new ActionGroupImpl (getZone ());, q3 w' _" E! `; R
; o3 ` w* |# d) ]& Q7 D try {
( S( ?3 _/ T$ M# S5 ?( V+ s0 _ modelActions.createActionTo$message) g/ [# b! F% y' G4 X
(heat, new Selector (heat.getClass (), "stepRule", false));
6 N- o2 }! [0 u2 `1 n) } } catch (Exception e) {4 u+ c4 @" A9 e% V% s, k
System.err.println ("Exception stepRule: " + e.getMessage ());" B. a0 K3 Y k* W* r. m
}8 Z) {# U& |. |1 j# |
2 j6 L* e$ F7 F1 F4 F8 @) e try {1 W- `7 D2 }# P% @8 X
Heatbug proto = (Heatbug) heatbugList.get (0);4 U& Z/ E$ N: m1 E& r! `
Selector sel =
q$ T! F/ \, G* E new Selector (proto.getClass (), "heatbugStep", false);, V* O* ^7 J* R* i
actionForEach =
. u d3 i2 F" U* y2 Q! [ modelActions.createFActionForEachHomogeneous$call
: `2 s3 i5 V/ H% ~- _! @5 ~0 k (heatbugList,
# o5 Y3 f: V0 V: _) H! j2 \ new FCallImpl (this, proto, sel,
$ [( i0 I- q# c) G- D6 ^% Z8 W0 S new FArgumentsImpl (this, sel)));1 O% E, z: ]$ w- `
} catch (Exception e) {9 E1 B8 ` D$ Q( |7 y: B0 o
e.printStackTrace (System.err);- i4 x1 m$ c0 W6 t
}0 U3 X: w9 B1 U2 V) Z9 P
. t$ K) j5 t2 u9 `- E/ v5 M8 y7 g syncUpdateOrder ();
3 o; B$ X# C# t! l: Q$ o' N! `9 C- V/ h8 ?
try {
$ N- U3 q r+ i! c: Z* f! q2 f1 O modelActions.createActionTo$message
0 S! H7 X$ y f6 y- d$ H$ s (heat, new Selector (heat.getClass (), "updateLattice", false));, w. B" y# z6 F" u* x6 p, x* E1 H
} catch (Exception e) {
- T4 m% Z$ b3 w System.err.println("Exception updateLattice: " + e.getMessage ());5 \$ G3 ?. C) K. O2 q8 A
}
2 s8 Z* k* r+ M, t7 d1 x: c6 ^ ; H1 U. Z! {* [0 R# M' M' o7 U1 c7 ~
// Then we create a schedule that executes the( P- w z6 C( ]( P6 G: S
// modelActions. modelActions is an ActionGroup, by itself it3 w9 ~/ ?' d, \; i' r0 ~# T9 c
// has no notion of time. In order to have it executed in' {! G# Y0 q8 C& F% m# g
// time, we create a Schedule that says to use the: c' _: L( K4 \3 {; @4 |
// modelActions ActionGroup at particular times. This
0 t2 e! ^" \, ]) t // schedule has a repeat interval of 1, it will loop every+ [- f( T* C6 d: b6 X
// time step. The action is executed at time 0 relative to
% D1 V4 Z3 z0 i& G7 e1 q6 i // the beginning of the loop.
, U4 F- w/ t9 x* p0 n' Z
" c. B. @- Y2 \' K% K" _ // This is a simple schedule, with only one action that is
|7 M, x: O1 Z3 O2 I // just repeated every time. See jmousetrap for more
$ d9 l' \5 T1 L8 c0 R // complicated schedules.
6 \6 q0 o# |6 t
, J& y# |7 S4 |$ i, J3 ? modelSchedule = new ScheduleImpl (getZone (), 1);; C- {- F( ^( C" e" x6 f# g6 _$ L
modelSchedule.at$createAction (0, modelActions);
0 g' D$ B$ u/ }$ T) d
* C; v/ C! T2 u$ ?* Y return this;, c4 G! h5 i2 q
} |