📄 xelopesserviceimpl.java
字号:
/**
* Builds mining model by the service and returns the model as
* PMML string.
*
* @param algorithmName algorithm name
* @param parameters algorithm parameters
* @param sourceName name of data source
* @return PMML document with mining model as string
* @throws RemoteException cannot build model
*/
public String buildModelService(String algorithmName, ServiceAlgorithmParameter[] parameters,
String sourceName) throws RemoteException {
String pmmlModel = null;
try {
// Get Mining Agorithm Specification:
MiningAlgorithmSpecification mas = MiningAlgorithmSpecification.getMiningAlgorithmSpecification(algorithmName,null);
// Create mining algorithm:
String className = mas.getClassname();
if( className == null )
throw new RemoteException( "algorithm classname attribute expected." );
MiningAlgorithm algorithm = GeneralUtils.createMiningAlgorithmInstance(className);
// Split parameters by domain:
int npar = 0;
if (parameters != null) npar = parameters.length;
Vector<ServiceAlgorithmParameter> msvec = new Vector<ServiceAlgorithmParameter>();
Vector<ServiceAlgorithmParameter> masvec = new Vector<ServiceAlgorithmParameter>();
for (int i = 0; i < npar; i++) {
if (parameters[i].getDomain() == 0)
msvec.addElement(parameters[i]);
else
if (parameters[i].getDomain() == 1)
masvec.addElement(parameters[i]);
};
int nMsPar = msvec.size();
ServiceAlgorithmParameter[] msPar = new ServiceAlgorithmParameter[nMsPar];
for (int i = 0; i < nMsPar; i++)
msPar[i] = msvec.elementAt(i);
int nMasPar = masvec.size();
ServiceAlgorithmParameter[] masPar = new ServiceAlgorithmParameter[nMasPar];
for (int i = 0; i < nMasPar; i++)
masPar[i] = masvec.elementAt(i);
// Open data source and get metadata:
MiningInputStream inputData = getInputStreamFromDataSource(sourceName);
MiningDataSpecification metaData = inputData.getMetaData();
// Get mining settings:
MiningSettings settings = algorithm.createMiningSettings();
settings.setDataSpecification(metaData);
LookupService.buildMiningSettingsFromAlgorithmParam(settings, msPar);
settings.verifySettings();
// Get mining algorithm specification:
MiningAlgorithmParameter[] params = mas.getInputAttribute();
int nParams = 0;
if (params != null) nParams = params.length;
for (int i = 0; i < nMasPar; i++)
for (int j = 0; j < nParams; j++)
if ( params[j].getName().equals( masPar[i].getName() ) )
params[j].setValue( masPar[i].getValue() );
GeneralUtils.displayMiningAlgSpecParameters( mas );
// Put it all together:
algorithm.setMiningInputStream( inputData );
algorithm.setMiningSettings( settings );
algorithm.setMiningAlgorithmSpecification( mas );
algorithm.verify();
// Build the mining model:
MiningModel model = algorithm.buildModel();
// Write model to PMML:
StringWriter writer = new StringWriter();
model.writePmml(writer);
pmmlModel = writer.getBuffer().toString();
// Close input stream:
try {
inputData.close();
}
catch (Exception ex) {
};
}
catch(Exception ex) {
throw new RemoteException("Can't run algorithm:\n" + getExceptionInfo(ex));
};
return pmmlModel;
}
/**
* Applies mining model (given as PMML string) by the service and returns the
* array of double values from the application of the model to all vectors
* of the data source.
*
* @param pmmlModel mining model as PMML string
* @param sourceName name of data source where the model is applied
* @return values of model application to all data source vectors
* @throws RemoteException cannot apply model to data source
*/
public double[] applyModelFunctionService(String pmmlModel, String sourceName)
throws RemoteException {
double[] appVal = null;
try {
// Parse PMML string and get mining model:
StringReader reader = new StringReader(pmmlModel);
MiningModel model = LookupService.readPmmlModelFactory(reader);
reader = new StringReader(pmmlModel);
model.readPmml(reader);
MiningDataSpecification modelMetaData =
model.getMiningSettings().getDataSpecification();
// Open mining input stream of data source:
MiningInputStream inputData0 = getInputStreamFromDataSource(sourceName);
// Transform input data (dynamically) if required:
MiningInputStream inputData = inputData0;
if ( modelMetaData.isTransformed() )
inputData = new MiningFilterStream(inputData0, modelMetaData.getMiningTransformationActivity());
// Apply model to data source:
Vector<Double> av = new Vector<Double>();
while ( inputData.next() ) {
MiningVector mv = inputData.read();
double val = model.applyModelFunction(mv);
av.addElement( new Double(val) );
};
// Copy result to array:
int size = av.size();
appVal = new double[size];
for (int i = 0; i < size; i++)
appVal[i] = av.elementAt(i).doubleValue();
}
catch(Exception ex) {
throw new RemoteException("Can't apply model:\n" + getExceptionInfo(ex));
};
return appVal;
}
/**
* Creates mining input stream for a given data source.
*
* @param sourceName data source name
* @return mining input stream of data source
* @exception MiningException cannot open stream for data source
*/
private MiningInputStream getInputStreamFromDataSource(String sourceName)
throws MiningException {
// Read data sources from file:
try {
if (sourceNames == null) readDataSources("config/datasources.xml");
}
catch (Exception ex) {
throw new MiningException("can't read from file 'datasources.xml':\n" +
getExceptionInfo(ex));
};
// Open data source:
DataSource ds = source2desc.get(sourceName);
if (ds == null)
throw new MiningDataException("unknown data source name");
MiningInputStream inputData = null;
FileSource fs = ds.getFileSource();
if (fs != null) {
String path = fs.getPath();
if ( path.toLowerCase().indexOf(".arff") > 0 )
inputData = new MiningArffStream(path);
else
inputData = new MiningCsvStream(path, null);
};
DatabaseSource dbs = ds.getDatabaseSource();
if (dbs != null) {
MiningSqlSource sqlSource = new MiningSqlSource(
dbs.getUrl(), dbs.getUser(), dbs.getPassword(), dbs.getDriver());
inputData = new MiningSqlStream( sqlSource, dbs.getRequest() );
};
if (inputData == null)
throw new MiningDataException("can't access data source");
return inputData;
}
/**
* Returns detailed description of exception.
*
* @param ex exception to be explained
* @return ex explanation of exception
*/
protected String getExceptionInfo(Exception ex)
{
StringWriter writer = new StringWriter();
writer.write("Java exception: " + ex.getClass().getName());
String message = ex.getMessage();
if(message != null)
writer.write(" with message: " + message);
writer.write("\n");
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
return writer.toString();
}
/**
* Reads data sources from XML file.
*
* @param filename name of file containing data sources
* @throws RemoteException cannot read XML file
*/
private void readDataSources(String filename) throws RemoteException
{
FileReader reader;
try
{
reader = new FileReader(filename);
}
catch (FileNotFoundException ex)
{
throw new RemoteException("Can't find data source descriptions file");
}
DataSources sources = DataSources.unmarshal(reader);
if(sources == null)
{
throw new RemoteException("Can't load data source descriptions");
}
if(!sources.isValid())
{
throw new RemoteException("Not valid XML document with data source descriptions");
};
DataSource[] ds = sources.getDataSource();
source2desc = new Hashtable<String, DataSource>();
for( int i = 0; i < ds.length; i++)
{
source2desc.put( ds[i].getName(), ds[i] );
};
sourceNames = new String[ source2desc.size() ];
int iname = 0;
Enumeration<String> keys = source2desc.keys();
while ( keys.hasMoreElements() ) {
sourceNames[iname] = keys.nextElement();
iname = iname + 1;
};
try
{
reader.close();
}
catch (IOException ex)
{
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -