[rules-users] forall with nested collections
Wolfgang Laun
wolfgang.laun at gmail.com
Wed May 14 10:09:58 EDT 2014
The hierarchical data model is rather unwieldy.
A relational model would have the following entities:
Person( personId,... )
Vehicle( vehicleId, make,... )
Ownership( personId, vehicleId, year )
and it would be quite easy to handle such questions:
Person( $pid: persondId)
Ownership( personId == $pid, $vid1: vehicleId )
Vechicle( vehicleId == $vid1. $make1: make )
not ( Ownership( personId == $pid, $vid2: vehicleId != $vid1 )
and
Vehicle( vehicleId == $vid2, make != $make1 ) )
That said, here's some code to do it in Drools:
$result: PersonData( $id: identifierCode )
Set( count == 1 )
from accumulate( $vd: VehicleData( ownerIdentifierCode ==
$result.identifierCode )
init( Set ms = new HashSet() ),
action( forall( VehicleYearData vyd: $vd.getData() ){
forall( VehicleEntity ve: vyd.getVehicleEntity() ){
ms.add( ve.getMake() );
}
} ),
result( ms ) )
The rule should fire if that person owns cars of just one make; use >1
for the opposite condition.
Untested!
-W
On 14/05/2014, braveheart85 <andrea.pacifico.85 at gmail.com> wrote:
> Hello,
>
> I'm pretty new to Drools hence i apologize if my question is newbie but i'm
> getting crazy.
> I'm trying to write a functioning query since yesterday without fortune.
> I have data about vehicles owned by person organized for year. My structure
> is:
>
> VechicleData
> |___VehicleYearData
> |____Vehicle
>
> I have inserted in Drools only the root of the hierarchy (VehicleData)
> because insert all objects has been problematic for me. Now i need to check
> if a person has all vehicle of the same make.
> I have tried this:
>
> $result: PersonData( $id: identifierCode )
> forall
> (
> ($o: VehicleData( ownerIdentifierCode == $result.identifierCode )) &&
> ($y: VehicleYearData( ) from $o.data) &&
> ($v: VehicleEntity( ) from $y.vehicleEntity)
> )
> VehicleEntity( this == $v, make == "BMW" )
> )
>
> and this:
>
> PersonData( $id: identifierCode )
> $list: List( ) from accumulate (
> (and
> $o: VehicleData( ownerIdentifierCode == $id )
> $y: VehicleYearData( ) from $o.data
> $e: VehicleEntity( ) from $y.vehicleEntity
> ),
> collectList( $e )
> )
> forall (
> $v: VehicleEntity( ) from $list
> VehicleEntity( this == $v, make == "BMW" )
> )
>
> and many other...
> Someone can help me??
>
> Thank you.
> Andrea
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/forall-with-nested-collections-tp4029572.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
More information about the rules-users
mailing list