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.sample;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.util.Collection;
029    import java.util.Map;
030    
031    import org.ujmp.core.Matrix;
032    import org.ujmp.core.MatrixFactory;
033    import org.ujmp.core.enums.FileFormat;
034    import org.ujmp.core.exceptions.MatrixException;
035    import org.ujmp.core.interfaces.HasDataMap;
036    import org.ujmp.core.util.MathUtil;
037    
038    public abstract class SampleFactory {
039    
040            public static Sample EMPTYSAMPLE = new DefaultSample();
041    
042            public static final Sample createFromObject(Object o) {
043                    if (o instanceof Map) {
044                            return linkToMap((Map) o);
045                    } else if (o instanceof HasDataMap) {
046                            return linkToMap(((HasDataMap) o).getDataMap());
047                    } else if (o instanceof Matrix) {
048                            return linkToMatrix((Matrix) o);
049                    } else {
050                            return linkToMatrix(MathUtil.getMatrix(o));
051                    }
052            }
053    
054            public static final Sample labeledSample(String label) {
055                    Sample s = new DefaultSample();
056                    s.setLabel(label);
057                    return s;
058            }
059    
060            public static final Sample linkToMatrix(Matrix input) {
061                    Sample s = new DefaultSample();
062                    s.setMatrix(Sample.INPUT, input);
063                    s.setLabel(input.getLabel());
064                    return s;
065            }
066    
067            public static final Sample linkToMap(Map<?, ?> map) {
068                    Sample s = new MapSample(map);
069                    return s;
070            }
071    
072            public static Sample linkToFile(FileFormat fileFormat, File file, Object... parameters)
073                            throws MatrixException, IOException {
074                    Matrix map = MatrixFactory.linkToFile(FileFormat.FILE, file, fileFormat);
075                    return linkToMap((Map) map);
076            }
077    
078            public static Sample linkToFile(File file) throws MatrixException, IOException {
079                    return linkToFile(FileFormat.guess(file), file);
080            }
081    
082            public static final ClassificationSample classificationSample(Matrix input, Matrix target) {
083                    ClassificationSample s = new ClassificationSample();
084                    s.setMatrix(Sample.INPUT, input);
085                    s.setMatrix(Sample.TARGET, target);
086                    return s;
087            }
088    
089            public static final Sample emptySample() {
090                    return new DefaultSample();
091            }
092    
093            public static final RelationalSample relationalSample(Collection<?> items) {
094                    RelationalSample s = new RelationalSample();
095                    for (Object o : items) {
096                            s.addObject(o);
097                    }
098                    return s;
099            }
100    
101            public static final ClassificationSample classificationSample() {
102                    ClassificationSample s = new ClassificationSample();
103                    return s;
104            }
105    
106            public static final ClassificationSample classificationSample(String label) {
107                    ClassificationSample s = new ClassificationSample();
108                    s.setLabel(label);
109                    return s;
110            }
111    
112            public static final RelationalSample relationalSample(String label) {
113                    RelationalSample s = new RelationalSample();
114                    s.setLabel(label);
115                    return s;
116            }
117    
118    }