001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.experiment.gmm.retrieval;
031
032import java.io.IOException;
033import java.util.ArrayList;
034import java.util.List;
035
036import org.apache.commons.vfs2.FileObject;
037import org.apache.commons.vfs2.FileSystemException;
038import org.apache.commons.vfs2.FileSystemManager;
039import org.apache.commons.vfs2.VFS;
040import org.openimaj.data.dataset.ReadableListDataset;
041import org.openimaj.data.identity.Identifiable;
042import org.openimaj.io.ObjectReader;
043
044/**
045 *
046 * @param <IMAGE>
047 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
048 */
049public class UKBenchListDataset<IMAGE> extends ReadableListDataset<IMAGE, FileObject> implements Identifiable{
050        private int object;
051        private List<String> ids;
052        private FileObject base;
053        
054        /**
055         * @param path
056         * @param reader
057         * @param object
058         */
059        public UKBenchListDataset(String path, ObjectReader<IMAGE, FileObject> reader, int object) {
060                super(reader);
061                this.object = object;
062                this.ids = heldIDs();
063                FileSystemManager fsManager;
064                try {
065                        fsManager = VFS.getManager();
066                        this.base = fsManager.resolveFile(path);
067                } catch (FileSystemException e) {
068                        throw new RuntimeException(e);
069                }
070        }
071        @Override
072        public IMAGE getInstance(int index) {
073                FileObject fo = null; 
074                try {
075                        fo = createFileObject(this.ids.get(index));
076                        return this.reader.read(fo);
077                } catch (IOException e) {
078                        throw new RuntimeException(e);
079                } finally {
080                        if(fo!=null){
081                                try {
082                                        fo.close();
083                                } catch (FileSystemException e) {
084                                        throw new RuntimeException(e);
085                                }
086                        }
087                }
088        }
089
090        private FileObject createFileObject(String string) {
091                try {
092                        FileObject child = base.getChild(string);
093                        return child;
094                } catch (FileSystemException e) {
095                        throw new RuntimeException(e);
096                }
097        }
098        @Override
099        public int numInstances() {
100                return  4;
101        }
102
103        @Override
104        public String getID() {
105                List<String> ids = heldIDs();
106                return String.format("UKBench{%s}",ids.toString());
107        }
108        private List<String> heldIDs() {
109                List<String> ids = new ArrayList<String>();
110                ids.add(String.format("ukbench%05d.jpg",object * 4 + 0));
111                ids.add(String.format("ukbench%05d.jpg",object * 4 + 1));
112                ids.add(String.format("ukbench%05d.jpg",object * 4 + 2));
113                ids.add(String.format("ukbench%05d.jpg",object * 4 + 3));
114                return ids;
115        }
116        /**
117         * @return the objects this group represents
118         */
119        public int getObject() {
120                return object;
121        }
122        
123        
124
125}