Advertisement
Guest User

[rules-users] Creating packages with Guvnor 5.1

a guest
Oct 7th, 2010
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package guvnor.connection;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.Writer;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import org.drools.KnowledgeBase;
  11. import org.drools.KnowledgeBaseFactory;
  12. import org.drools.agent.KnowledgeAgent;
  13. import org.drools.agent.KnowledgeAgentConfiguration;
  14. import org.drools.agent.KnowledgeAgentFactory;
  15. import org.drools.core.util.FileManager;
  16. import org.drools.io.ResourceChangeScannerConfiguration;
  17. import org.drools.io.ResourceFactory;
  18. import org.drools.io.impl.ResourceChangeScannerImpl;
  19. import org.drools.runtime.StatefulKnowledgeSession;
  20.  
  21. import org.mortbay.jetty.Server;
  22. import org.mortbay.jetty.handler.ResourceHandler;
  23.  
  24. public class KnowledgeAgent2Guvnor {
  25.  
  26.     private final Object lock = new Object();
  27.     private static volatile boolean kbaseUpdated;
  28.     private static Server server;
  29.  
  30.     /**
  31.      * @param args
  32.      */
  33.     public static void main(String[] args) throws Exception {
  34.  
  35.         FileManager fileManager;
  36.  
  37.         fileManager = new FileManager();
  38.         fileManager.setUp();
  39.         ((ResourceChangeScannerImpl) ResourceFactory
  40.                 .getResourceChangeScannerService()).reset();
  41.  
  42.         ResourceFactory.getResourceChangeNotifierService().start();
  43.         ResourceFactory.getResourceChangeScannerService().start();
  44.  
  45.         server = new Server(0);
  46.         ResourceHandler resourceHandler = new ResourceHandler();
  47.         resourceHandler.setResourceBase(fileManager.getRootDirectory()
  48.                 .getPath());
  49.  
  50.         server.setHandler(resourceHandler);
  51.  
  52.         server.start();
  53.  
  54.         kbaseUpdated = false;
  55.         System.gc();
  56.         Thread.sleep(300);
  57.  
  58.         String xml = "";
  59.         xml += "<change-set xmlns='http://drools.org/drools-5.0/change-set'";
  60.         xml += "    xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
  61.         xml += "        xs:schemaLocation='http://drools.org/drools-5.0/change-setdrools-change-set-5.0.xsd' >";
  62.         xml += "    <add> ";
  63.         xml += "        <resource source='http://localhost/drools-guvnor/org.drools.guvnor.Guvnor/package/mortgages/TEST' ";
  64.         xml += " type='PKG' basicAuthentication='enabled' username='admin' password='admin'/>";
  65.         xml += "    </add> ";
  66.         xml += "</change-set>";
  67.  
  68.         File fxml = fileManager.newFile("changeset.xml");
  69.         Writer output = new BufferedWriter(new FileWriter(fxml));
  70.         output.write(xml);
  71.         output.close();
  72.  
  73.         KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
  74.  
  75.         ResourceChangeScannerConfiguration sconf = ResourceFactory
  76.                 .getResourceChangeScannerService()
  77.                 .newResourceChangeScannerConfiguration();
  78.         sconf.setProperty("drools.resource.scanner.interval", "2");
  79.         ResourceFactory.getResourceChangeScannerService().configure(sconf);
  80.  
  81.         KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory
  82.                 .newKnowledgeAgentConfiguration();
  83.         aconf.setProperty("drools.agent.scanDirectories", "true");
  84.         aconf.setProperty("drools.agent.scanResources", "true");
  85.         aconf.setProperty("drools.agent.newInstance", "true");
  86.         KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent(
  87.                 "test agent", kbase, aconf);
  88.  
  89.         kagent.applyChangeSet(ResourceFactory.newUrlResource(fxml.toURI()
  90.                 .toURL()));
  91.  
  92.         StatefulKnowledgeSession ksession = kagent.getKnowledgeBase()
  93.                 .newStatefulKnowledgeSession();
  94.         List<String> list = new ArrayList<String>();
  95.         ksession.setGlobal("list", list);
  96.         ksession.fireAllRules();
  97.         ksession.dispose();
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement