DRL not resolve dynamically loaded class

I am using drools 6 package to build and run drl files for my application. it’s working fine with static classes.but,when i use dynamic classes in rules it throwing error as

Rule Compilation error : [Rule name=’rule 1′] com/sample/Rule_rule_11982550347.java (2:28) : Only a type can be imported. com.chainsys.csapp.dynaclass.vo.SupplierdivVO resolves to a package

even though i’ve loaded my class loader in knowledge Base. following is my code.

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.samplenn");
    ruleContent.append("import com.chainsys.csapp.dynaclass.vo.SupplierdivVO;nn");
    ruleContent.append("import com.echain.vo.Messagenn");
    ruleContent.append("global java.io.PrintStream out nn");
    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("endn");
    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;
}

}

Answer

Always Pass the URL class loader for creating knowledge base. because drools load the URL class loader classes to its working memory.

  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;
}

Above method will load the rules without throwing error.

Leave a Reply

Your email address will not be published. Required fields are marked *