comparison src/samer/maths/VGenerator.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
3 * All rights reserved.
4 *
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
6 * without even the implied warranty of MERCHANTABILITY or
7 * FITNESS FOR A PARTICULAR PURPOSE.
8 */
9
10 package samer.maths;
11 import samer.core.*;
12 import samer.core.types.*;
13 import samer.core.util.*;
14 import java.awt.*;
15 import java.util.*;
16
17 /**
18 A Viewable random number generator.
19 The Generator itself must expose any parameters
20 it has, but this object allows the user to
21 control what class of generator to use.
22 */
23
24 public class VGenerator extends Viewable implements Agent, Generator
25 {
26 Generator gen;
27 String desc; // description of the current generator
28
29 public VGenerator(String nm) throws Exception { this(nm,new Zero()); }
30 public VGenerator(String nm, Generator g) throws Exception {
31 super(nm); setAgent(this); gen = g;
32 Shell.registerViewable(this);
33 }
34
35 public double next() { return gen.next(); }
36 public void next(double[] x) { gen.next(x); }
37
38 public void dispose()
39 {
40 Shell.deregisterViewable(this);
41 gen.dispose();
42 super.dispose();
43 }
44
45 public Generator getGenerator() { return gen; }
46 public void setGenerator(Generator g) {
47 gen.dispose(); gen=g; changed();
48 }
49
50 public Viewer getViewer() { return new UI(); }
51
52 private class UI extends BaseViewer {
53 UI() {
54 super(VGenerator.this);
55 update(VGenerator.this,null);
56 }
57
58 public void update(Observable o, Object arg) {
59 super.update(o,arg);
60 Shell.trace("VGenerator update to "+gen.toString());
61 setText(getLabel()+": "+gen.toString());
62 }
63 }
64
65 public void getCommands(Registry r) { r.add("set").add("next"); }
66
67 public void execute(String cmd, Environment env) throws Exception
68 {
69 if (cmd.equals("set")) {
70 Shell.push(getNode());
71 setGenerator((Generator)X.object(env.datum()));
72 Shell.pop();
73 } else if (cmd.equals("next")) {
74 env.add(new Double(gen.next()));
75 }
76 }
77 }
78