001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.utilities.datastructures;
020
021import java.util.Random;
022import java.util.SortedSet;
023import java.util.TreeSet;
024
025import org.semanticweb.owlapi.model.OWLIndividual;
026
027import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
028
029public class SetManipulation {
030        
031        private static final OWLDataFactoryImpl df = new OWLDataFactoryImpl();
032
033        /**
034         * shrinks a set to the limit fuzzy here means the elements will be randomly
035         * picked
036         * 
037         * @param set the set
038         * @param limit the limit
039         */
040        public static <T> SortedSet<T> fuzzyShrink(SortedSet<T> set, int limit) {
041                if (set.size() <= limit) {
042                        return set;
043                }
044                SortedSet<T> ret = new TreeSet<>();
045                Random r = new Random();
046                double treshold = ((double) limit) / set.size();
047                // System.out.println("treshold"+howmany);
048                // System.out.println("treshold"+allRetrieved.size());
049                // System.out.println("treshold"+treshold);
050
051                while (ret.size() < limit) {
052                        for (T oneInd : set) {
053                                if (r.nextDouble() < treshold) {
054                                        ret.add(oneInd);
055                                        if (ret.size() >= limit)
056                                                break;
057                                }
058                        }
059                }
060                return ret;
061        }
062        
063        /**
064         * shrinks a set to the limit fuzzy here means the elements will be randomly
065         * picked
066         *
067         * @param set the set
068         * @param limit the limit
069         */
070        public static SortedSet<OWLIndividual> fuzzyShrinkInd(SortedSet<OWLIndividual> set, int limit) {
071                if (set.size() <= limit) {
072                        return set;
073                }
074                SortedSet<OWLIndividual> ret = new TreeSet<>();
075                Random r = new Random();
076                double treshold = ((double) limit) / set.size();
077                // System.out.println("treshold"+howmany);
078                // System.out.println("treshold"+allRetrieved.size());
079                // System.out.println("treshold"+treshold);
080
081                while (ret.size() < limit) {
082                        for (OWLIndividual oneInd : set) {
083                                if (r.nextDouble() < treshold) {
084                                        ret.add(oneInd);
085                                        if (ret.size() >= limit)
086                                                break;
087                                }
088                        }
089                }
090                return ret;
091        }
092        
093        /**
094         * shrinks a set to the limit takes the first elements up to limit
095         *
096         * @param set the set
097         * @param limit the limit
098         */
099        public static <T> SortedSet<T> stableShrink(SortedSet<T> set,
100                        int limit) {
101                if (set.size() <= limit) {
102                        return set;
103                }
104                SortedSet<T> ret = new TreeSet<>();
105
106                for (T oneInd : set) {
107                        ret.add(oneInd);
108                        if (ret.size() >= limit)
109                                break;
110                }
111                return ret;
112        }
113        
114        /**
115         * shrinks a set to the limit takes the first elements up to limit
116         *
117         * @param set the set
118         * @param limit the limit
119         */
120        public static SortedSet<OWLIndividual> stableShrinkInd(SortedSet<OWLIndividual> set,
121                        int limit) {
122                if (set.size() <= limit) {
123                        return set;
124                }
125                SortedSet<OWLIndividual> ret = new TreeSet<>();
126
127                for (OWLIndividual oneInd : set) {
128                        ret.add(oneInd);
129                        if (ret.size() >= limit)
130                                break;
131
132                }
133
134                return ret;
135        }
136
137}