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.Arrays;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Set;
026    import java.util.TreeSet;
027    
028    import org.dllearner.core.owl.NamedClass;
029    import org.dllearner.core.owl.Individual;
030    import org.dllearner.core.owl.ObjectProperty;
031    
032    /**
033     * Conversion between different data structures.
034     * 
035     * @author Jens Lehmann
036     * @author Sebastian Hellmann
037     *
038     */
039    public class Datastructures {
040    
041            public static boolean strToBool(String str) {
042                    if (str.equals("true"))
043                            return true;
044                    else if (str.equals("false"))
045                            return false;
046                    else
047                            throw new Error("Cannot convert to boolean.");
048            }
049    
050            /**
051             * easy conversion
052             * 
053             * @param s
054             */
055            public static String[] setToArray(Set<String> s) {
056                    if(s==null)return null;
057                    String[] ret=new String[s.size()];
058                    int i=0;
059                    for (Iterator<String> iter = s.iterator(); iter.hasNext();) {
060                            ret[i] = iter.next();
061                            i++;
062                            
063                    }
064                    return ret;
065                    
066            }
067            
068            public static String[] sortedSet2StringListIndividuals(Set<Individual> individuals){
069                    
070                    String[] ret=new String[individuals.size()];
071                    Iterator<Individual> i=individuals.iterator();
072                    int a=0;
073                    while (i.hasNext()){
074                            ret[a++]=((Individual)i.next()).getName();
075                    }
076                    Arrays.sort(ret);
077                    return ret;
078            }
079            
080            public static String[] sortedSet2StringListRoles(Set<ObjectProperty> s){
081                    
082                    String[] ret=new String[s.size()];
083                    Iterator<ObjectProperty> i=s.iterator();
084                    int a=0;
085                    while (i.hasNext()){
086                            ret[a++]=((ObjectProperty)i.next()).getName();
087                    }
088                    Arrays.sort(ret);
089                    return ret;
090            }
091            
092            public static String[] sortedSet2StringListConcepts(Set<NamedClass> s){
093                    
094                    String[] ret=new String[s.size()];
095                    Iterator<NamedClass> i=s.iterator();
096                    int a=0;
097                    while (i.hasNext()){
098                            ret[a++]=((NamedClass)i.next()).getName();
099                    }
100                    Arrays.sort(ret);
101                    return ret;
102            }       
103            
104            public static Set<String> individualSetToStringSet(Set<Individual> individuals) {
105                    Set<String> ret = new TreeSet<String>();
106                    for(Individual ind : individuals) {
107                            ret.add(ind.toString());
108                    }
109                    return ret;
110            }       
111            
112            public static Set<String> individualListToStringSet(List<Individual> individuals) {
113                    Set<String> ret = new TreeSet<String>();
114                    for(Individual ind : individuals) {
115                            ret.add(ind.toString());
116                    }
117                    return ret;
118            }               
119    }