⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bimldaofile.java

📁 为了下东西 随便发了个 datamining 的源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program 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 General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package eti.bi.alphaminer.core.dao;
   

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; 

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import eti.bi.alphaminer.vo.BICase;
import eti.bi.alphaminer.vo.CaseFactory;
import eti.bi.alphaminer.vo.CaseInfoList;
import eti.bi.alphaminer.vo.CaseInformation;
import eti.bi.alphaminer.vo.NodeFactory;
import eti.bi.alphaminer.vo.SearchCriteria;
import eti.bi.alphaminer.vo.SearchedCaseInfoList;
import eti.bi.common.Constants;
import eti.bi.common.System.SysConfig;
import eti.bi.common.System.SysLog;
import eti.bi.exception.BaseException;
import eti.bi.exception.CaseNotFoundException;
import eti.bi.exception.SysException;
import eti.bi.util.Utils;

/**
 * 
 * This is an implementation of the interface BIMLDAO which used the file system as
 * the BIML repository. The indexing of the BIML files are based of the file name of
 * the BIML file. The file name is in the form <prefix>_<id>_<type>.xml, where the
 * prefix is stored in the class Constants. The path of the repository is stored in
 * the system configuration which can be retrieved by the class SysConfig. Under the
 * root path of the BIML repository, there are two sub directories which called 
 * "activate" and "trash" which separate the BIML files in these two states.
 * 
 * @since 		1.0
 * @version 	$Revision$ $Date$
 * @author		$Author$

 */
public class BIMLDAOFile implements BIMLDAO{
	

