简体   繁体   English

我如何在Drools 7中实现Drools 5示例

[英]How can I implement a Drools 5 example in Drools 7

I am new to Drools and I came across some Drools 5 examples which I intend to implement in Drools 7. However some classes which existed in Drools 5 are no longer available in Drools 7 for example DroolsParserException, PackageBuilder etc. I was wondering what their replacements are in Drools 7. I assume there may be KIE methods that correspond to these. 我是Drools的新手,遇到了一些打算在Drools 7中实现的Drools 5示例。但是,Drools 7中不再提供Drools 5中存在的某些类,例如DroolsParserException,PackageBuilder等。我想知道它们的替代品是什么在Drools 7中。我认为可能存在与之相对应的KIE方法。 This is the piece of code I am working on 这是我正在处理的代码

public void executeDrools() throws DroolsParserException, IOException 
{
   PackageBuilder packbuild = new PackageBuilder();

   String ruleFile = "/com/rules/manufacturerRules.drl";

   InputStream inStream = getClass().getResourceAsStream(ruleFile);

   Reader reader = new InputStreamReader(inStream);

   packageBuilder.addPackageFromDrl(reader);

   Package rulesPackage = packbuild.getPackage();

   RuleBase ruleBase = RuleBaseFactory.newRuleBase();

   ruleBase.addPackage(rulesPackage);

   WorkingMemory inmemory = ruleBase.newStatefulSession();

   Garment manufactuer = new Garment();

   manufactuer.setQuota(10000);

   manufactuer.setExpectation(45000);

   manufactuer.setTimeline(10);

   inmemory.insert(manufactuer);

   inmemory.fireAllRules();

}

Sincerely appreciate some guidance on how to implement this using Drools 7. Thank you. 衷心感谢您使用Drools 7实施此操作的一些指导。谢谢。

I finally accomplished my goal and I was able to successfully write the above piece of code in Drools 7. So to make it easy for anyone who is learning Drools 7 I cooked up a different example using some Java classes(ie Person and Insurance) which I already had. 我终于实现了我的目标,并且能够在Drools 7中成功编写以上代码。因此,为了使学习Drools 7的任何人都容易实现,我使用一些Java类(例如Person和Insurance)编写了一个不同的示例,我已经吃过了 So for anyone using Drools this is the code you need to implement, everything except the runTheRule() and the ruleencapsulate() methods are generic you will have to use them in your code. 因此,对于使用Drools的任何人来说,这都是您需要实现的代码,除了runTheRule()和Ruleencapsulate()方法之外的所有方法都是通用的,您将必须在代码中使用它们。

import java.io.IOException;
import org.drools.compiler.kie.builder.impl.InternalKieModule;
import org.drools.compiler.kie.builder.impl.KieContainerImpl;
import org.drools.compiler.kie.builder.impl.KieModuleKieProject;
import org.drools.compiler.kie.builder.impl.KieProject;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;

import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;


public class ruleClass {

     private static final String RULES_PATH = "C:/MyDrools_files/";
     private static final String RULES_FILE="DroolsLogic.drl";
     public ruleClass()
     {

     }
     public void runTheRule()
        {
            Person p=new Person();
            p.setTime(15);
            p.setName("Ulugh Khan");
            Insurance i=new Insurance();
            ruleencapsulate(p, i);

            System.out.println("--The Following is the output---");
            System.out.println(p.getGreet()+" "+i.getMessage());
        }
        private void ruleencapsulate(Person p, Insurance i)
        {
            try {
                KieSession chamSession=chkieSession();
                chamSession.insert(p);
                chamSession.setGlobal("insure", i);
                chamSession.fireAllRules();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

        }
        private KieFileSystem chkieFileSystem() throws IOException 
        {
            KieFileSystem kieFileSystem = getchKieServices().newKieFileSystem();            
           try {
                for (Resource file : getchRuleFiles()) 
                {
                    org.kie.api.io.Resource theResource=ResourceFactory.newFileResource(file.getFile());        
                    kieFileSystem.write(theResource);                 
                }
           }
           catch(Exception ex)
           {
               ex.printStackTrace();
           }
                return kieFileSystem;
        }

        private KieContainer chkieContainer() throws IOException  
        {
           final KieRepository kieRepository = getchKieServices().getRepository();

            kieRepository.addKieModule(new KieModule() {
                public ReleaseId getReleaseId() 
                {
                    return kieRepository.getDefaultReleaseId();
                }
            });

            KieBuilder kieBuilder = getchKieServices().newKieBuilder(chkieFileSystem());
            kieBuilder.buildAll();

            ReleaseId release=kieRepository.getDefaultReleaseId();
            KieProject kieProject = new KieModuleKieProject((InternalKieModule) kieBuilder.getKieModule());
            KieContainer contains=new KieContainerImpl(kieProject, KieServices.Factory.get().getRepository(), release);
           return contains;   
        } 

        private KieSession chkieSession() throws IOException 
        {
            return chkieContainer().newKieSession();
        }

          private KieServices getchKieServices() 
          {
                return KieServices.Factory.get();
          }
          private Resource[] getchRuleFiles() throws IOException 
          {
             ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
             Resource[] resc=resourcePatternResolver.getResources("file:"+RULES_PATH+RULES_FILE);

             return resc; 
          }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM