HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. N# A/ ~( Q$ j5 l0 j% _' c: ^/ I. m
public Object buildActions () {
. F; o/ G% J, h* u, J: U super.buildActions();
1 V$ o3 s0 v! m9 \6 r. w + U0 s7 O6 ]/ D' w; m
// Create the list of simulation actions. We put these in1 \" O5 N: o1 ?
// an action group, because we want these actions to be. d2 n: ~+ v; M: W. h4 v
// executed in a specific order, but these steps should# i b6 a3 e* ^# l" @
// take no (simulated) time. The M(foo) means "The message
) o9 U7 E% ] y; m1 { // called <foo>". You can send a message To a particular; X7 r+ w* o% s' n. w9 u
// object, or ForEach object in a collection.
! e- s) \4 Q# {, t: e+ L ( C+ C' X" G* j4 d! N- t I. j
// Note we update the heatspace in two phases: first run; N5 R2 r. d% S8 j1 ?, X9 B9 A
// diffusion, then run "updateWorld" to actually enact the+ C8 w6 g2 @8 l7 ?) i
// changes the heatbugs have made. The ordering here is$ o4 B9 O9 L6 c+ v Z& m
// significant!, V) ~! R5 T: X9 [# k
, o2 h( K8 h0 E+ o7 n( L3 ?1 M. V" P: Y // Note also, that with the additional
4 V7 l% R, k2 F2 W* ]3 h( X6 q I // `randomizeHeatbugUpdateOrder' Boolean flag we can
; j5 p9 R; e) S- E% x$ `& l // randomize the order in which the bugs actually run
! u) ?+ V1 A2 I; U& D- {2 D+ a // their step rule. This has the effect of removing any, ?+ E g! e# o1 h1 b" x3 l. a
// systematic bias in the iteration throught the heatbug
5 L5 p6 O) S* ?5 v0 P7 S7 \7 E // list from timestep to timestep
l, R4 j B$ v/ ^/ ~ 0 R% {) N$ d* ]: g' H
// By default, all `createActionForEach' modelActions have
# A) G, Y5 a$ S+ k // a default order of `Sequential', which means that the
0 M/ n1 m: Q+ @! z% Y) E6 ~) X // order of iteration through the `heatbugList' will be- Q: l, R+ ~6 s" ~
// identical (assuming the list order is not changed
A" {7 T4 Q/ [) t // indirectly by some other process).
& E& n! m, h; F2 M2 ?* j5 t1 i# \
0 D% E2 q8 `( }2 M7 m, P modelActions = new ActionGroupImpl (getZone ());
& c$ D! o5 K0 U
1 k; H, t- B) Y1 Z S try {$ i: ]% z" Y1 R% K
modelActions.createActionTo$message/ \) r( a4 G+ r" e5 A
(heat, new Selector (heat.getClass (), "stepRule", false));% U2 z9 G; O3 d! {# h# `2 v
} catch (Exception e) {
( D4 ]: v& Y& N/ N8 k, V System.err.println ("Exception stepRule: " + e.getMessage ());* R b. z. L0 ^3 i/ w- H
}+ S1 P3 j4 ^0 A/ k- L1 T' y9 Q# R* i
# ^& V. U4 \% s' H+ Z' S" C try {
* ^& D# ~$ W4 Q/ d, _& R h# q' A' W Heatbug proto = (Heatbug) heatbugList.get (0);2 K* r2 [" I/ `) d5 D
Selector sel = ' _$ |6 J' a4 p' T0 ?, v
new Selector (proto.getClass (), "heatbugStep", false);* L' r6 ]2 ?/ z: h# I! a
actionForEach =1 q5 Y- h2 d8 B: y9 t3 z% {1 b! K
modelActions.createFActionForEachHomogeneous$call: g* a4 f' B. K2 [
(heatbugList," Y! N1 Y$ \3 I8 `+ a
new FCallImpl (this, proto, sel,
7 |& q$ W, W) S" U. ]6 F. \ new FArgumentsImpl (this, sel)));
* G2 A; u* S! e. b% ? } catch (Exception e) {
9 Z N+ {: j! G+ J7 I9 @ e.printStackTrace (System.err);# u M m- o! b: {; i
}
7 e" H1 ]' B" Y8 i; w
! W- r) T7 Z3 j M. e- A+ S syncUpdateOrder ();
1 }2 ~. u3 x* v8 k
7 h3 c7 n) h |$ ?/ s) x try {
1 I. @ J( M1 L2 W modelActions.createActionTo$message
6 X1 ?& E% C R" p$ ^& q (heat, new Selector (heat.getClass (), "updateLattice", false));
6 h0 d; `1 ]+ P* o: K6 m q } catch (Exception e) {/ u% J) d4 P1 `& ?6 H
System.err.println("Exception updateLattice: " + e.getMessage ());8 O- L+ I& k4 T
}# b* `+ |& X6 E$ }6 i
- c4 ~% O% r. _9 p) @6 @5 Y
// Then we create a schedule that executes the
7 q8 A& P' Q8 g6 Y" W- M! A* R // modelActions. modelActions is an ActionGroup, by itself it; Z8 s) m, ]* E3 f6 R
// has no notion of time. In order to have it executed in
0 M2 F2 s0 N! U8 M$ n* b1 L // time, we create a Schedule that says to use the0 }2 l, x7 s# N E( f3 X* O
// modelActions ActionGroup at particular times. This- K0 g; i5 T: K k& j: B: h2 |; f& I
// schedule has a repeat interval of 1, it will loop every, J0 h6 B: M& ~
// time step. The action is executed at time 0 relative to& Y) x/ y* E% z
// the beginning of the loop.
- G& M7 O1 j! q" N1 Q5 M! \8 U; n- o! [
// This is a simple schedule, with only one action that is
+ C: N7 C$ Z9 P0 c // just repeated every time. See jmousetrap for more
Z. O. L1 l$ h- j& H3 T // complicated schedules.) ?) @& D4 p' f: Y
. S+ f! f2 \# A4 h modelSchedule = new ScheduleImpl (getZone (), 1);
7 r/ i7 r! f* }, e9 d modelSchedule.at$createAction (0, modelActions);3 _/ Z* m8 I9 Z; r/ v! D- V) ?
. ~3 i- i" G/ V# f. w return this;
3 M- Z" w0 e- V6 S1 h- n } |