	/**
	 * Find all the files in both "activate" and "inprogress" directories of th
	 * BIML repository path.
	 * 
	 * @see hk.hku.eti.bi.bdm.BIMLDAO#listAllBIML()
	 */
	public Collection<BICaseInfo> listAllBIML()
	{
		ArrayList<BICaseInfo> aBIMLList = new ArrayList<BICaseInfo>();
		
			String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
			File aDir = new File(aDirname, "activate");
			File [] aFiles = aDir.listFiles(new FilenameFilter()
				{
					public boolean accept(File dir, String name)
					{
						if (name.matches(".*xml") && !name.matches(".*template.*"))
							return true;
						else
							return false;
					}
				}
				
			);

			Integer aStatus = BICaseInfo.ACTIVATE;			
			for (int i=0; i<aFiles.length; i++)
			{
				String aBIMLName = aFiles[i].getName();
				String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), aBIMLName.length()-8);						
				String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);				
				BICaseInfo aBICase = new BICaseInfo(aBIMLId, aType, null, aStatus, false, false, null);
				
				//aBIMLList.add(aFiles[i].getName());
				aBIMLList.add(aBICase);
			}

			aDir = new File(aDirname, "inprogress");
			aFiles = aDir.listFiles(new FilenameFilter()
				{
					public boolean accept(File dir, String name)
					{
						if (name.matches(".*xml") && !name.matches(".*template.*"))
							return true;
						else
							return false;
					}
				}
				
			);

			aStatus = BICaseInfo.INPROGRESS;
			for (int i=0; i<aFiles.length; i++)
			{
				String aBIMLName = aFiles[i].getName();
				String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), aBIMLName.length()-8);						
				String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);				

				BICaseInfo aBICase = new BICaseInfo(aBIMLId, aType, null, aStatus, false, false, null);
				
				//aBIMLList.add(aFiles[i].getName());
				aBIMLList.add(aBICase);
			}

			
				
		return aBIMLList;
	}

	public CaseInfoList getCaseInfoList()
	{
		BufferedReader aBufferedReader = null;
		CaseInfoList list = new CaseInfoList();
		
		String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
		File aDir = new File(aDirname, "activate");
		File [] aFiles = aDir.listFiles(new FilenameFilter()
			{
				public boolean accept(File dir, String name)
				{
					if (name.matches(".*xml") && !name.matches(".*template.*"))
						return true;
					else
						return false;
				}
			}
			
		);

		CaseInformation caseInfo = null;
		for (int i=0; i<aFiles.length; i++)
		{
			String aBIMLName = aFiles[i].getName();
			String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), aBIMLName.length()-8);						
			String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);				

			if (aType.equals("CML")) 
			{
				try
				{
					aBufferedReader = new BufferedReader(new FileReader(aFiles[i]));
					StringBuffer aFileContent = new StringBuffer();
					String aLine = null;
					while ((aLine = aBufferedReader.readLine()) != null)
					{
						aFileContent.append(aLine);
					}
					BICase aCase = CaseFactory.createCase(aBIMLId, aFileContent.toString());
					caseInfo = aCase.getCaseInfo();
					//caseInfo = new CaseInformation(aBIMLId,aFileContent.toString());
					list.setCaseInfo(caseInfo);
				}catch(FileNotFoundException e)
				{
				}catch(IOException e)
				{
				}finally
				{
					try
					{
						aBufferedReader.close();
					}catch(IOException e)
					{}
				}
			}
		}

		return list;
	}

	public SearchedCaseInfoList searchCase(SearchCriteria a_SearchCriteria, double a_Threshold)
	{
		BufferedReader aBufferedReader = null;
		SearchedCaseInfoList list = new SearchedCaseInfoList();
		list.setSearchCriteria(a_SearchCriteria);
		String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);
		File aDir = new File(aDirname, "activate");
		File [] aFiles = aDir.listFiles(new FilenameFilter()
			{
				public boolean accept(File dir, String name)
				{
					if (name.matches(".*xml") && !name.matches(".*template.*"))
						return true;
					else
						return false;
				}
			}
			
		);

		CaseInformation caseInfo = null;
		for (int i=0; i<aFiles.length; i++)
		{
			String aBIMLName = aFiles[i].getName();
			String aBIMLId = aBIMLName.substring(Constants.BIML_FILE_PREFIX.length(), aBIMLName.length()-8);						
			String aType = aBIMLName.substring(aBIMLName.length()-7, aBIMLName.length()-4);				

			if (aType.equals("CML")) 
			{
				try
				{
					aBufferedReader = new BufferedReader(new FileReader(aFiles[i]));
					StringBuffer aFileContent = new StringBuffer();
					String aLine = null;
					while ((aLine = aBufferedReader.readLine()) != null)
					{
						aFileContent.append(aLine);
					}						
					BICase aCase = CaseFactory.createCase(aBIMLId, aFileContent.toString());
					caseInfo = aCase.getCaseInfo();
					genScore(caseInfo, a_SearchCriteria);
					if (caseInfo.getScore()>=a_Threshold)
					{
						list.setCaseInfo(caseInfo);
					}
				}catch(FileNotFoundException e)
				{
				}catch(IOException e)
				{
				}finally
				{
					try
					{
						aBufferedReader.close();
					}catch(IOException e)
					{}
				}
			}
		}

		return list;
	}

	public void genScore(CaseInformation aCaseInfo, SearchCriteria aSearchCriteria)
	{
		double comparedItem = 0;
		double finalScore = 0;
		
		String source;
		String match;
		
		match = aSearchCriteria.getKeywords();
		//key search
		if(match!=null){
			match = match.toUpperCase().trim();
			source = aCaseInfo.getCaseName();
			source = source.toUpperCase().trim();
			
			finalScore = matchScore(source,match);
			aCaseInfo.setScore(finalScore);
			return;
		}
		
		//advanced search
		match = aSearchCriteria.getBusinessObjective();
		if (match!=null )
		{
			comparedItem++;
			match = match.toUpperCase().trim();
			
			source = aCaseInfo.getBusinessObjective();
			source = source.toUpperCase().trim();
			finalScore += matchScore(source,match);
		}
		match = aSearchCriteria.getIndustry();
		if (match!=null)
		{
			comparedItem++;
			match = match.toUpperCase().trim();
			
			source = aCaseInfo.getIndustry();
			source = source.toUpperCase().trim();
			finalScore += matchScore(source,match);
		}
		match = aSearchCriteria.getProblemType();
		if (match!=null)
		{
			comparedItem++;
			match = match.toUpperCase().trim();
			
			source = aCaseInfo.getProblemType();
			source = source.toUpperCase().trim();
			finalScore += matchScore(source,match);
		}
		match = aSearchCriteria.getDataMiningGoal();
		if (match!=null)
		{
			comparedItem++;
			match = match.toUpperCase().trim();
			
			source = aCaseInfo.getDataMiningGoal();
			source = source.toUpperCase().trim();
			finalScore += matchScore(source,match);
		}
		match = aSearchCriteria.getCompanyName();
		if (match!=null)
		{
			comparedItem++;
			match = match.toUpperCase().trim();
			
			source = aCaseInfo.getCompanyName();
			source = source.toUpperCase().trim();
			finalScore += matchScore(source,match);
		}
		match = aSearchCriteria.getDepartmentName();
		if (match!=null)
		{
			comparedItem++;
			match = match.toUpperCase().trim();
			
			source = aCaseInfo.getDepartmentName();
			source = source.toUpperCase().trim();
			finalScore += matchScore(source,match);
		}
		
		if(comparedItem>0){
			finalScore = finalScore/comparedItem * 100;
		}
		aCaseInfo.setScore(finalScore);
	}

	
	private double matchScore(String source, String matcher){
		if(source==null||matcher==null){
			return 0;
		}
		if(source.length()<matcher.length()){
			if(matcher.contains(source)){
				return (double)source.length()/2*matcher.length();
			}
			else{
				return 0;
			}
		}
		if(source.length()==matcher.length()){
			return source.equals(matcher)?1:0;
		}
		if(source.contains(matcher)){
			return (double)matcher.length()/source.length();
		}
		else{
			return 0;
		}
	}
	
/*	
 *  The below methods is remarked because these are replaced by the method getBIMLString
 *   because content in String is necessary for the JSP page to handle the XML content.
 * 	These methods can be deleted later.
	
	public Document getBIMLById(String aBimlId, String aType) throws BIMLException
	{
		String aBimlName = Constants.BIML_FILE_PREFIX + aBimlId + "_" +aType + ".xml";
		return getBIMLByName(aBimlName);		
	}

	public Document getBIMLByName(String aBimlName) throws BIMLException
	{
		try {
			String aDirname = SysConfig.getProperty(Constants.BIML_REPOSITORY_KEY);


			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			File aFile = new File(aDirname, aBimlName);
			log.debug("Parsing file: " + aFile.getName());
			Document aDocument = builder.parse(aFile);
			return aDocument;
				
		} catch (Exception e) {
			log.error("Error getting or parsing the BIML file with name: " + aBimlName + " Exception: " + e);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -