001    /*
002     * DropMatch.java
003     *
004     * Created on July 5, 2006, 11:59 AM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler.dataset.datasetManipulators;
025    
026    import gov.bnl.star.offline.scheduler.dataset.Dataset;
027    import gov.bnl.star.offline.scheduler.request.Request;
028    
029    import java.util.List;
030    import java.util.ArrayList;
031    import org.apache.log4j.Logger;
032    
033    import java.io.BufferedReader;
034    import java.io.DataInputStream;
035    import java.io.FileInputStream;
036    import java.io.FileNotFoundException;
037    import java.io.FileReader;
038    import java.io.IOException;
039    
040    /**
041     * Drop any entry matching the configured regular expression
042     *
043     * @author Levente B. Hajdu
044     */
045    public class DropMatchingRegX implements DatasetManipulator{
046        
047        static private Logger log = Logger.getLogger(DropMatchingRegX.class.getName());
048        
049        /** Creates a new instance of DropMatch 
050         * 
051         * @param regX the regular expression, that will be dropped if it matches any entry in the dataset. The defualt value the ".*" (everything)
052         */
053        public DropMatchingRegX(String regX) {
054            if(regX == null) throw new RuntimeException("DropMatchingSubset(String regX) can not have a null vaue for regX");
055            setMustMatch(regX);
056        }
057        
058        /** Used to pass the dataset to the dataset manipulator
059          * @param dataset The dataset to be modifyed 
060          * @param request The request object of the current request for with will use the dataset 
061         **/
062        public void modify(Dataset dataset, Request request){
063            
064            log.info("dropping all entries in the dataset tha match \"" +  getMustMatch() + "\"");
065            //System.out.println("dropping entries matching " + "\"" + getMustMatch());
066            
067            int droppedEntries = 0;
068        
069           // super.modify(dataset, request);
070    
071            try {
072                
073                BufferedReader currentDataset = new BufferedReader( new FileReader(dataset.getDatasetName() )); //open the datset
074                String datsetEntry;
075                //int dots = 0;
076                while ((datsetEntry = currentDataset.readLine()) != null) { //lopp over each entry
077                    //if(((dots++) % 1000) == 0) System.out.print('.'); // print a dot every 1000 lines
078                    
079                    if(! datsetEntry.matches(regxMatch)){ //if the entry does not match write it to the buffer, else just get ride of it.
080                        dataset.writeToBuffer(datsetEntry);
081                    }
082                    else{
083                        droppedEntries ++;
084                        dataset.decrementSize();
085                    }  
086                }
087            } catch (FileNotFoundException ex) {
088                ex.printStackTrace();
089                throw new RuntimeException("The file was not found, there is an error with the dataset");
090            } catch (IOException ex) {
091                ex.printStackTrace();
092                throw new RuntimeException("Can't read from the dataset.");
093            }
094            
095            dataset.swap_buffer_dataset_with_dataset();
096            //System.out.println("dopped " + droppedEntries + " file, dataset now " + dataset.getDatasetSize() + "files."  );    
097        }
098        
099        
100        private String regxMatch = ".*";
101        /** @param regxMatch Any data set entry matching this regular expression will be dropped from the dataset.*/
102        public void setMustMatch(String regxMatch){ this.regxMatch = regxMatch; }
103        /** @return Any dataset entry matching this regular expression will be dropped from the dataset.*/
104        public String getMustMatch(){return regxMatch;} 
105        /** @return true of the class was able to modify the dataset as requested. If it could not meet the request it returns false.*/
106        public boolean requirementsSatisfied() { return true; }
107    
108        
109    }