我正在使用 drools 6 包为我的应用程序构建和运行 drl 文件。它与静态类一起工作正常。但是,当我在规则中使用动态类时,它会抛出错误
规则编译错误:[Rule name='rule 1'] com/sample/Rule_rule_11982550347.java (2:28):只能导入一个类型。com.chainsys.csapp.dynaclass.vo.SupplierdivVO 解析为一个包
即使我已经在知识库中加载了我的类加载器。以下是我的代码。
package com.echain.handler;
import java.sql.SQLException;
import java.util.List;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderConfiguration;
import org.kie.internal.builder.KnowledgeBuilderError;
import org.kie.internal.builder.KnowledgeBuilderErrors;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import com.chainsys.dynui.handler.DynamicFormHandler;
import com.chainsys.dynui.vo.PageEntityVO;
import com.chainsys.dynui.vo.PageInfoVO;
import com.chainsys.fwk.exception.BaseFrameworkException;
import com.chainsys.fwk.util.ChainsysReflection;
import com.chainsys.fwk.util.DynamicClassLoader;
import com.echain.vo.Message;
public class Testing {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Testing test = new Testing();
test.fire();
}
@SuppressWarnings("deprecation")
private void fire() throws Exception {
DynamicClassLoader loader = getClassLoader("Supplier");
KnowledgeBase kBase = readKnowledgeBase(loader);
KieSession kSession = kBase.newKieSession();
KieServices kService = KieServices.Factory.get();
kSession.setGlobal("out", System.out);
kSession.insert(createMessage(kService.getKieClasspathContainer(loader)));
kSession.insert(new Message("Pravin", "Hi"));
kSession.fireAllRules();
kSession.dispose();
}
public KnowledgeBase readKnowledgeBase(ClassLoader loader) throws Exception {
KnowledgeBuilderConfiguration kBuilderConfiguration =
KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
System.out.println(kBuilderConfiguration);
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kBuilderConfiguration);
KieBaseConfiguration kbaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader);
kbuilder.add(ResourceFactory.newByteArrayResource(getRule().getBytes()), ResourceType.DRL);
// kbuilder.add(ResourceFactory.newFileResource("./rulefiles/testing.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors)
System.err.println(error);
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConfig);
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
private String getRule() {
StringBuffer ruleContent = new StringBuffer();
ruleContent.append("package com.sample\n\n");
ruleContent.append("import com.chainsys.csapp.dynaclass.vo.SupplierdivVO;\n\n");
ruleContent.append("import com.echain.vo.Message\n\n");
ruleContent.append("global java.io.PrintStream out \n\n");
ruleContent.append("rule \"rule 1\" when \n");
ruleContent.append(" m : Message( ) \n");// SupplierdivVO(test1==\"welc\")
ruleContent.append("then \n");
ruleContent.append("out.println(\"hello\");");
ruleContent.append("end\n");
return ruleContent.toString();
}
private Object createMessage(KieContainer kContainer) {
Object o = null;
try {
Class<?> cl = kContainer.getClassLoader().loadClass("com.chainsys.csapp.dynaclass.vo.SupplierdivVO");
o = cl.newInstance();
ChainsysReflection.setProperty(o, "test1", "welc");
} catch (Exception e) {
e.printStackTrace();
}
return o;
}
public DynamicClassLoader getClassLoader(String pageName) throws BaseFrameworkException, SQLException,
ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
DynamicFormHandler handler = new DynamicFormHandler();
PageInfoVO infoVO = handler.getPageInfoVO(pageName, 1);
List<PageEntityVO> eVO = infoVO.getPageEntityList();
DynamicClassLoader classLoader = new DynamicClassLoader(eVO);
return classLoader;
}
}