Tag Archives: Drools

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();
  }

}
Advertisements

How to Get a Process Instance with the Drools Knowledge API

import static java.lang.System.out;

import org.drools.runtime.process.ProcessInstance;
import org.drools.runtime.StatefulKnowledgeSession;

public final class DroolsSample {

  public static void main(final String[] args) {
    final StatefulKnowledgeSession ksession = ...
    
    final ProcessInstance kinstance = ksession.getProcessInstance(1L);

    out.println(kinstance.getId());
  }

}

How to List Process Instances with the Drools Knowledge API

import static java.lang.System.out;

import org.drools.runtime.process.ProcessInstance;
import org.drools.runtime.StatefulKnowledgeSession;

public final class DroolsSample {

  public static void main(final String[] args) {
    final StatefulKnowledgeSession ksession = ...
    
    for (final ProcessInstance kinstance : ksession.getProcessInstances())
      out.println(kinstance.getId());

  }

}

How to Create a Process Instance with the Drools Knowledge API

import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.process.ProcessInstance;
import org.drools.runtime.StatefulKnowledgeSession;

public final class DroolsSample {

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

    kbuilder.add(ResourceFactory.newClassPathResource("SampleProcess.bpmn"), ResourceType.BPMN2); // or newByteArrayResource, newDescrResource, newFileResource, newInputStreamResource, newReaderResource, newUrlResource

    final KnowledgeBase kbase = kbuilder.newKnowledgeBase();

    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    final Map<String, Object> params = ...

    final ProcessInstance kinstance = ksession.createProcessInstance("SampleProcess", params);

    ksession.startProcessInstance(kinstance.getId());
  }

}

How to Start a Process with the Drools Knowledge API

import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public final class DroolsSample {

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

    kbuilder.add(ResourceFactory.newClassPathResource("SampleProcess.bpmn"), ResourceType.BPMN2); // or newByteArrayResource, newDescrResource, newFileResource, newInputStreamResource, newReaderResource, newUrlResource

    final KnowledgeBase kbase = kbuilder.newKnowledgeBase();

    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    final Map<String, Object> params = ...

    ksession.startProcess("SampleProcess", params);
  }

}

How to Create an Asset with the Drools Guvnor REST API

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleGuvnorServlet", value = "/")
public final class SampleGuvnorServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("username", "password"));

    client.register(MultiPartFeature.class);

    final MultiPart multiPart = new FormDataMultiPart();

    multiPart.bodyPart(new FormDataBodyPart("asset", ..., MediaType.APPLICATION_XML_TYPE));

    multiPart.bodyPart(new FormDataBodyPart("binary", ...));

    client.target("http://localhost:8080/guvnor/rest/packages/samplePackage/assets").request(MediaType.MULTIPART_FORM_DATA_TYPE).post(Entity.entity(multiPart));

    client.close();
  }
 
}

How to Delete a Category with the Drools Guvnor REST API

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleGuvnorServlet", value = "/")
public final class SampleGuvnorServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();
 
    client.register(HttpAuthenticationFeature.basic("username", "password"));

    client.target("http://localhost:8080/guvnor/rest/categories/Sample%20Category").request().delete();
 
    client.close();
  }

}

How to Create a Category with the Drools Guvnor REST API

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleGuvnorServlet", value = "/")
public final class SampleGuvnorServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("username", "password"));

    client.target("http://localhost:8080/guvnor/rest/categories/Sample%20Category").request().put(Entity.json(""));

    client.close();
  }

}

How to Create a Package Snapshot with the Drools Guvnor REST API

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleGuvnorServlet", value = "/")
public final class SampleGuvnorServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();
 
    client.register(HttpAuthenticationFeature.basic("username", "password"));

    client.target("http://localhost:8080/guvnor/rest/packages/samplePackage/snapshot/sampleSnapshot").request().post(Entity.json("")));

    client.close();
  }
 
}