01: package org.cougaar.demo.mandelbrot.v3;
02:
03: import java.util.Iterator;
04: import org.cougaar.core.blackboard.IncrementalSubscription;
05: import org.cougaar.core.plugin.ComponentPlugin;
06: import org.cougaar.demo.mandelbrot.util.Arguments;
07: import org.cougaar.util.FutureResult;
08: import org.cougaar.util.UnaryPredicate;
09:
10: /**
11: * This plugin is a base class that listens for {@link Job} blackboard objects
12: * and calculates the result.
13: */
14: public abstract class CalculatorBase extends ComponentPlugin {
15:
16: private static final UnaryPredicate JOB_PRED = new UnaryPredicate() {
17: public boolean execute(Object o) {
18: return (o instanceof Job);
19: }
20: };
21:
22: private IncrementalSubscription sub;
23:
24: protected void setupSubscriptions() {
25: sub = (IncrementalSubscription) blackboard.subscribe(JOB_PRED);
26: }
27:
28: protected void execute() {
29: if (!sub.hasChanged())
30: return;
31: for (Iterator iter = sub.getAddedCollection().iterator(); iter
32: .hasNext();) {
33: Job job = (Job) iter.next();
34: Arguments args = job.getArguments();
35: FutureResult future = job.getFutureResult();
36:
37: try {
38: byte[] data = calculate(args.getInt("width"), args
39: .getInt("height"), args.getDouble("x_min"),
40: args.getDouble("x_max"), args
41: .getDouble("y_min"), args
42: .getDouble("y_max"));
43: future.set(data);
44: } catch (Exception e) {
45: future.setException(e);
46: }
47: }
48: }
49:
50: /** Implement this method. */
51: protected abstract byte[] calculate(int width, int height,
52: double min_x, double max_x, double min_y, double max_y);
53: }
|