HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ r- o' @0 p7 h
. |3 W5 o. M* T4 l public Object buildActions () {/ O+ `3 x' }" Z' ~, d8 \
super.buildActions();
1 L3 o. X3 j3 h. I% f t5 O ( ^2 ?) z$ t* _' y# o" \. V* Y
// Create the list of simulation actions. We put these in* Y7 H/ Q& S; a+ q
// an action group, because we want these actions to be
* v9 U$ D0 \$ F: S I- i // executed in a specific order, but these steps should9 M7 x5 j7 B: \, ~0 _
// take no (simulated) time. The M(foo) means "The message2 f' ? Q% O9 A! i" n$ n- X
// called <foo>". You can send a message To a particular( o4 o5 f8 F8 D. a
// object, or ForEach object in a collection./ V Q6 {! L! I& u
; w- P. x, o6 P( `! u7 F6 D! r // Note we update the heatspace in two phases: first run
2 M3 Y' \; _" l' ?+ T+ | // diffusion, then run "updateWorld" to actually enact the
" g0 z" y0 @2 K& y2 Y+ x7 s // changes the heatbugs have made. The ordering here is
$ H+ \2 ]% Z" u9 K& F7 _ // significant!* h. D; b2 G' V1 e, l% b
4 R) c9 u% H, D8 S' R1 A // Note also, that with the additional
6 n5 |$ C- G5 o! c: n3 t2 A // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 z/ \8 [0 J# P) \ // randomize the order in which the bugs actually run
9 ^1 U F% Q, x) L // their step rule. This has the effect of removing any9 Y: R* S4 S7 V, o1 t3 P
// systematic bias in the iteration throught the heatbug/ S9 N/ j& i8 r; b) T1 b. \
// list from timestep to timestep
w8 A* Y& x' B' ^6 s' \6 a8 @
8 G2 D1 J, K* M$ k; M( n+ L // By default, all `createActionForEach' modelActions have+ x" I% v6 u! m7 k
// a default order of `Sequential', which means that the
. A0 h$ C: h" R // order of iteration through the `heatbugList' will be) l/ K9 _: N9 X O* O$ W1 S/ ~2 _
// identical (assuming the list order is not changed, C, p# Y3 c8 B/ s, _% [- V! f! N
// indirectly by some other process).
* P3 r* v9 [: b 0 b1 g6 _% m( x! T
modelActions = new ActionGroupImpl (getZone ());
6 j; h, `8 t. v) z" [- v; n% D& P |
, j4 `6 U5 S) ]& T# e: ^ try {
6 \( B; {" @; E) ? modelActions.createActionTo$message( R4 I4 f/ V5 Z8 W. x% R
(heat, new Selector (heat.getClass (), "stepRule", false));
2 H" c- H: S, s: z+ } } catch (Exception e) {
' b: m4 v7 @/ K7 k, B* e System.err.println ("Exception stepRule: " + e.getMessage ());
' O* m, e2 X( E0 j& k; G& G, t }2 |. d$ w) L) d" E" @- e
2 Q: l S, C# m& F
try {
2 z$ `) P7 c7 s# o. s @' z8 J' U Heatbug proto = (Heatbug) heatbugList.get (0);7 \8 x' l1 X9 z* a! k
Selector sel =
- @3 d/ L# t. F; s# H new Selector (proto.getClass (), "heatbugStep", false);
# w* j8 l/ x. |6 C actionForEach =
+ t! W- s4 V' o7 d modelActions.createFActionForEachHomogeneous$call4 h/ B& `( N V' ] y8 P2 I
(heatbugList,* G: T7 W3 I9 S# P
new FCallImpl (this, proto, sel,
6 Q* Q4 x; K$ h, r new FArgumentsImpl (this, sel)));" ^6 \9 X9 N# @4 W) i1 [# Y a
} catch (Exception e) {! r% n6 Q) a" F$ Y7 p" J: a6 O" I
e.printStackTrace (System.err);
+ y- Q8 O4 O6 L1 \ I4 w0 [; Y }! L6 V K0 [( n( m
2 U @/ ^3 u7 U* m% _+ |3 S5 a syncUpdateOrder ();
5 V5 S' R2 t1 r& G# f* w9 o$ u$ m6 E: Y$ K8 h1 t% r$ k& y
try {) z6 {1 v+ \& i5 [
modelActions.createActionTo$message
6 u' u" A7 C" P3 w2 ?" ~ (heat, new Selector (heat.getClass (), "updateLattice", false));
# a3 r$ c9 K6 n+ Q } } catch (Exception e) {/ x* k M! S6 n! b2 L1 m8 @7 P
System.err.println("Exception updateLattice: " + e.getMessage ());
8 k7 X( v3 X- e; l; B+ }3 d# q( s }
) Z! c L! G- B. i3 ^7 S- z2 _ * A- w( t/ G: y; V, d$ `9 J( e# ^
// Then we create a schedule that executes the% {- Q8 O5 p& x# r+ e& {
// modelActions. modelActions is an ActionGroup, by itself it
% c6 W( I3 \: `" _ // has no notion of time. In order to have it executed in7 E" q9 D) f. t( {
// time, we create a Schedule that says to use the
% p0 L: |7 b. ` // modelActions ActionGroup at particular times. This* Y# I- w3 x$ ^% X: S2 c. d. H
// schedule has a repeat interval of 1, it will loop every0 y2 i1 H* S- H. X% g
// time step. The action is executed at time 0 relative to
' Z" o$ c5 P0 M# X+ r. P( A // the beginning of the loop.
5 a# H* e2 d- C/ n6 b$ v$ E2 L) K
8 R: Z: C9 c% x1 V( A4 @+ L* I0 l // This is a simple schedule, with only one action that is
0 V# y& A3 p8 j; F // just repeated every time. See jmousetrap for more
* z* T d9 ~1 w // complicated schedules.
$ h' q- o% O1 e3 U* ?6 G$ N
' }7 u4 V: f) D6 G modelSchedule = new ScheduleImpl (getZone (), 1);- O* L, s4 H1 ^* U2 W' z! q8 e
modelSchedule.at$createAction (0, modelActions);
9 u& ?" D0 T7 b z { 5 M, l3 d6 @9 p! M. T% N
return this;5 q) h6 ~* k- q1 d A- s! y& f
} |