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.module;
025    
026    import java.lang.reflect.Constructor;
027    import java.util.logging.Level;
028    
029    import org.jdmp.core.AbstractCoreObject;
030    import org.jdmp.core.algorithm.Algorithm;
031    import org.jdmp.core.dataset.DataSet;
032    import org.jdmp.core.sample.Sample;
033    import org.jdmp.core.util.DefaultObservableMap;
034    import org.jdmp.core.util.ObservableMap;
035    import org.jdmp.core.variable.Variable;
036    import org.ujmp.core.Matrix;
037    import org.ujmp.core.MatrixFactory;
038    import org.ujmp.core.interfaces.GUIObject;
039    
040    public abstract class AbstractModule extends AbstractCoreObject implements Module {
041            private static final long serialVersionUID = 4621466897617405575L;
042    
043            private transient GUIObject guiObject = null;
044    
045            protected final ObservableMap<Algorithm> algorithms = new DefaultObservableMap<Algorithm>();
046    
047            protected final ObservableMap<Sample> samples = new DefaultObservableMap<Sample>();
048    
049            protected final ObservableMap<DataSet> dataSets = new DefaultObservableMap<DataSet>();
050    
051            protected ObservableMap<Variable> variables = new DefaultObservableMap<Variable>();
052    
053            protected final ObservableMap<Module> modules = new DefaultObservableMap<Module>();
054    
055            public ObservableMap<Module> getModules() {
056                    return modules;
057            }
058    
059            public final ObservableMap<DataSet> getDataSets() {
060                    return dataSets;
061            }
062    
063            public final ObservableMap<Algorithm> getAlgorithms() {
064                    return algorithms;
065            }
066    
067            public final ObservableMap<Variable> getVariables() {
068                    return variables;
069            }
070    
071            public final ObservableMap<Sample> getSamples() {
072                    return samples;
073            }
074    
075            public void clear() {
076                    algorithms.clear();
077                    dataSets.clear();
078                    modules.clear();
079                    samples.clear();
080                    variables.clear();
081            }
082    
083            public final GUIObject getGUIObject() {
084                    if (guiObject == null) {
085                            try {
086                                    Class<?> c = Class.forName("org.jdmp.gui.module.ModuleGUIObject");
087                                    Constructor<?> con = c.getConstructor(new Class<?>[] { Module.class });
088                                    guiObject = (GUIObject) con.newInstance(new Object[] { this });
089                            } catch (Exception e) {
090                                    logger.log(Level.WARNING, "cannot create module gui object", e);
091                            }
092                    }
093                    return guiObject;
094            }
095    
096            @Override
097            public final String toString() {
098                    if (getLabel() == null) {
099                            return getClass().getSimpleName();
100                    } else {
101                            return getClass().getSimpleName() + " [" + getLabel() + "]";
102                    }
103            }
104    
105            public final void setObject(Object variableKey, Object value) {
106                    if (value == null) {
107                            setMatrix(variableKey, MatrixFactory.emptyMatrix());
108                    } else if (value instanceof Matrix) {
109                            setMatrix(variableKey, (Matrix) value);
110                    } else {
111                            setMatrix(variableKey, MatrixFactory.linkToValue(value));
112                    }
113            }
114    
115            public void notifyGUIObject() {
116                    if (guiObject != null) {
117                            guiObject.fireValueChanged();
118                    }
119            }
120    
121            public void setVariables(ObservableMap<Variable> variables) {
122                    this.variables = variables;
123            }
124    
125    }