如何删除有错误的规则

时间:2017-04-14 13:35:50

标签: drools

我有一个动态创建的规则列表。创建后立即检查是否有错误。但我无法找到一种方法来动态删除那些有错误的规则。

Table1[c(3, 6:9),]

Drools文件以进行验证

 public void validateRule(List<String> rules, Collection<AbstractFact> facts) {
        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem  kieFileSystem = kieServices.newKieFileSystem();

        int ruleIndex = 0;
        for (String rule : rules) {
            StringBuilder ruleName = new StringBuilder("src/main/resources/rule");
            ruleName.append(ruleIndex).append(".drl");
            kieFileSystem.write(ruleName.toString(), rule);
            ruleIndex ++;
        }
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem).buildAll();

        if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
            //Remove rules which are failing 

        }

        KieContainer container = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());

        KieSession ksession = container.newKieSession();

        for (AbstractFact fact : facts) { // Insert All the facts
            ksession.insert(fact);
        }
        ksession.fireAllRules();
    } 

2 个答案:

答案 0 :(得分:0)

将每个规则写入KieFileSystem文件try.drl并调用buildAll。跟踪备案规则。

删除try.drl。

在第二遍中,像现在一样写,跳过第一遍中失败的规则。

答案 1 :(得分:0)

首先编译包然后使用构建包加载知识包来解决问题。通过这种方式,KieContainer不会失败,只会忽略不正确的规则。

public Collection<KiePackage> createAllPackages(List<String> rules) {
        Collection<KiePackage> compiledPackages = new ArrayList<>();
        int ruleIndex = 0;
        for (String rule : rules) {
            StringBuilder ruleName = new StringBuilder("src/main/resources/rule");
            ruleName.append(ruleIndex).append(".drl");
            kieFileSystem.write(ruleName.toString(), rule);
            KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem).buildAll();

            if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
                List<Message> messages = kieBuilder.getResults().getMessages(Message.Level.ERROR);
                continue;
            }
            kieFileSystem.generateAndWritePomXML(kieServices.getRepository().getDefaultReleaseId());
            KieContainer container = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());

            KieBase kBase = container.getKieBase();
            compiledPackages.addAll(kBase.getKiePackages());
            ruleIndex ++;
        }
        return compiledPackages;
    }

    public void buildAllPackages(Collection<KiePackage> packages) {
        KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
        kieFileSystem.writeKModuleXML(kieModuleModel.toXML());
        kieFileSystem.generateAndWritePomXML(kieServices.getRepository().getDefaultReleaseId());

        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem).buildAll();
        KieContainer kContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        KnowledgeBaseImpl kBase = (KnowledgeBaseImpl)kContainer.getKieBase();

        Collection<KnowledgePackage> allPacksTrans = Collections2.transform(packages, new Function<KiePackage, KnowledgePackage>() {
            @Override
            public KnowledgePackage apply(KiePackage kiePackage) {
                return (KnowledgePackage) kiePackage;
            }
        });

        kBase.addKnowledgePackages(allPacksTrans);
    }