如何在Drools 6.5中从字符串加载规则 [英] How to load rules from a string in Drools 6.5

查看:98
本文介绍了如何在Drools 6.5中从字符串加载规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组字符串形式的规则,这些规则作为参数传递给我的函数,该函数未存储在任何文件中.从我到目前为止所读的内容来看,对于6之前的版本有很多解决方案,我猜测API会有很大的不同.(他们建议使用6.5中已弃用的KnowledgeBase)

I have a set of rules in the form of a String that are passed as an argument to my function, which is not stored in any file. From what I read so far, there are many solutions to this for versions before 6, where I am guessing APIs were very different. (They suggest using KnowledgeBase, which is deprecated in 6.5)

到目前为止,这是我的解决方案:

This is my solution so far:

KieFileSystem kfs = kService.newKieFileSystem();
Resource drlResource = ResourceFactory.newByteArrayResource(rules.getBytes());
drlResource.setResourceType(ResourceType.DRL);
kfs.write(drlResource);
KieBuilder builder = kService.newKieBuilder(kfs).buildAll();

但是当我跑步时,这会引发一个错误:

But when I run, this is throwing an error saying:

java.lang.RuntimeException: Resource does not have neither a source nor a target path. Impossible to add it to the bundle. Please set either the source or target name of the resource before adding it.ByteArrayResource[bytes=[105, 109, 112, 111, 114, 116, 32, 106, 97, 118, ...], encoding=null]
at org.drools.compiler.kie.builder.impl.KieFileSystemImpl.write(KieFileSystemImpl.java:95)

但是我这里没有源文件,如何将字符串转换为规则资源?

But I do not have a source file here, how can I convert a String into a rules resource?

我正在使用Drools 6.5.0.Final.

I am using Drools 6.5.0.Final.

推荐答案

您可以使用此

String drl = "...";
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write( "src/main/resources/simple.drl",
    kieServices.getResources().newReaderResource( new StringReader(drl) ) );
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();

在Drools API中就可以找到它-只需找到它即可.

It's right there in the Drools API - one just has to find it.

修改Javadoc有一个索引,您可以在其中查找方法和类型名称.接口是

Edit Javadoc has an index where you can look up method and type names. The interfaces are

org.kie.api.KieServices 
org.kie.api.builder.KieFileSystem
org.kie.api.io.KieResources
org.kie.api.io.Resource

Javadoc:"KieFileSystem是用于...的内存文件系统."要查看其工作原理,请查看源代码.

Javadoc: "The KieFileSystem is an in-memory file system used to ..." To see how it works, look at the source code.

这篇关于如何在Drools 6.5中从字符串加载规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