How to Load a Package with the Drools Knowledge API

import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;

import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;

public final class DroolsSample {

  public static void main(final String[] args) throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    Authenticator.setDefault(new Authenticator() {
 
      public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("username", "password".toCharArray());
      }

    });

    final URL url = new URL("http://localhost:8080/guvnor/org.drools.guvnor.Guvnor/package/samplePackage/LATEST");

    final HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    final InputStream in = conn.getInputStream();

    kbuilder.add(ResourceFactory.newInputStreamResource(in), ResourceType.CHANGE_SET); // or newClassPathResource("ChangeSet.xml") if you add the change set to your application

    in.close();
  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s