📄 algorithmmanager.java
字号:
/*ARMiner - Association Rules MinerCopyright (C) 2000 UMass/Boston - Computer Science DepartmentThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307USAThe ARMiner Server was written by Dana Cristofor and LaurentiuCristofor.The ARMiner Client was written by Abdelmajid Karatihy, Xiaoyong Kuang,and Lung-Tsung Li.The ARMiner package is currently maintained by Laurentiu Cristofor(laur@cs.umb.edu).*/import java.util.Vector;import java.util.Hashtable;import java.io.*;/** AlgorithmManager.java<P> This class implements the algorithm manager module that runs algorithms and hands back the results computed by the algorithms.<P> *//* This file is a part of the ARMiner project. (P)1999-2000 by ARMiner Server Team: Dana Cristofor Laurentiu Cristofor*/public class AlgorithmManager{ private DBConfig dbconf; /** * Construct a new AlgorithmManager object */ public AlgorithmManager() throws DBConfigException { dbconf = DBConfig.getDBConfig(); } private LargeItemsetsFinder loadAlgorithm(String algName) throws AlgorithmManagerException { try { return (LargeItemsetsFinder)(Class.forName(algName).newInstance()); } catch (ClassNotFoundException e) { // try loading the class from a jar file try { JarClassLoader jcl = new JarClassLoader(algName + ".jar"); return (LargeItemsetsFinder)(jcl.loadClass(algName).newInstance()); } catch (Exception e2) { throw new AlgorithmManagerException("Cannot load algorithm " + algName + " : " + e + " and " + e2); } } catch (Exception e) { throw new AlgorithmManagerException("Cannot load algorithm " + algName + " : " + e); } } private Vector mine(String user, String dbName, String algName, boolean advanced, float minSupport, float minConfidence, Vector inAntecedent, Vector inConsequent, Vector ignored, int maxAntecedent, int minConsequent) throws AlgorithmManagerException { int old_cache_status = 0; float old_cache_support; int db_usage_count; boolean algorithm_marked = false; boolean database_marked = false; DBCacheWriter cache_writer = null; DBCacheReader cache_reader = null; DBReader db_reader = null; // *** begin critical section ***, lock on dbconf synchronized (dbconf) { try { // first mark algorithm in use dbconf.markAlgorithmInUse(user, algName); algorithm_marked = true; // get cache status and support for this database old_cache_status = dbconf.getCacheStatus(dbName); old_cache_support = dbconf.getCacheSupport(dbName); // get use count for database db_usage_count = dbconf.getDatabaseUseCount(dbName); switch (old_cache_status) { case DBConfig.CACHE_INITIAL_CREATION: case DBConfig.CACHE_IN_CREATION: // if cache is being created // then we cannot use this database, exit dbconf.unmarkAlgorithmInUse(algName); throw new AlgorithmManagerException("A database cache is being created, cannot use database at this time"); //break; case DBConfig.CACHE_CREATED: // see if we can use the already created cache if (old_cache_support <= minSupport) { // we can use the cache ! dbconf.markDatabaseInUse(user, dbName); database_marked = true; // we won't use the algorithm ! dbconf.unmarkAlgorithmInUse(algName); String cache_path = dbconf.getPathCache(dbName); cache_reader = new DBCacheReader(cache_path); } else { // we cannot use the cache, see if we can create new one if (db_usage_count > 0) { dbconf.unmarkAlgorithmInUse(algName); throw new AlgorithmManagerException("Cannot create cache if database is in use"); } else { dbconf.markDatabaseInUse(user, dbName); database_marked = true; dbconf.setCacheStatus(dbName, DBConfig.CACHE_IN_CREATION); String db_path = dbconf.getPathDatabase(dbName); String cache_path = dbconf.getPathCache(dbName); db_reader = new DBReader(db_path); // we'll try to create new cache into a temporary file cache_writer = new DBCacheWriter(cache_path + "tmp"); } } break; case DBConfig.CACHE_NOT_CREATED: dbconf.markDatabaseInUse(user, dbName); database_marked = true; dbconf.setCacheStatus(dbName, DBConfig.CACHE_INITIAL_CREATION); String db_path = dbconf.getPathDatabase(dbName); String cache_path = dbconf.getPathCache(dbName); db_reader = new DBReader(db_path); // we'll try to create new cache cache_writer = new DBCacheWriter(cache_path); break; default: throw new AlgorithmManagerException("Internal error: Invalid cache status!"); } } catch (AlgorithmManagerException e) { throw e; // rethrow } catch (Exception e) { try { if (algorithm_marked) dbconf.unmarkAlgorithmInUse(algName); if (database_marked) { dbconf.unmarkDatabaseInUse(dbName); if (old_cache_status != 0) dbconf.setCacheStatus(dbName, old_cache_status); } } catch (Exception e2) { throw new AlgorithmManagerException("Two errors occurred:\n" + e + "\nand\n" + e2); } throw new AlgorithmManagerException(e.toString()); } } // *** end of critical section *** // now we have to run the algorithms !!! // now either cache_reader or cache_writer has been initialized if (cache_writer != null) { // we must first create the cache try { // get hold of the algorithm object LargeItemsetsFinder algorithm = loadAlgorithm(algName); // we try to run the algorithm algorithm.findLargeItemsets(db_reader, cache_writer, minSupport); } catch (Throwable e) // catches both Error and Exception ! { try { // we catch out of memory errors here (among others) String cache_path = dbconf.getPathCache(dbName); if (old_cache_status == DBConfig.CACHE_NOT_CREATED) { File f = new File(cache_path); f.delete(); } else { File f = new File(cache_path + "tmp"); f.delete(); } dbconf.unmarkDatabaseInUse(dbName); dbconf.setCacheStatus(dbName, old_cache_status); } catch (Exception e2) { throw new AlgorithmManagerException("Two errors occurred:\n" + e + "\nand\n" + e2); } throw new AlgorithmManagerException("An error occurred: " + e); } finally { // close these objects, they're not needed anymore try { db_reader.close(); cache_writer.close(); // we won't use the algorithm anymore ! dbconf.unmarkAlgorithmInUse(algName); } catch (Exception e) { throw new AlgorithmManagerException("PANIC! " + e); } } // clean-up after we created cache try { String cache_path = dbconf.getPathCache(dbName); if (old_cache_status != DBConfig.CACHE_NOT_CREATED) { // replace old cache with new one // first we delete old cache File f_target = new File(cache_path); f_target.delete(); // then we rename new cache File f_source = new File(cache_path + "tmp"); f_source.renameTo(f_target); } // indicate that we have created the cache dbconf.setCacheStatus(dbName, DBConfig.CACHE_CREATED); // indicate current cache support dbconf.setCacheSupport(dbName, minSupport); // open the cache for reading cache_reader = new DBCacheReader(cache_path); } catch (Exception e) { throw new AlgorithmManagerException("PANIC! " + e); } } // read database columns Itemset is_in_antecedent = null; Itemset is_in_consequent = null; Itemset is_ignored = null; if (advanced) { try { // get first the column names String db_path = dbconf.getPathDatabase(dbName); db_reader = new DBReader(db_path); Vector column_names = db_reader.getColumnNames(); db_reader.close(); // create a map from names to indexes Hashtable name_to_index = new Hashtable(); for (int i = 0; i < column_names.size(); i++) name_to_index.put((String)(column_names.get(i)), new Integer(i + 1)); if (inAntecedent.size() > 0) { // create an itemset corresponding to inAntecedent is_in_antecedent = new Itemset(inAntecedent.size()); for (int i = 0; i < inAntecedent.size(); i++) { String item_name = (String)(inAntecedent.get(i)); int index = ((Integer)(name_to_index.get(item_name))).intValue(); is_in_antecedent.addItem(index); } } if (inConsequent.size() > 0) { // create an itemset corresponding to inConsequent is_in_consequent = new Itemset(inConsequent.size()); for (int i = 0; i < inConsequent.size(); i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -