12 Replies Latest reply on Sep 25, 2013 2:49 PM by ikrambabai

    How to get a package from Guvnor and start it

    doboss

      Can anyone tell me how to get a process/package from Guvnor and run it from a java app using the demo setup?

       

      I have installed jbpm via the JBPM 5.4 full installer. I have taken the ScriptTask.bpmn from jbpm-examples and successfully put it in the Guvnor. I have added a Model Pojo and was able to build the package inside the default package. I have even created a snapshot.

       

      Guvnor lists these URLS:

      URL for package documentation:

      http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST/documentation.pdf

      Use this url to download package documentation PDF.

      URL for package source:

      http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/source

      Use this URL to download the source, or in the 'runtime agent' to access the rules in source form

      URL for package binary:

      http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/binary

      Use this url in the 'runtime agent' to fetch a pre compiled binary.

      URL for running tests:

      http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST/SCENARIOS

      Use this url to run the scenarios remotely and collect results.

      Change Set:

      http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST/ChangeSet.xml

      URL to change set for deployment agents.

      POJO Model:

      http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST/MODEL

      URL to download Model Set.

       

      Additionally it has this link Download binary package

      Which is to: http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST

       

      Now I have my main java app:

       

      package org.jbpm.examples.quickstarts;

       

      import java.util.HashMap;

      import java.util.Map;

       

      import org.drools.KnowledgeBase;

      import org.drools.agent.KnowledgeAgent;

      import org.drools.agent.KnowledgeAgentConfiguration;

      import org.drools.agent.KnowledgeAgentFactory;

      import org.drools.io.Resource;

      import org.drools.io.ResourceFactory;

      import org.drools.runtime.StatefulKnowledgeSession;

       

      public class HelloRunner {

       

      public static final void main(String[] args) {

              try {

                  // load up the knowledge base

                  KnowledgeBase kbase = readKnowledgeBase();

                  StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

                  Map<String, Object> params = new HashMap<String, Object>();

                  Person p = new Person("Krisv");

                  params.put("person", p);

                  ksession.startProcess("com.sample.script", params);

              } catch (Throwable t) {

                  t.printStackTrace();

              }

          }

       

          private static KnowledgeBase readKnowledgeBase() throws Exception {

              KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();

       

              KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("testKAgent", aconf);

       

              String fileName = "http://localhost:8080/drools-guvnor/rest/packages/defaultPackage/binary";

       

              Resource newUrlResource = ResourceFactory.newUrlResource(fileName);

              kagent.applyChangeSet(newUrlResource);

       

              return kagent.getKnowledgeBase();

          }

      }

       

       

      I have also tried the URL: http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST

       

      But I keep getting this error:

       

      java.lang.RuntimeException: Unable to parse ChangeSet
          at org.drools.agent.impl.KnowledgeAgentImpl.getChangeSet(KnowledgeAgentImpl.java:454)
          at org.drools.agent.impl.KnowledgeAgentImpl.applyChangeSet(KnowledgeAgentImpl.java:184)
          at org.jbpm.examples.quickstarts.HelloRunner.readKnowledgeBase(HelloRunner.java:52)
          at org.jbpm.examples.quickstarts.HelloRunner.main(HelloRunner.java:30)
      Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST
          at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
          at org.drools.io.impl.UrlResource.grabStream(UrlResource.java:210)
          at org.drools.io.impl.UrlResource.getInputStream(UrlResource.java:146)
          at org.drools.io.impl.UrlResource.getReader(UrlResource.java:214)
          at org.drools.agent.impl.KnowledgeAgentImpl.getChangeSet(KnowledgeAgentImpl.java:451)
          ... 3 more
      java.lang.RuntimeException: Unable to parse ChangeSet
          at org.drools.agent.impl.KnowledgeAgentImpl.getChangeSet(KnowledgeAgentImpl.java:465)
          at org.drools.agent.impl.KnowledgeAgentImpl.applyChangeSet(KnowledgeAgentImpl.java:184)
          at org.jbpm.examples.quickstarts.HelloRunner.readKnowledgeBase(HelloRunner.java:52)
          at org.jbpm.examples.quickstarts.HelloRunner.main(HelloRunner.java:30)
      

       

       

      I've read various posts about removing authentication and/or putting in the username/password.

       

      I am running the demo stuff, so my understanding is there is no authentication.

       

      So how do I specify a username/password?

       

      I've read something about putting the password in a changeset.xml, but haven't figured out how to do that either...

       

      Thanks!

        • 1. Re: How to get a package from Guvnor and start it
          swiderski.maciej

          here is an example of change set that includes authentication info:

           

           

          <change-set xmlns='http://drools.org/drools-5.0/change-set'            xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
                      xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >
                      
              <add>
                <resource  basic-authentication="enabled" username="someUser" password="somePassword" type="PKG" source="http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST"/>
              </add>
          </change-set>
          
          
          

           

          HTH

          1 of 1 people found this helpful
          • 2. Re: How to get a package from Guvnor and start it
            doboss

            Thank you Maciej!

             

            Is it possible to do it with just some java code or do I have to create a changeset.xml?

             

            I finally figured out that while Resource does not have setUsername() and setPassword(), UrlResource does. But when I try this:

             

                    UrlResource newUrlResource = new UrlResource(fileName); // fileName should really be called "url" ;-)

                    newUrlResource.setUsername("guest");

                    newUrlResource.setPassword("guest");

                    kagent.applyChangeSet(newUrlResource);

             

            I still get the error.

             

            If I add in this:

                    newUrlResource.setBasicAuthentication("enabled");

             

            I get this error:

                  ERROR drools.xml.ExtensibleXmlParser.fatalError  - (null: 1, 1): Content is not allowed in prolog.

             

            So is there a way to do the authentication without creating a changeset.xml?

             

            If not, where is a good example of reading the changeset.xml into the KnowledgeAgent?

             

            Thank you!

            • 3. Re: How to get a package from Guvnor and start it
              swiderski.maciej

              I believe the change set needs to be xml format, could be you can assemble it with some code but I would need to look it up....

              • 4. Re: How to get a package from Guvnor and start it
                doboss

                Thank you again!

                 

                So you are saying that as far as you know, the only way to specify username and password is via the changeset.xml?

                 

                If this is the case, I will just create a file, I don't need to get too fancy (although I saw the changeset.xml created and consumed via code here: http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/xml/changeset/ChangeSetTest.java).

                 

                I just would prefer just do something simple like set the username and password on the agent or resource or whatever.

                 

                Thank you for helping!

                • 5. Re: How to get a package from Guvnor and start it
                  doboss

                  Okay, I am doing something wrong.   If I use the following code the authentication works, and it looks like it is downloading the binary package:

                   

                   

                  private static KnowledgeBase readKnowledgeBase() throws Exception {
                          KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
                  
                          KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("test", aconf);
                  
                          String url = "http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST/";
                  
                          UrlResource resource = (UrlResource) ResourceFactory.newUrlResource(url);
                          resource.setBasicAuthentication("enabled");
                          resource.setUsername("guest");
                          resource.setPassword("guest");        
                          kagent.applyChangeSet(resource);
                  
                          return kagent.getKnowledgeBase();
                      }
                  

                   

                  I figured this out by sending it through the eclipse tcp/ip monitor proxy. And it looks like it is indeed getting the binary package. But I think the code is expecting a changeset, not a binary package. (Which is why I get the "ERROR drools.xml.ExtensibleXmlParser.fatalError  - (null: 1, 1): Content is not allowed in prolog." - because it's expecting a changeset.xml, not the binary package.)

                   

                  Which makes since as I am calling: kagent.applyChangeSet(resource);

                   

                  So I think I'm almost there, I have to do something with the binary package, not try to apply it as a changeset...

                  • 6. Re: How to get a package from Guvnor and start it
                    swiderski.maciej

                    do you need to use knowledge agent for this? Or your requirement is to simply fetch packaged from guvnor? If the second you could simply use KnowledgeBuilder and add the UrlResource there meaning skipping knowledge agent and change set completely.

                     

                    HTH

                    • 7. Re: How to get a package from Guvnor and start it
                      doboss

                      I don't think I need the agent... I just thought that's how things were done. I did try changing the code around to get the file from the UrlResource, but once I have the file, I don't know how to start the process. I assume I have to get it in the session some how? 

                       

                      So in short, yes, happy to skip the agent, but then what do I do with the file?

                       

                      Thank you again Maciej!

                      • 8. Re: How to get a package from Guvnor and start it
                        swiderski.maciej

                        it should be enough to do like this:

                         

                        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
                        kbuilder.add(YOUR_URL_RESOURCE, ResourceType.BPMN2);
                        
                        KnowledgeBase kbase = kbuilder.newKnowledgeBase();
                        
                        

                        and then do the same way as usualy to create session using kbase.

                        • 9. Re: How to get a package from Guvnor and start it
                          doboss

                          I am so sad I couldn't figure that out myself!!!

                           

                          You are absolutely right Maciej! I just had to change the ResourceType to PKG and it works!!

                           

                          For anyone else, here is the final working solution:

                           

                          package org.jbpm.examples.quickstarts;
                          
                          import java.util.HashMap;
                          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.io.impl.UrlResource;
                          import org.drools.runtime.StatefulKnowledgeSession;
                          
                          public class HelloRunner {
                          
                              public static final void main(String[] args) {
                                  try {
                                      // load up the knowledge base
                                      KnowledgeBase kbase = readKnowledgeBase();
                                      StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
                                      Map<String, Object> params = new HashMap<String, Object>();
                                      Person p = new Person("Maciej Swiderski");
                                      params.put("person", p);
                                      ksession.startProcess("com.sample.script", params);
                                  } catch (Throwable t) {
                                      t.printStackTrace();
                                  }
                              }
                          
                              private static KnowledgeBase readKnowledgeBase() throws Exception {
                                  String url = "http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST";
                                  System.out.println("Going: " + url);
                          
                                  UrlResource resource = (UrlResource) ResourceFactory.newUrlResource(url);
                                  resource.setBasicAuthentication("enabled");
                                  resource.setUsername("guest");
                                  resource.setPassword("guest");
                                  
                                  KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
                                  kbuilder.add(resource, ResourceType.PKG);
                                   
                                  return kbuilder.newKnowledgeBase();
                              }
                          }
                          

                           

                          Thank you Meciej! I owe you a beer or four!

                           

                           

                          • 10. Re: How to get a package from Guvnor and start it
                            ikrambabai

                            Hi doboss,

                             

                            I wouldn't really mark the anwer correct because you have been asked to change the way you talk to Guvnor - I would like to stay with the KnowledgeAgent (not KnowledgeBuilder) for reason that it lets me poll for a resource in the changeset automatically and still being able to get the authentication thing working - In other words, my problem is that I know how authentication works with the KnowledgeBuilder, but I guess there isn't any clear way on how it works with the KnowledgeAgent?

                             

                            Can any one please help me with using KnowledgeAgent and working with this Authentication?

                             

                            Regards,

                            -Ikramullah.

                            • 11. Re: How to get a package from Guvnor and start it
                              kmkalai

                              Hi Doboss,

                               

                              I tried with the final soltuin provided by you, but still I am getting the same java.io.IOException: Server returned HTTP response code: 401 Error.

                               

                              Thanks,

                              Kalai

                              • 12. Re: How to get a package from Guvnor and start it
                                ikrambabai

                                I think I have finally found a way - Following method takes care of building you Knowledge Base using Knowledge Agent. I have also overridden the default scanning interval from 60 to 10 seconds - do not use the ResourceChangeScannerConfiguration if you don't have to.

                                 

                                -Cheers.

                                -Ikramullah

                                 

                                private KnowledgeBase loadKnowledgeUsingKnowledgeAgent(){

                                 

                                        try{

                                            KnowledgeAgentConfiguration conf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();

                                            Authenticator.setDefault(new Authenticator() {

                                                // Check https://issues.jboss.org/browse/JBRULES-3465 for any future changes.

                                                @Override

                                                protected PasswordAuthentication getPasswordAuthentication() {

                                                    return new PasswordAuthentication(

                                                            "username",

                                                            "password".toCharArray());

                                                }

                                            });

                                 

                                            knowledgeAgent = KnowledgeAgentFactory.newKnowledgeAgent("myAgent",    conf);

                                            String changeSetUrl = "http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/ScannablePackage";

                                            knowledgeAgent.applyChangeSet(ResourceFactory.newUrlResource(changeSetUrl + "/LATEST/ChangeSet.xml"));

                                            ResourceChangeScannerConfiguration sconf = ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();

                                            sconf.setProperty( "drools.resource.scanner.interval",  "10");

                                            ResourceFactory.getResourceChangeScannerService().configure(sconf);

                                 

                                            ResourceFactory.getResourceChangeNotifierService().start();

                                            ResourceFactory.getResourceChangeScannerService().start();

                                           

                                            return knowledgeAgent.getKnowledgeBase();

                                           

                                        }

                                        catch(Exception e){

                                            e.printStackTrace();

                                            logger.error("Couldn't create the knowledge session !!!! ", e);

                                            return null;

                                        }

                                    }