Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package guvnor.connection;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.List;
- import org.drools.KnowledgeBase;
- import org.drools.KnowledgeBaseFactory;
- import org.drools.agent.KnowledgeAgent;
- import org.drools.agent.KnowledgeAgentConfiguration;
- import org.drools.agent.KnowledgeAgentFactory;
- import org.drools.core.util.FileManager;
- import org.drools.io.ResourceChangeScannerConfiguration;
- import org.drools.io.ResourceFactory;
- import org.drools.io.impl.ResourceChangeScannerImpl;
- import org.drools.runtime.StatefulKnowledgeSession;
- import org.mortbay.jetty.Server;
- import org.mortbay.jetty.handler.ResourceHandler;
- public class KnowledgeAgent2Guvnor {
- private final Object lock = new Object();
- private static volatile boolean kbaseUpdated;
- private static Server server;
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- FileManager fileManager;
- fileManager = new FileManager();
- fileManager.setUp();
- ((ResourceChangeScannerImpl) ResourceFactory
- .getResourceChangeScannerService()).reset();
- ResourceFactory.getResourceChangeNotifierService().start();
- ResourceFactory.getResourceChangeScannerService().start();
- server = new Server(0);
- ResourceHandler resourceHandler = new ResourceHandler();
- resourceHandler.setResourceBase(fileManager.getRootDirectory()
- .getPath());
- server.setHandler(resourceHandler);
- server.start();
- kbaseUpdated = false;
- System.gc();
- Thread.sleep(300);
- String xml = "";
- xml += "<change-set xmlns='http://drools.org/drools-5.0/change-set'";
- xml += " xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
- xml += " xs:schemaLocation='http://drools.org/drools-5.0/change-setdrools-change-set-5.0.xsd' >";
- xml += " <add> ";
- xml += " <resource source='http://localhost/drools-guvnor/org.drools.guvnor.Guvnor/package/mortgages/TEST' ";
- xml += " type='PKG' basicAuthentication='enabled' username='admin' password='admin'/>";
- xml += " </add> ";
- xml += "</change-set>";
- File fxml = fileManager.newFile("changeset.xml");
- Writer output = new BufferedWriter(new FileWriter(fxml));
- output.write(xml);
- output.close();
- KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
- ResourceChangeScannerConfiguration sconf = ResourceFactory
- .getResourceChangeScannerService()
- .newResourceChangeScannerConfiguration();
- sconf.setProperty("drools.resource.scanner.interval", "2");
- ResourceFactory.getResourceChangeScannerService().configure(sconf);
- KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory
- .newKnowledgeAgentConfiguration();
- aconf.setProperty("drools.agent.scanDirectories", "true");
- aconf.setProperty("drools.agent.scanResources", "true");
- aconf.setProperty("drools.agent.newInstance", "true");
- KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent(
- "test agent", kbase, aconf);
- kagent.applyChangeSet(ResourceFactory.newUrlResource(fxml.toURI()
- .toURL()));
- StatefulKnowledgeSession ksession = kagent.getKnowledgeBase()
- .newStatefulKnowledgeSession();
- List<String> list = new ArrayList<String>();
- ksession.setGlobal("list", list);
- ksession.fireAllRules();
- ksession.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement