1.读取csv参数进行批量预测
public static void main(String[] args) throws IOException {
PmmlInvoker invoker = new PmmlInvoker(“E:/jar/gaea/SASEM13_2_iris_DecTree.xml”);
List<Map<FieldName, String>> paramList = readInParams(“E:/jar/gaea/IrisScoreDecTree.csv”);
int lineNum = 0; //当前处理行数
File file = new File(“E:/jar/gaea/result.txt”);
for(Map<FieldName, String> param : paramList){
lineNum++;
Files.append(“当前行: " + lineNum + "=”,file, Charset.forName(utf8));
Map<FieldName, ?> result = invoker.invoke(param);
Set keySet = result.keySet(); //获取结果的keySet
for(FieldName fn : keySet){
if (fn.toString().equals(“species”)) {
System.out.println(result.get(fn).toString());
}
String tempString = result.get(fn).toString()+"\n";
Files.append(tempString,file,Charset.forName(utf8));
}
}
}
public static List<Map<FieldName,String>> readInParams(String filePath) throws IOException{
InputStream is;
is = PmmlCalc.class.getClassLoader().getResourceAsStream(filePath);
if(is==null){
is = new FileInputStream(filePath);
}
InputStreamReader isreader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isreader);
String[] nameArr = br.readLine().split(","); //读取表头的名字
ArrayList<Map<FieldName,String>> list = new ArrayList<>();
String paramLine; //一行参数
//循环读取 每次读取一行数据
while((paramLine = br.readLine()) != null){
Map<FieldName,String> map = new HashMap<>();
String[] paramLineArr = paramLine.split(",");
for(int i=0; i<paramLineArr.length; i++){//一次循环处理一行数据
map.put(new FieldName(nameArr[i]), paramLineArr[i]); //将表头和值组成map 加入list中
}
list.add(map);
}
is.close();
return list;
}
}
public class PmmlInvoker {
private ModelEvaluator modelEvaluator;
// 通过文件读取模型
public PmmlInvoker(String pmmlFileName) {
PMML pmml = null;
InputStream is = null;
try {
if (pmmlFileName != null) {
is = PmmlInvoker.class.getClassLoader().getResourceAsStream(pmmlFileName);
if(is==null){
is = new FileInputStream(pmmlFileName);
}
pmml = PMMLUtil.unmarshal(is);
}
this.modelEvaluator = ModelEvaluatorFactory.newInstance().newModelEvaluator(pmml);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(is!=null)
is.close();
} catch (Exception localIOException3) {
localIOException3.printStackTrace();
}
}
this.modelEvaluator.verify();
System.out.println(“模型读取成功”);
}
// 通过输入流读取模型
public PmmlInvoker(InputStream is) {
PMML pmml;
try {
pmml = PMMLUtil.unmarshal(is);
try {
is.close();
} catch (IOException localIOException) {
}
this.modelEvaluator = ModelEvaluatorFactory.newInstance().newModelEvaluator(pmml);
} catch (Exception e) {
pmml = null;
} finally {
try {
is.close();
} catch (IOException localIOException3) {
}
}
this.modelEvaluator.verify();
}
public Map<FieldName, String> invoke(Map<FieldName, String> paramsMap) {
return this.modelEvaluator.evaluate(paramsMap);
}
}
2.公司项目引用
2.1 模型预测以及结果返回
public static String pmmlModel(Map<FieldName, String> map, String fileName, String key) throws IOException {
PmmlInvoker invoker = new PmmlInvoker(fileName);
Map<FieldName, ?> result = invoker.invoke(map);
Set keySet = result.keySet(); //获取结果的keySet
String value = null;
for(FieldName fn : keySet){
if (fn.toString().equals(key)) {
value = result.get(fn).toString();
}
}
return value;
}
2.2 获取前台传过来的数据模型参数和key值
Map<FieldName, String> map = new HashMap<>();
for (String str : preData.keySet()) {
FieldName fieldName = new FieldName(str);
map.put(fieldName, String.valueOf(preData.get(str)) /“1”/);
}
try {
String result = PmmlCalc.pmmlModel(map, jarName, resultKey);
System.out.println(result.trim());
return result.trim();
} catch (IOException e) {
e.printStackTrace();
}
2.3 获取路径和名称从本地获取pmml文件
String path = SpringBeanHolder.getBean(AiModelServiceImpl.class).path();
if (!path.substring(path.length()-1, path.length()).equals("//")
|| !path.substring(path.length()-1, path.length()).equals("/")) {
path = path + “//”;
}
System.out.println(path);
pkg.setExpression(
String.format(
“
A
i
M
o
d
e
l
H
a
n
d
l
e
r
.
h
a
n
d
l
e
(
AiModelHandler.handle(
AiModelHandler.handle(Fact_%s, “%s”, “%s”)”,
fact.getId(),
path + model.getJarName(),
value.getJSONObject(“resultKey”) != null ? value.getJSONObject(“resultKey”).getString(“data”) : “”
)
);
3.遇到的问题及解决办法
获取不到配置文件中的动态路径,把类添加到spring容器才可以获取
SpringBeanHolder.getBean(AiModelServiceImpl.class).path();通过这句获得配置文件中的动态路径