HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% P" F( O! M! ~" h G9 S& ~. i1 F M# }- D$ m& A
public Object buildActions () {
& r: r5 M7 D/ ?& z9 h& p super.buildActions();# j( N8 |) |5 o
% }$ w$ M) d; V' C$ s: } // Create the list of simulation actions. We put these in" n% d* n* v9 }# D, N+ L
// an action group, because we want these actions to be
. Q- o* {+ j( O- L d, X# M7 R! p // executed in a specific order, but these steps should# W# T, x. ?- ~& p
// take no (simulated) time. The M(foo) means "The message K/ }- Z+ m% i& w6 U% K
// called <foo>". You can send a message To a particular
/ x7 W2 j: V9 n7 y" g1 ~ // object, or ForEach object in a collection.3 P' g* w0 M( k( ~
8 w- ]' n8 h) X# G // Note we update the heatspace in two phases: first run5 Z4 y0 y+ P$ K* ~! h
// diffusion, then run "updateWorld" to actually enact the w! N1 A3 X. q# @& L- U# V1 R7 _
// changes the heatbugs have made. The ordering here is
T9 z8 ?, \, u& u( N- _ // significant!
8 @( r7 ~& i+ u4 c1 b * E/ }- t A2 f
// Note also, that with the additional
; }2 m2 t' _# ?! b9 D // `randomizeHeatbugUpdateOrder' Boolean flag we can
' C- n' }/ ?" j Y: `! v- M2 \ // randomize the order in which the bugs actually run8 Y& E4 R9 t, @& a5 V- k' Q2 b4 e
// their step rule. This has the effect of removing any# @; T# G% Q; j% _
// systematic bias in the iteration throught the heatbug. [' z6 E% j7 Y
// list from timestep to timestep1 W3 O& ~' R/ P9 f, j
0 I$ e8 b2 f. N/ `" H0 R5 e x
// By default, all `createActionForEach' modelActions have
6 Q `1 f2 i2 K+ V( H // a default order of `Sequential', which means that the
$ z7 R9 X/ c# N. s4 F( C // order of iteration through the `heatbugList' will be
+ X& h6 `1 o$ X7 M4 I // identical (assuming the list order is not changed& A1 ^, Q1 j" n% P' }* {$ `
// indirectly by some other process).% i9 _; i2 ]9 B8 l6 }
9 R* j# v7 j2 b2 ]2 B modelActions = new ActionGroupImpl (getZone ());
: C% u8 F/ Q+ K0 r6 E* ?; @3 o
# o7 n( b4 Y3 T& D try {6 d& `# j+ s% `. x$ h: S. ^
modelActions.createActionTo$message
- M5 Y/ W8 M. N% {( S7 @( V' C, w (heat, new Selector (heat.getClass (), "stepRule", false));
0 C" [( \/ G# | } catch (Exception e) {) h- |! o/ @0 {6 H, H; ^; H: E# }
System.err.println ("Exception stepRule: " + e.getMessage ()); W8 ]- @1 a6 G; h, ~2 Y. U# _/ s
}
! y9 o/ P4 Y% O$ r
" f5 p/ ]' d8 k try {
7 l$ i% ?" e' z- r7 h$ q+ W( K8 O Heatbug proto = (Heatbug) heatbugList.get (0);
* l! k4 f' y* q/ Q' l8 [3 L. Y Selector sel = & F2 Q" x1 K, Q
new Selector (proto.getClass (), "heatbugStep", false);- L) E5 M( t% c, x5 ^, W3 T
actionForEach =. _! p3 Z9 U! R! f
modelActions.createFActionForEachHomogeneous$call2 D2 x5 }1 o5 q1 ~7 Z& p
(heatbugList,
# b/ S1 _) c' S' f new FCallImpl (this, proto, sel,
B- b, N4 y% I& j* V new FArgumentsImpl (this, sel)));' I( Y, m$ `# I4 ]* c
} catch (Exception e) {: M) p# _/ K0 u0 p
e.printStackTrace (System.err);) R# C# @+ h/ k: `1 H
}
( P' h w; D I9 k + q9 j8 e3 z5 g1 x, j. ]5 H& _0 Y
syncUpdateOrder ();
% Z7 z. _& c2 @* D* |
5 s( N- A8 ~6 h try {" _" Z& k3 P% D% A$ q& L$ v" o
modelActions.createActionTo$message 9 M( M0 q' ?* n. u' E$ b, H
(heat, new Selector (heat.getClass (), "updateLattice", false));7 c$ d- ^/ }8 q J2 }. p
} catch (Exception e) {7 n/ {& t% \" R9 }1 |
System.err.println("Exception updateLattice: " + e.getMessage ()); w$ _2 W; J. r
}# _- j; G+ A% `) q# N
/ s: ?# J3 M x- n // Then we create a schedule that executes the
0 y( L' P, M" c/ R // modelActions. modelActions is an ActionGroup, by itself it
, }! F# |- I8 U7 ?) F h" c6 s" e6 C // has no notion of time. In order to have it executed in) ^, o% ^5 Y+ b6 w+ m i$ Z
// time, we create a Schedule that says to use the
, w- C: Y, B) D( T. E // modelActions ActionGroup at particular times. This( h, \6 W: V+ ]+ r& a) u7 a
// schedule has a repeat interval of 1, it will loop every S/ B0 t, z3 u0 _! _' x( t
// time step. The action is executed at time 0 relative to! I% k. f5 a; F2 ?, e
// the beginning of the loop./ x0 L; Z6 I. G% d5 U
) E3 h% r7 L) S+ r6 b& B8 ~
// This is a simple schedule, with only one action that is
% d0 N) T/ p6 y: Q // just repeated every time. See jmousetrap for more
) ~& L' a2 f# A: h9 Y // complicated schedules.
8 B5 Y" \1 F! f/ o ) O1 j; f1 N0 B G5 _
modelSchedule = new ScheduleImpl (getZone (), 1);5 N' Y, i* i0 f) p
modelSchedule.at$createAction (0, modelActions);% r9 _( d2 F7 w: ?4 B, v- ?9 R
. b- U* ] K+ c0 m0 n- L+ \" X {* c
return this;! b6 b2 p2 U+ N2 W" I
} |