3 Replies Latest reply on Feb 6, 2020 8:13 PM by tkobayashi

    can Drools API tell me how many rules are using a particular attribute

    meabhishek80

      Say i have a rule file like below. Below rules are built on 2 properties Instrument and maverickModelMappingId . Can Drools tell me how many rules uses the field "Instrument".  My use case is that I want to know if i am deleting a attrbite or a field, how many rules and which rules are going to be affected.

       

      package abc.modelMapping.ruleEngine;

      dialect "java"

       

      declare FRONTOFFICESYSTEM

      Instrument : String

      maverickModelMappingId : String

      end

       

      rule "rule_MurexCredit_Rule_3"

      salience 3

      no-loop true

           when

              $frontOfficeSystem : FRONTOFFICESYSTEM("Default swap".equalsIgnoreCase(Instrument))

          then

          $frontOfficeSystem.setMaverickModelMappingId("001282");

          System.out.println("001282 "+"MurexCredit_Rule_3+");

      end

       

       

      rule "rule_MurexCredit_Rule_2"

      salience 2

      no-loop true

           when

              $frontOfficeSystem : FRONTOFFICESYSTEM("Euro credit index option".equalsIgnoreCase(Instrument))

          then

          $frontOfficeSystem.setMaverickModelMappingId("001283");

          System.out.println("001283 "+"MurexCredit_Rule_2+");

      end

       

       

      rule "rule_MurexCredit_Rule_1"

      salience 1

      no-loop true

           when

              $frontOfficeSystem : FRONTOFFICESYSTEM("Credit index".equalsIgnoreCase(Instrument))

          then

          $frontOfficeSystem.setMaverickModelMappingId("001282");

          System.out.println("001282 "+"MurexCredit_Rule_1+");

      end

        • 1. Re: can Drools API tell me how many rules are using a particular attribute
          tkobayashi

          Drools doesn't have a convenient API for the purpose. You may write a tool using internal APIs but it could be complex and error-prone.

           

          Rather, I think the following approaches would be easier to measure the impact.

           

          A) Simply grep your rules with "Instrument"

           

          B) Comment out the "Instrument" line of "declare FRONTOFFICESYSTEM" and build the rules. You will see build errors so you will see how many / which rules are affected.

          • 2. Re: can Drools API tell me how many rules are using a particular attribute
            meabhishek80

            I agree but that's manual process. I wanted to do it programatically.

             

            I have an application in background where we can define attributes, rules etc. So, if somebody deletes attributes which is already used in any of the rules, i need to stop that and I need to tell back in response that For example Rule1, Rule10 are using this attribute and so you cant delete.

             

             

             

            So how to achieve that

            • 3. Re: can Drools API tell me how many rules are using a particular attribute
              tkobayashi

              You can build rules and check errors programatically.

               

              KieServices ks = KieServices.Factory.get();
              KieFileSystem kfs = ks.newKieFileSystem();
              
              // You can add your DRL files as InputStream here. Existing rules + a modified rule (e.g. where a field of a declared fact is removed)
              kfs.write("src/main/resources/abc/modelMapping/ruleEngine/Sample.drl",
                  ks.getResources().newInputStreamResource(new FileInputStream(new File("my-src/abc/modelMapping/ruleEngine/Sample.drl"))));
              
              // You can add more...
              
              // It's just for verification so give it a ReleaseId which is different from your production ReleaseId
              ReleaseId releaseId = ks.newReleaseId("com.sample", "my-sample-for-verify", "1.0.0");
              kfs.generateAndWritePomXML(releaseId);
              
              // Build
              KieBuilder kbuilder = ks.newKieBuilder( kfs ).buildAll();
              
              // Get results including errors
              Results results = kbuilder.getResults();
              
              if (results.hasMessages(Message.Level.ERROR)) {;
                List messages = results.getMessages(Message.Level.ERROR);
                // report the error
              }

               

              I hope it helps.