1 package JSci.beans; 2 3 import java.awt.event.*; 4 import java.beans.*; 5 import java.util.*; 6 import JSci.maths.*; 7 import JSci.maths.matrices.AbstractComplexMatrix; 8 import JSci.maths.matrices.AbstractDoubleMatrix; 9 import JSci.maths.matrices.AbstractIntegerMatrix; 10 import JSci.maths.vectors.AbstractComplexVector; 11 import JSci.maths.vectors.AbstractDoubleVector; 12 import JSci.maths.vectors.AbstractIntegerVector; 13 import JSci.io.*; 14 import JSci.util.*; 15 16 public final class MathBean extends Object implements java.io.Serializable , 17 VariableListener, ActionListener { 18 private PropertyChangeSupport changes=new PropertyChangeSupport(this); 19 private MathMLExpression expr; 20 private String mathml=""; 21 private Hashtable variables=new Hashtable(); 22 private Object result=new MathDouble(Double.NaN); 23 24 public MathBean() {} 25 public synchronized void setMathML(String uri) { 26 try { 27 MathMLParser parser=new MathMLParser(); 28 parser.parse(uri); 29 expr=(MathMLExpression)(parser.translateToJSciObjects()[0]); 30 } catch(Exception e) {} 31 String oldUri=mathml; 32 mathml=uri; 33 changes.firePropertyChange("mathml",oldUri,uri); 34 } 35 public synchronized String getMathML() { 36 return mathml; 37 } 38 public synchronized double getResultAs0DArray() { 39 if(result instanceof MathDouble) 40 return ((MathDouble)result).value(); 41 else if(result instanceof MathInteger) 42 return ((MathInteger)result).value(); 43 else 44 return Double.NaN; 45 } 46 public synchronized double[] getResultAs1DArray() { 47 if(result instanceof Complex) { 48 double array[]={((Complex)result).real(),((Complex)result).imag()}; 49 return array; 50 } else if(result instanceof AbstractDoubleVector) { 51 return VectorToolkit.toArray((AbstractDoubleVector)result); 52 } else if(result instanceof AbstractIntegerVector) { 53 return VectorToolkit.toArray(((AbstractIntegerVector)result).toDoubleVector()); 54 } else 55 return null; 56 } 57 public synchronized double[][] getResultAs2DArray() { 58 if(result instanceof AbstractComplexVector) { 59 double array[][]=new double[2][]; 60 array[0]=VectorToolkit.toArray(((AbstractComplexVector)result).real()); 61 array[1]=VectorToolkit.toArray(((AbstractComplexVector)result).imag()); 62 return array; 63 } else if(result instanceof AbstractDoubleMatrix) 64 return MatrixToolkit.toArray((AbstractDoubleMatrix)result); 65 else if(result instanceof AbstractIntegerMatrix) 66 return MatrixToolkit.toArray(((AbstractIntegerMatrix)result).toDoubleMatrix()); 67 else 68 return null; 69 } 70 public synchronized double[][][] getResultAs3DArray() { 71 if(result instanceof AbstractComplexMatrix) { 72 double array[][][]=new double[2][][]; 73 array[0]=MatrixToolkit.toArray(((AbstractComplexMatrix)result).real()); 74 array[1]=MatrixToolkit.toArray(((AbstractComplexMatrix)result).imag()); 75 return array; 76 } else 77 return null; 78 } 79 public void variableChanged(VariableEvent evt) { 80 variables.put(evt.getVariable(),evt.getValue()); 81 } 82 public void actionPerformed(ActionEvent evt) { 83 MathMLExpression evalExp=expr; 84 Enumeration vars=variables.keys(); 85 while(vars.hasMoreElements()) { 86 Object var=vars.nextElement(); 87 evalExp=evalExp.substitute(var.toString(),variables.get(var)); 88 } 89 result=evalExp.evaluate(); 90 changes.firePropertyChange("resultAs0DArray",null,new Double (getResultAs0DArray())); 91 changes.firePropertyChange("resultAs1DArray",null,getResultAs1DArray()); 92 changes.firePropertyChange("resultAs2DArray",null,getResultAs2DArray()); 93 changes.firePropertyChange("resultAs3DArray",null,getResultAs3DArray()); 94 } 95 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 96 changes.addPropertyChangeListener(l); 97 } 98 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 99 changes.removePropertyChangeListener(l); 100 } 101 } 102 103 | Popular Tags |