title: Dynamic rule file(.drl) generation from database using Rule Template
body: <p>We generated the rule file from database using the Rule Template , it is working but we are facing one problem i.e the rules in the rule generated as much as records present in the table.So according my requirement it should not generate rule files based on records present in the tables.</p>
  
  <p>Rule Template:</p>
  
  <pre><code>template header
  alert_playload
  package com.trinity;
  import com.trinity.DroolsBolt;
  import com.trinity.Tuplebean;
  dialect "mvel"
  template "sensorRule"
  rule "Safety Alert"
      when
         t : Tuplebean(@{alert_playload})
      then
  DroolsBolt.insertToAlertLog(t.getSmokeKey().toString(),t.getMacAddress().toString());     
  end
  
  end template
  </code></pre>
  
  <p>Drools Bolt logic:</p>
  
  <pre><code>      Class.forName("org.postgresql.Driver");
            Connection conn = DriverManager.getConnection("jdbc:postgresql://52.172.14.126:5432/iotdb",
                              "postgres", "track@123");
            Statement sta = conn.createStatement();
        String sql="SELECT min_threshould , max_threshould , sensor_name , alert_playload FROM iot.alert_configuration WHERE drools_boolean=true and alert_configuration_id=94";
        ResultSet rs = sta.executeQuery(sql);
        final ResultSetGenerator converter = new ResultSetGenerator();
        final String drl = converter.compile(rs, getRulesStream());
        System.out.println(drl);
        sta.close();
        KnowledgeBuilder kbuilder =
                  KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()),
                       ResourceType.DRL);
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        StatefulKnowledgeSession kSession = 
           kbase.newStatefulKnowledgeSession();
     /*// now create some test data
        Sensor a = new Sensor("A", 100);     
        Sensor b = new Sensor("B", 520);
        Sensor c = new Sensor("C", 610);
        kSession.insert(a); 
        kSession.insert(b);   
        kSession.insert(c);
        kSession.fireAllRules();
        System.out.println(a.getName() + "," + a.getThreshould());
        System.out.println(b.getName() + "," + b.getThreshould());
        System.out.println(c.getName() + "," + c.getThreshould());
        kSession.destroy();      
        kSession.dispose();
        kSession = kbase.newStatefulKnowledgeSession();
        Sensor aa = new Sensor("AA", 820); 
        Sensor bb = new Sensor("BB", 200);
        Sensor cc = new Sensor("CC", 141);
        kSession.insert(aa);
        kSession.insert(bb);    
        kSession.insert(cc);*/
     //   kSession.destroy();      
      //  kSession.dispose();
   //     kSession.fireAllRules();
        /*System.out.println(aa.getName() + "," + aa.getName());
        System.out.println(bb.getName() + "," + bb.getName());
        System.out.println(cc.getName() + "," + cc.getName());*/
        //kSession.fireAllRules();
  
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("C:/IotHub/conf/alert.drl"));
            out.write(drl.toString());
            out.close();
            }
            catch (IOException e){
            System.out.println("Exception ");
            }
  
  
        kSession.destroy();      
        kSession.dispose();
        kSession = kbase.newStatefulKnowledgeSession();
        kSession.fireAllRules();
  </code></pre>
  
  <p>Generated rule file is:</p>
  
  <pre><code>rule "sensorRule_2"
      when
         $sensor : Sensor(getThreshould()&gt;=0)
      then
        $sensor.sensor_name=":" + "Generator Fuel Level";
  end
  
  rule "sensorRule_1"
      when
         $sensor : Sensor(getThreshould()&gt;=0)
      then
        $sensor.sensor_name=":" + "motion";
  end
  
  rule "sensorRule_0"
      when
         $sensor : Sensor(getThreshould()&gt;=0)
      then
        $sensor.sensor_name=":" + "smke";
  end
  </code></pre>