HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ f1 J9 ~" ^! f, h. F0 q8 d6 M7 w/ Q
% E6 L2 y9 e6 b, m& G public Object buildActions () {; O# ]5 q8 _) b9 Y" R8 p/ J
super.buildActions();1 ]1 [- T3 e8 l9 c- I% d. X8 T
% J4 O" B7 o( F" C, i // Create the list of simulation actions. We put these in; ^. |/ y- e6 d0 l
// an action group, because we want these actions to be5 }, o6 G- S9 B; _+ p
// executed in a specific order, but these steps should
& }$ i i: q1 l0 G3 I! ? // take no (simulated) time. The M(foo) means "The message& o; u$ Q, a7 \4 s# u$ I
// called <foo>". You can send a message To a particular
7 V. }1 o# [% ~& m' p // object, or ForEach object in a collection.( O/ z* V4 c3 o/ o9 g3 N
, U0 T6 e% z% b // Note we update the heatspace in two phases: first run
+ _# m8 V% S$ ^- k/ x // diffusion, then run "updateWorld" to actually enact the
7 I: |7 a9 S- ?( I) e( p // changes the heatbugs have made. The ordering here is1 d- i) u; }3 P" S! S
// significant!5 Z2 s6 I. S# ?" D
* D* }, t6 K) f& ?$ W k
// Note also, that with the additional3 {7 o: C, ?: |% v: a( R
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 }- m( i: p) G; H/ \& @* Z. {4 \ // randomize the order in which the bugs actually run
- ~9 \0 o# V5 ^% K // their step rule. This has the effect of removing any
' O9 W# c" k5 Q" W // systematic bias in the iteration throught the heatbug; J4 m, `4 t, g$ E- |+ h
// list from timestep to timestep
! z8 \8 K" ]5 X3 w+ x t2 z9 c, r
% V$ P4 B$ e3 C; ~. \ // By default, all `createActionForEach' modelActions have
4 ?+ D f! H% B3 y( r8 D: m // a default order of `Sequential', which means that the
) j# X$ c; X1 t5 d# i, ~ // order of iteration through the `heatbugList' will be/ _6 f; \+ a& u6 x- ^, R
// identical (assuming the list order is not changed
; P* u" A" P& C' `/ ? // indirectly by some other process).
% ^. Z7 w3 Q, u
7 s8 |. ^) Q$ V modelActions = new ActionGroupImpl (getZone ());- p8 W/ x" f* ~
4 o& |- n+ R1 ^2 n try {6 e. S& p5 M8 }
modelActions.createActionTo$message6 ~+ J7 k" K$ Q, J7 y! p
(heat, new Selector (heat.getClass (), "stepRule", false));
. r. J4 h6 Q$ H8 d } catch (Exception e) {2 e! _" U' b) I: t# c% j
System.err.println ("Exception stepRule: " + e.getMessage ());
4 D: |9 i: t* `1 O; ?) e }
4 e& ~, o9 Z5 |6 ^( P# x5 |3 ~2 m& G2 j B5 @6 Z; j. Y% _1 X; S
try {) A7 x; k5 v0 q3 X
Heatbug proto = (Heatbug) heatbugList.get (0);
% ^8 ^; H4 K, x* t- g& H' I, u Selector sel = : ?" ~ ]$ z% w, L
new Selector (proto.getClass (), "heatbugStep", false);
4 A/ e6 v/ s9 v actionForEach =$ H: [% \; t$ Q7 g* ^
modelActions.createFActionForEachHomogeneous$call
- o0 R, Y2 B+ P( O4 e (heatbugList,! c- T( N* k5 p+ F1 s) M
new FCallImpl (this, proto, sel,
6 m5 k V' ]: I* g6 _ new FArgumentsImpl (this, sel)));0 m: c& d: C4 A, d, T* M7 X
} catch (Exception e) {3 u1 a, g4 J% Y q+ D
e.printStackTrace (System.err);
G6 O- B. B. D }* [ }
# y' P- O: |3 n8 D4 ~$ M9 m ! K" g1 A3 E) g% Q2 t+ a
syncUpdateOrder ();* d+ V* K7 v5 k" ~) h
' k$ Y! R1 ~+ m try {/ r: w/ D# I" e- S
modelActions.createActionTo$message 7 U; _. A3 x3 {; W
(heat, new Selector (heat.getClass (), "updateLattice", false));+ b/ y2 v3 ~8 M5 `% I8 u4 T
} catch (Exception e) {& x% A4 h0 t R% U
System.err.println("Exception updateLattice: " + e.getMessage ());; k% r" @. k8 j s- L1 m s/ f' |
}
% d- R7 l) e3 J
% z5 a% P% N. _5 a. T // Then we create a schedule that executes the
: K9 `% E; r! s8 n // modelActions. modelActions is an ActionGroup, by itself it
# ?; @: `) T7 s4 j6 u8 } // has no notion of time. In order to have it executed in
/ W* x/ h* b5 p8 F/ J$ K! t // time, we create a Schedule that says to use the
* u3 y1 T. _8 H // modelActions ActionGroup at particular times. This
: D( w! d% a" H' r$ @ // schedule has a repeat interval of 1, it will loop every, B# h4 `) N9 d' A* B( O% G( ~
// time step. The action is executed at time 0 relative to
$ ?6 B0 m8 m9 v2 e // the beginning of the loop.
$ E) j5 M8 s: g5 n5 n+ W W! g ?& ]) G& l8 o
// This is a simple schedule, with only one action that is7 y* T9 Y2 L/ H9 I
// just repeated every time. See jmousetrap for more! ]' t5 {( H1 `. A; c
// complicated schedules.
3 e! n4 u; B" ?$ m4 q & j% k; v; Q( g/ R% J+ y
modelSchedule = new ScheduleImpl (getZone (), 1);- F. h5 d# }) k( D5 K) b$ Y" R8 E
modelSchedule.at$createAction (0, modelActions);2 @5 r' u0 M# L& W- g: Q5 `
' C. O* ?1 f1 j6 t2 [
return this;
5 Z6 y) X( d# ~& z8 r } |