001    /**
002     *  "TRMSim-WSN, Trust and Reputation Models Simulator for Wireless 
003     * Sensor Networks" is free software: you can redistribute it and/or 
004     * modify it under the terms of the GNU Lesser General Public License
005     * as published by the Free Software Foundation, either version 3 of 
006     * the License, or (at your option) any later version always keeping 
007     * the additional terms specified in this license.
008     *
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU Lesser General Public License for more details.
013     * 
014     * 
015     * Additional Terms of this License
016     * --------------------------------
017     * 
018     * 1. It is Required the preservation of specified reasonable legal notices
019     *   and author attributions in that material and in the Appropriate Legal
020     *   Notices displayed by works containing it.
021     * 
022     * 2. It is limited the use for publicity purposes of names of licensors or
023     *   authors of the material.
024     * 
025     * 3. It is Required indemnification of licensors and authors of that material
026     *   by anyone who conveys the material (or modified versions of it) with
027     *   contractual assumptions of liability to the recipient, for any liability
028     *   that these contractual assumptions directly impose on those licensors
029     *   and authors.
030     * 
031     * 4. It is Prohibited misrepresentation of the origin of that material, and it is
032     *   required that modified versions of such material be marked in reasonable
033     *   ways as different from the original version.
034     * 
035     * 5. It is Declined to grant rights under trademark law for use of some trade
036     *   names, trademarks, or service marks.
037     * 
038     * You should have received a copy of the GNU Lesser General Public License
039     * along with this program (lgpl.txt).  If not, see <http://www.gnu.org/licenses/>
040    */
041    
042    package es.ants.felixgm.trmsim_wsn.trm;
043    
044    import java.util.Properties;
045    import java.io.FileInputStream;
046    import java.io.FileOutputStream;
047    
048    /**
049     * <p>This class represents the generic set of parameters' values of
050      every Trust and Reputation Model</p>
051     <font color="#FF0000">
052     <p><strong>A subclass of this class, containing the specific parameters, has to be 
053     implemented in order to add a new Trust and Reputation Model</strong></p>
054     </font>
055     * <p>A parameters file has the following structure (name=value):</p>
056     * <pre>
057     *    # List of default parameters for a Trust and Reputation Model
058     *    parameter_name1=parameter_value1
059     *    parameter_name2=parameter_value2
060     *    ...
061     * </pre>
062     * @author <a href="http://ants.dif.um.es/~felixgm/en" target="_blank">F&eacute;lix G&oacute;mez M&aacute;rmol</a>, <a href="http://webs.um.es/gregorio" target="_blank">Gregorio Mart&iacute;nez P&eacute;rez</a>
063     * @version 0.4
064     * @since 0.2
065     */
066    public abstract class TRMParameters {
067        /** File containing the parameters of a certain trust and reputation model */
068        protected String parametersFile;
069        /** Header for the file containing these parameters */
070        protected String parametersFileHeader;
071        /** Trust and Reputation Model parameters */
072        protected Properties parameters;
073        
074        /** 
075         * Creates a new instance of TRMParameters 
076         */
077        public TRMParameters() {
078            parameters = new Properties();
079        }
080        
081        /** 
082         * Creates a new instance of TRMParameters from a parameters file
083         * @param parametersFile File containing the parameters of a certain trust and reputation model
084         * @throws java.lang.Exception If any parameter can not be successfully retrieved
085         */
086        public TRMParameters(String parametersFile) throws Exception {
087            this.parametersFile = parametersFile;
088            parameters = new Properties();
089            parameters.load(ClassLoader.getSystemClassLoader().getResourceAsStream(parametersFile));
090        }
091        
092        /**
093         * This method returns a double value parameter from its name
094         * @param parameterName Parameter's name
095         * @return Parameter's double value
096         */
097        protected double getDoubleParameter(String parameterName) {
098            return Double.parseDouble(parameters.getProperty(parameterName));
099        }
100        
101        /**
102         * This method returns a integer value parameter from its name
103         * @param parameterName Parameter's name
104         * @return Parameter's integer value
105         */
106        protected int getIntegerParameter(String parameterName) {
107            return Integer.parseInt(parameters.getProperty(parameterName));
108        }
109        
110        /**
111         * This method returns a String value parameter from its name
112         * @param parameterName Parameter's name
113         * @return Parameter's String value
114         */
115        protected String getStringParameter(String parameterName) {
116            return parameters.getProperty(parameterName);
117        }
118        
119        /**
120         * This method returns a boolean value parameter from its name
121         * @param parameterName Parameter's name
122         * @return Parameter's boolean value
123         */
124        protected boolean getBooleanParameter(String parameterName) {
125            return Boolean.parseBoolean(parameters.getProperty(parameterName));
126        }
127        
128        /**
129         * This method sets a double value parameter
130         * @param parameterName Parameter's name
131         * @param parameterValue Parameter's double value
132         */
133        protected void setDoubleParameter(String parameterName, double parameterValue) {
134            parameters.setProperty(parameterName, String.valueOf(parameterValue));
135        }
136        
137        /**
138         * This method sets a integer value parameter
139         * @param parameterName Parameter's name
140         * @param parameterValue Parameter's integer value
141         */
142        protected void setIntegerParameter(String parameterName, int parameterValue) {
143            parameters.setProperty(parameterName, String.valueOf(parameterValue));
144        }
145        
146        /**
147         * This method sets a boolean value parameter
148         * @param parameterName Parameter's name
149         * @param parameterValue Parameter's boolean value
150         */
151        protected void setBooleanParameter(String parameterName, boolean parameterValue) {
152            parameters.setProperty(parameterName, String.valueOf(parameterValue));
153        }
154        
155        /**
156         * This method sets a String value parameter
157         * @param parameterName Parameter's name
158         * @param parameterValue Parameter's String value
159         */
160        protected void setStringParameter(String parameterName, String parameterValue) {
161            parameters.setProperty(parameterName, parameterValue);
162        }
163        
164        /**
165         * This method saves current parameter into its associated parameters file
166         */
167        public void saveToFile() {
168            saveToFile(parametersFile);
169        }
170    
171        /**
172         * This method saves current parameters into a specified file
173         * @param parametersFile File where to save current parameters
174         */
175        public void saveToFile(String parametersFile) {
176            try {
177                parameters.store(new FileOutputStream(parametersFile),parametersFileHeader);
178            } catch (Exception ex) {
179                ex.printStackTrace();
180            }
181        }
182        
183        @Override
184        public abstract String toString();
185    }