001: package org.jmatlab.semantic;
002:
003: import java.io.Serializable;
004: import java.util.List;
005: import java.util.Vector;
006:
007: import org.jmatlab.linalg.DefaultComplexImpl;
008: import org.jmatlab.linalg.DefaultMatrixImpl;
009: import org.jmatlab.linalg.ICell;
010: import org.jmatlab.linalg.IComplex;
011: import org.jmatlab.linalg.IMatrix;
012: import org.jmatlab.linalg.IStruct;
013: import org.jmatlab.util.FormatNumber;
014:
015: /**
016: * User: Ali
017: * Date: Jun 18, 2003
018: * Time: 9:36:03 AM
019: */
020: public class Symbol implements Serializable {
021: //TODO: make it fail safe by checking type before get
022: private int type = DataType.INVALID;
023: private String name;
024: private Object value;
025:
026: public Symbol(String name) {
027: this .name = name;
028: this .type = DataType.UNDEFINED;
029: }
030:
031: public Symbol(Symbol symbol) {
032: this .name = symbol.getName();
033: this .type = symbol.getType();
034: if (type == DataType.MATRIX) {
035: this .value = new DefaultMatrixImpl(symbol.getMatrix());
036: } else {
037: this .value = symbol.getValue();
038: }
039: }
040:
041: public Symbol(String name, Symbol symbol) {
042: this .name = name;
043: this .type = symbol.getType();
044: this .value = symbol.getValue();
045: }
046:
047: private Object getValue() {
048: return this .value;
049: }
050:
051: public Symbol(String name, Object value) {
052: this .name = name;
053: setValue(value);
054: }
055:
056: public void setType(int type) {
057: this .type = type;
058: }
059:
060: public void setCorrectType() {
061: if (value instanceof Double) {
062: this .type = DataType.DOUBLE;
063: } else if (value instanceof String) {
064: this .type = DataType.STRING;
065: } else if (value instanceof IMatrix) {
066: this .type = DataType.MATRIX;
067: } else if (value instanceof Boolean) {
068: this .type = DataType.BOOLEAN;
069: } else if (value instanceof IComplex) {
070: this .type = DataType.COMPLEX;
071: } else if (value instanceof IStruct) {
072: this .type = DataType.STRUCT;
073: } else if (value instanceof ICell) {
074: this .type = DataType.CELL;
075: } else if (value instanceof Vector) {
076: this .type = DataType.VECTOR;
077: } else if (value instanceof List) {
078: this .type = DataType.LIST;
079: }
080: }
081:
082: public void setValue(Object value) {
083: this .value = value;
084: setCorrectType();
085: }
086:
087: public List getList() {
088: if (type == DataType.LIST) {
089: return (List) value;
090: } else {
091: return null;
092: }
093: }
094:
095: public Boolean getBoolean() {
096: if (type == DataType.BOOLEAN) {
097: return (Boolean) value;
098: } else {
099: return null;
100: }
101: }
102:
103: public IMatrix getMatrix() {
104: if (type == DataType.MATRIX) {
105: return (IMatrix) value;
106: } else {
107: return null;
108: }
109: }
110:
111: public IStruct getStruct() {
112: if (type == DataType.STRUCT) {
113: return (IStruct) value;
114: } else {
115: return null;
116: }
117: }
118:
119: public ICell getCell() {
120: if (type == DataType.CELL) {
121: return (ICell) value;
122: } else {
123: return null;
124: }
125: }
126:
127: public IComplex getComplex() {
128: if (type == DataType.DOUBLE) {
129: IComplex c = new DefaultComplexImpl(((Double) value)
130: .doubleValue(), 0);
131: return c;
132: }
133: if (type == DataType.COMPLEX) {
134: return (IComplex) value;
135: } else {
136: return null;
137: }
138: }
139:
140: public Double getDouble() {
141: if (type == DataType.DOUBLE) {
142: return (Double) value;
143: } else {
144: return null;
145: }
146: }
147:
148: public int getType() {
149: return type;
150: }
151:
152: public String getString() {
153: if (type == DataType.STRING) {
154: return (String) value;
155: } else {
156: return null;
157: }
158: }
159:
160: public String getName() {
161: return name;
162: }
163:
164: public void setName(String name) {
165: this .name = name;
166: }
167:
168: public String toString() {
169: String output = null;
170: if (type == DataType.MATRIX) {
171: output = getMatrix().toString();
172: } else if (type == DataType.COMPLEX) {
173: output = getComplex().toString();
174: } else if (type == DataType.DOUBLE) {
175: double d = getDouble().doubleValue();
176: if (Double.isInfinite(d)) {
177: output = "Inf";
178: } else if (Double.isInfinite(-d)) {
179: output = "-Inf";
180: } else if (Double.isNaN(d)) {
181: output = "NaN";
182: } else {
183: output = FormatNumber.format(d);
184: }
185: } else if (type == DataType.BOOLEAN) {
186: output = "" + getBoolean().booleanValue();
187: } else if (type == DataType.STRING) {
188: output = getString();
189: } else if (type == DataType.STRUCT) {
190: output = getStruct().toString();
191: } else if (type == DataType.CELL) {
192: output = getCell().toString();
193: } else {
194: throw new SemanticException("Unknow datatype to print.");
195: }
196: return output;
197: }
198:
199: public static void main(String[] args) {
200: Symbol s = new Symbol("x", new Double(1.0));
201: Symbol s1 = new Symbol("y", s);
202: }
203:
204: public Vector getVector() {
205: if (type == DataType.VECTOR) {
206: return (Vector) value;
207: } else {
208: return null;
209: }
210: }
211: }
|