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

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/samer/maths/VGenerator.java	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,78 @@
+/*
+ *	Copyright (c) 2000, Samer Abdallah, King's College London.
+ *	All rights reserved.
+ *
+ *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
+ *	without even the implied warranty of MERCHANTABILITY or 
+ *	FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+package	samer.maths;
+import	samer.core.*;
+import	samer.core.types.*;
+import	samer.core.util.*;
+import	java.awt.*;
+import	java.util.*;
+
+/**
+	A Viewable random number generator.
+	The Generator itself must expose any parameters
+	it has, but this object allows the user to
+	control what class of generator to use. 
+  */
+
+public class VGenerator extends Viewable implements Agent, Generator
+{
+	Generator	gen;
+	String		desc;		// description of the current generator
+
+	public VGenerator(String nm) throws Exception { this(nm,new Zero()); }
+	public VGenerator(String nm, Generator g) throws Exception {
+		super(nm); setAgent(this);	gen = g;
+		Shell.registerViewable(this);
+	}
+	
+	public double next() { return gen.next(); }
+	public void   next(double[] x) { gen.next(x); }
+
+	public void dispose() 
+	{
+		Shell.deregisterViewable(this);
+		gen.dispose();
+		super.dispose();
+	}
+
+	public Generator getGenerator() { return gen; }
+	public void setGenerator(Generator g) {
+		gen.dispose(); gen=g; changed();
+	}
+
+	public Viewer getViewer() { return new UI(); }
+
+	private class UI extends BaseViewer {
+		UI() {
+			super(VGenerator.this);
+			update(VGenerator.this,null);
+		}
+
+		public void update(Observable o, Object arg) {
+			super.update(o,arg);
+			Shell.trace("VGenerator update to "+gen.toString());
+			setText(getLabel()+": "+gen.toString());
+		}
+	}
+
+	public void getCommands(Registry r)	{ r.add("set").add("next"); }	
+
+	public void execute(String cmd, Environment env) throws Exception
+	{
+		if (cmd.equals("set")) {
+			Shell.push(getNode());
+			setGenerator((Generator)X.object(env.datum()));
+			Shell.pop();
+		} else if (cmd.equals("next")) {
+			env.add(new Double(gen.next()));
+		}
+	}
+}
+