HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 u5 ]" C; Q1 P( J( V7 T4 u/ x( P5 R+ i& w1 m# }2 J+ ^
public Object buildActions () {
# i' v: v2 I9 A super.buildActions();
! J3 w! a# c8 V/ Y" z - }+ E: Q' }" B& ^- `
// Create the list of simulation actions. We put these in
* X! Q% j7 d0 n. H+ J% ` // an action group, because we want these actions to be
& r- u' @4 V3 P: r' e( L // executed in a specific order, but these steps should
3 @! A- V' p9 E! E // take no (simulated) time. The M(foo) means "The message
! P# U0 L# v5 V' @# s8 ~ // called <foo>". You can send a message To a particular
+ A+ |3 }- E% G" m) K& }5 ?8 H% b // object, or ForEach object in a collection.
, ?' p2 ?: q0 l4 K6 ~1 f3 f6 V5 D
; s2 o/ z1 p+ `' h8 L+ f' [ // Note we update the heatspace in two phases: first run
. H; N# v* Q* X- n; |* e // diffusion, then run "updateWorld" to actually enact the# X6 m3 S) D! U/ u8 o K
// changes the heatbugs have made. The ordering here is
( N$ N. u& L" B+ O0 j // significant!* J' T6 e# D& j' A
: m6 w; p" s* K% W4 C // Note also, that with the additional
% y3 V+ x1 V9 k# V3 X: w. ]: ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 U7 G1 {9 E0 f. W% Q0 J1 b // randomize the order in which the bugs actually run
9 x* a; @ U9 P7 @+ M- a6 _+ d // their step rule. This has the effect of removing any# o( Q; P* F" q# W% }& K. t
// systematic bias in the iteration throught the heatbug- N1 q1 r7 m" U" H' o, ^9 f( j
// list from timestep to timestep
/ K9 ?% @3 ~- @5 T1 g
5 \3 v. |9 Q3 f // By default, all `createActionForEach' modelActions have
* V; E# o" b/ Y& | // a default order of `Sequential', which means that the
/ P+ c/ {2 _: O( _ // order of iteration through the `heatbugList' will be
# J7 x; ^5 E9 a/ j // identical (assuming the list order is not changed2 F" l( o0 a4 k) X( u3 k& a9 | @
// indirectly by some other process).0 y2 v+ U6 C" b
6 x) K$ ]' Q6 O# T9 \ modelActions = new ActionGroupImpl (getZone ());. C! P5 r+ B# I& L
. C" T- l# w% @" M6 W5 e& @ try {
1 q" S8 Z" \7 [" {1 f modelActions.createActionTo$message8 H8 U6 o! \, O& ]2 E% @: {
(heat, new Selector (heat.getClass (), "stepRule", false));
6 X/ C9 J3 E* q. z } catch (Exception e) {6 i( u" m. {. X
System.err.println ("Exception stepRule: " + e.getMessage ());) c7 R3 C+ V" _1 N- _* ~5 l e6 M; a. h
}
1 A Q$ _. _6 [' S$ E; L; n8 f; H: N0 D
6 e6 D4 V( R+ b5 f+ Z: [* d- e try {
8 G5 m! U0 a. v, J. e5 { Heatbug proto = (Heatbug) heatbugList.get (0);! c5 E0 g& I$ |
Selector sel =
; p) _ }+ E7 `! R! I new Selector (proto.getClass (), "heatbugStep", false);
s1 M, p3 R: p actionForEach =
5 r) B% ~% g6 O* T modelActions.createFActionForEachHomogeneous$call
2 a3 ?1 h/ A/ y+ [ (heatbugList,
; G- S7 [# _# R! G! x3 [ new FCallImpl (this, proto, sel, Y* d0 N/ @2 Q) m
new FArgumentsImpl (this, sel)));
! Y7 D) u% C' ~ } catch (Exception e) {
B4 ~4 b9 _) p1 _$ e0 Z e.printStackTrace (System.err);
) i" ?" h" L1 P% T5 k" e }) [0 Q8 z! H! }' R& z, t$ o& R4 a6 J
8 ~" R: D) m3 y. E$ w$ g/ h, U syncUpdateOrder ();/ J0 V% G! \& ^6 T4 K
5 H9 W+ s+ F1 A# l try {* t& I, L: ?! g6 S
modelActions.createActionTo$message
6 D; C, @5 X' L- @, W. S' I7 P (heat, new Selector (heat.getClass (), "updateLattice", false));. b4 S w, T0 z1 e% g
} catch (Exception e) {' @) h" w( ]; ~! G& i/ u
System.err.println("Exception updateLattice: " + e.getMessage ());+ M- H) g0 a n( D, B% D
}' a; G3 h+ [* {
1 u1 v4 I! q* Q9 |. K+ v
// Then we create a schedule that executes the1 o. Z0 C& ], l2 n
// modelActions. modelActions is an ActionGroup, by itself it
! R* R) P% `5 c5 C& t // has no notion of time. In order to have it executed in
, r: e. \- C% M) L$ J$ { // time, we create a Schedule that says to use the$ Q1 Q0 P$ ?" R. q1 P( { j0 D! U
// modelActions ActionGroup at particular times. This
; R2 ]0 T+ Q ` // schedule has a repeat interval of 1, it will loop every' b0 t4 G. `1 {& `$ G6 q: C$ j
// time step. The action is executed at time 0 relative to0 h) g5 U1 @1 |& T: l1 l4 U
// the beginning of the loop.
: z+ d! h6 c5 b! \. N% k& X
7 G* u7 |/ I: d( @0 Y% N( L // This is a simple schedule, with only one action that is
9 t- N! [9 l4 m2 g" i // just repeated every time. See jmousetrap for more
6 G+ A. S) G6 h& E$ g6 N# V' ? // complicated schedules.! G, Q1 ]/ v Z( E3 H( ^
* e2 U, y. E' e* @* s0 d+ K5 x modelSchedule = new ScheduleImpl (getZone (), 1);) R( }5 y5 v2 I; t
modelSchedule.at$createAction (0, modelActions);( n4 N" e& P- M1 O$ I k% n$ S# |& \; ?
$ G0 \" _1 k3 u; \4 g return this;$ t! G& X- [0 e D4 i
} |