检索所有通过Drools查询在KieSession中打印的插入到Drools会话中的规则。

huangapple go评论68阅读模式
英文:

retrieving all rules inserted into drools session which are being printed in KieSession using drools query

问题

我有许多规则,这些规则被插入到 drools 会话中,并且我已经使用 KieSession 提供的事件监听器将它们打印出来。以下是代码部分:

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

我想要在 objectInserted 方法中获取所有已插入的规则,并且这些规则不能由 objectDeleted 方法触发。我想使用 drools 查询来实现这一点。我不想使用 Java,而是使用 Drools。我对 Drools 还不太熟悉,在互联网上没有找到太多相关信息。对于任何帮助,我将不胜感激。谢谢。

我的尝试目标
我试图编写一个查询,使用 Drools 查询从 drool 会话中返回所有存在的值。
类似于这样的查询:

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

这是我已经在系统中使用的查询。我希望这个查询能够通用化,并且只使用一个查询来获取所有不同类别的对象。

我的 KieBaseModel

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

对需求的更新
我可以从 Drools 中获取对象,但是否有办法以图像中定义的树结构获取数据?我还想获取问题之间的链接。例如,如果回答了问题 1,之后问了哪个问题;如果问了一级问题 1,之后又问了哪个问题。其中一些问题也会从树中删除。我需要获取与图像中描述的类似树状结构的连接。是否有办法从 Drools 中获取这样的树结构?谢谢。

(图片已省略)

英文:

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

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.

My KieBaseModel

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

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

检索所有通过Drools查询在KieSession中打印的插入到Drools会话中的规则。

答案1

得分: -1

在您的DRL文件中,您可以访问名为drools的变量。这是KnowledgeHelper的一个实例,它为您提供了有关规则、环境、工作内存等的许多信息。链接是指向KnowledgeHelper的源代码,因为文档仅讨论了最常用的方法(通常用于获取诸如规则名称之类的内容)。

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

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

drools.getWorkingMemory()
      .iterateObjects(new ObjectFilter() {
          @Override
          public boolean accept(Object object) {
            return object instanceOf AttackCategory;
          }
       }) // 现在您有一个Iterator<? extends AttackCategory>的实例

如果希望迭代事实句柄,您也可以将iterateFactHandles替换为iterateObjects

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

您还可以在Java中执行此操作,并从DRL中调用此类实用方法,但您表示您更愿意在Drools本身中执行此操作。不幸的是,虽然Drools确实使您能够访问工作内存和一般环境,但监听器位于此之外。Drools只知道其工作内存中的事实,但不知道它们是如何到达那里的。监听器的工作方式是它们连接到insertdelete处理程序,并在这些方法执行时触发;一旦插入,这些对象就像工作内存中的任何其他对象一样(也就是说,您无法区分它们是如何到达那里的)。

英文:

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.)

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.

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>

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

Note that these iterators are not threadsafe.

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.)

huangapple
  • 本文由 发表于 2020年9月8日 09:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63785910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定