HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% S, F8 l# V( m9 d6 T0 h
0 q8 u4 D5 i7 k9 y public Object buildActions () {
* }2 M! h) u( ~! r0 _ super.buildActions();" _7 n4 N7 D, a5 i t
- J% I" b1 |; p7 ~ // Create the list of simulation actions. We put these in
/ e; U/ S2 S0 n* C7 W // an action group, because we want these actions to be5 ]) d( q* V/ C4 x
// executed in a specific order, but these steps should
- T% B |5 k4 _$ ^+ g* I // take no (simulated) time. The M(foo) means "The message5 ~' ]0 H4 W4 `1 l2 _
// called <foo>". You can send a message To a particular
2 Q9 L; f) F( ]" {0 h // object, or ForEach object in a collection.
+ T7 g! Q' O- E+ o) Z& b: W8 i 7 b2 q; C: l5 ?4 u
// Note we update the heatspace in two phases: first run* I3 `! M8 t" m' k
// diffusion, then run "updateWorld" to actually enact the% H. Z! Q5 k8 T% p' Y' r: q
// changes the heatbugs have made. The ordering here is7 J8 l: O! e. Q3 V k
// significant!3 ~/ Z8 U7 U8 S, f3 }2 s3 V
& V5 x) G; c8 }$ } // Note also, that with the additional
, n$ Y8 m' V0 f( g0 r) L, f8 | // `randomizeHeatbugUpdateOrder' Boolean flag we can
, n/ j( J9 n0 Z% P // randomize the order in which the bugs actually run
$ y! j1 D1 b9 w/ Z$ G // their step rule. This has the effect of removing any
+ U0 f* O* G3 h' w8 |8 C4 \" E% ?5 ` // systematic bias in the iteration throught the heatbug, I9 [6 x0 ?% H5 d! a
// list from timestep to timestep
8 S" t) x4 r' ] : X7 {' V0 m- h$ A5 J. O* M# S
// By default, all `createActionForEach' modelActions have& e% T j9 e- O
// a default order of `Sequential', which means that the1 \! P% t2 `( @
// order of iteration through the `heatbugList' will be
; q d# A% s9 n( F: F // identical (assuming the list order is not changed, K0 C: g/ K2 v$ x4 X4 i/ \2 G
// indirectly by some other process).
5 ]% i" o- w. P; N' m+ h0 g
- V/ D( u7 g" F modelActions = new ActionGroupImpl (getZone ());
6 f: u- m/ v G- t
7 m- Y' m9 B2 M* v9 Q2 L try { L0 H6 F& b k' X
modelActions.createActionTo$message
& b& w: L* [6 G* G% G6 V% C (heat, new Selector (heat.getClass (), "stepRule", false));
0 P& @0 ]9 H: s1 R% a } catch (Exception e) {) v. t0 j. ?5 {0 z( }7 Z0 I& _0 X/ r
System.err.println ("Exception stepRule: " + e.getMessage ());! r7 K- D3 \8 Z4 ~6 }$ e
}
3 ^9 o: }/ ~. D$ i# \. ]2 r4 z$ I) j0 @
try {/ \6 I5 Y3 [8 ]8 L/ O
Heatbug proto = (Heatbug) heatbugList.get (0);
0 b4 | U' B& `9 c* l! }# t0 q Selector sel = 9 j; \7 [* G8 t/ O1 \; I
new Selector (proto.getClass (), "heatbugStep", false);
( u" Y& m8 H. \" p9 a# m actionForEach =
6 B Z+ Y+ A% v2 ~ modelActions.createFActionForEachHomogeneous$call* |4 q! b6 H0 F7 ~5 F
(heatbugList,& |$ r( |, H! i. _/ ?4 e' r
new FCallImpl (this, proto, sel,, V z) s( d3 M* D- K W# D/ j: ^) Z
new FArgumentsImpl (this, sel)));
, a% E0 Z$ Q y+ ^ } catch (Exception e) {- I' M: @: y4 C! d O$ ^
e.printStackTrace (System.err);5 c5 C( R) G0 ]0 f* P5 u% U- o2 l
}5 Z" i2 y# z6 G! B, p
" j0 u9 ?( R( g h
syncUpdateOrder ();. v, F m# S o, d' u$ @9 ~
: y7 P" w: ~" ~ X C, p* k6 b try {
8 Q, C! R t4 c7 B4 S( N modelActions.createActionTo$message
1 S6 U4 C& e+ K8 N: b( Z1 I, H (heat, new Selector (heat.getClass (), "updateLattice", false));+ H) ]# H3 O: j- L+ b# K/ ?
} catch (Exception e) {; N2 n$ L5 D" J( Y- O5 O0 {+ y
System.err.println("Exception updateLattice: " + e.getMessage ());6 j% `, w# C2 b; e6 J
}
4 s/ t1 B1 D3 P/ h' @/ e$ ~ " l9 r/ i# e x- S& Y* v6 H9 _
// Then we create a schedule that executes the$ ^- m; c1 j$ [/ F/ {
// modelActions. modelActions is an ActionGroup, by itself it: L3 r2 e V( `. O
// has no notion of time. In order to have it executed in: f$ V$ x( E! R3 p
// time, we create a Schedule that says to use the- Z i, y) q9 M4 `+ q" f
// modelActions ActionGroup at particular times. This7 m- I% N6 T; H, w Z$ ^" W
// schedule has a repeat interval of 1, it will loop every2 a8 k: O8 U3 T/ A: M4 Y3 x
// time step. The action is executed at time 0 relative to* j2 t4 r* V9 b3 @/ {' M/ s
// the beginning of the loop.1 T* {) V- H' W) g! j5 _
1 {- M2 e% ?! e4 a7 J! t7 y3 ^" C3 [
// This is a simple schedule, with only one action that is) I) R& m8 X3 D' R7 u8 [& n
// just repeated every time. See jmousetrap for more
$ [3 e! y4 s" k+ O* d3 K9 Z- X // complicated schedules.
* W# x) P& z4 @* s8 e0 h
8 [2 o( m' |+ L0 N8 [ modelSchedule = new ScheduleImpl (getZone (), 1);3 _/ D# ], w. A+ I9 h% c3 l
modelSchedule.at$createAction (0, modelActions);6 q- A: y" j$ m
- _# a# d2 k3 V1 X return this;" ]% ]" D: V) p/ o
} |