2017-09-05 180 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

谢谢。是工作。 –