001    /**
002     * Copyright (C) 2007-2011, 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     */
019    
020    package org.dllearner.utilities.datastructures;
021    
022    import java.util.List;
023    import java.util.Random;
024    import java.util.SortedSet;
025    import java.util.TreeSet;
026    
027    import org.apache.log4j.Level;
028    import org.apache.log4j.Logger;
029    import org.dllearner.core.owl.Individual;
030    
031    public class SetManipulation {
032    
033            /**
034             * shrinks a set to the limit fuzzy here means the elements will be randomly
035             * picked
036             * 
037             * @param set
038             * @param limit
039             */
040            public static SortedSet<String> fuzzyShrink(SortedSet<String> set, int limit) {
041                    if (set.size() <= limit) {
042                            return set;
043                    }
044                    SortedSet<String> ret = new TreeSet<String>();
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 (String 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
068             * @param limit
069             */
070            public static SortedSet<Individual> fuzzyShrinkInd(SortedSet<Individual> set, int limit) {
071                    if (set.size() <= limit) {
072                            return set;
073                    }
074                    SortedSet<Individual> ret = new TreeSet<Individual>();
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 (Individual 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
097             * @param limit
098             */
099            public static SortedSet<String> stableShrink(SortedSet<String> set,
100                            int limit) {
101                    if (set.size() <= limit) {
102                            return set;
103                    }
104                    SortedSet<String> ret = new TreeSet<String>();
105    
106                    for (String oneInd : set) {
107                            ret.add(oneInd);
108                            if (ret.size() >= limit)
109                                    break;
110    
111                    }
112    
113                    return ret;
114            }
115            
116            /**
117             * shrinks a set to the limit takes the first elements up to limit
118             * 
119             * @param set
120             * @param limit
121             */
122            public static SortedSet<Individual> stableShrinkInd(SortedSet<Individual> set,
123                            int limit) {
124                    if (set.size() <= limit) {
125                            return set;
126                    }
127                    SortedSet<Individual> ret = new TreeSet<Individual>();
128    
129                    for (Individual oneInd : set) {
130                            ret.add(oneInd);
131                            if (ret.size() >= limit)
132                                    break;
133    
134                    }
135    
136                    return ret;
137            }
138    
139            /**
140             * XXX
141             * getFirst n Elements from list.
142             * changes the list object!!!
143             * @param list
144             * @param nrElements
145             * @return returns the list shrunken to size. 
146             */
147            public static <T> List<T> getFirst(List<T> list, int nrElements) {
148                    int size;
149                    while ((size = list.size()) > nrElements) {
150                            list.remove(size - 1);
151                    }
152                    return list;
153            }
154    
155            public static SortedSet<Individual> stringToInd(SortedSet<String> set) {
156                    SortedSet<Individual> ret = new TreeSet<Individual>();
157                    for (String ind : set) {
158                            ret.add(new Individual(ind));
159                    }
160                    return ret;
161            }
162            
163            public static SortedSet<String>indToString(SortedSet<Individual> set) {
164                    SortedSet<String> ret = new TreeSet<String>();
165                    for (Individual ind : set) {
166                            ret.add(ind.toString());
167                    }
168                    return ret;
169            }
170            
171            public static void printSet(String s, SortedSet<String> set, Logger logger) {
172                    if(logger.getLevel().equals(Level.DEBUG)){
173                            logger.info(s +" ["+ set.size()+"]: "+set);
174                    }else{
175                            logger.info(s +" ["+ set.size()+"]");
176                    }
177                    
178            }
179            
180            public static <T> void printSet(String s, SortedSet<T> set) {
181                    System.out.println(s +" ["+ set.size()+"]: "+set);
182                    
183            }
184    }