source: trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/MySQLResultStorage.java @ 118

Last change on this file since 118 was 98, checked in by sherbold, 9 years ago
  • added the new configuration parameter repetitions and implemented its usage in the AbstractCrossProjectExperiment?. It now allows for multiple storages of the same result in an IResultContainer to allow for repetitions of experiments with random components.
  • Property svn:mime-type set to text/plain
File size: 6.7 KB
Line 
1// Copyright 2015 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.cpdp.eval;
16
17import java.io.FileInputStream;
18import java.io.IOException;
19import java.sql.ResultSet;
20import java.sql.SQLException;
21import java.sql.Statement;
22import java.util.Properties;
23
24
25import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
26
27import de.lmu.ifi.dbs.elki.logging.Logging.Level;
28import de.ugoe.cs.util.console.Console;
29
30/**
31 * <p>
32 * Implements a storage of experiment results in a MySQL database.
33 * </p>
34 *
35 * @author Steffen Herbold
36 */
37public class MySQLResultStorage implements IResultStorage {
38
39    /**
40     * Connection to the database
41     */
42    //private Connection con = null;
43   
44    /**
45     * Connection pool for the data base.
46     */
47    private MysqlDataSource connectionPool = null;
48
49    /**
50     * <p>
51     * Creates a new results storage. Tries to read a properties file mysql.cred located in the
52     * working directory. If this file is not found, the default database configuration is used:
53     * <ul>
54     * <li>dbHost = localhost</li>
55     * <li>dbPort = 3306</li>
56     * <li>dbName = crosspare</li>
57     * <li>dbUser = crosspare</li>
58     * <li>dbPass = benchmark</li>
59     * </p>
60     */
61    public MySQLResultStorage() {
62        Properties dbProperties = new Properties();
63        try {
64            dbProperties.load(new FileInputStream("mysql.cred"));
65        }
66        catch (IOException e) {
67            Console.printerr("Could not load mysql.cred file: " + e.getMessage());
68            Console.printerr("Must be a properties file located in working directory.");
69            Console
70                .traceln(Level.WARNING,
71                         "Using default DB configuration since mysql.cred file could not be loaded");
72        }
73        String dbHost = dbProperties.getProperty("db.host", "localhost");
74        String dbPort = dbProperties.getProperty("db.port", "3306");
75        String dbName = dbProperties.getProperty("db.name", "crosspare");
76        String dbUser = dbProperties.getProperty("db.user", "crosspare");
77        String dbPass = dbProperties.getProperty("db.pass", "benchmark");
78        connectToDB(dbHost, dbPort, dbName, dbUser, dbPass);
79    }
80
81    /**
82     * <p>
83     * Sets up the database connection
84     * </p>
85     *
86     * @param dbHost
87     *            host of the database
88     * @param dbPort
89     *            port of the database
90     * @param dbName
91     *            name of the database
92     * @param dbUser
93     *            user of the database
94     * @param dbPass
95     *            password of the user
96     */
97    private void connectToDB(String dbHost,
98                             String dbPort,
99                             String dbName,
100                             String dbUser,
101                             String dbPass)
102    {
103        connectionPool = new MysqlDataSource();
104        connectionPool.setUser(dbUser);
105        connectionPool.setPassword(dbPass);
106        connectionPool.setUrl("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName);
107    }
108
109    /*
110     * (non-Javadoc)
111     *
112     * @see de.ugoe.cs.cpdp.eval.IResultStorage#addResult(de.ugoe.cs.cpdp.eval.ExperimentResult)
113     */
114    @Override
115    public void addResult(ExperimentResult result) {
116        StringBuilder sql = new StringBuilder();
117        sql.append("INSERT INTO crosspare.results VALUES (NULL,");
118        sql.append("\'" + result.getConfigurationName() + "\',");
119        sql.append("\'" + result.getProductName() + "\',");
120        sql.append("\'" + result.getClassifier() + "\',");
121        sql.append(result.getSizeTestData() + ",");
122        sql.append(result.getSizeTrainingData() + ",");
123        sql.append(result.getSuccHe() + ",");
124        sql.append(result.getSuccZi() + ",");
125        sql.append(result.getSuccG75() + ",");
126        sql.append(result.getSuccG60() + ",");
127        sql.append(result.getError() + ",");
128        sql.append(result.getRecall() + ",");
129        sql.append(result.getPrecision() + ",");
130        sql.append(result.getFscore() + ",");
131        sql.append(result.getGscore() + ",");
132        sql.append(result.getMcc() + ",");
133        sql.append(result.getAuc() + ",");
134        sql.append(result.getAucec() + ",");
135        sql.append(result.getTpr() + ",");
136        sql.append(result.getTnr() + ",");
137        sql.append(result.getFpr() + ",");
138        sql.append(result.getFnr() + ",");
139        sql.append(result.getTp() + ",");
140        sql.append(result.getFn() + ",");
141        sql.append(result.getTn() + ",");
142        sql.append(result.getFp() + ");");
143
144        Statement stmt;
145        try {
146            stmt = connectionPool.getConnection().createStatement();
147            stmt.executeUpdate(sql.toString().replace("NaN", "NULL"));
148        }
149        catch (SQLException e) {
150            Console.printerr("Problem with MySQL connection: ");
151            Console.printerr("SQLException: " + e.getMessage());
152            Console.printerr("SQLState: " + e.getSQLState());
153            Console.printerr("VendorError: " + e.getErrorCode());
154            return;
155        }
156    }
157
158    /*
159     * (non-Javadoc)
160     *
161     * @see de.ugoe.cs.cpdp.eval.IResultStorage#containsResult(java.lang.String, java.lang.String)
162     */
163    @Override
164    public int containsResult(String experimentName, String productName) {
165        String sql = "SELECT COUNT(*) as cnt FROM crosspare.results WHERE configurationName=\'" +
166            experimentName + "\' AND productName=\'" + productName + "\';";
167        Statement stmt;
168        try {
169            stmt = connectionPool.getConnection().createStatement();
170            ResultSet results = stmt.executeQuery(sql);
171            results.next();
172            return results.getInt("cnt");
173        }
174        catch (SQLException e) {
175            Console.printerr("Problem with MySQL connection: \n");
176            Console.printerr("SQLException: " + e.getMessage() + "\n");
177            Console.printerr("SQLState: " + e.getSQLState() + "\n");
178            Console.printerr("VendorError: " + e.getErrorCode() + "\n");
179            return 0;
180        }
181    }
182}
Note: See TracBrowser for help on using the repository browser.