1 Reply Latest reply on Nov 10, 2009 1:20 PM by tirelli

    Use Drools for the Bundle Resolver

    thomas.diesler

      I currently investigate the use of Drools for the OSGi Bundle Resolver algorithm.

      To that respect I wonder what is the recommended way to count a certain fact. I'd like to fire an action if there is exactly one fact for a given constraint

      rule "Use WireCandidate"
      when
       $import : ImportPackage( exporter == null )
       $wc : WireCandidate( importPackage == $import )
       // TODO: only do this if there is exactly one but not more than one WireCandidate
      then
       System.out.println("Use WireCandidate " + $wc.getImportPackage() + " --> " + $wc.getExportPackage());
       modify ( $wc.getImportPackage() ) { setExporter( $wc.getExportPackage() ) };
       retract ( $wc );
      end
      
      


        • 1. Re: Use Drools for the Bundle Resolver
          tirelli

          If you want to check that there is no other WireCandidate besides the one matched, you could use "not":

          rule "Use WireCandidate"
          when
           $import : ImportPackage( exporter == null )
           $wc : WireCandidate( importPackage == $import )
           not( WireCandidate( this != $wc, importPackage == $import ) )
          then
           System.out.println("Use WireCandidate " + $wc.getImportPackage() + " --> " + $wc.getExportPackage());
           modify ( $wc.getImportPackage() ) { setExporter( $wc.getExportPackage() ) };
           retract ( $wc );
          end
          


          Now, if you want a more general way of counting instances, use the count() function on accumulate:

          rule "Use WireCandidate"
          when
           $import : ImportPackage( exporter == null )
           $wc : WireCandidate( importPackage == $import )
           // checks if the is exactly 1 instance
           Number( intValue == 1 ) from accumulate(
           $c : WireCandidate( importPackage == $import ),
           count($c) )
          then
           System.out.println("Use WireCandidate " + $wc.getImportPackage() + " --> " + $wc.getExportPackage());
           modify ( $wc.getImportPackage() ) { setExporter( $wc.getExportPackage() ) };
           retract ( $wc );
          end