如何将 drools 的输出发送到 java

how to send the output from drools to java

以下是我根据条件使用的流口水

import com.sample.TestExample;
import com.sample.TestExample.Message;

rule "Hello World"
when
    m : Message( product == Message.tv, myMessage : message )
then
    System.out.println(Message.tv);
    System.out.println("Product Spec is:: Service.TV_SAT"); // This output i want send back to java       
end

rule "GoodBye"
when
    n  : Message( product == Message.smart, myMessage : message )
then
    System.out.println(Message.smart);
    System.out.println("Product Spec is:: Service.SMARTOFFICE");  // This output i want send back to java   
end

rule "Else condition"
when
    e  : Message(product != TestExample.productClass)
then
    System.out.println(TestExample.productClass);
    System.out.println("in drools else condition"); // This output i want send back to java   
end 

我尝试使用 set 和 get 方法,但它不起作用。

任何帮助将不胜感激:-)

不确定 "send back to Java" 是什么意思。

您需要做什么取决于您希望将此 "output" 存储在何处。如果您在 Message 中有一个字段 messageText,那么您可以

 m.setMessageText( "your message" );

如果这不起作用,您可能没有声明此 setter,或者您可能犯了其他一些错误。 (我们需要查看 setter 的声明才能告诉您更多信息。)

您可以使用全局 List<String> 并将消息文本添加到此集合对象。请参阅文档如何使用全局变量。

你好我的代码在java代码

中进行了以下更改后可以正常工作

public 静态 class 消息 {

    private String message;

    public static String product;
    public static String tv="TV_SAT";
    public static String smart="SMART_OFFICE";

    public static String productSpec;

    public static int status;

    public String getProduct(){
        return Message.product;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setProduct(String product){
        Message.product=product;
    }

    public void setProductSpec(String product){

        Message.productSpec=product;
    }

    public String getProductSpec(){

        return
                Message.productSpec;

    }
}

最终的drl文件如下:

package com.sample

import com.sample.TestExample;
import com.sample.TestExample.Message;

global java.util.List statuses;

rule "Hello World"
    when
        m : Message( product == Message.tv, myMessage : message )
    then
        System.out.println("Product Class::"+Message.tv);
        m.setProductSpec("Service.TV_SAT");
end

rule "GoodBye"
    when
        n  : Message( product == Message.smart, myMessage : message )
    then
        System.out.println("Product Class::"+Message.smart);
        n.setProductSpec("Service.SmartOffice");
end

rule "Else condition"
    when
        e  : Message(product != TestExample.productClass)
    then
        System.out.println(TestExample.productClass);
        System.out.println("in drools else condition");
end