001    /*
002     * GenericResourceRequirementStringDefinition.java
003     *
004     * Created on July 15, 2004, 4:29 PM
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.util;
025    import gov.bnl.star.offline.scheduler.Job;
026    /**
027     * Analysis jobs and generate a resource using string. 
028     * @author  Levente Hajdu
029     */
030    public class GenericResourceRequirementStringDefinition {
031        
032        /** Creates a new instance of GenericResourceRequirementStringDefinition */
033        public GenericResourceRequirementStringDefinition() {
034        }
035        
036        /**
037         * @param args the command line arguments
038         */
039        public static void main(String[] args) {
040            // TODO code application logic here
041        }
042        
043        
044        private String delimiter =" && "; //The string to be used as the delimiter
045        
046        private String maxStorageSpaceFormat = "tmp>maxStorageSpace";
047        private String minStorageSpaceFormat = "tmp<minStorageSpace";
048        private String maxMemoryFormat = "mem>maxMemory";
049        private String minMemoryFormat = "mem<minMemory";
050        
051        public String startingString = "\"";
052        public String endString = "\"";
053        
054        private String maxStorageSpaceVariable = "maxStorageSpace";
055        private String minStorageSpaceVariable = "minStorageSpace";
056        private String minMemoryVariable = "minMemory";
057        private String maxMemoryVariable = "maxMemory";
058        
059        public void setStartString(String startingString){
060            this.startingString = startingString;
061        }
062        
063        public void SetEndString(String endString){
064            this.endString = endString;
065        }
066        
067        public void setMaxStorageSpaceFormat(String maxStorageSpaceFormat){
068            this.maxStorageSpaceFormat = maxStorageSpaceFormat;
069        }
070        
071        public void setMinStorageSpaceFormat(String minStorageSpaceFormat){
072            this.minStorageSpaceFormat = minStorageSpaceFormat;
073        }
074        
075        public void setMaxMemoryFormat(String maxMemoryFormat){
076            this.maxMemoryFormat = maxMemoryFormat;
077        }
078        
079        public void setMinMemoryFormat(String minMemoryFormat){
080            this.minMemoryFormat = minMemoryFormat;
081        }
082    
083        public void setMaxStorageSpaceVariable(String maxStorageSpaceVariable){
084            this.maxStorageSpaceVariable = maxStorageSpaceVariable;
085        }
086        
087        public void setMinStorageSpaceVariable(String minStorageSpaceVariable){
088            this.minStorageSpaceVariable = minStorageSpaceVariable;
089        }
090        
091        public void setMaxMemoryVariable(String maxMemoryVariable){
092            this.maxMemoryVariable = maxMemoryVariable;
093        }
094        
095        public void setMinMemoryVariable(String minMemoryVariable){
096            this.minMemoryVariable = minMemoryVariable;
097        }
098        
099        public String makeString(Job job){
100            
101            boolean needsDelimiter = false;
102            String GenericResourcesString = startingString;
103            
104            if((job.getMaxStorageSpace() != -1) && (maxStorageSpaceFormat  != null)){
105                
106                if(maxStorageSpaceFormat.length() > 0){
107                    GenericResourcesString = GenericResourcesString.concat(maxStorageSpaceFormat.replaceAll(maxStorageSpaceVariable, String.valueOf(job.getMaxStorageSpace())));
108                    needsDelimiter = true;
109                }
110            }
111            
112            if((job.getMinStorageSpace() != -1) && (minStorageSpaceFormat != null)){
113                if(minStorageSpaceFormat.length() > 0){
114                    if(needsDelimiter) GenericResourcesString = GenericResourcesString.concat(delimiter); 
115                    GenericResourcesString = GenericResourcesString.concat(minStorageSpaceFormat.replaceAll(minStorageSpaceVariable, String.valueOf(job.getMinStorageSpace())));
116                    needsDelimiter = true;
117                }    
118            }
119            
120            if((job.getMaxMemory() != -1) && (maxMemoryVariable != null)){
121                if(maxMemoryFormat.length() > 0){
122                    if(needsDelimiter) GenericResourcesString = GenericResourcesString.concat(delimiter); 
123                    GenericResourcesString = GenericResourcesString.concat(maxMemoryFormat.replaceAll(maxMemoryVariable, String.valueOf(job.getMaxMemory())));
124                    needsDelimiter = true;
125                }    
126            }
127            
128            if((job.getMinMemory() != -1) &&(minMemoryFormat != null)){
129                if(minMemoryFormat.length() > 0){
130                    if(needsDelimiter) GenericResourcesString = GenericResourcesString.concat(delimiter); 
131                    GenericResourcesString = GenericResourcesString.concat(minMemoryFormat.replaceAll(minMemoryVariable, String.valueOf(job.getMinMemory())));
132                    needsDelimiter = true;
133                }    
134            }
135            
136            GenericResourcesString = GenericResourcesString.concat(endString);
137            
138            return GenericResourcesString;
139        }
140        
141        public boolean hasResourcesDefinition(Job job){
142            if((job.getMaxStorageSpace() == -1)&&(job.getMinStorageSpace() == -1)&&(job.getMaxMemory() == -1)&&(job.getMinMemory() == -1)) 
143                    return false;
144            return true;
145        }
146        
147        public void clearAllDefaults(){
148            maxStorageSpaceFormat = "";
149            maxStorageSpaceFormat = "";
150            minStorageSpaceFormat = "";
151            maxMemoryFormat = "";
152            minMemoryFormat = "";
153            startingString = "";
154            endString = "";
155            maxStorageSpaceVariable = "";
156            minStorageSpaceVariable = "";
157            minMemoryVariable = "";
158            maxMemoryVariable = "";  
159        }   
160        
161    }