HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 W/ m7 R/ |8 @$ a
% k& _4 }, p! d W) ?9 ^4 z public Object buildActions () {8 J! c* [! D/ I" L: X) o
super.buildActions();- P$ r+ O8 i& Q# [3 q: @8 u) F
E9 a" [* g/ s: T1 x$ U5 A7 j // Create the list of simulation actions. We put these in3 E- \5 `! E9 U& I) Z
// an action group, because we want these actions to be
7 f7 P6 v9 P8 f- C // executed in a specific order, but these steps should% [( R: P1 r+ n _% e# F1 U
// take no (simulated) time. The M(foo) means "The message
% h; E- ^; a3 U // called <foo>". You can send a message To a particular+ P% J! p+ x8 r# {, _( D+ q! a
// object, or ForEach object in a collection.
- F) o1 f" J6 H0 J % Z& i; w8 K" @/ \4 Z0 d% N
// Note we update the heatspace in two phases: first run
+ i" a9 c/ {( F; l2 \! U9 \ // diffusion, then run "updateWorld" to actually enact the: w- m4 c- z% h6 u
// changes the heatbugs have made. The ordering here is0 I1 I1 Y% b) C- B$ l$ K
// significant!7 v% B7 z9 {% U! [( |
" q% f" U3 k# R j2 }. I% N6 d
// Note also, that with the additional
- |) [" m, A% Y1 L5 ]4 M // `randomizeHeatbugUpdateOrder' Boolean flag we can
; R4 R( d* N1 _; B9 c // randomize the order in which the bugs actually run
1 ?! @8 S+ ?0 Z6 ~. c. p# X // their step rule. This has the effect of removing any
5 a5 E7 e! D% J // systematic bias in the iteration throught the heatbug
[5 k" v. Q9 u0 p4 z8 w/ V4 o, G // list from timestep to timestep* S$ z# O7 z {/ y5 A& A
; C6 i% Q! R' ?# E( {/ _! s' J
// By default, all `createActionForEach' modelActions have/ b$ X$ J) m( k7 F2 H
// a default order of `Sequential', which means that the
! B/ S3 G) v6 H2 z // order of iteration through the `heatbugList' will be
8 m. B/ N! M8 X( u# i; N% M // identical (assuming the list order is not changed
2 P3 X V3 d' l+ D3 ` // indirectly by some other process). u! x& y. |- o+ W
9 o4 J. |3 A" A" P' k) q2 E' n
modelActions = new ActionGroupImpl (getZone ());
, l1 f- A; W3 ]; ^; b
. j' v, t# V F: ]( x$ G6 `9 c1 F try {
( Q- l) v8 q @! M2 M5 G modelActions.createActionTo$message
7 i6 z. y# C- l: ?% M/ K" j (heat, new Selector (heat.getClass (), "stepRule", false));3 S3 u; D/ Y. { |0 B
} catch (Exception e) {4 V8 J( A$ n1 {9 ?- q1 z8 Q: `& o! D
System.err.println ("Exception stepRule: " + e.getMessage ());
! X! ]; Z5 o( f( v; ]. b }, F c. B' S9 s' g% [! N. ]
8 Y8 V4 O: Y* | try {
: z3 k0 g. k# S( P2 _ Heatbug proto = (Heatbug) heatbugList.get (0);
! E4 Y* b. b6 ^ Selector sel = , o2 D7 b1 |+ N' l5 O3 c
new Selector (proto.getClass (), "heatbugStep", false);9 A: b: D9 u6 l8 d# k9 \
actionForEach =
8 D* x2 p- l$ a9 B% w5 Z modelActions.createFActionForEachHomogeneous$call5 t' |7 H& a$ S! M
(heatbugList,
4 p! D! Q4 u+ K" p7 }- ?4 @; ` new FCallImpl (this, proto, sel,2 z9 C- G* W' ]# _9 `
new FArgumentsImpl (this, sel)));
( `/ u1 A5 B5 P M4 a: c! d } catch (Exception e) {: F& P# U. Y0 c
e.printStackTrace (System.err);
& S$ x7 i5 j3 F- }3 l }
z2 a' r1 u" t& J
' b: c7 I1 V2 j, A% m syncUpdateOrder ();
8 P L1 F" E+ Z |: E& O
2 d% S2 V7 ?* x8 L. V try {2 u) `( d; D; m/ M) B+ c8 k6 D# C
modelActions.createActionTo$message
- _% ^- }+ W1 H9 [ (heat, new Selector (heat.getClass (), "updateLattice", false));
* Z- W8 ^7 p0 V2 I& u& x5 ^ a } catch (Exception e) {
, M: k9 Z- D9 r2 |# H' I- M System.err.println("Exception updateLattice: " + e.getMessage ());
% N$ l% j! _( _! c0 r }
+ }# k, ^5 f- d1 g! |* y , I! V" F: I4 b3 E* |
// Then we create a schedule that executes the1 j( {3 a) @1 L5 a9 G3 Q+ h
// modelActions. modelActions is an ActionGroup, by itself it
( d# g3 q7 A6 J4 m, [ // has no notion of time. In order to have it executed in7 F0 S1 {2 Y% Z+ j z, C; x2 y+ P: K/ y
// time, we create a Schedule that says to use the" _) `3 m; m _6 Q& l2 J
// modelActions ActionGroup at particular times. This/ ]* c/ S1 H: \4 F4 K6 V0 L
// schedule has a repeat interval of 1, it will loop every
) _) x( w6 b; L9 T$ l // time step. The action is executed at time 0 relative to6 T' s" X( V. \* Z, z$ N% C5 A
// the beginning of the loop.9 G7 S7 h+ x$ q
3 l1 I/ y& x' W0 u7 X9 Z // This is a simple schedule, with only one action that is
9 H( E1 e9 S5 [0 w6 Y // just repeated every time. See jmousetrap for more
6 T! I- G9 T- x4 |7 Y) ]* w7 Z // complicated schedules.1 L7 U$ ?7 `6 [2 D- b0 y* L
$ t, v6 b, U: E3 T modelSchedule = new ScheduleImpl (getZone (), 1);1 r3 l, ^6 Q/ @8 k5 s; X
modelSchedule.at$createAction (0, modelActions);7 F0 q* i3 {! B1 M8 ] |
" z5 d2 Z# Z3 N return this;
/ H" ~% }6 \. q } |