001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.core.algorithm;
025
026 import java.lang.reflect.Constructor;
027 import java.util.ArrayList;
028 import java.util.Arrays;
029 import java.util.HashMap;
030 import java.util.LinkedList;
031 import java.util.List;
032 import java.util.Map;
033
034 import org.jdmp.core.AbstractCoreObject;
035 import org.jdmp.core.util.DefaultObservableMap;
036 import org.jdmp.core.util.ObservableMap;
037 import org.jdmp.core.variable.Variable;
038 import org.jdmp.core.variable.VariableFactory;
039 import org.ujmp.core.Matrix;
040 import org.ujmp.core.MatrixFactory;
041 import org.ujmp.core.exceptions.MatrixException;
042 import org.ujmp.core.interfaces.GUIObject;
043 import org.ujmp.core.util.MathUtil;
044 import org.ujmp.core.util.StringUtil;
045
046 public abstract class AbstractAlgorithm extends AbstractCoreObject implements Algorithm {
047 private static final long serialVersionUID = 3219035032582720106L;
048
049 private transient GUIObject guiObject = null;
050
051 private ObservableMap<Variable> variableMap = new DefaultObservableMap<Variable>();
052
053 private final ObservableMap<Algorithm> algorithmList = new DefaultObservableMap<Algorithm>();
054
055 private final Map<Object, String> edgeLabels = new HashMap<Object, String>();
056
057 private final Map<Object, EdgeDirection> edgeDirections = new HashMap<Object, EdgeDirection>();
058
059 private final List<Object> variableKeys = new ArrayList<Object>();
060
061 private String label = "";
062
063 private String description = "";
064
065 public final void setEdgeLabel(Object key, String edgeLabel) {
066 edgeLabels.put(key, edgeLabel);
067 }
068
069 public final void addVariableKey(Object key) {
070 variableKeys.add(key);
071 }
072
073 public void setVariables(Variable... variables) {
074 for (int i = 0; i < variables.length && i < getVariableKeys().size(); i++) {
075 variableMap.put(variableKeys.get(i), variables[i]);
076 }
077 }
078
079 public final List<Object> getVariableKeys() {
080 return variableKeys;
081 }
082
083 public final void setObject(Object variableKey, Object value) {
084 if (value == null) {
085 setMatrix(variableKey, MatrixFactory.emptyMatrix());
086 } else if (value instanceof Matrix) {
087 setMatrix(variableKey, (Matrix) value);
088 } else {
089 setMatrix(variableKey, MatrixFactory.linkToValue(value));
090 }
091 }
092
093 public final String getDescription() {
094 return description;
095 }
096
097 public final void setDescription(String description) {
098 this.description = description;
099 }
100
101 public String getLabel() {
102 return label;
103 }
104
105 public final void setLabel(String label) {
106 this.label = label;
107 }
108
109 public AbstractAlgorithm() {
110 super();
111 setLabel(getClass().getSimpleName());
112 }
113
114 public final void setEdgeDirection(Object key, EdgeDirection direction) {
115 edgeDirections.put(key, direction);
116 }
117
118 public void setVariable(Object index, Variable variable) {
119 variableMap.put(index, variable);
120 }
121
122 public final void setAlgorithm(Object index, Algorithm a) {
123 algorithmList.put(index, a);
124 }
125
126 public void clear() {
127 algorithmList.clear();
128 variableMap.clear();
129 }
130
131 public final Map<Object, Object> calculate() throws Exception {
132 Map<Object, Object> input = new HashMap<Object, Object>();
133
134 for (Object v : getInputKeys()) {
135 input.put(v, getMatrix(v));
136 }
137
138 Map<Object, Object> output = calculateObjects(input);
139
140 for (Object v : getOutputKeys()) {
141 setMatrix(v, MathUtil.getMatrix(output.get(v)));
142 }
143
144 return output;
145 }
146
147 public final List<Object> getInputKeys() {
148 List<Object> inputKeys = new ArrayList<Object>();
149 for (Object v : getVariableKeys()) {
150 if (EdgeDirection.Incoming.equals(getEdgeDirection(v))
151 || EdgeDirection.Bidirectional.equals(getEdgeDirection(v))) {
152 inputKeys.add(v);
153 }
154 }
155 return inputKeys;
156 }
157
158 public final List<Object> getOutputKeys() {
159 List<Object> outputKeys = new ArrayList<Object>();
160 for (Object v : getVariableKeys()) {
161 if (EdgeDirection.Outgoing.equals(getEdgeDirection(v))
162 || EdgeDirection.Bidirectional.equals(getEdgeDirection(v))) {
163 outputKeys.add(v);
164 }
165 }
166 return outputKeys;
167 }
168
169 public final Map<Object, Object> calculate(Matrix... input) throws Exception {
170 return calculate(Arrays.asList(input));
171 }
172
173 public final Map<Object, Object> calculateObjects(Object... input) throws Exception {
174 return calculateObjects(Arrays.asList(input));
175 }
176
177 public final Map<Object, Object> calculate(double... input) throws Exception {
178 List<Matrix> inputA = new LinkedList<Matrix>();
179 for (int i = 0; i < input.length; i++) {
180 inputA.add(MatrixFactory.linkToValue(input[i]));
181 }
182 return calculate(inputA);
183 }
184
185 public final Map<Object, Object> calculate(List<Matrix> matrices) throws Exception {
186 Map<Object, Object> map = new HashMap<Object, Object>();
187 List<Object> keys = getInputKeys();
188 for (int i = 0; i < matrices.size(); i++) {
189 Object key = keys.get(i);
190 map.put(key, matrices.get(i));
191 }
192 return calculateObjects(map);
193 }
194
195 public final Map<Object, Object> calculateObjects(List<Object> matrices) throws Exception {
196 Map<Object, Object> map = new HashMap<Object, Object>();
197 List<Object> keys = getInputKeys();
198 for (int i = 0; i < matrices.size(); i++) {
199 Object key = keys.get(i);
200 map.put(key, matrices.get(i));
201 }
202 return calculateObjects(map);
203 }
204
205 // this is not the best way
206 public Map<Object, Object> calculateObjects(Map<Object, Object> objects) throws Exception {
207 Map<Object, Object> result = new HashMap<Object, Object>();
208 return result;
209 }
210
211 public final EdgeDirection getEdgeDirection(Object key) {
212 return edgeDirections.get(key);
213 }
214
215 public final String getAsString(Object variableKey) {
216 return StringUtil.convert(getMatrix(variableKey));
217 }
218
219 public Matrix getMatrix(Object variableKey) {
220 Variable v = getVariables().get(variableKey);
221 if (v != null) {
222 return v.getMatrix();
223 } else {
224 return null;
225 }
226 }
227
228 public void setMatrix(Object variableKey, Matrix matrix) {
229 Variable v = getVariables().get(variableKey);
230 if (v == null) {
231 v = VariableFactory.labeledVariable(variableKey.toString());
232 getVariables().put(variableKey, v);
233 }
234 v.addMatrix(matrix);
235 }
236
237 public final ObservableMap<Variable> getVariables() {
238 return variableMap;
239 }
240
241 public final ObservableMap<Algorithm> getAlgorithms() {
242 return algorithmList;
243 }
244
245 public final String getEdgeLabel(Object key) {
246 return edgeLabels.get(key);
247 }
248
249 public final GUIObject getGUIObject() {
250 if (guiObject == null) {
251 try {
252 Class<?> c = Class.forName("org.jdmp.gui.algorithm.AlgorithmGUIObject");
253 Constructor<?> con = c.getConstructor(new Class<?>[] { Algorithm.class });
254 guiObject = (GUIObject) con.newInstance(new Object[] { this });
255 } catch (Exception e) {
256 throw new MatrixException("cannot create sample gui object", e);
257 }
258 }
259 return guiObject;
260 }
261
262 @Override
263 public final String toString() {
264 if (getLabel() == null) {
265 return getClass().getSimpleName();
266 } else {
267 return getClass().getSimpleName() + " [" + getLabel() + "]";
268 }
269 }
270
271 public void notifyGUIObject() {
272 guiObject.fireValueChanged();
273 }
274
275 public void setVariables(ObservableMap<Variable> variables) {
276 this.variableMap = variables;
277 }
278
279 }