📄 pmmlutils.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.Utils;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import com.prudsys.pdm.Adapters.PmmlVersion20.Application;
import com.prudsys.pdm.Adapters.PmmlVersion20.Header;
import com.prudsys.pdm.Adapters.PmmlVersion20.PMML;
import com.prudsys.pdm.Adapters.PmmlVersion20.Timestamp;
import com.prudsys.pdm.Core.MiningDataException;
import com.prudsys.pdm.Core.MiningException;
/**
* Collection of useful utilities, maily for PMML handling.
*/
public class PmmlUtils
{
/** Constant defining default encoding string. */
public static final String DEFAULT_ENCODING = "UTF-8";
/** Default encoding string. */
public static String codeString = DEFAULT_ENCODING;
/**
* Returns header for PMML documents.
*
* @return header for PMML documents
* @exception MiningException could not create PMML header
*/
public static Header getHeader() throws MiningException
{
return getHeader("Xelopes", "1.2.3");
}
/**
* Returns header for PMML documents with given application name
* and version.
*
* @param applicationName name of application
* @param applicationVersion version of application
* @return header for PMML documents
* @exception MiningException could not create PMML header
*/
public static Header getHeader(String applicationName, String applicationVersion) throws MiningException
{
Header header = new Header();
header.setCopyright( "Copyright (c) 2004 prudsys AG" );
header.setDescription( "Xelopes mining model. See www.zsoft.ru or www.prudsys.com" );
Application application = new Application();
application.setName( applicationName );
application.setVersion( applicationVersion );
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat ( "yyyy-MM-dd hh:mm:ss z" );
Timestamp timestamp = new Timestamp( formatter.format( new Date() ) );
header.setApplication( application );
header.setTimestamp( timestamp );
return header;
}
/**
* Returns default encoding string for all PMML document.
*
* @return String default encoding string
*/
public static String getCodeString()
{
return codeString;
}
/**
* Sets default encoding string for all PMML documents.
*
* @param codeStr String default encoding string
*/
public static void setCodeString(String codeStr)
{
codeString = codeStr;
}
/**
* Sets encoding for PMML documents.
*
* @throws MiningDataException can't set encoding
*/
public static void setEncoding(String codeString) throws MiningDataException
{
try
{
com.borland.xml.toolkit.XmlUtil.setEncoding(codeString);
}
catch ( Exception e)
{
throw new MiningDataException( "Can't set encoding: " + e.getMessage() );
}
}
/**
* Setss encoding for PMML documents.
*
* @throws MiningDataException can't add encoding
*/
public static void setEncoding() throws MiningDataException
{
setEncoding(codeString);
}
/**
* Writes PMML document.
*
* @param writer Writer PMML writer
* @param pmml PMML PMML document
* @throws MiningDataException can't write PMML to file
*/
public static void marshalPmml(Writer writer, PMML pmml) throws MiningDataException
{
try
{
pmml.marshal( writer );
}
catch( IOException ex )
{
throw new MiningDataException( "Can't write PMML to file." );
}
}
/**
* Parses PMML header and returns application name and version.
* If name not found, null is returned. If no version contained,
* version "-1" is returned.
*
* @param header PMML header to parse
* @return array with first value - application name, second value -
* application version, null if not found
* @throws MiningException coul not parse PMML header
*/
public static String[] parseHeader(Header header) throws MiningException
{
if (header == null) return null;
Application application = header.getApplication();
if (application == null) return null;
String[] app = new String[2];
String name = application.getName();
if (name == null) return null;
app[0] = name;
String version = application.getVersion();
if (version == null) app[1] = "-1";
app[1] = version;
return app;
}
/**
* Open IE to show PMML model.
*
* @param pmmlFile PMML file to display
*/
public static void openPmmlBrowser(String pmmlFile) {
try {
// Show in explorer:
String explorer = "explorer";
String path = "data\\pmml\\";
String file = pmmlFile;
Runtime.getRuntime().exec(explorer + " " + path + file);
}
catch (Exception ex) {
System.err.println("Xelopes cannot display PMML representation");
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -