HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* l9 n @! r6 X f" a. J/ M2 ^' Q9 v5 @$ P
public Object buildActions () {( {) z$ J) s. b
super.buildActions();
6 H t7 d; n* E& @8 \/ X ! v1 `6 W4 D3 C$ B! i5 F* O. ?4 q
// Create the list of simulation actions. We put these in8 \! \) T/ k3 u9 c# F4 i# ?
// an action group, because we want these actions to be
) y- e& S* C! g6 R // executed in a specific order, but these steps should
9 i7 M( I7 P1 T4 N( \ // take no (simulated) time. The M(foo) means "The message* X2 X% q/ f7 N: y
// called <foo>". You can send a message To a particular
# p) e2 e. `7 b# C // object, or ForEach object in a collection.( v# o5 o0 |' X9 [; e
1 f1 D2 l* t5 B5 `
// Note we update the heatspace in two phases: first run* P$ @3 L% Q2 o: n
// diffusion, then run "updateWorld" to actually enact the
1 T3 @- _% ^% Q+ y; L5 X // changes the heatbugs have made. The ordering here is, J d" K) u ~' c
// significant!7 k8 ]- h( ~2 P4 a. O) W+ k+ t
) M9 E: ~; V: l7 s) D( k
// Note also, that with the additional) C; f: \' J* D9 h# }4 u
// `randomizeHeatbugUpdateOrder' Boolean flag we can! T2 W/ m, n3 t* G7 G' m
// randomize the order in which the bugs actually run$ x Z4 z4 [1 M& B [& c8 h) R+ R
// their step rule. This has the effect of removing any
7 u" d. r: N% D) u // systematic bias in the iteration throught the heatbug
! I6 ]# J! l) L$ O4 g1 Y // list from timestep to timestep5 j' F& w" n, W
5 O" w- v+ {$ P8 J Y
// By default, all `createActionForEach' modelActions have
. g3 [2 Z+ u( \: @: ?/ f+ Z // a default order of `Sequential', which means that the
" w+ g+ F* G9 G( o) Y8 r // order of iteration through the `heatbugList' will be
2 X' N/ |3 @1 W c7 }: D0 n // identical (assuming the list order is not changed
4 r, u7 Z: _" r- M1 P, ` // indirectly by some other process).
# q: _& M& ]6 |& g: K) Z4 ^ / Z5 C2 E# d0 D/ \, D" E
modelActions = new ActionGroupImpl (getZone ());- T; D$ B5 f8 b* v, }
' r9 I7 s! t- _ n: B try {; w( M4 f: @2 ` u
modelActions.createActionTo$message
; z/ b; r! h" W1 I n" A$ J (heat, new Selector (heat.getClass (), "stepRule", false));
9 l& G/ D6 S# X } catch (Exception e) {
% Y" \% x5 `) ~! k System.err.println ("Exception stepRule: " + e.getMessage ());% P" n/ ?7 k d, ~8 B
}9 q; J, H* M. p) u+ U& w
+ L2 d1 [3 K5 c6 l+ N7 m try {( U3 F# _0 B9 `: k
Heatbug proto = (Heatbug) heatbugList.get (0);" d8 U k0 j O3 u
Selector sel =
" ?1 q0 {. R* R7 P new Selector (proto.getClass (), "heatbugStep", false);8 y6 A9 |- A$ u, }
actionForEach =. N# g: B3 ]. ^4 `9 \
modelActions.createFActionForEachHomogeneous$call
- S1 d1 y. y: a. N( ? (heatbugList,
! G m. ?: x0 ]" n new FCallImpl (this, proto, sel,+ e+ {( S# Q; ]+ C( y
new FArgumentsImpl (this, sel)));* Q. V' s! Z$ E2 H& P; L
} catch (Exception e) {
2 C( {, T, {7 S" ?; o$ s e.printStackTrace (System.err);
, T& R% K1 W. W- A# l2 M1 } }
) `9 M" ]& S% X6 j/ h ' C* P, }- z9 y/ _5 i. ^
syncUpdateOrder ();' J6 R( M; _" S/ T& h* A
; E: A: }) |# q1 b+ r( }; j4 Q) c try {' r6 u1 k0 r3 Q( f$ m& U$ `: R5 d
modelActions.createActionTo$message
/ p2 _+ x7 a: _, l/ A } (heat, new Selector (heat.getClass (), "updateLattice", false));. r. _- l' j; E+ o& T
} catch (Exception e) {
* X% f7 [$ k \7 F% _4 d2 } System.err.println("Exception updateLattice: " + e.getMessage ());
$ {4 `7 f) \# `$ w! J# E }
# T* a% V% u F: J! O0 W5 S ' R8 j' R6 L4 W, {
// Then we create a schedule that executes the9 m- T( M, v: s
// modelActions. modelActions is an ActionGroup, by itself it k0 V# h" {) i# i
// has no notion of time. In order to have it executed in
2 X- q- p& w; G# H // time, we create a Schedule that says to use the
3 P5 ^3 i) l5 F* e) @0 B2 X // modelActions ActionGroup at particular times. This3 W8 J) E- r9 f% J
// schedule has a repeat interval of 1, it will loop every3 E {/ {8 E, H* M2 a
// time step. The action is executed at time 0 relative to4 d2 H4 B! g* |( e
// the beginning of the loop.' v& d' A; v4 f2 D; Q( ~$ c
+ @3 f; D7 @7 W* T0 C) s
// This is a simple schedule, with only one action that is
' r9 @# S: }- S: I+ ]$ n: Z // just repeated every time. See jmousetrap for more4 y, Y/ _* v1 t
// complicated schedules.# v7 i1 c: \! a- l; D0 I$ w9 a/ Y
6 l. S, W2 b+ y0 \% L/ b- x/ G& S modelSchedule = new ScheduleImpl (getZone (), 1);
3 k9 D4 X1 ]9 W5 O$ E& h/ W modelSchedule.at$createAction (0, modelActions);* x* Y7 F; I8 s, X3 K1 F
' d! k4 p; u* d2 l# _ return this;
6 B: u7 e! k' Q# U$ k9 a } |