Ignore:
Timestamp:
08/05/14 10:00:41 (10 years ago)
Author:
sherbold
Message:
Location:
trunk/CrossPare/src/de/ugoe/cs/cpdp/loader
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/AbstractFolderLoader.java

    r2 r4  
    99import de.ugoe.cs.cpdp.versions.SoftwareVersion; 
    1010 
    11  
     11/** 
     12 * Abstract class for loading data from a folder. The subfolders of a defined 
     13 * folder define the projects, the file contained in the subfolder are the 
     14 * versions of a project. 
     15 *  
     16 * @author Steffen Herbold 
     17 */ 
    1218public abstract class AbstractFolderLoader implements IVersionLoader { 
    13          
    14         // TODO 
    15         interface SingleVersionLoader { 
    16                 Instances load(File file); 
    17                 boolean filenameFilter(String filename); 
    18         } 
    1919 
    2020        /** 
     
    2222         */ 
    2323        private String path = ""; 
    24          
     24 
    2525        /** 
    2626         * @see de.ugoe.cs.cpdp.loader.IVersionLoader#setLocation(java.lang.String) 
     
    2828        @Override 
    2929        public void setLocation(String location) { 
    30                 path=location; 
     30                path = location; 
    3131        } 
    32          
     32 
    3333        /** 
    3434         * @see de.ugoe.cs.cpdp.loader.IVersionLoader#load() 
     
    3737        public List<SoftwareVersion> load() { 
    3838                final List<SoftwareVersion> versions = new LinkedList<SoftwareVersion>(); 
    39                  
     39 
    4040                final File dataDir = new File(path); 
    4141                final SingleVersionLoader instancesLoader = getSingleLoader(); 
    42                  
    43                 for( File projectDir : dataDir.listFiles() ) { 
    44                         if( projectDir.isDirectory() ) { 
     42 
     43                for (File projectDir : dataDir.listFiles()) { 
     44                        if (projectDir.isDirectory()) { 
    4545                                String projectName = projectDir.getName(); 
    46                                 for( File versionFile : projectDir.listFiles() ) { 
    47                                         if( versionFile.isFile() && instancesLoader.filenameFilter(versionFile.getName()) ) { 
     46                                for (File versionFile : projectDir.listFiles()) { 
     47                                        if (versionFile.isFile() 
     48                                                        && instancesLoader.filenameFilter(versionFile 
     49                                                                        .getName())) { 
    4850                                                String versionName = versionFile.getName(); 
    4951                                                Instances data = instancesLoader.load(versionFile); 
    50                                                 versions.add(new SoftwareVersion(projectName, versionName, data)); 
     52                                                versions.add(new SoftwareVersion(projectName, 
     53                                                                versionName, data)); 
    5154                                        } 
    5255                                } 
     
    5558                return versions; 
    5659        } 
    57          
     60 
     61        /** 
     62         * Returns the concrete {@link SingleVersionLoader} to be used with this 
     63         * folder loader. 
     64         *  
     65         * @return 
     66         */ 
    5867        abstract protected SingleVersionLoader getSingleLoader(); 
    5968} 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/CSVDataLoader.java

    r2 r4  
    88import weka.core.DenseInstance; 
    99import weka.core.Instances; 
    10 import de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader; 
    1110import de.ugoe.cs.util.FileTools; 
    1211 
    1312/** 
    14  * Loads the instances for a software version from a CSV file of the // TODO dataset citation 
    15  * data set.  
     13 * Loads the instances for a software version from a CSV file of the PROMISE 
     14 * data set mined by Jurezko and Madeyski. 
     15 *  
    1616 * @author Steffen Herbold 
    1717 */ 
    1818class CSVDataLoader implements SingleVersionLoader { 
    19          
    20         /** 
    21          * Loads the instances. 
    22          * @param file handle to the file of the instances 
    23          * @return the instances 
     19 
     20        /* 
     21         * (non-Javadoc) 
     22         *  
     23         * @see 
     24         * de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader#load( 
     25         * java.io.File) 
    2426         */ 
    2527        @Override 
     
    3133                        throw new RuntimeException(e); 
    3234                } 
    33                  
     35 
    3436                // configure Instances 
    3537                final ArrayList<Attribute> atts = new ArrayList<Attribute>(); 
    36                  
    37                 String[] lineSplit = lines[0].split(",");                
    38                 for( int j=0 ; j<lineSplit.length-4 ; j++ ) { 
    39                         atts.add(new Attribute(lineSplit[j+3])); 
     38 
     39                String[] lineSplit = lines[0].split(","); 
     40                for (int j = 0; j < lineSplit.length - 4; j++) { 
     41                        atts.add(new Attribute(lineSplit[j + 3])); 
    4042                } 
    4143                final ArrayList<String> classAttVals = new ArrayList<String>(); 
     
    4446                final Attribute classAtt = new Attribute("bug", classAttVals); 
    4547                atts.add(classAtt); 
    46                  
     48 
    4749                final Instances data = new Instances(file.getName(), atts, 0); 
    4850                data.setClass(classAtt); 
    49                  
     51 
    5052                // fetch data 
    51                 for( int i=1 ; i<lines.length ; i++ ) { 
     53                for (int i = 1; i < lines.length; i++) { 
    5254                        lineSplit = lines[i].split(","); 
    53                         double[] values = new double[lineSplit.length-3]; 
    54                         for( int j=0 ; j<values.length-1 ; j++ ) { 
    55                                 values[j] = Double.parseDouble(lineSplit[j+3].trim()); 
     55                        double[] values = new double[lineSplit.length - 3]; 
     56                        for (int j = 0; j < values.length - 1; j++) { 
     57                                values[j] = Double.parseDouble(lineSplit[j + 3].trim()); 
    5658                        } 
    57                         values[values.length-1] = lineSplit[lineSplit.length-1].trim().equals("0") ? 0 : 1; 
     59                        values[values.length - 1] = lineSplit[lineSplit.length - 1].trim() 
     60                                        .equals("0") ? 0 : 1; 
    5861                        data.add(new DenseInstance(1.0, values)); 
    5962                } 
    60                  
     63 
    6164                return data; 
    6265        } 
    63          
     66 
     67        /* 
     68         * (non-Javadoc) 
     69         *  
     70         * @see de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader# 
     71         * filenameFilter(java.lang.String) 
     72         */ 
    6473        @Override 
    6574        public boolean filenameFilter(String filename) { 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/CSVFolderLoader.java

    r2 r4  
    22 
    33/** 
    4  * Implements a {@link IVersionLoader} for data from // TODO data reference 
    5  * Each folder contained in the defined location ({@link #setLocation(String)}) represents a project, the data files 
    6  * within the versions.   
     4 * Implements the {@link AbstractFolderLoader} for data from the PROMISE 
     5 * repository mined by Jurezko and Madeyski. 
     6 *  
    77 * @author Steffen Herbold 
    88 */ 
    99public class CSVFolderLoader extends AbstractFolderLoader { 
    1010 
     11        /* 
     12         * (non-Javadoc) 
     13         *  
     14         * @see de.ugoe.cs.cpdp.loader.AbstractFolderLoader#getSingleLoader() 
     15         */ 
    1116        @Override 
    1217        protected SingleVersionLoader getSingleLoader() { 
     
    1419        } 
    1520 
    16          
    1721} 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/IVersionLoader.java

    r2 r4  
    77/** 
    88 * Implements the interface for loading software versions from a data source. 
     9 *  
    910 * @author Steffen Herbold 
    1011 */ 
    1112public interface IVersionLoader { 
    12          
     13 
    1314        /** 
    14          * Sets the location of the data.  
    15          * @param location location of the data 
     15         * Sets the location of the data. 
     16         *  
     17         * @param location 
     18         *            location of the data 
    1619         */ 
    1720        public void setLocation(String location); 
    18          
     21 
    1922        /** 
    2023         * Loads the data. 
     24         *  
    2125         * @return the data 
    2226         */ 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/NasaARFFFolderLoader.java

    r2 r4  
    11package de.ugoe.cs.cpdp.loader; 
    22 
     3/** 
     4 * Implements the {@link AbstractFolderLoader} for the NASA/SOFTLAB/MDP data 
     5 * set. 
     6 *  
     7 * @author Steffen Herbold 
     8 */ 
    39public class NasaARFFFolderLoader extends AbstractFolderLoader { 
    410 
     11        /* 
     12         * (non-Javadoc) 
     13         *  
     14         * @see de.ugoe.cs.cpdp.loader.AbstractFolderLoader#getSingleLoader() 
     15         */ 
    516        @Override 
    617        protected SingleVersionLoader getSingleLoader() { 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/NasaARFFLoader.java

    r3 r4  
    1010import java.util.Map; 
    1111 
    12 import de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader; 
    1312import weka.core.Instances; 
    1413import weka.filters.Filter; 
     
    1615import weka.filters.unsupervised.attribute.Reorder; 
    1716 
     17/** 
     18 * Loads the instances for a software version from an ARFF file of the 
     19 * NASA/SOFTLAB/MDP data. 
     20 *  
     21 * @author Steffen Herbold 
     22 */ 
    1823public class NasaARFFLoader implements SingleVersionLoader { 
    1924 
     25        /** 
     26         * used to map attributes the same attribute with different names to each 
     27         * other 
     28         */ 
    2029        Map<String, String> attributeNameMap; 
     30 
     31        /** 
     32         * used to ensure that the attribute order is the same after loading 
     33         */ 
    2134        List<String> attributeOrder; 
    22          
     35 
     36        /** 
     37         * Constructor. Creates a new NasaARFFLoader. 
     38         */ 
    2339        public NasaARFFLoader() { 
    2440                attributeNameMap = new HashMap<>(); 
    25                  
     41 
    2642                // Map entries for ar project 
    2743                attributeNameMap.put("total_loc", "LOC_TOTAL"); 
     
    3248                attributeNameMap.put("unique_operators", "NUM_UNIQUE_OPERATORS"); 
    3349                attributeNameMap.put("total_operands", "NUM_OPERANDS"); 
    34                 attributeNameMap.put("total_operators",  "NUM_OPERATORS"); 
    35                 attributeNameMap.put("halstead_length",  "HALSTEAD_LENGTH"); 
     50                attributeNameMap.put("total_operators", "NUM_OPERATORS"); 
     51                attributeNameMap.put("halstead_length", "HALSTEAD_LENGTH"); 
    3652                attributeNameMap.put("halstead_volume", "HALSTEAD_VOLUME"); 
    3753                attributeNameMap.put("halstead_difficulty", "HALSTEAD_DIFFICULTY"); 
     
    4157                attributeNameMap.put("branch_count", "BRANCH_COUNT"); 
    4258                attributeNameMap.put("cyclomatic_complexity", "CYCLOMATIC_COMPLEXITY"); 
    43                 attributeNameMap.put("design_complexity",  "DESIGN_COMPLEXITY"); 
    44                  
     59                attributeNameMap.put("design_complexity", "DESIGN_COMPLEXITY"); 
     60 
    4561                // Map entries for KC2 
    46                 attributeNameMap.put("loc", "LOC_TOTAL"); // TODO these first two LOCs are guesses 
    47                 attributeNameMap.put("lOCode", "LOC_EXECUTABLE"); // TODO 
     62                attributeNameMap.put("loc", "LOC_TOTAL"); 
     63                attributeNameMap.put("lOCode", "LOC_EXECUTABLE"); 
    4864                attributeNameMap.put("lOComment", "LOC_COMMENTS"); 
    4965                attributeNameMap.put("lOCodeAndComment", "LOC_CODE_AND_COMMENT"); 
     
    5672                attributeNameMap.put("d", "HALSTEAD_DIFFICULTY"); 
    5773                attributeNameMap.put("e", "HALSTEAD_EFFORT"); 
    58                 attributeNameMap.put("b",  "HALSTEAD_ERROR_EST"); // TODO not sure about this one 
     74                attributeNameMap.put("b", "HALSTEAD_ERROR_EST"); 
    5975                attributeNameMap.put("t", "HALSTEAD_PROG_TIME"); 
    6076                attributeNameMap.put("branchCount", "BRANCH_COUNT"); 
    61                 attributeNameMap.put("v(g)",  "CYCLOMATIC_COMPLEXITY"); 
     77                attributeNameMap.put("v(g)", "CYCLOMATIC_COMPLEXITY"); 
    6278                attributeNameMap.put("iv(g)", "DESIGN_COMPLEXITY"); 
    63                                  
    64                 attributeNameMap.put("defects",  "bug"); 
     79 
     80                attributeNameMap.put("defects", "bug"); 
    6581                attributeNameMap.put("Defective", "bug"); 
    6682                attributeNameMap.put("problems", "bug"); 
    6783                attributeNameMap.put("label", "bug"); 
    68                  
     84 
    6985                // build list with normalized attribute order 
    7086                attributeOrder = new LinkedList<>(); 
    71                  
     87 
    7288                attributeOrder.add("LOC_TOTAL"); 
    7389                attributeOrder.add("LOC_EXECUTABLE"); 
     
    89105                attributeOrder.add("bug"); 
    90106        } 
    91          
    92         /** 
    93          * Loads the instances. 
    94          * @param file handle to the file of the instances 
    95          * @return the instances 
    96          */ 
     107 
     108        /* 
     109         * (non-Javadoc) 
     110         *  
     111         * @see de.ugoe.cs.cpdp.loader.SingleVersionLoader#load(java.io.File) 
     112         */ 
     113        @Override 
    97114        public Instances load(File file) { 
    98115                BufferedReader reader; 
     
    103120                        reader.close(); 
    104121                } catch (IOException e) { 
    105                         // TODO Auto-generated catch block 
    106                         throw new RuntimeException(e); 
    107                 } 
    108                  
    109                 //setting class attribute 
     122                        throw new RuntimeException("Error reading data", e); 
     123                } 
     124 
     125                // setting class attribute 
    110126                data.setClassIndex(data.numAttributes() - 1); 
    111                  
     127 
    112128                // normalize attribute names 
    113                 for( int i=0; i<data.numAttributes(); i++) { 
     129                for (int i = 0; i < data.numAttributes(); i++) { 
    114130                        String mapValue = attributeNameMap.get(data.attribute(i).name()); 
    115                         if( mapValue!= null ) { 
     131                        if (mapValue != null) { 
    116132                                data.renameAttribute(i, mapValue); 
    117133                        } 
    118134                } 
    119                  
    120                 // determine new attribute order (unwanted attributes are implicitly removed 
     135 
     136                // determine new attribute order (unwanted attributes are implicitly 
     137                // removed 
    121138                String orderString = ""; 
    122                 for( String attName : attributeOrder ) { 
    123                         for( int i=0; i<data.numAttributes(); i++) { 
    124                                 if(attName.equals(data.attribute(i).name())) { 
    125                                         orderString += (i+1) + ","; 
     139                for (String attName : attributeOrder) { 
     140                        for (int i = 0; i < data.numAttributes(); i++) { 
     141                                if (attName.equals(data.attribute(i).name())) { 
     142                                        orderString += (i + 1) + ","; 
    126143                                } 
    127144                        } 
    128145                } 
    129                 orderString = orderString.substring(0, orderString.length()-1); 
    130                  
     146                orderString = orderString.substring(0, orderString.length() - 1); 
     147 
    131148                String relationName = data.relationName(); 
    132149                String[] options = new String[2]; 
     
    139156                        data = Filter.useFilter(data, reorder); 
    140157                } catch (Exception e) { 
    141                         // TODO Auto-generated catch block 
    142                         throw new RuntimeException(); 
    143                 } 
    144                 if( data.numAttributes()!=attributeOrder.size() ) { 
    145                         throw new RuntimeException("Invalid number of attributes; filename: " + file.getName()); 
    146                 } 
    147                  
     158                        throw new RuntimeException("Error while reordering the data", e); 
     159                } 
     160                if (data.numAttributes() != attributeOrder.size()) { 
     161                        throw new RuntimeException( 
     162                                        "Invalid number of attributes; filename: " + file.getName()); 
     163                } 
     164 
    148165                // normalize bug nominal values 
    149166                Add add = new Add(); 
    150167                add.setAttributeIndex("last"); 
    151         add.setNominalLabels("0,1"); 
    152         add.setAttributeName("bug-new"); 
    153         try { 
     168                add.setNominalLabels("0,1"); 
     169                add.setAttributeName("bug-new"); 
     170                try { 
    154171                        add.setInputFormat(data); 
    155172                        data = Filter.useFilter(data, add); 
    156173                } catch (Exception e) { 
    157                         // TODO Auto-generated catch block 
    158                         e.printStackTrace(); 
    159                 } 
    160         data.setRelationName(relationName); 
    161                  
    162         double classValue; 
    163          
    164         String firstValue = data.classAttribute().enumerateValues().nextElement().toString(); 
    165         if( firstValue.equals("Y") || firstValue.equals("yes") || firstValue.equals("true") ) { 
    166                 classValue = 0.0; 
    167         } else { 
    168                 classValue = 1.0; 
    169         } 
    170          
    171                 for( int i=0 ; i<data.numInstances() ; i++ ) { 
    172                         if( data.instance(i).classValue() == classValue ) { 
    173                                 data.instance(i).setValue(data.classIndex()+1, 1.0); 
     174                        throw new RuntimeException( 
     175                                        "Error while normalizing the bug nonminal values", e); 
     176                } 
     177                data.setRelationName(relationName); 
     178 
     179                double classValue; 
     180 
     181                String firstValue = data.classAttribute().enumerateValues() 
     182                                .nextElement().toString(); 
     183                if (firstValue.equals("Y") || firstValue.equals("yes") 
     184                                || firstValue.equals("true")) { 
     185                        classValue = 0.0; 
     186                } else { 
     187                        classValue = 1.0; 
     188                } 
     189 
     190                for (int i = 0; i < data.numInstances(); i++) { 
     191                        if (data.instance(i).classValue() == classValue) { 
     192                                data.instance(i).setValue(data.classIndex() + 1, 1.0); 
    174193                        } else { 
    175                                 data.instance(i).setValue(data.classIndex()+1, 0.0); 
     194                                data.instance(i).setValue(data.classIndex() + 1, 0.0); 
    176195                        } 
    177196                } 
    178                  
     197 
    179198                int oldClassIndex = data.classIndex(); 
    180                 data.setClassIndex(oldClassIndex+1); 
     199                data.setClassIndex(oldClassIndex + 1); 
    181200                data.deleteAttributeAt(oldClassIndex); 
    182                  
     201 
    183202                return data; 
    184203        } 
    185          
     204 
     205        /* 
     206         * (non-Javadoc) 
     207         *  
     208         * @see de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader# 
     209         * filenameFilter(java.lang.String) 
     210         */ 
    186211        @Override 
    187212        public boolean filenameFilter(String filename) { 
    188213                return filename.endsWith(".arff"); 
    189214        } 
    190          
     215 
    191216} 
Note: See TracChangeset for help on using the changeset viewer.