<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Re: jbpm, process definitions, jboss - looking for knowledge
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/dekadence">Matus Majchrak</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/596844#596844">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hi Piotr,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>First things first, In order to access process definitions you have to have access to the guvnor repository(or fake it as Jbpm5 demo does using -Djbpm.console.directory)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent(<font color="red">"Guvnor default"</font>);
kagent.applyChangeSet(ResourceFactory.newClassPathResource(<font color="red">"ChangeSet.xml"</font>));
kagent.monitorResourceChangeEvents(<font color="navy"><b>false</b></font>);
KnowledgeBase kbase = kagent.getKnowledgeBase();
&#160;
</code></pre><p>ChangeSet.xml file will direct the knowledge agent to resources it should care about. For example:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><span>&lt;change-set xmlns='</span><a class="jive-link-external-small" href="http://drools.org/drools-5.0/change-set" target="_blank">http://drools.org/drools-5.0/change-set</a><span>'
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; xmlns:xs='</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>'
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; xs:schemaLocation='</span><a class="jive-link-external-small" href="http://drools.org/drools-5.0/change-set" target="_blank">http://drools.org/drools-5.0/change-set</a><span> </span><a class="jive-link-external-small" href="http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd" target="_blank">http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd</a><span>' &gt;</span></span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;add&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag"><span>&lt;resource source='</span><a class="jive-link-external-small" href="http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST" target="_blank">http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST</a><span>' type='PKG' basicAuthentication="enabled" username="admin" password="admin" /&gt;</span></span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/add&gt;</span>
<span class="jive-xml-tag">&lt;/change-set&gt;</span>
</code></pre><p>This will tell your agent to look for your localhost's repository default package. </p><p><span>Try to deploy some processes into default package so that you have something to work with: </span><a class="jive-link-external-small" href="http://people.redhat.com/kverlaen/jBPM5-guvnor-integration.swf" target="_blank">http://people.redhat.com/kverlaen/jBPM5-guvnor-integration.swf</a></p><p>You can now test it by listing deployed processes:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>for</b></font> (Process process : kbase.getProcesses()) <font color="navy">{</font>
&#160;&#160;&#160;&#160; System.out.println(<font color="red">"I found process in Guvnor: "</font> + process.getId());
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now for the knowledgeSession.Depending on where does your application run, you have to obtain an EntityManagerFactory for drools persistence.</p><p>See the persistence.xml in jbpm5-installers/db folder.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">EntityManagerFactory emf = Persistence.createEntityManagerFactory(<font color="red">"org.drools.persistence.jpa"</font>);
Environment env = KnowledgeBaseFactory.newEnvironment();
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
Properties properties = <font color="navy"><b>new</b></font> Properties();
properties.put(<font color="red">"drools.processInstanceManagerFactory"</font>,<font color="red">"org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory"</font>);
properties.put(<font color="red">"drools.processSignalManagerFactory"</font>,<font color="red">"org.jbpm.persistence.processinstance.JPASignalManagerFactory"</font>);
KnowledgeSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);
kSession = JPAKnowledgeService.loadStatefulKnowledgeSession(1, kbase, config, env);
</code></pre><p>The above code will try to load an existing Statefull knowledge session. If this does not exsit you will receive an error and you have to create a fresh kSession like this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">env = KnowledgeBaseFactory.newEnvironment();
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
kSession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, config, env);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>And thats basicaly it. You can now get a list of processes: </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">knowledgeSession.getKnowledgeBase().getProcesses()
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>register a ProcessEventListener to intercept process lifecycle events:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">knowledgeSession.addEventListener(... your <font color="navy"><b>class</b></font> implementing ProcessEventListener)
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>or get list of process instances:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">knowledgeSession.getProcessInstances();
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So basicaly you have to hook up your application to guvnor to access process related resources and obtain JPA entity management factory to be able to construct or obtain knowledge session. After that, you should have all the thigs you need to fulfill your tasks.</p><p>regards </p><p>matus </p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/596844#596844">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>