断点

每天进步一点点!
posts - 174, comments - 56, trackbacks - 0, articles - 21

Drools读取Xsl文件

Posted on 2010-03-10 22:29 断点 阅读(1261) 评论(0)  编辑  收藏 所属分类: Drools

package com.premium.rules;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.drools.rule.Package;

public class DrlPackageManager {

 // 用于缓存Drools的Package,key为Excel的完整路径,value为此文件编译过后的Package
 static Map<String, Package> pkgMap = new HashMap<String, Package>();
 private static Logger logger = Logger.getLogger(DrlPackageManager.class);


/**
  * 通过Excel的文件名缓存Drools的Package
  *
  * @param fileName
  * @return
  * @throws Exception
  */
 public static Package getPackageByXsl(String fileName) throws Exception {
  Package pkg = (Package) pkgMap.get(fileName);
  if (pkg != null)
   return pkg;

  final SpreadsheetCompiler converter = new SpreadsheetCompiler();
  InputStream is = null;
  try {
   is = DrlPackageManager.class.getResourceAsStream(fileName);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   is.close();
   throw new Exception("====读取规则的资源文件" + fileName + "出错,请检查文件"+ fileName + "=====", e);
  }
  String drl = null;
  try {
   drl = converter.compile(is, InputType.XLS);
   logger.debug("Drools Excel规则文件:" + fileName + "编译成.drl文件的结果:"+ drl);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   is.close();
   throw new Exception("xls文件编译成drl文件出错", e);
  }

  PackageBuilder builder = new PackageBuilder();
  StringReader srd = new StringReader(drl);
  builder.addPackageFromDrl(srd);
  pkg = builder.getPackage();
  pkgMap.put(fileName, pkg);
  is.close();
  return pkg;
 }

}

只有注册用户登录后才能发表评论。


网站导航: