HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 O4 f% ^' z' ?# K( u) k! h' F
public Object buildActions () {
5 o/ n7 Y0 Q' O% @9 g1 F/ z3 J super.buildActions();+ p; V" R* f+ B1 q
4 t" ?* i8 l8 w: w# J // Create the list of simulation actions. We put these in1 O8 l0 o3 W9 y3 R9 A
// an action group, because we want these actions to be4 a& N2 k* U; h- M, {) C
// executed in a specific order, but these steps should8 X* D# C& E& `0 M2 \- `8 y: W6 q Y
// take no (simulated) time. The M(foo) means "The message
' l2 l) ~$ R0 D, j // called <foo>". You can send a message To a particular
3 x% H4 V6 N9 D! ~3 | // object, or ForEach object in a collection.
( M9 n2 a$ l; Q7 Y * u6 K& m/ o O' H! z& m# U) Q
// Note we update the heatspace in two phases: first run
: ` X) ~% n- b# Q- n | // diffusion, then run "updateWorld" to actually enact the" C9 s) N% Z/ H$ @
// changes the heatbugs have made. The ordering here is
, r7 T/ j2 B& y( u: m; w' o // significant!$ ~6 q9 G7 C/ C4 E0 p
, \+ r; Q' t( C( R
// Note also, that with the additional
3 ~5 u; n8 f: C t9 E+ n2 c( Q // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ j! n4 Q* o8 C7 ?! u3 Z5 L6 ~ // randomize the order in which the bugs actually run
& H4 e ~0 U Z; d( y // their step rule. This has the effect of removing any ~- ?3 {% l& O- G
// systematic bias in the iteration throught the heatbug
% t% w: x: Q3 B) ?: m' c // list from timestep to timestep$ ^% A3 w5 [' E$ F* A9 O8 y' k
X, ~& _! ~/ W5 ^8 S7 H% h // By default, all `createActionForEach' modelActions have
) z: r7 G2 k4 L // a default order of `Sequential', which means that the4 Y% Z- y. E1 w) B
// order of iteration through the `heatbugList' will be9 a5 e5 Z5 v+ N& t" Z0 B: A8 y
// identical (assuming the list order is not changed
- R6 v! {' }% M( f7 z8 V+ w2 q // indirectly by some other process).
: W) R1 W# }3 D4 x( ?
( Z0 z7 ?. k) W) j6 Y9 s4 v, s modelActions = new ActionGroupImpl (getZone ());5 V. ]0 o2 X( D+ J# X% K+ L
# A3 _6 `7 s$ U$ I' |; F try {
8 Q5 B0 J( Q% A1 z4 z' a modelActions.createActionTo$message
& a, F3 g. ]4 J; o0 r+ D: V* Z _ (heat, new Selector (heat.getClass (), "stepRule", false));
& X ~# T. S" Z& [# L- q* H } catch (Exception e) {6 _7 Q+ d+ v- i8 D# p( K% ^* B
System.err.println ("Exception stepRule: " + e.getMessage ());
. |0 C" j: h" c/ F9 o" x$ Q& M }
5 o4 g( E) I4 Q. c4 J3 E, Z$ F1 n
try {
9 f+ Y' a: C8 q' C I2 I Heatbug proto = (Heatbug) heatbugList.get (0);
+ k% Y) c, g% A H9 g$ g2 S8 T' Y Selector sel = , n$ l% ]; y1 \) q
new Selector (proto.getClass (), "heatbugStep", false);
* H4 H! D" k, T) B& `! s actionForEach =: E7 T4 _2 L X( |
modelActions.createFActionForEachHomogeneous$call
' A; f8 F0 f" @" |* a) ?9 d d0 v7 @ (heatbugList,5 h5 K" b }2 t5 T( }, o
new FCallImpl (this, proto, sel,
) t$ U2 }) m& F% ?# @- w- H# a' _ new FArgumentsImpl (this, sel)));9 s' x* ^& U) S4 D9 D) r
} catch (Exception e) {
" D% a. @3 ` u" z e.printStackTrace (System.err);6 s& G( u( s, ]" H4 M4 p: {
}
( U, k( H7 b# Y) ?' v+ }" x F! \) d9 K% Q* y- v/ r
syncUpdateOrder ();
8 ~" M! L, H, N# S
% n' L9 `4 }; l& J try {
9 F2 ]" H, v" z' ?8 i9 \ modelActions.createActionTo$message
7 ]2 h; O. |+ ~4 A8 x4 e (heat, new Selector (heat.getClass (), "updateLattice", false));
+ R! u- y/ e6 H9 k } catch (Exception e) {
* G7 R; I/ ?! K( J* _ System.err.println("Exception updateLattice: " + e.getMessage ());" |% q$ X _8 S1 t* i
}6 w& n9 P% i% T6 e- _ L8 u
/ j* R9 ?) D; e7 X# @: c // Then we create a schedule that executes the
) X4 k+ V3 t: A* L) z // modelActions. modelActions is an ActionGroup, by itself it
9 [# h& W8 r' y. ?4 T/ J& L // has no notion of time. In order to have it executed in
( W/ I) l4 q% V // time, we create a Schedule that says to use the% A/ t# A1 L' H/ T
// modelActions ActionGroup at particular times. This
* c& ]$ _+ N1 ?8 W+ s9 S3 H- q // schedule has a repeat interval of 1, it will loop every
2 ~) K7 [4 |2 h' B9 B // time step. The action is executed at time 0 relative to, G1 k+ S* ^( W: b% \
// the beginning of the loop.2 T# J4 M2 b/ @4 F
5 x( b! t5 t$ z* v) h
// This is a simple schedule, with only one action that is* k* ^8 A) @& A2 z4 [
// just repeated every time. See jmousetrap for more
) K o p3 i2 u7 J0 q* x1 r) | // complicated schedules.
$ A& I6 M4 `0 G: w! o! p
# T* ]- ]$ B* I6 B modelSchedule = new ScheduleImpl (getZone (), 1);, {2 _; Q; g) q( k) D; \" s' P
modelSchedule.at$createAction (0, modelActions);1 t/ b. O3 x0 e" y. o% a1 t
# ]7 q9 Q, u) }0 U& H return this;
& k8 H8 h1 P( g: I) [- b } |