2017-09-27 40 views
0

我是新來的流口水。 我想檢查RHS,如果一個字段是否存在於POJO中。提供在RHS中檢查存在pojo內的字段在drools

{ 
    "category": [ 
    { 
     "nlp": [ 
     { 
      "mainCategory": "Politics" 
     } 
     ], 
     "crawler": [ 
     { 
      "isNLP": true, 
      "mainCategory": "Politics" 
     } 
     ] 
    } 
    ] 
} 

這是產生

  1. EsRootDoc.java
  2. Crawler.java
  3. Nlp.java

EsRootDoc的POJO的列表具有主類方法和領域。「當

$RHSmap1:EsRootDoc() 
    and 

    $map1:EsRootDoc($categorylist1:category) 

    $category2:Category($nlplist2:nlp) from $categorylist1 

    $nlp2:Nlp(mainCategory=="politics") from $nlplist2 



then 

    $RHSmap1.setEvent("fight"); 

end` 

這是給我下面的錯誤

[Error: unable to resolve method using strict-mode: com.orkash.EnrichmentService.EnrichmentController.tempJsonToClass.EsRootDoc.setEvent(java.lang.String)] [Near : {... $RHSmap1.setEvent("fight"); ....}]

我需要提供在RHS檢查: - 如果EsRootDoc有一個字段事件或不

+0

規則的RHS是強類型。你的'EsRootDoc'類是否有'setEvent(String)'方法? –

+1

不,它沒有這種方法。我的用例是,我需要提供一個檢查之前調用setter方法,如果EsRootDoc有事件字段,那麼只有它應該調用** setEvent(String)**方法 – gaurav9620

+0

你用普通的Java來做到這一點?你的'EsRootDocument'類是怎樣的?我假設它有某些子類具有這種方法,而另一些子類沒有。對? –

回答

1

沒有什麼在Drools中(據我所知),可以讓你做你正在尋找的東西。一條規則的RHS是 - 幾乎純粹的Java。那麼基本規則是:如果它不能用Java完成,那麼它不能在Drools中的規則的RHS中完成。

好消息是,你正在嘗試做的事情可以通過使用反射的Java來實現。我的建議是爲你創建一個輔助類,它使用反射設置對象中屬性的值,如果該屬性存在。那麼你的規則是這樣的:

rule 'Some Rule' 
when 
    $RHSmap1:EsRootDoc() 
    $map1:EsRootDoc($categorylist1:category) 
    $category2:Category($nlplist2:nlp) from $categorylist1 
    $nlp2:Nlp(mainCategory=="politics") from $nlplist2 
then 
    Helper.setAttribute($RHSmap1, "event", "fight"); 
end 

希望它能幫助,