DRL无法解析动态加载的类

时间:2014-09-04 05:22:41

标签: java drools dynamic-class-loaders

我正在使用drools 6软件包来构建和运行我的应用程序的drl文件。它在静态类中工作正常。但是,当我在规则中使用动态类时,它会将错误抛出为

  

规则编译错误:[规则名称='规则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;
}

}

1 个答案:

答案 0 :(得分:0)

始终传递URL类加载器以创建知识库。因为drools将URL类加载器类加载到其工作内存中。

  public KnowledgeBase newKBase(URLClassLoader loader) {

    KnowledgeBuilderConfiguration kbuilderConfig =
        KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
    // Create the agent using the builder configuration
    KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
    KnowledgeBaseConfiguration kbaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConfig);
    KnowledgeAgent kagent =
        KnowledgeAgentFactory.newKnowledgeAgent(knowledgeAgentName, kbase, aconf, kbuilderConfig);
    kbase = kagent.getKnowledgeBase();
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConfig);

    addRules(kbuilder);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();

    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors)
            System.err.println(error);
        throw new IllegalArgumentException("Could not parse knowledge.");
    }

    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    return kbase;
}

上面的方法将加载规则而不会抛出错误。