[rules-users] Drools Flow: Variable Persistence Strategies

Dale Wyttenbach dale.wyttenbach at gmail.com
Thu Feb 18 15:59:52 EST 2010


Excellent, I have attached a unit test that demonstrates a query made
possible by the first patch, in case you are interested.  The unit test is
designed to run in your example DroolsFlowVPS project,
since StringVariablePersister is not part of drools-persistence-jpa.
 (Could StringVariablePersister be contributed to drools?)

2010/2/18 Mauricio Salatino <salaboy at gmail.com>

> The first patch is already in the trunk.
> About the second problem, we are trying to remove the
> @CollectionOfElements annotation, (because it's hibernate specific) that's
> why you see the other one commented out. I will try fix it asap, to be able
> to use toplink as persistence provider as well. Probably that will fix your
> problem too.
>
>
>
> 2010/2/18 Dale Wyttenbach <dale.wyttenbach at gmail.com>
>
>> That's correct, it works fine using h2.  Thanks.
>>
>> 2010/2/18 Mauricio Salatino <salaboy at gmail.com>
>>
>> Let me take a look at that too.. probably it's an oracle problem right? I
>>> mean, probably it just happen with oracle.. we can create a fix for that too
>>>
>>>
>>> 2010/2/18 Dale Wyttenbach <dale.wyttenbach at gmail.com>
>>>
>>> My pleasure.
>>>>
>>>> Within ProcessInstanceInfo, I've got a similar observation about the
>>>> eventTypes member.  'private Set<EventType>' is currently commented out in
>>>> favor of 'private Set<String>'.  When using an Oracle hibernate dialect,
>>>> this results in the DDL 'create table ProcessInstanceInfo_eventTypes' which
>>>> yields the error 'ORA-00972: identifier is too long'.  I imagine this will
>>>> be resolved when the String is promoted to EventType but I thought you might
>>>> want to know.
>>>>
>>>> 2010/2/13 Mauricio Salatino <salaboy at gmail.com>
>>>>
>>>> done.. I will apply the patch into the trunk.. it seems to be correct.
>>>>> Thanks a lot!
>>>>>
>>>>> 2010/2/13 Dale Wyttenbach <dale.wyttenbach at gmail.com>
>>>>>
>>>>> Please see attached.
>>>>>>
>>>>>>
>>>>>> 2010/2/12 Mauricio Salatino <salaboy at gmail.com>
>>>>>>
>>>>>>> I just want to make sure before apply the patch that the change will
>>>>>>> not impact in other places.
>>>>>>> Thanks
>>>>>>>
>>>>>>>
>>>>>>> On Fri, Feb 12, 2010 at 5:38 PM, Mauricio Salatino <
>>>>>>> salaboy at gmail.com> wrote:
>>>>>>>
>>>>>>>> can you post both, previous and new, sql schema generated for the
>>>>>>>> VariableInstanceInfo and ProcessInstanceInfo tables?
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> 2010/2/12 Dale Wyttenbach <dale.wyttenbach at gmail.com>
>>>>>>>>
>>>>>>>>> If you try to write a query that
>>>>>>>>> requires VariableInstanceInfo.processId, it won't work because that class
>>>>>>>>> has no processId member.
>>>>>>>>> I guessed that processInstanceId would be a better join column, and
>>>>>>>>> that worked (patch attached)
>>>>>>>>>
>>>>>>>>> Thanks for the reply.
>>>>>>>>>
>>>>>>>>> 2010/2/12 Mauricio Salatino <salaboy at gmail.com>
>>>>>>>>>
>>>>>>>>> Hmm..
>>>>>>>>>> I'm looking at the source and I see the following mapping:
>>>>>>>>>>
>>>>>>>>>> @OneToMany(cascade = CascadeType.ALL)
>>>>>>>>>>     @JoinColumn(name = "processId")
>>>>>>>>>>     @MapKey(name = "name")
>>>>>>>>>>     private Map<String, VariableInstanceInfo> variables          =
>>>>>>>>>> new HashMap<String, VariableInstanceInfo>();
>>>>>>>>>>
>>>>>>>>>> That makes me think that there is a join column called processId
>>>>>>>>>> between VariableInstanceInfo and ProcessInstanceInfo.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> 2010/2/12 Dale Wyttenbach <dale.wyttenbach at gmail.com>
>>>>>>>>>>
>>>>>>>>>>> I have a patch for drools-persistence-jpa should anyone agree
>>>>>>>>>>> with me that ProcessInstanceInfo and VariableInstanceInfo should be joined
>>>>>>>>>>> by processInstanceId.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> On Fri, Feb 12, 2010 at 10:52 AM, Dale Wyttenbach <
>>>>>>>>>>> dale.wyttenbach at gmail.com> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> I'm trying to adapt the StringVariablePersister presented in
>>>>>>>>>>>> http://blog.athico.com/2009/09/drools-flow-variable-persistence.html
>>>>>>>>>>>>
>>>>>>>>>>>> <http://blog.athico.com/2009/09/drools-flow-variable-persistence.html>I
>>>>>>>>>>>> can create process instances with variable "color":
>>>>>>>>>>>>
>>>>>>>>>>>>   private static WorkflowProcessInstance
>>>>>>>>>>>> startProcess(StatefulKnowledgeSession ksession, String color) {
>>>>>>>>>>>>     Map<String, Object> vars = new HashMap<String, Object>();
>>>>>>>>>>>>     vars.put("color", color);
>>>>>>>>>>>>     WorkflowProcessInstance p1 = (WorkflowProcessInstance)
>>>>>>>>>>>> ksession.startProcess("com.sample.ruleflow", vars);
>>>>>>>>>>>>     return p1;
>>>>>>>>>>>>   }
>>>>>>>>>>>>
>>>>>>>>>>>> I can count them:
>>>>>>>>>>>>
>>>>>>>>>>>> select
>>>>>>>>>>>>     count(*)
>>>>>>>>>>>> from
>>>>>>>>>>>>     ProcessInstanceInfo
>>>>>>>>>>>>
>>>>>>>>>>>> However I'm stymied as to counting process instances with a
>>>>>>>>>>>> given color, as there appears to be no join column between
>>>>>>>>>>>> ProcessInstanceInfo and VariableInstanceInfo.
>>>>>>>>>>>> Can such a query be written?
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks.
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> _______________________________________________
>>>>>>>>>>> rules-users mailing list
>>>>>>>>>>> rules-users at lists.jboss.org
>>>>>>>>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> - http://salaboy.wordpress.com
>>>>>>>>>> - http://www.jbug.com.ar
>>>>>>>>>> - Salatino "Salaboy" Mauricio -
>>>>>>>>>>
>>>>>>>>>> _______________________________________________
>>>>>>>>>> rules-users mailing list
>>>>>>>>>> rules-users at lists.jboss.org
>>>>>>>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> _______________________________________________
>>>>>>>>> rules-users mailing list
>>>>>>>>> rules-users at lists.jboss.org
>>>>>>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> - http://salaboy.wordpress.com
>>>>>>>> - http://www.jbug.com.ar
>>>>>>>> - Salatino "Salaboy" Mauricio -
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> - http://salaboy.wordpress.com
>>>>>>> - http://www.jbug.com.ar
>>>>>>> - Salatino "Salaboy" Mauricio -
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> rules-users mailing list
>>>>>>> rules-users at lists.jboss.org
>>>>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> rules-users mailing list
>>>>>> rules-users at lists.jboss.org
>>>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> - http://salaboy.wordpress.com
>>>>> - http://www.jbug.com.ar
>>>>> - Salatino "Salaboy" Mauricio -
>>>>>
>>>>> _______________________________________________
>>>>> rules-users mailing list
>>>>> rules-users at lists.jboss.org
>>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> rules-users mailing list
>>>> rules-users at lists.jboss.org
>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>
>>>>
>>>
>>>
>>> --
>>> - http://salaboy.wordpress.com
>>> - http://www.jbug.com.ar
>>> - Salatino "Salaboy" Mauricio -
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
>
> --
> - http://salaboy.wordpress.com
> - http://www.jbug.com.ar
> - Salatino "Salaboy" Mauricio -
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20100218/13177ace/attachment.html 
-------------- next part --------------
Index: src/test/java/org/test/QueryTest.java
===================================================================
--- src/test/java/org/test/QueryTest.java	(revision 0)
+++ src/test/java/org/test/QueryTest.java	(revision 1007)
@@ -0,0 +1,130 @@
+package org.test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import javax.persistence.Query;
+
+import junit.framework.TestCase;
+
+import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderError;
+import org.drools.builder.KnowledgeBuilderErrors;
+import org.drools.builder.KnowledgeBuilderFactory;
+import org.drools.builder.ResourceType;
+import org.drools.io.ResourceFactory;
+import org.drools.persistence.jpa.JPAKnowledgeService;
+import org.drools.persistence.processinstance.VariablePersistenceStrategyFactory;
+import org.drools.runtime.Environment;
+import org.drools.runtime.EnvironmentName;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.runtime.process.WorkflowProcessInstance;
+
+import bitronix.tm.resource.jdbc.PoolingDataSource;
+
+/**
+ * This is a sample file to launch a process.
+ */
+public class QueryTest extends TestCase {
+
+  static EntityManagerFactory emf;
+
+  static KnowledgeBase kbase;
+  
+  static Environment env;
+
+  StatefulKnowledgeSession ksession;
+
+  static {
+
+    VariablePersistenceStrategyFactory.getVariablePersistenceStrategy()
+    .setPersister("java.lang.String",
+        "org.plugtree.labs.variablepersistence.StringVariablePersister");
+
+    // define the datasource
+    initH2();
+
+    // create the entity manager factory and register it in the environment
+    emf = Persistence.createEntityManagerFactory("org.drools.persistence.jpa");
+    env = KnowledgeBaseFactory.newEnvironment();
+    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
+
+
+    // load up the knowledge base
+    kbase = readKnowledgeBase();
+}
+
+  @Override
+  protected void setUp() throws Exception {
+    ksession = JPAKnowledgeService
+        .newStatefulKnowledgeSession(kbase, null, env);
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    if (ksession != null) {
+      ksession.dispose();
+    }
+  }
+
+  public void testQuery() throws Exception {
+
+    // start two processes
+    startProcess(ksession, "red");
+    startProcess(ksession, "blue");
+
+    // count processes and variables
+    assertEquals(new Long(2), getCount("ProcessInstanceCount"));
+    assertEquals(new Long(4), getCount("VariableInstanceCount"));
+    assertEquals(new Long(1), getCount("BlueProcessInstanceCount"));
+  }
+
+  private static WorkflowProcessInstance startProcess(
+      StatefulKnowledgeSession ksession, String color) {
+    Map<String, Object> vars = new HashMap<String, Object>();
+    vars.put("color", color);
+    vars.put("size", "large");
+    WorkflowProcessInstance p1 = (WorkflowProcessInstance) ksession
+        .startProcess("org.plugtree.labs.droolsflow", vars);
+    return p1;
+  }
+
+  private static Long getCount(String query) {
+    Query namedQuery = emf.createEntityManager().createNamedQuery(query);
+    Long count = (Long) namedQuery.getSingleResult();
+    return count;
+  }
+
+  public static void initH2() {
+    PoolingDataSource ds = new PoolingDataSource();
+    ds.setUniqueName("jdbc/testDS1");
+    ds.setClassName("org.h2.jdbcx.JdbcDataSource");
+    ds.setMaxPoolSize(3);
+    ds.setAllowLocalTransactions(true);
+    ds.getDriverProperties().put("user", "sa");
+    ds.getDriverProperties().put("password", "sasa");
+    ds.getDriverProperties().put("URL", "jdbc:h2:mem:");
+    ds.init();
+  }
+
+  private static KnowledgeBase readKnowledgeBase() {
+    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+    kbuilder.add(ResourceFactory.newClassPathResource("VariablePersistenceStrategyProcess.rf"),
+        ResourceType.DRF);
+    KnowledgeBuilderErrors errors = kbuilder.getErrors();
+    if (errors.size() > 0) {
+      for (KnowledgeBuilderError error : errors) {
+        System.err.println(error);
+      }
+      throw new IllegalArgumentException("Could not parse knowledge.");
+    }
+    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+    return kbase;
+  }
+
+}
\ No newline at end of file
Index: src/test/resources/jndi.properties
===================================================================
--- src/test/resources/jndi.properties	(revision 0)
+++ src/test/resources/jndi.properties	(revision 1007)
@@ -0,0 +1 @@
+java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory
Index: src/main/resources/META-INF/persistence.xml
===================================================================
--- src/main/resources/META-INF/persistence.xml	(revision 995)
+++ src/main/resources/META-INF/persistence.xml	(working copy)
@@ -29,7 +29,9 @@
       <property name="hibernate.max_fetch_depth" value="3"/>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
       
-      <property name="hibernate.show_sql" value="false"/>
+			<property name="hibernate.show_sql" value="true" />
+			<property name="hibernate.format_sql" value="true" />
+			<property name="hibernate.use_sql_comments" value="true" />
       <property name="hibernate.connection.autocommit" value="false"/>
       <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup"/>
     </properties>
Index: src/main/resources/META-INF/orm.xml
===================================================================
--- src/main/resources/META-INF/orm.xml	(revision 995)
+++ src/main/resources/META-INF/orm.xml	(working copy)
@@ -13,14 +13,37 @@
     :type in elements(processInstanceInfo.eventTypes)
           </query>
       </named-query>
-       <!--named-query name="VariableInstancesInfoByProcessId">
-          <query>
+	<named-query name="ProcessInstanceCount">
+		<query>
 select
-    v
+count(*)
 from
-    VariableInstanceInfo v
+ProcessInstanceInfo
+          </query>
+	</named-query>
+
+	<named-query name="VariableInstanceCount">
+		<query>
+select
+count(*)
+from
+VariableInstanceInfo
+          </query>
+	</named-query>
+
+	<named-query name="BlueProcessInstanceCount">
+		<query>
+select
+	count(*)
+from
+	ProcessInstanceInfo p, VariableInstanceInfo v
 where
-    v.processInstanceInfo.id = :processId
+	p.processInstanceId = v.processInstanceId
+and
+	v.name = 'color'
+and
+	v.string = 'blue'
           </query>
-      </named-query-->
+	</named-query>
+
 </entity-mappings>


More information about the rules-users mailing list