简体   繁体   English

根据此生产规则构建POJO对象

[英]Build POJO object from this Production Rule

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. 我需要为给定规则创建一个JAVA POJO(设置程序/获取程序)。 How can I do it? 我该怎么做?

Is there any external parser tool that should be created. 是否应该创建任何外部解析器工具。 I am able to create for OPERATOR part: 我可以为OPERATOR部分创建:

//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 如何为规则Condition->Condition OPERATOR Condition | Condition创建POJO Condition->Condition OPERATOR Condition | Condition ? Condition->Condition OPERATOR Condition | Condition

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. 看来您有一种具有特定语法的规则语言,并且希望将用语法表示的规则表示为Java对象。

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). 实际上,您可能想构建一个抽象语法树 (AST)。

Example: 3 and 2 (whatever that means) 示例: 3 and 2 (无论如何)

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: 现在,要表示3 and 2 and 5您将(例如)嵌套:

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

Example: 3 and 2 -> 1 (whatever that means...) 示例: 3 and 2 -> 1 (无论什么意思...)

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). ...在这里我很懒,并假设例如, And extends ExpressionValue extends Expression (可能不是您想要的),这是事实。

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 . 您可以构建自己的解析器,但是有诸如JavaCC之类的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM