HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; @3 A1 N0 @$ |$ {, ]. w: C# z* F. ]7 z+ h6 b
public Object buildActions () {0 {7 ?8 C2 P# [1 G8 C3 t
super.buildActions();
g5 a) p' V; j! l5 x% B1 v. p 5 U, e& a3 @8 L% I- u/ [1 n
// Create the list of simulation actions. We put these in
0 e: r: r8 s$ b2 ^6 V9 b // an action group, because we want these actions to be" J u/ s0 c: A* C+ L0 y; g$ B
// executed in a specific order, but these steps should r" ^$ W0 f- m% r2 }
// take no (simulated) time. The M(foo) means "The message
! S1 v( M0 x1 w9 ~, o& Y // called <foo>". You can send a message To a particular6 `+ w; {2 n7 x% `+ C7 p; t, w, Y
// object, or ForEach object in a collection.
# \% u6 c1 i9 k) a* e# A
D( I" M# F/ g' k+ u7 ~5 g // Note we update the heatspace in two phases: first run4 p" [0 N7 u* p: g0 Q' D1 t b: q
// diffusion, then run "updateWorld" to actually enact the& ~, {$ u4 L ^! u: V' i) G7 D
// changes the heatbugs have made. The ordering here is
" ]+ M% n" @$ J( J2 a // significant!
4 L) ] P7 m8 y) C# D 6 l e) P3 [8 |/ h$ }0 |
// Note also, that with the additional$ l8 U3 b: r) l T, I7 d4 g& Z; V! p
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# q% k8 _1 t! ?; z4 N" z8 C2 k // randomize the order in which the bugs actually run
9 P4 t( M; U( ^, o' f1 y6 P // their step rule. This has the effect of removing any
/ U" Z0 N H. c% I // systematic bias in the iteration throught the heatbug
8 A: J8 F d4 s4 k; M, k4 y // list from timestep to timestep
0 R* _' @! Y7 w: O
~$ I& J8 o8 J+ p // By default, all `createActionForEach' modelActions have# ~( U% z/ D8 [
// a default order of `Sequential', which means that the
/ T/ X: C0 G; e8 b4 i3 x7 l // order of iteration through the `heatbugList' will be3 c. ^4 V) {$ p T: U* t9 }5 n
// identical (assuming the list order is not changed# G) O7 Z7 z2 v3 u, W; ]
// indirectly by some other process).
2 q6 ]6 b5 U$ j$ f7 L1 y: y / j, m" s7 }8 ]0 e
modelActions = new ActionGroupImpl (getZone ());
g$ `% k2 Y0 u& M9 Z4 k3 r) U
& e, S- ~- C; z) a try {
+ X+ P) X) I* v6 ?/ f. C modelActions.createActionTo$message
3 d! M# E; H1 E# r+ ` (heat, new Selector (heat.getClass (), "stepRule", false));
# Q' W! Z" o3 F* S } catch (Exception e) {
4 E: I# O. J. r0 r+ q2 H7 ~% m: m System.err.println ("Exception stepRule: " + e.getMessage ());
0 z; S \3 _% U4 D4 M# N }- M. J6 y% I! e; R Q# ?3 O+ @
/ F e7 w8 s% K+ m* @ try {! Y' I5 x% O) O
Heatbug proto = (Heatbug) heatbugList.get (0);/ F8 _# h" X: v5 t" l0 d% ~5 g
Selector sel =
7 [* n' G+ Q" y( z new Selector (proto.getClass (), "heatbugStep", false);# n6 G& C. }/ @7 @1 M
actionForEach =3 [% L/ g J* |& i# C0 R1 S& p3 l
modelActions.createFActionForEachHomogeneous$call
# @) M! D1 x$ U (heatbugList,. c5 r8 A; B: i" W) t
new FCallImpl (this, proto, sel,( Q! v6 y8 f7 y4 b
new FArgumentsImpl (this, sel)));
0 C' v! t1 [* K" w } catch (Exception e) {
0 s8 |6 J: M' M e.printStackTrace (System.err);
5 g3 l& X A% f% _ }
9 ^8 r' h9 X! R7 `" K% O " o+ [; X# Y2 I" {1 j
syncUpdateOrder ();! i8 U% U- L' m9 z4 X/ W
9 d1 p! |! C8 _+ @; s/ d
try {3 D4 _9 O, V* t. r/ y1 x' j5 n/ o2 B7 t
modelActions.createActionTo$message 8 d2 B& ^8 R4 _. }
(heat, new Selector (heat.getClass (), "updateLattice", false));9 Z; Z: O4 @2 s3 r
} catch (Exception e) {: _5 R1 C+ l; P- h
System.err.println("Exception updateLattice: " + e.getMessage ());" T+ `' o1 ^5 f7 @( T
}
4 n, c5 T' D S# M" N2 l& h) X5 `$ | 5 g1 ]) w" X% d3 U: S
// Then we create a schedule that executes the
4 A) S& s3 P7 D4 c+ B% s8 _ // modelActions. modelActions is an ActionGroup, by itself it0 }8 f/ |& x# l) F( q% Y$ Y9 S
// has no notion of time. In order to have it executed in
7 {) S1 h- e1 {+ I9 F // time, we create a Schedule that says to use the, K- I/ e0 v( \3 G' b8 \
// modelActions ActionGroup at particular times. This, \/ H! z w: R( [& h0 D/ J i1 x' v
// schedule has a repeat interval of 1, it will loop every1 ]2 O2 n" E! Q& V* V o+ R* w! \
// time step. The action is executed at time 0 relative to4 n$ X/ z5 a, k# `0 \; _: }7 S5 o
// the beginning of the loop.
* S5 A* n6 _+ y) z) g1 T8 U ~" I2 ]* k
// This is a simple schedule, with only one action that is! k9 W5 e6 G/ _! u
// just repeated every time. See jmousetrap for more
- l* p" P. q2 ]6 A; ] // complicated schedules.! C9 Q( ]: C$ y2 y& o- L
* e# a6 `' C, C; L, I- _) j modelSchedule = new ScheduleImpl (getZone (), 1);
1 N8 S$ I& n, f P% F1 Y5 d modelSchedule.at$createAction (0, modelActions);
8 P7 [. n Y- ?* g2 I' R
( I' {5 P# E, ?5 X0 s+ _ return this; |; I# `* f% @4 u: B7 t% Q! o' K
} |