📄 utils.java
字号:
/**
*
* AgentAcademy - an open source Data Mining framework for
* training intelligent agents
*
* Copyright (C) 2001-2003 AA Consortium.
*
* This library is open source software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation;
* either version 2.0 of the License, or (at your option) any later
* version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
package org.agentacademy.modules.dataminer.core;
/**
* <p>Title: The Data Miner prototype</p>
* <p>Description: A prototype for the DataMiner (DM), the Agent Academy (AA) module responsible for performing data mining on the contents of the Agent Use Repository (AUR). The extracted knowledge is to be sent back to the AUR in the form of a PMML document.</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: CERTH</p>
* @author asymeon
* @version 0.3
*/
import java.lang.Math;
import java.util.StringTokenizer;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import org.apache.log4j.Logger;
/**
* Class implementing some simple utility methods.
*
*/
public final class Utils {
public static Logger log = Logger.getLogger(Utils.class);
/** The natural logarithm of 2. */
public static double log2 = Math.log(2);
/** The small deviation allowed in double comparisons */
public static double SMALL = 1e-6;
/**
* Reads properties that inherit from three locations. Properties
* are first defined in the system resource location (i.e. in the
* CLASSPATH). These default properties must exist. Properties
* defined in the users home directory (optional) override default
* settings. Properties defined in the current directory (optional)
* override all these settings.
*
* @param resourceName the location of the resource that should be
* loaded. e.g.: "weka/core/Utils.props". (The use of hardcoded
* forward slashes here is OK - see
* jdk1.1/docs/guide/misc/resources.html) This routine will also
* look for the file (in this case) "Utils.props" in the users home
* directory and the current directory.
* @return the Properties
* @exception Exception if no default properties are defined, or if
* an error occurs reading the properties files.
*/
public static Properties readProperties(String resourceName)
throws Exception{
Properties defaultProps = new Properties();
try {
// Apparently hardcoded slashes are OK here
// jdk1.1/docs/guide/misc/resources.html
defaultProps.load(ClassLoader.getSystemResourceAsStream(resourceName));
} catch (Exception ex) {
/* throw new Exception("Problem reading default properties: "
+ ex.getMessage()); */
System.err.println("Warning, unable to load properties file from "
+"system resource (Utils.java)");
}
// Hardcoded slash is OK here
// eg: see jdk1.1/docs/guide/misc/resources.html
int slInd = resourceName.lastIndexOf('/');
if (slInd != -1) {
resourceName = resourceName.substring(slInd + 1);
}
// Allow a properties file in the home directory to override
Properties userProps = new Properties(defaultProps);
File propFile = new File(System.getProperties().getProperty("user.home")
+ File.separatorChar
+ resourceName);
if (propFile.exists()) {
try {
userProps.load(new FileInputStream(propFile));
} catch (Exception ex) {
throw new Exception("Problem reading user properties: " + propFile);
}
}
// Allow a properties file in the current directory to override
Properties localProps = new Properties(userProps);
propFile = new File(resourceName);
if (propFile.exists()) {
try {
localProps.load(new FileInputStream(propFile));
} catch (Exception ex) {
throw new Exception("Problem reading local properties: " + propFile);
}
}
return localProps;
}
/**
* Returns the correlation coefficient of two double vectors.
*
* @param y1 double vector 1
* @param y2 double vector 2
* @param n the length of two double vectors
* @return the correlation coefficient
*/
public final static double correlation(double y1[],double y2[],int n) {
int i;
double av1 = 0.0, av2 = 0.0, y11 = 0.0, y22 = 0.0, y12 = 0.0, c;
if (n <= 1) {
return 1.0;
}
for (i = 0; i < n; i++) {
av1 += y1[i];
av2 += y2[i];
}
av1 /= (double) n;
av2 /= (double) n;
for (i = 0; i < n; i++) {
y11 += (y1[i] - av1) * (y1[i] - av1);
y22 += (y2[i] - av2) * (y2[i] - av2);
y12 += (y1[i] - av1) * (y2[i] - av2);
}
if (y11 * y22 == 0.0) {
c=1.0;
} else {
c = y12 / Math.sqrt(Math.abs(y11 * y22));
}
return c;
}
/**
* Removes all occurrences of a string from another string.
*
* @param inString the string to remove substrings from.
* @param substring the substring to remove.
* @return the input string with occurrences of substring removed.
*/
public static String removeSubstring(String inString, String substring) {
StringBuffer result = new StringBuffer();
int oldLoc = 0, loc = 0;
while ((loc = inString.indexOf(substring, oldLoc))!= -1) {
result.append(inString.substring(oldLoc, loc));
oldLoc = loc + substring.length();
}
result.append(inString.substring(oldLoc));
return result.toString();
}
/**
* Replaces with a new string, all occurrences of a string from
* another string.
*
* @param inString the string to replace substrings in.
* @param substring the substring to replace.
* @param replaceString the replacement substring
* @return the input string with occurrences of substring replaced.
*/
public static String replaceSubstring(String inString, String subString,
String replaceString) {
StringBuffer result = new StringBuffer();
int oldLoc = 0, loc = 0;
while ((loc = inString.indexOf(subString, oldLoc))!= -1) {
result.append(inString.substring(oldLoc, loc));
result.append(replaceString);
oldLoc = loc + subString.length();
}
result.append(inString.substring(oldLoc));
return result.toString();
}
/**
* Pads a string to a specified length, inserting spaces on the left
* as required. If the string is too long, characters are removed (from
* the right).
*
* @param inString the input string
* @param length the desired length of the output string
* @return the output string
*/
public static String padLeft(String inString, int length) {
return fixStringLength(inString, length, false);
}
/**
* Pads a string to a specified length, inserting spaces on the right
* as required. If the string is too long, characters are removed (from
* the right).
*
* @param inString the input string
* @param length the desired length of the output string
* @return the output string
*/
public static String padRight(String inString, int length) {
return fixStringLength(inString, length, true);
}
/**
* Pads a string to a specified length, inserting spaces as
* required. If the string is too long, characters are removed (from
* the right).
*
* @param inString the input string
* @param length the desired length of the output string
* @param right true if inserted spaces should be added to the right
* @return the output string
*/
private static String fixStringLength(String inString, int length,
boolean right) {
if (inString.length() < length) {
while (inString.length() < length) {
inString = (right ? inString.concat(" ") : " ".concat(inString));
}
} else if (inString.length() > length) {
inString = inString.substring(0, length);
}
return inString;
}
/**
* Rounds a double and converts it into String.
*
* @param value the double value
* @param afterDecimalPoint the (maximum) number of digits permitted
* after the decimal point
* @return the double as a formatted string
*/
public static String doubleToString(double value, int afterDecimalPoint) {
StringBuffer stringBuffer;
double temp;
int i,dotPosition;
long precisionValue;
temp = value * Math.pow(10.0, afterDecimalPoint);
if (Math.abs(temp) < Long.MAX_VALUE) {
precisionValue = (temp > 0) ? (long)(temp + 0.5)
: -(long)(Math.abs(temp) + 0.5);
if (precisionValue == 0) {
stringBuffer = new StringBuffer(String.valueOf(0));
} else {
stringBuffer = new StringBuffer(String.valueOf(precisionValue));
}
if (afterDecimalPoint == 0) {
return stringBuffer.toString();
}
dotPosition = stringBuffer.length() - afterDecimalPoint;
while (((precisionValue < 0) && (dotPosition < 1)) ||
(dotPosition < 0)) {
if (precisionValue < 0) {
stringBuffer.insert(1, 0);
} else {
stringBuffer.insert(0, 0);
}
dotPosition++;
}
stringBuffer.insert(dotPosition, '.');
if ((precisionValue < 0) && (stringBuffer.charAt(1) == '.')) {
stringBuffer.insert(1, 0);
} else if (stringBuffer.charAt(0) == '.') {
stringBuffer.insert(0, 0);
}
int currentPos = stringBuffer.length() - 1;
while ((currentPos > dotPosition) &&
(stringBuffer.charAt(currentPos) == '0')) {
stringBuffer.setCharAt(currentPos--, ' ');
}
if (stringBuffer.charAt(currentPos) == '.') {
stringBuffer.setCharAt(currentPos, ' ');
}
return stringBuffer.toString().trim();
}
return new String("" + value);
}
/**
* Rounds a double and converts it into a formatted decimal-justified String.
* Trailing 0's are replaced with spaces.
*
* @param value the double value
* @param width the width of the string
* @param afterDecimalPoint the number of digits after the decimal point
* @return the double as a formatted string
*/
public static String doubleToString(double value, int width,
int afterDecimalPoint) {
String tempString = doubleToString(value, afterDecimalPoint);
char[] result;
int dotPosition;
if ((afterDecimalPoint >= width)
|| (tempString.indexOf('E') != -1)) { // Protects sci notation
return tempString;
}
// Initialize result
result = new char[width];
for (int i = 0; i < result.length; i++) {
result[i] = ' ';
}
if (afterDecimalPoint > 0) {
// Get position of decimal point and insert decimal point
dotPosition = tempString.indexOf('.');
if (dotPosition == -1) {
dotPosition = tempString.length();
} else {
result[width - afterDecimalPoint - 1] = '.';
}
} else {
dotPosition = tempString.length();
}
int offset = width - afterDecimalPoint - dotPosition;
if (afterDecimalPoint > 0) {
offset--;
}
// Not enough room to decimal align within the supplied width
if (offset < 0) {
return tempString;
}
// Copy characters before decimal point
for (int i = 0; i < dotPosition; i++) {
result[offset + i] = tempString.charAt(i);
}
// Copy characters after decimal point
for (int i = dotPosition + 1; i < tempString.length(); i++) {
result[offset + i] = tempString.charAt(i);
}
return new String(result);
}
/**
* Tests if a is equal to b.
*
* @param a a double
* @param b a double
*/
public static boolean eq(double a, double b){
return (a - b < SMALL) && (b - a < SMALL);
}
/**
* Checks if the given array contains any non-empty options.
*
* @param strings an array of strings
* @exception Exception if there are any non-empty options
*/
public static void checkForRemainingOptions(String [] options)
throws Exception {
int illegalOptionsFound = 0;
StringBuffer text = new StringBuffer();
if (options == null) {
return;
}
for (int i = 0; i < options.length; i++) {
if (options[i].length() > 0) {
illegalOptionsFound++;
text.append(options[i] + ' ');
}
}
if (illegalOptionsFound > 0) {
throw new Exception("Illegal options: " + text);
}
}
/**
* Checks if the given array contains the flag "-Char". Stops
* searching at the first marker "--". If the flag is found,
* it is replaced with the empty string.
*
* @param flag the character indicating the flag.
* @param strings the array of strings containing all the options.
* @return true if the flag was found
* @exception Exception if an illegal option was found
*/
public static boolean getFlag(char flag, String [] options)
throws Exception {
if (options == null) {
return false;
}
for (int i = 0; i < options.length; i++) {
if ((options[i].length() > 1) && (options[i].charAt(0) == '-')) {
try {
Double dummy = Double.valueOf(options[i]);
} catch (NumberFormatException e) {
if (options[i].length() > 2) {
throw new Exception("Illegal option: " + options[i]);
}
if (options[i].charAt(1) == flag) {
options[i] = "";
return true;
}
if (options[i].charAt(1) == '-') {
return false;
}
}
}
}
return false;
}
/**
* Gets an option indicated by a flag "-Char" from the given array
* of strings. Stops searching at the first marker "--". Replaces
* flag and option with empty strings.
*
* @param flag the character indicating the option.
* @param options the array of strings containing all the options.
* @return the indicated option or an empty string
* @exception Exception if the option indicated by the flag can't be found
*/
public static String getOption(char flag, String [] options)
throws Exception {
String newString;
if (options == null)
return "";
for (int i = 0; i < options.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -