2017-09-05 174 views
0

我有對象列表Person。對象Person包含對象Car的列表。我如何從列表中選擇只有thoose Person,其中包含Car選擇的類型。對於exmplain:品牌「寶馬」的汽車。我不知道這樣做沒有循環。Drools中列表中的搜索元素

person[0].addCar(new Car("BMW")); 
person[0].addCar(new Car("Ford")); 

person[1].addCar(new Car("Ford")); 
person[1].addCar(new Car("Ford")); 
person[1].addCar(new Car("Ford")); 

如何我可以在drools-regulations中返回person [0]。

我的代碼不起作用。

rule "HardDrool" 
salience 100 
when 
    $world : World(); 

    $persons: Human(
     (name == "Yura"), 
     (!cars.isEmpty()), 
     (Car(name == "BMW") from getCars()) 
     ) from $world.getPersons() 
then 
    System.out.println($persons); 

end 

回答

0
rule "HardDrool" 
when 
    $world : World(); 
    $person: Human(name == "Yura", !cars.isEmpty()) 
    from $world.getPersons() 
    exists Car(name == "BMW") from $person.getCars()) 
then 
    System.out.println($person); 
end 

這應該爲每個人至少擁有一個BMW火一次。如果你想看到每輛寶馬,請省略存在。

+0

謝謝。是工作。 –