Implementing Drools Rules

I have a rule that I don't know if it can be implemented in drools.

Description: A

, L

, P

- all the facts / POJO, which are connected. I need to find out if everyone can be A

assigned free P

via L

. If not, how many items are A

left unassigned.

Fact model diagram

In the case above, one A

will remain unassigned.

I came up with an algorithm that is easy to describe:

  • Find A

    with smallest edges

    • If it A

      does not have edges, increase the result counter by 1, removeA

  • Select a random L

    P

    from this A

    and remove elements A

    , L

    ,P

  • Repeat until it's A

    left

It's hard for me to describe it in drooling. I'm not an expert on drooling rules. In JAVA, you will need to do a lot of collection manipulation, including sorting, which doesn't seem to help. Is it possible to do it somehow in drooling?

+3


source to share


1 answer


Here is a set of rules that implement the algorithm. It will not be very effective with a lot of As and Ps. A clean Java solution doesn't have to be complicated either. Note that a full sort is not needed after removing one A from the set and removing all the dangling objects L and P.



rule findMin
when
    $mina: A( $edges: edges )
    not A( edges.size() < $edges.size() )
then
    System.out.println( "retract " + $mina );
    retract( $mina );
end

rule countFail extends findMin
when
    eval( $edges.size() == 0 )
then
    Main.counter++;
    System.out.println( "fail to match " + $mina );
end

rule matchLP extends findMin
when
    $l: L( this memberOf $edges, $p: p )
then
    retract( $p );
    retract( $l );
    System.out.println( "retract " + $p + " and " + $l );
end

rule cleanup
salience 10
when
    $l: L( $p: p )
    not P( this == $p )
    $a: A( edges contains $l )
then
    retract( $l );
    modify( $a ){
        remove( $l );
    }
    System.out.println( "cleanup " + $l + ", " + $a );
end

      

+1


source







All Articles