HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ z$ l) Q. M* j
, Y+ I/ t& q! V5 o2 o% ^ public Object buildActions () {
, Y I% [3 F/ x: J super.buildActions();
5 B% @; s5 m l- e K
8 J) L+ w j$ ~6 p& G; C/ r3 d // Create the list of simulation actions. We put these in6 s. H3 Q- e: p
// an action group, because we want these actions to be
' u _; t* L. K- l( t* S // executed in a specific order, but these steps should
& k, i* s$ b& m; T: o // take no (simulated) time. The M(foo) means "The message
5 P; V; }5 F; f& A" g$ B7 f // called <foo>". You can send a message To a particular M, F3 R' c- x: b E8 d2 o
// object, or ForEach object in a collection.8 B* A" }# H6 }$ S) g' s! s
7 M, i1 V" H1 I- A
// Note we update the heatspace in two phases: first run, L; T7 S/ Q9 ^, Z. R) U
// diffusion, then run "updateWorld" to actually enact the% T& p8 f6 d. i5 Q# {3 X( u" |
// changes the heatbugs have made. The ordering here is
8 m- [0 ^8 J6 s6 p# _5 p2 g$ [ // significant!2 F( w' W8 g0 _+ d
% A1 Y, H) h1 o1 U
// Note also, that with the additional: u2 g' i! T5 J- P+ Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 f! g% U$ P! V, i8 \
// randomize the order in which the bugs actually run
4 Z9 s) r$ N. `+ u( j // their step rule. This has the effect of removing any# U# j8 v+ z- c
// systematic bias in the iteration throught the heatbug
& U2 o5 i; @% X+ o$ S // list from timestep to timestep% ?" u8 ]" K& \7 V
+ M4 w7 D o7 F. M3 M
// By default, all `createActionForEach' modelActions have1 I2 ^6 ^2 v9 ^0 [
// a default order of `Sequential', which means that the, o; T) y _. F2 r* B& V% G
// order of iteration through the `heatbugList' will be
) v$ s" m3 y% a2 X // identical (assuming the list order is not changed4 [0 t6 K& [# l! B9 Z2 d v
// indirectly by some other process)., @2 M% o; v6 r) I! G3 y' a
0 Y6 X+ G5 a8 U ] modelActions = new ActionGroupImpl (getZone ());3 i! N) L2 {5 G
. h* _) d) S5 H0 w
try {
; B5 U: l$ a/ c0 [ modelActions.createActionTo$message2 Q/ r) ? a+ S L; p- C
(heat, new Selector (heat.getClass (), "stepRule", false));
8 p) t' m. s6 ~0 s, {2 E( x$ {# M } catch (Exception e) {( `0 p0 }' p* B0 ]& z2 O6 f
System.err.println ("Exception stepRule: " + e.getMessage ());
" A4 n; X) W7 r# {$ @2 ^ }2 @' d: U& f6 B$ x
! @" B8 p+ R) M+ o: {+ f* R7 [: v
try {
8 S" q! h- X: X/ w7 L- P2 F) b Heatbug proto = (Heatbug) heatbugList.get (0);7 r' _9 m! V+ s- e6 O9 o Z
Selector sel =
6 A: k0 \. {* A8 N new Selector (proto.getClass (), "heatbugStep", false);$ i6 C7 O( C {& [! D
actionForEach =& i# u2 c& B$ N0 U% C/ J* Q
modelActions.createFActionForEachHomogeneous$call
' p$ D, }5 T3 Z, t* y% G (heatbugList,* M, f2 l+ U- E% w ?
new FCallImpl (this, proto, sel,
' X2 {4 K9 P" `. I& @' U8 p new FArgumentsImpl (this, sel)));1 _+ B. l c5 ?* ^" ?; t9 b
} catch (Exception e) {/ o# x3 t2 ^0 `# }6 [- W! p; R
e.printStackTrace (System.err);5 K* C4 v2 W, n; S3 G; |
}6 E/ s) n2 t4 ~! U% S! s
( w5 T& p* o- o7 x
syncUpdateOrder ();
8 \5 u/ V. J$ S" N2 j6 u8 r# R' [5 g$ I6 O' q
try {: B4 C0 `- _0 K3 h
modelActions.createActionTo$message
4 z) O4 ~# {% `, P" L( n6 E (heat, new Selector (heat.getClass (), "updateLattice", false));- p" h& L" Q; U( }2 l$ W- a: Y
} catch (Exception e) {8 b- p4 Q. f5 i% i
System.err.println("Exception updateLattice: " + e.getMessage ());
! H' n0 v4 z, ~3 }5 s/ k }+ a. K: f" C( R/ M$ V. y3 q
3 m" i3 ]0 B& X: j7 F0 n4 N I
// Then we create a schedule that executes the" ]7 u6 z. D9 T! ~% k2 o5 N2 R, l
// modelActions. modelActions is an ActionGroup, by itself it- A5 G0 A/ b, |" |8 ? z: t5 O3 j
// has no notion of time. In order to have it executed in- e" \; b. K* U7 b
// time, we create a Schedule that says to use the
* b& j9 M# S) B( G // modelActions ActionGroup at particular times. This
$ i" w; ^3 t f, l& ?0 C. G; b" S // schedule has a repeat interval of 1, it will loop every. g: \# t% z0 @$ j
// time step. The action is executed at time 0 relative to
6 P/ \) n4 j5 g5 T/ ?) z // the beginning of the loop.
2 K7 ]( ?+ T# G5 V2 R& @9 q
% Z5 H3 v& X+ P0 y // This is a simple schedule, with only one action that is$ {( r8 C* h9 c7 f; @7 x
// just repeated every time. See jmousetrap for more% s+ d; S! \0 g5 J2 x/ H4 p4 N
// complicated schedules.
+ D! r3 V* T1 }& Z L) n: I& g6 O
B7 H: F! l% w% I+ p% o modelSchedule = new ScheduleImpl (getZone (), 1);
+ B5 R7 n2 X, u) V6 n; } modelSchedule.at$createAction (0, modelActions);
1 r3 \% w8 q2 r* j+ t 0 i z7 z6 E4 b% ]4 ?: \/ E
return this;0 _$ A# W- H- @- U
} |