0

I have rules of this format:

Condition -> Condition OPERATOR Condition | Condition 
Condition -> attribute OPERATOR 
value OPERATOR -> EQUALS | STARTS WITH | ENDS WITH | AND | OR | NOT EQUALS | CONTAINS 

I need to create a JAVA POJO (setters/getters) for the given rules. How can I do it?

Is there any external parser tool that should be created. I am able to create for OPERATOR part:

//POJO CLASS
Class Condtion{
    private String attr;
    private String op;
    private String value;

    public String getAttr(){
        this.attr=attr;
    } 

    public String getOp(){
        this.op=op;
    }

    public String getValue(){
        this.value=value;
    }

    //setters for above three 
}

How to create POJO for the rules Condition->Condition OPERATOR Condition | Condition?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

It seems that you have a rule language with a specific syntax and you want to represent the rules expressed in your syntax as Java objects.

Typically you will need to nest the objects of various types (Rule, Operator, Value, ...). In fact, you probably want to build an abstract syntax tree (AST).

Example: 3 and 2 (whatever that means)

Could be represented as:

new And(3,2)

where

class And { 
  int left; 
  int right; 

  And(int l, int r) { 
    left = l; right = r;
  } 
  //getters and setters, etc. 
}

Now, to represent 3 and 2 and 5 you would (for example) nest:

 new And(new And(3, 2), 5);

Example: 3 and 2 -> 1 (whatever that means...)

You could represent as:

new Rule(new And(3, 2), new Value(1))

where

class Rule {
    Expression body;
    Expression head;

    Rule(Expression b, Expression h) {
        body = b; head = h;
    }
}

... where I'm being lazy and assuming that it is true that for example And extends Expression and Value extends Expression (which may not be what you want).

Similarly for other predicates, operators, etc. You need to have a very clear idea about your rule language and then it will be relatively easy. There are many ways to compose the objects.

Now, it's not clear whether you need to do some parsing or whether you want to just compose these objects programmatically.

You could build your own parser but there are libraries for it such as JavaCC.

Jakub Kotowski
  • 7,411
  • 29
  • 38