0 Replies Latest reply on Sep 3, 2019 2:37 AM by meabhishek80

    Traits in Drools - Rules with dynamic attributes in the object

    meabhishek80

      I was trying to write a dynamic rule engine through Drools. Means, if a new property/attribute is added to the object which will be used for evaluation, i should not need to code a new field in that DTO, and redeploy that code.

       

       

      Found something trait close to that. I find 2 issues in it. Can somebody help?

       

       

      1. knowledgeSession.fireAllRules() returns the number of matches to the provided object. Here it returns more than the number of rules documented in the drl.

      2. whatever i do my insert (new PolypathSystem()) does not work. It keeps saying org/drools/core/factmodel/traits/test/Rule_PolyPathSystem11121790227.java (9:566) : Cannot instantiate the type PolypathSystem

       

       

      Please help[/size].

       

       

      my java code looks like below. As its a test code and so the content of the drl file is hardcoded here itself

       

       

      @Test

          public void testMapCore2(  ) {

             

       

              String source= "package org.drools.core.factmodel.traits.test;\n" +

                      "\n" +

                      "import java.util.*;\n" +

                      "import org.drools.core.factmodel.traits.Traitable;\n" +

                      "" +

                      "global java.util.List list;\n " +

                      "" +

                      "declare HashMap @Traitable(logical=true)  end \n" +

                      "\n" +

                      "\n" +

                      "global List list; \n" +

                      "\n" +

                      "declare trait PolypathSystem\n" +

                      "@Traitable  \n" +

                      "   secType : String  = \"asdasd\"\n" +

                      "   modelMapId : String  \n" +

                      "end\n" +

                      "\n" +

                      "rule PolyPathSystem1  \n" +

                      "when  \n" +

                      "then  \n" +

                      "    Map map = new HashMap();\n" +

                      "    insert(map);\n" +

                      "    PolypathSystem p = new PolypathSystem();\n" +

                      "    insert(p);\n" +

                      "    list.add(\"initialized\");\n" +

                      "end\n" +

                      "\n"+

                      "rule PolypathSystem2  \n" +

                      "when  \n" +

                      "  $m : PolypathSystem( secType == \"BONDFUT\" )\n" +

                      "then  \n" +

                      "   modify ( $m ) {  \n" +

                      "       setModelMapId( \"1021\" );  \n" +

                      "   }\n" +

                      "   System.out.println(\"Log: \" +  $m );\n" +

                      "end\n" +

                      "\n";

       

             

              /*KieServices kieServices = KieServices.Factory.get();

              KieFileSystem kfs = kieServices.newKieFileSystem();

              kfs.write( "src/main/resources/simple.drl",

                  kieServices.getResources().newReaderResource( new StringReader(source) ) );

              KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();*/

             

              Resource myResource = ResourceFactory.newReaderResource((Reader) new StringReader(source));

              KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

              kbuilder.add(myResource, ResourceType.DRL);

             

              // Check the builder for errors

              if ( kbuilder.hasErrors() ) {

                  System.out.println( kbuilder.getErrors().toString() );

                  throw new RuntimeException( "Unable to compile drl\"." );

              }

              // get the compiled packages (which are serializable)

              Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();

              InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

              // add the packages to a knowledgebase (deploy the knowledge packages).

              kbase.addPackages( pkgs );

              KieSession ks = kbase.newKieSession();

              TraitFactory.setMode( VirtualPropertyMode.MAP, ks.getKieBase() );

       

              List list = new ArrayList();

              ks.setGlobal( "list", list );

       

              Map<String,Object> map = new HashMap<String, Object>(  );

              map.put( "secType", "BONDFUT" );

              ks.insert( map );

       

              int n = ks.fireAllRules();

       

       

              for ( Object o : ks.getObjects() ) {

                  System.err.println( o );

              }

       

              Assert.assertEquals( "1021", map.get( "modelMapId" ) );

             /* Assert.assertEquals( 184.0, map.get( "height" ) );

              Assert.assertEquals( 4.0, map.get( "GPA" ) );

              Assert.assertEquals( true, map.get( "final" ) );*/

       

          }