0

I am trying to define the conditions of my Drools rules in configuration (as a JSON) so that they can be easily parsed. For example, one of my Drools rules currently looks like this:

    rule "Rule 1"
        salience 100
        activation-group "xxx"
    when
        key1( key1() == "value1" )
        key2( key2() == "value2" )
        key3( List.of("value3a", "value3b").contains(key3()) )
    then
        ...

Instead of this I want to define the condition for each rule in a JSON file. Example:

{
   "key1": ["value1"],
   "key2": ["value2"],
   "key3": ["value3a", "value3b"],
   "returnValue": "Rule1Result"
}

I would want to be able to iterate through the objects in the JSON file and match the request against each of key/value pairs, and return whatever is in the "returnValue" field. Although hardcoding the rule conditions in code satisfies my business requirements, I am looking at moving the conditions into a JSON file for better testability, and to make it easier to parse the conditions for display on a UI.

Is there any easy way through Drools to accomplish this? Or are there any other rule engine solutions which may be better suited? Thanks!

user2963365
  • 81
  • 1
  • 3
  • 8

2 Answers2

0

No this is not a Drools use case. You'll need to write your own file parsing solution based on what your rules and desired output actually look like.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
0

You might want to consider this: https://github.com/kiegroup/drools/tree/main/drools-drlonyaml-parent#readme

given YAML is a superset of JSON, so you could re-use the YAML projection of the DRL-subset for your JSON needs.

ie, the current translator allows you to express something like:

rules:
- name: Fix the PersistentVolume Claim Pod PENDING
  when:
  - given: PersistentVolumeClaim
    as: $pvc
    having:
    - status.phase == "Pending"
  - given: Pod
    as: $pod
    having:
    - status.phase == "Pending"
  - given: Volume
    having:
    - persistentVolumeClaim!.claimName == $pvc.metadata.name
    from: $pod.spec.volumes
  then: |
    insert(new Advice("Fix the PersistentVolume","Pod PENDING: "+$pod.getMetadata().getName() + " pvc PENDING: "+$pvc.getMetadata().getName()));

and get it translated as:

rule "Fix the PersistentVolume Claim Pod PENDING"
when
  $pvc : PersistentVolumeClaim( status.phase == "Pending" )
  $pod : Pod( status.phase == "Pending" )
  Volume( persistentVolumeClaim!.claimName == $pvc.metadata.name ) from $pod.spec.volumes
then
  insert(new Advice("Fix the PersistentVolume","Pod PENDING: "+$pod.getMetadata().getName() + " pvc PENDING: "+$pvc.getMetadata().getName()));
end

the difference in your case you'll need to wire the JSON unmarshalling instead of the YAML unmarshalling.

I should highlight this is an experimental module.

tarilabs
  • 2,178
  • 2
  • 15
  • 23