检索插入到drools会话中的所有规则,并使用drools查询在KieSession中打印这些规则 [英] retrieving all rules inserted into drools session which are being printed in KieSession using drools query

查看:570
本文介绍了检索插入到drools会话中的所有规则,并使用drools查询在KieSession中打印这些规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在drools会话中插入了许多规则,并使用KieSession提供的事件监听器将它们打印出来。这是代码:

I am having a number of rules which are inserted in the drools session and I've got them printed using event listener provided by KieSession. Here is the code:

  kSession.addEventListener(new RuleRuntimeEventListener() {
        @Override
        public void objectInserted(ObjectInsertedEvent event) {
            System.out.println("==> " + event.getObject() + " inserted");
        }

        @Override
        public void objectUpdated(ObjectUpdatedEvent event) {
            System.out.println("==> " + event.getObject() + " updated");

        }

        @Override
        public void objectDeleted(ObjectDeletedEvent event) {
            System.out.println("==> " + event.getOldObject() + " deleted");
        }
    });

我想使用drools查询将所有规则打印在objectInserted中,而不是由objectDeleted方法触发。我没有找到一种方法。我不想使用Java,但想要Drools。我是流口水的新手,在互联网上找不到很多有关此的信息。任何帮助将非常感激。谢谢

I want to get all the rules printed in objectInserted and are not triggered by objectDeleted method using drools query. I am not finding a way to do that. I don't want to it using Java but Drools. I am new to drools and did not find much regarding this over the internet. Any help would be really appreciated. Thanks

我要做什么
我正在尝试编写一个查询,该查询返回给我使用以下方法在流口水会话中存在的所有值流口水查询。
是这样的:

What I am trying to do I am trying to write a query that returns me all the values which are present in a drool session using drools query. something like this:

query "Query all attack categories"
AttackCategory($category : value)
end

这是我已经在使用的查询我的系统。我希望生成此查询并仅通过单个查询获取所有(不同类的)对象。

this is a query i am already using in my system. I want this query to be generified and fetch all the objects(of different classes ) with only a single query.

我的KieBaseModel

     KieBaseModel kbaseModel = kmoduleModel
            .newKieBaseModel(DEFAULT_KIE_BASE_NAME)
            .setDefault(true)
            .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
            .setEventProcessingMode(EventProcessingOption.CLOUD);

更新到需求
我可以从droll中获取对象可以通过任何方式获得图像中定义的树结构。我也想在问题之间建立联系。就像,如果回答了问题1,之后又问了哪个问题,又问了第1级q1,那么之后又问了哪个问题。一些问题也会从树中删除。我需要图像中描述的树状连接。有什么办法可以从流口水中得到这样的树吗?谢谢

Update to the requirements I can get the Objects from the drolls but is there any way I can get a tree structure as defined in the image. I want to get a link between the questions as well. Like, if question 1 was answered, which question was asked after it, and if the level 1 q1 was asked, then which question was asked after that. Some of the questions will be deleted from the tree as well. I need the connected tree-like described in the image. Is there any way to get such a tree from drools? Thanks

推荐答案

在您的您可以使用DRL文件访问名为 drools 的变量。这是 KnowledgeHelper ,它使您可以访问有关规则,环境,工作内存等的大量信息。链接是指向 KnowledgeHelper 的源代码,因为文档仅讨论最常用的方法(通常用于获取规则名称之类的东西)。

In your DRL file(s) you have access to a variable called drools. This is an instance of the KnowledgeHelper class, which gives you access to a bunch of information about the rules, environment, working memory, etc. The link is to the source code for KnowledgeHelper, since the documentation only discusses the most commonly used methods (usually for getting stuff like the rule name.)

drools 变量中,可以访问 WorkingMemory 对象(链接到源代码),其中使您可以访问所有工作内存中的对象。您应该能够根据需要使用 iterateObjects iterateFactHandles 在工作内存中遍历数据。

From the drools variable, you can access the WorkingMemory object (link to source), which gives you access to all the objects in working memory. You should be able to use iterateObjects or iterateFactHandles to walk the data in working memory as needed.

很显然,我不熟悉您的特定用例,因此您需要更新任何代码以匹配您的用例。但是,假设我想让所有 AttackCategory 实例保留在WorkingMemory中,我可能会这样做:

Obviously I'm not familiar with your specific use case, so you'll need to update any code to match your use case. But let's say I want to get all AttackCategory instances remaining in WorkingMemory, I might do something like this:

drools.getWorkingMemory()
      .iterateObjects(new ObjectFilter() {
          @Override
          public boolean accept(Object object) {
            return object instanceOf AttackCategory;
          }
       ) // now you have an instance of Iterator<? extends AttackCategory>

您也可以用 iterateFactHandles 代替 iterateObjects 如果您希望遍历事实句柄。

You could also substitute iterateFactHandles for iterateObjects if you wish to iterate over the fact handles instead.

请注意,这些迭代器不是线程安全的。

Note that these iterators are not threadsafe.

您也可以在Java中执行此操作,并从DRL中调用这种实用程序方法,但是您表示希望在Drools本身中执行此操作。不幸的是,尽管流口水确实使您能够访问工作记忆和一般环境,但侦听器不在此范围内。 Drools只知道其工作记忆中的事实,而不知道它们是如何到达那里的。侦听器的工作方式是,它们自己插入 insert delete 处理程序,并在这些方法执行时触发。这些对象一旦插入,就与工作内存中的任何其他对象一样(也就是说,您无法区分它们是如何到达的。)

You could alternatively do this in Java and call such a utility method from the DRL, but you indicated you'd prefer to do this in Drools itself. Unfortunately while drools does give you access to working memory and the general environment, the listeners are outside of this. Drools is only aware of facts in its working memory, but not how they got there. The way listeners work is that they hook into the insert and delete handlers themselves and trigger when those methods do; the objects, once inserted, are just like any other object in working memory (which is to say, you can't differentiate between how they got there.)

这篇关于检索插入到drools会话中的所有规则,并使用drools查询在KieSession中打印这些规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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