001: package JSci.beans;
002:
003: import java.awt.event.*;
004: import java.beans.*;
005: import java.util.*;
006: import JSci.maths.*;
007: import JSci.maths.matrices.AbstractComplexMatrix;
008: import JSci.maths.matrices.AbstractDoubleMatrix;
009: import JSci.maths.matrices.AbstractIntegerMatrix;
010: import JSci.maths.vectors.AbstractComplexVector;
011: import JSci.maths.vectors.AbstractDoubleVector;
012: import JSci.maths.vectors.AbstractIntegerVector;
013: import JSci.io.*;
014: import JSci.util.*;
015:
016: public final class MathBean extends Object implements
017: java.io.Serializable, VariableListener, ActionListener {
018: private PropertyChangeSupport changes = new PropertyChangeSupport(
019: this );
020: private MathMLExpression expr;
021: private String mathml = "";
022: private Hashtable variables = new Hashtable();
023: private Object result = new MathDouble(Double.NaN);
024:
025: public MathBean() {
026: }
027:
028: public synchronized void setMathML(String uri) {
029: try {
030: MathMLParser parser = new MathMLParser();
031: parser.parse(uri);
032: expr = (MathMLExpression) (parser.translateToJSciObjects()[0]);
033: } catch (Exception e) {
034: }
035: String oldUri = mathml;
036: mathml = uri;
037: changes.firePropertyChange("mathml", oldUri, uri);
038: }
039:
040: public synchronized String getMathML() {
041: return mathml;
042: }
043:
044: public synchronized double getResultAs0DArray() {
045: if (result instanceof MathDouble)
046: return ((MathDouble) result).value();
047: else if (result instanceof MathInteger)
048: return ((MathInteger) result).value();
049: else
050: return Double.NaN;
051: }
052:
053: public synchronized double[] getResultAs1DArray() {
054: if (result instanceof Complex) {
055: double array[] = { ((Complex) result).real(),
056: ((Complex) result).imag() };
057: return array;
058: } else if (result instanceof AbstractDoubleVector) {
059: return VectorToolkit.toArray((AbstractDoubleVector) result);
060: } else if (result instanceof AbstractIntegerVector) {
061: return VectorToolkit
062: .toArray(((AbstractIntegerVector) result)
063: .toDoubleVector());
064: } else
065: return null;
066: }
067:
068: public synchronized double[][] getResultAs2DArray() {
069: if (result instanceof AbstractComplexVector) {
070: double array[][] = new double[2][];
071: array[0] = VectorToolkit
072: .toArray(((AbstractComplexVector) result).real());
073: array[1] = VectorToolkit
074: .toArray(((AbstractComplexVector) result).imag());
075: return array;
076: } else if (result instanceof AbstractDoubleMatrix)
077: return MatrixToolkit.toArray((AbstractDoubleMatrix) result);
078: else if (result instanceof AbstractIntegerMatrix)
079: return MatrixToolkit
080: .toArray(((AbstractIntegerMatrix) result)
081: .toDoubleMatrix());
082: else
083: return null;
084: }
085:
086: public synchronized double[][][] getResultAs3DArray() {
087: if (result instanceof AbstractComplexMatrix) {
088: double array[][][] = new double[2][][];
089: array[0] = MatrixToolkit
090: .toArray(((AbstractComplexMatrix) result).real());
091: array[1] = MatrixToolkit
092: .toArray(((AbstractComplexMatrix) result).imag());
093: return array;
094: } else
095: return null;
096: }
097:
098: public void variableChanged(VariableEvent evt) {
099: variables.put(evt.getVariable(), evt.getValue());
100: }
101:
102: public void actionPerformed(ActionEvent evt) {
103: MathMLExpression evalExp = expr;
104: Enumeration vars = variables.keys();
105: while (vars.hasMoreElements()) {
106: Object var = vars.nextElement();
107: evalExp = evalExp.substitute(var.toString(), variables
108: .get(var));
109: }
110: result = evalExp.evaluate();
111: changes.firePropertyChange("resultAs0DArray", null, new Double(
112: getResultAs0DArray()));
113: changes.firePropertyChange("resultAs1DArray", null,
114: getResultAs1DArray());
115: changes.firePropertyChange("resultAs2DArray", null,
116: getResultAs2DArray());
117: changes.firePropertyChange("resultAs3DArray", null,
118: getResultAs3DArray());
119: }
120:
121: public synchronized void addPropertyChangeListener(
122: PropertyChangeListener l) {
123: changes.addPropertyChangeListener(l);
124: }
125:
126: public synchronized void removePropertyChangeListener(
127: PropertyChangeListener l) {
128: changes.removePropertyChangeListener(l);
129: }
130: }
|