📄 miningfilestream.java
字号:
/*
* 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.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
* @version 1.0
*/
package com.prudsys.pdm.Input;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import org.omg.cwm.analysis.transformation.ClassifierMap;
import org.omg.cwm.analysis.transformation.FeatureMap;
import org.omg.cwm.analysis.transformation.TransformationMap;
import org.omg.cwm.analysis.transformation.TransformationPackage;
import org.omg.cwm.objectmodel.core.CorePackage;
import org.omg.cwm.objectmodel.core.DataType;
import org.omg.cwm.resource.record.Field;
import org.omg.cwm.resource.record.RecordDef;
import org.omg.cwm.resource.record.RecordFile;
import org.omg.cwm.resource.record.RecordPackage;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
/**
* Mining input stream class for files.
*
* For special file formats this class should be
* extended.
*/
public class MiningFileStream extends MiningInputStream
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** File name. */
protected String fileName;
/** File reader object. */
protected Reader reader;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public MiningFileStream()
{
}
/**
* Mining file stream for a given file and given meta data.
*
* @param file path of file to access
* @param metaData meta data of file data
*/
public MiningFileStream( String file, MiningDataSpecification metaData ) throws MiningException
{
try
{
reader = new BufferedReader( new FileReader( file ) );
}
catch( IOException ex)
{
throw new MiningException( "Can't read from the file: "+file );
}
this.fileName = file;
this.metaData = metaData;
}
/**
* Mining file stream for a given file.
* The meta data is automatically determined using the
* method createMetaData.
*
* @param file path of file to access
*/
public MiningFileStream( String file ) throws MiningException
{
this( file, null );
metaData = recognize();
reset();
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns supported stream methods.
*
* @return supported stream methods
*/
public Enumeration getSupportedStreamMethods() {
Vector<String> suppmeth = new Vector<String>();
suppmeth.addElement("reset");
return suppmeth.elements();
}
/**
* Finds physical file model (CWM Resource Package "Record").
*
* @exception MiningException couldn't obtain physical model
*/
public void findPhysicalModel() throws MiningException {
com.prudsys.pdm.Cwm.CWMCompletePackage cwmFactory =
com.prudsys.pdm.Cwm.CWMCompletePackage.getCWMCompletePackage();
CorePackage cpg = cwmFactory.getCore();
RecordPackage rpg = cwmFactory.getRecord();
RecordFile rfile = rpg.getRecordFile().createRecordFile();
rfile.setName(fileName);
RecordDef recdef = rpg.getRecordDef().createRecordDef();
rfile.addRecord(recdef);
for (int i = 0; i < metaData.getAttributesNumber(); i++) {
Field field = rpg.getField().createField();
MiningAttribute ma = metaData.getMiningAttribute(i);
field.setName( ma.getName() );
DataType dataType = cpg.getDataType().createDataType();
String dtname = "unknownType";
if (ma instanceof NumericAttribute) {
if (ma.getDataType() == NumericAttribute.DOUBLE) dtname = "double";
else if (ma.getDataType() == NumericAttribute.FLOAT) dtname = "float";
else if (ma.getDataType() == NumericAttribute.INTEGER) dtname = "integer";
else if (ma.getDataType() == NumericAttribute.BOOLEAN) dtname = "boolean";
else if (ma.getDataType() == NumericAttribute.DATETIME_PRUDSYS) dtname = "datePrudsys";
else dtname = "numeric";
}
else {
if (ma.getDataType() == CategoricalAttribute.STRING) dtname = "string";
else if (ma.getDataType() == CategoricalAttribute.BOOLEAN) dtname = "boolean";
else dtname = "categorical";
};
dataType.setName(dtname);
field.setType(dataType);
recdef.addFeature(field);
}
physicalModel = rfile;
}
/**
* Returns the CWM mapping from the physical to the logical data model.
*
* @return transformation of physical to logical data model
* @throws MiningException couldn't get transformation
*/
public org.omg.cwm.analysis.transformation.TransformationMap getPhysicalToLogicalModelTransformation()
throws MiningException {
com.prudsys.pdm.Cwm.CWMCompletePackage cwmFactory =
com.prudsys.pdm.Cwm.CWMCompletePackage.getCWMCompletePackage();
TransformationPackage tpg = cwmFactory.getTransformation();
TransformationMap tm = tpg.getTransformationMap().createTransformationMap();
ClassifierMap cm = tpg.getClassifierMap().createClassifierMap();
tm.addOwnedElement(cm);
RecordFile rfile = (RecordFile) getPhysicalModel();
Iterator it = rfile.getRecord().iterator();
RecordDef recdef = (RecordDef) it.next();
it = recdef.getFeature().iterator();
for (int i = 0; i < metaData.getAttributesNumber(); i++) {
FeatureMap fm = tpg.getFeatureMap().createFeatureMap();
fm.addSource( (Field) it.next() );
fm.addTarget( metaData.getMiningAttribute(i) );
cm.addFeatureMap(fm);
};
metaData.addOwnedElement(tm);
physicalModel.addOwnedElement(tm);
return tm;
}
/**
* Returns file name.
*
* @return file name
*/
public String getFileName()
{
return fileName;
}
// -----------------------------------------------------------------------
// General stream methods
// -----------------------------------------------------------------------
/**
* Open mining file stream. This method can be left.
*
* @exception MiningException if a mining source access error occurs
*/
public void open() throws MiningException
{
}
/**
* Close mining file stream by closing reader.
*
* @exception MiningException if a mining source access error occurs
*/
public void close() throws MiningException
{
try
{
reader.close();
}
catch( IOException ex)
{
throw new MiningException( "Can't close reader from the file: "+fileName );
};
}
/**
* Recognize the input stream data specification. Not implemented.
*
* @return the MiningDataSpecification
* @exception MiningException always thrown
*/
public MiningDataSpecification recognize() throws MiningException
{
throw new MiningException( "Not implemented." );
}
// -----------------------------------------------------------------------
// Methods of cursor positioning
// -----------------------------------------------------------------------
/**
* Places the cursor before first row.
* This is done by closing and reopening the file reader.
*/
public void reset() throws MiningException
{
try
{
if( reader!= null) reader.close();
reader = new FileReader( fileName );
}
catch( IOException ex )
{
throw new MiningException( ex.getMessage() );
}
}
/**
* Advance cursor by one position. Not implemented.
*
* @return true if next vector exists, else false
* @MiningException always thrown
*/
public boolean next() throws MiningException
{
throw new MiningException( "Not implemented." );
}
/**
* Move cursor to given position. Not supported.
*
* @param position new cursor position
* @return true if possible, else false
* @MiningException always thrown
*/
public boolean move( int position ) throws MiningException
{
throw new MiningException( "Can't move cursor in the file source." );
}
// -----------------------------------------------------------------------
// Methods of reading from the stream
// -----------------------------------------------------------------------
/**
* Reads current data vector. Not implemented.
*
* @return data vector at current cursor position
* @exception MiningException always thrown
*/
public MiningVector read() throws MiningException
{
throw new MiningException( "Not implemented." );
}
// -----------------------------------------------------------------------
// Other methods
// -----------------------------------------------------------------------
/**
* Returns string representation of mining file stream
* (just the file name).
*
* @return string representation of mining file stream
*/
public String toString()
{
return fileName + "/n" + super.toString();
}
/**
* Returns HTML string representation of mining file
* stream.
*
* @return HTML string representation of mining file stream
*/
public String toHtmlString()
{
String description = "<html>";
// String path = "";
// String token = "";
// StringTokenizer st = new StringTokenizer( fileName, separator, false );
// while( st.hasMoreTokens() )
// {
// token = st.nextToken();
// path = path + token + separator;
// description = description + "<a href=http://this?" + path + ">" + token + separator + "</a>";
// }
// int d = description.lastIndexOf( separator + "</a>" );
// description = description.substring( 0, d ) + "</a>";
description = description + "Data source:<br>" +
"<a href=http:this?" + fileName + ">" + fileName + "</a>";
description = description + "</html>";
return description;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -