Search code examples
drools

How to get the value in the enumeration declared in the drl file in java


I started learning drools recently, when I declared an enum type in drl file, but in java, I don't know how to get the value of this enum, can anyone help me?

First: enums declared in drools file

declare enum OrderStatus
    CREATED(0, "新创建"),
    PAY(1, "已支付"),
    RECEIVED(2, "已接收");

    status: Integer;
    desc: String;
end

Second: I want to get the value of PAY in OrderStatus

// Get the declared fact type
FactType orderStatusFactType = kieBase.getFactType("rules", "OrderStatus");

I don't know how to write it after that, does anyone know?


Solution

  • Generally speaking, you don't do it like that.

    If you need it in Java, declare it in Java. You can reference Java enums by importing them as you would any other class.

    OrderStatus.java:

    package com.example;
    
    public enum OrderStatus {
      // ...
    }
    

    example.drl

    import com.example.OrderStatus
    
    rule "ExampleRule"
    when
      $order: Order( status == null )
    then
      modify($order) {
        setStatus( OrderStatus.PAY )
      }
    end
    

    Since it is already Java, you can import and reference it normally in Java.