HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 _0 ]8 a$ x8 k0 ], q# O
0 h2 K' s9 n1 D public Object buildActions () {
7 j" Z* L, P" u( O0 [+ _/ R super.buildActions(); N! Y. O9 @/ X8 w
4 t6 q r/ @* t9 c) S" q+ v
// Create the list of simulation actions. We put these in
. \0 |2 Q( I2 w. \+ C' W // an action group, because we want these actions to be
* e; W$ ] X) W+ E5 [ // executed in a specific order, but these steps should
0 C% E3 A! u2 O // take no (simulated) time. The M(foo) means "The message
9 R4 ]4 o6 o W; ~/ o0 j; G) ] // called <foo>". You can send a message To a particular
$ f- x k" J/ G/ r1 \ q% t1 N // object, or ForEach object in a collection.
# L0 z+ Z4 G8 O; g" W B 9 |& t; z$ b7 P
// Note we update the heatspace in two phases: first run
; \3 a+ Y5 K9 o$ _7 X' } // diffusion, then run "updateWorld" to actually enact the
$ b8 v. T! Y8 S, C // changes the heatbugs have made. The ordering here is6 w( }# O( L2 t N
// significant!
7 e1 a6 k1 w5 ~ ! E8 L. ~6 f+ V
// Note also, that with the additional
- T% s3 f" U& G6 z* P // `randomizeHeatbugUpdateOrder' Boolean flag we can
: v% q H! p% i s4 _, M2 u // randomize the order in which the bugs actually run8 P+ k( L0 ^' O) F0 p) c# I R& z( P
// their step rule. This has the effect of removing any
: L6 K0 R& U1 }" m/ W0 h // systematic bias in the iteration throught the heatbug
) c* p2 ^, N8 Q* r4 o o7 _ // list from timestep to timestep
% G( P3 }% |2 ]: T( o0 o$ G
* I# [+ W2 |( D! } // By default, all `createActionForEach' modelActions have
) u5 G9 l5 F K2 m( J% ^& T3 b // a default order of `Sequential', which means that the
* v$ ]7 w+ c& U% `8 h // order of iteration through the `heatbugList' will be) E9 O6 k7 ]8 S! F' Y% R: a6 H2 V) j$ }
// identical (assuming the list order is not changed
# i0 F- }( {2 f7 U: ] // indirectly by some other process).
% t* z$ j* y- k( p @- T 5 E, _$ a; w, V3 P
modelActions = new ActionGroupImpl (getZone ());
) U3 ~9 X6 M i# _9 n4 n* p! W2 c& V( c7 X
try {
( L! J# W+ o e3 _9 f( Z4 n modelActions.createActionTo$message1 ]" O8 ~0 u& H" m
(heat, new Selector (heat.getClass (), "stepRule", false));: B; e: z" \* O: V- K) V
} catch (Exception e) {" a2 W: v0 b. J
System.err.println ("Exception stepRule: " + e.getMessage ());# d; G0 h# \$ S
}
3 V1 t- L, x. X& R9 |$ L
: h' o) F+ {3 _. f5 F0 }; x! u try {2 w4 W) H- [) e. o& y
Heatbug proto = (Heatbug) heatbugList.get (0);% S! E$ t# m `- W; H
Selector sel = ; h% q2 z- i" r5 @8 d4 \: w
new Selector (proto.getClass (), "heatbugStep", false);
1 T9 c' y% C. t actionForEach =- A2 B; M2 T3 o1 N
modelActions.createFActionForEachHomogeneous$call
$ e2 z9 n1 Q2 k- W. B5 r/ M (heatbugList,
7 C8 Q; S: G) {% p6 @7 u @; w* S new FCallImpl (this, proto, sel,0 ]0 i7 S/ G/ @: o2 ~
new FArgumentsImpl (this, sel)));! F1 v$ ^1 r& k* {$ H9 l
} catch (Exception e) {2 w6 }6 }* a2 o! l
e.printStackTrace (System.err);
$ u7 q0 s) o) R) a7 j$ \( y9 e, p* U }
8 C8 [3 Z, P% s9 O7 \
8 K$ F$ J' K0 A+ O syncUpdateOrder ();- O# t4 s: o) R% ?8 h1 u# l! {
. E9 l& e; b& b% J( C" g d4 V
try {( H8 J6 @$ u' W
modelActions.createActionTo$message
& y- P; F' u( e/ L( m2 }6 K& t (heat, new Selector (heat.getClass (), "updateLattice", false));7 h# ?4 e& V6 h/ s4 ~: O
} catch (Exception e) {
( @& z. P2 X1 z/ A. ]9 D2 R System.err.println("Exception updateLattice: " + e.getMessage ());# }- C0 r# E8 s
}7 ?* v" x7 M; U' h$ O+ K8 K
* l! D4 D- h3 J- q: A( |. [ // Then we create a schedule that executes the- ~# q" x; w; M
// modelActions. modelActions is an ActionGroup, by itself it- v6 c: m4 I( Y4 _/ N
// has no notion of time. In order to have it executed in
8 ]0 l$ W$ C5 b) l/ {+ O! D // time, we create a Schedule that says to use the" T% z! s, o3 M4 L$ }8 I7 X( o6 y
// modelActions ActionGroup at particular times. This
! j7 h+ u% `9 ^; X5 } v // schedule has a repeat interval of 1, it will loop every p" r% B( D/ r5 _
// time step. The action is executed at time 0 relative to: \+ k9 F, r. L1 N
// the beginning of the loop.) s5 X7 g4 B6 S) _
5 F7 y7 J6 h" w. f# n' O
// This is a simple schedule, with only one action that is
: l1 ?. w- H: I // just repeated every time. See jmousetrap for more7 E9 O; S0 m: Z- m$ V. a4 N7 }, ^
// complicated schedules.. X; |1 c. h: F+ q, `
- ^" l9 v7 W8 E1 {5 E- U modelSchedule = new ScheduleImpl (getZone (), 1);0 w# c0 i4 ?: v; q' \
modelSchedule.at$createAction (0, modelActions);
. F. ]6 ?0 e/ P+ ^: \2 R5 y 6 C0 Y& \7 m9 q$ _" P* m! t9 b- E m
return this;0 e8 X. ~7 I9 B8 p6 z$ o
} |