HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 q; O% h" Y; b2 D2 x6 F
. |$ W' F* S$ A& `1 \7 k
public Object buildActions () {
& a& c& W+ p3 Q) i4 h! w super.buildActions();
# L" i' J/ g. ?
5 u' ? R. e# B! R7 E // Create the list of simulation actions. We put these in
$ {5 v' d/ E+ q% V. q" d! u // an action group, because we want these actions to be
' p- o8 C' D5 h // executed in a specific order, but these steps should; G( h1 U6 r- k" B
// take no (simulated) time. The M(foo) means "The message( |5 Z/ K8 v; M5 }4 b w8 ^
// called <foo>". You can send a message To a particular
3 h& F$ N5 P: ~1 G6 X6 A // object, or ForEach object in a collection.
* j& }( p1 R X; ?# B/ n6 e
; S+ \' j/ _, \! t8 G, l // Note we update the heatspace in two phases: first run
$ r0 c i, \. | D // diffusion, then run "updateWorld" to actually enact the' `7 f' M# i* d/ h+ K; v
// changes the heatbugs have made. The ordering here is K4 B3 ]% s6 y; z3 e# I' u& o) I
// significant!) m% K) r4 d$ b+ c) A8 V# H% L1 m/ x
& D3 _" ~) w# q: J; X
// Note also, that with the additional
o1 r, I3 r2 ]4 j // `randomizeHeatbugUpdateOrder' Boolean flag we can7 U1 ]" t9 x6 b C1 T% D2 B
// randomize the order in which the bugs actually run3 Q. O: r& E. S$ E: f, r' n* ~
// their step rule. This has the effect of removing any9 |" Q: `3 i& {( }8 {
// systematic bias in the iteration throught the heatbug
8 F0 l" m8 E; R* P( A" H3 G4 m // list from timestep to timestep
3 N# o0 m! X2 }/ A " p8 K( g" p, e" V/ `
// By default, all `createActionForEach' modelActions have- p9 v* n% l. F% ?
// a default order of `Sequential', which means that the1 S4 {( T* F4 A' B
// order of iteration through the `heatbugList' will be
! e1 Z3 I4 `$ Q$ N# _ // identical (assuming the list order is not changed
4 `9 Z4 Z+ } l8 E v5 O // indirectly by some other process).
! c0 H/ D! _! W4 _$ D. ` j
0 `- t$ s; e* _! C6 X( h$ F1 i modelActions = new ActionGroupImpl (getZone ());
+ L) d3 ~: {( `2 [# ^
4 |. y) B- w/ F4 j5 { try {
) p+ g) n! u# ]! p modelActions.createActionTo$message
1 g' v0 h& }$ v: u& r, C (heat, new Selector (heat.getClass (), "stepRule", false));
9 R, ?. L! ]! Z5 u* e } catch (Exception e) {# N- @* v% ]7 Y* P
System.err.println ("Exception stepRule: " + e.getMessage ());
$ g- D7 {' y Z+ z2 d8 z }* y5 G, D/ o% q; i6 s% f8 g+ s
4 u# } T7 I! X# }, V3 r7 ^ try {
7 ]& W' z$ Y6 M+ b3 [; m* O; ]3 y Heatbug proto = (Heatbug) heatbugList.get (0);
* f$ `1 \& c1 k3 l% ] Selector sel = 9 x: ?* y' S. F
new Selector (proto.getClass (), "heatbugStep", false);
0 T. v- |4 S+ y actionForEach =
5 M. l+ S( o' I4 S) x0 h6 M modelActions.createFActionForEachHomogeneous$call
1 d; }' a* }9 k6 ?. j (heatbugList," F; i! a& e) H4 T/ j8 V
new FCallImpl (this, proto, sel,. O8 E7 Y9 K3 k) ]6 I* `% c1 D! @
new FArgumentsImpl (this, sel)));
( c$ N. q' _0 Z } catch (Exception e) {
/ C% x, w! e! \, L$ | e.printStackTrace (System.err);/ o5 U( w. r [1 X3 J$ y
}
- j* r. W* l( o/ G! u2 i - }, Q0 q4 r1 b! B& t
syncUpdateOrder ();
) O5 A# [3 n- K) ~! z) R7 D* j; K: M/ H+ T4 S5 P9 Q, C
try {
P- ~2 c6 L4 Q. e, n! G, g modelActions.createActionTo$message
# G d2 Z3 E4 q; a7 i (heat, new Selector (heat.getClass (), "updateLattice", false));4 Q7 \5 i0 q+ R3 k
} catch (Exception e) {
* ?7 `/ K% t H9 S3 Z$ v( y System.err.println("Exception updateLattice: " + e.getMessage ());5 q$ G# j; l, @6 B% v5 M4 m
}
& i6 v# u7 e( n) L+ S- G ) n5 Q5 M. _. F) ~
// Then we create a schedule that executes the$ C4 i' g4 O+ c5 x# v% `
// modelActions. modelActions is an ActionGroup, by itself it) I% { d/ }; z" d5 g$ y
// has no notion of time. In order to have it executed in4 ]( B3 g4 d) l3 n; @/ [6 h
// time, we create a Schedule that says to use the' J2 H( s$ E$ E: \+ e0 t
// modelActions ActionGroup at particular times. This
% A& i4 |) x' Y/ y // schedule has a repeat interval of 1, it will loop every
D+ F$ y" ]+ e( p3 e3 Z // time step. The action is executed at time 0 relative to
4 B; M0 ]& F2 F4 `' A& L // the beginning of the loop.
9 w9 x( w+ Z2 n- D
0 n! |5 x/ b+ m // This is a simple schedule, with only one action that is
, b' o) P N/ C, c7 N% u // just repeated every time. See jmousetrap for more8 ?0 L4 }$ I: S7 P5 j
// complicated schedules.4 A. ?: S7 M/ T- ]4 |/ g% R/ f
% O; o- d4 v7 E* a; [ modelSchedule = new ScheduleImpl (getZone (), 1);- }2 G H. j9 e- {
modelSchedule.at$createAction (0, modelActions);
8 |& {6 q* _" }7 e @6 c
8 g5 {7 y. P/ e: r4 a) U return this;% d' Q# ^6 j- j+ S) w; T# j4 }
} |