[seam-commits] Seam SVN: r12440 - in modules/drools/trunk: api/src/main/java/org/jboss/seam/drools and 6 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Apr 13 02:23:49 EDT 2010


Author: tsurdilovic
Date: 2010-04-13 02:23:47 -0400 (Tue, 13 Apr 2010)
New Revision: 12440

Added:
   modules/drools/trunk/api/.settings/
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/TemplateDataProvider.java
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java
   modules/drools/trunk/docs/.settings/
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyDummyWorkItemHandler.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyKnowledgeSessionEventListener.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyOtherDummyWorkItemHandler.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MySecondKnowledgeBaseEventListener.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/SimpleTemplateDataProvider.java
Removed:
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/FactProvider.java
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java
Modified:
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/TemplateData.java
   modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeBaseProducer.java
   modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeSessionProducer.java
   modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/bootstrap/DroolsExtension.java
   modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/KBaseTest-beans.xml
   modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/kbasetest.drl
Log:
Added support for Drools Templates. Also the Drools Extension looks for and registers event listeners, workitemhandlers and template data providers now.

Added: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/TemplateDataProvider.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/TemplateDataProvider.java	                        (rev 0)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/TemplateDataProvider.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,22 @@
+package org.jboss.seam.drools;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Interface for template data providers.
+ *
+ * @author Tihomir Surdilovic
+ */
+public interface TemplateDataProvider
+{
+   public Collection<Map<String,Object>> getTemplateData();
+}
+

Deleted: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/FactProvider.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/FactProvider.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/FactProvider.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,29 +0,0 @@
-package org.jboss.seam.drools.qualifiers;
-
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.PARAMETER;
-import static java.lang.annotation.ElementType.TYPE;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.Inherited;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import javax.enterprise.util.Nonbinding;
-import javax.inject.Qualifier;
-
-/**
- * 
- * @author Tihomir Surdilovic
- */
- at Qualifier
- at Target( { TYPE, METHOD, FIELD, PARAMETER })
- at Documented
- at Retention(RUNTIME)
- at Inherited
-public @interface FactProvider {
-	@Nonbinding String name();
-	@Nonbinding String entryPointName();
-}

Deleted: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,27 +0,0 @@
-package org.jboss.seam.drools.qualifiers;
-
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.PARAMETER;
-import static java.lang.annotation.ElementType.TYPE;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.Inherited;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import javax.inject.Qualifier;
-
-/**
- * 
- * @author Tihomir Surdilovic
- */
- at Qualifier
- at Target( { TYPE, METHOD, FIELD, PARAMETER })
- at Documented
- at Retention(RUNTIME)
- at Inherited
-public @interface KBaseEventListener {
-
-}

Added: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java	                        (rev 0)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KBaseEventListener.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,27 @@
+package org.jboss.seam.drools.qualifiers;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * 
+ * @author Tihomir Surdilovic
+ */
+ at Qualifier
+ at Target( TYPE )
+ at Documented
+ at Retention(RUNTIME)
+ at Inherited
+public @interface KBaseEventListener {
+
+}

Deleted: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,27 +0,0 @@
-package org.jboss.seam.drools.qualifiers;
-
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.PARAMETER;
-import static java.lang.annotation.ElementType.TYPE;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.Inherited;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import javax.inject.Qualifier;
-
-/**
- * 
- * @author Tihomir Surdilovic
- */
- at Qualifier
- at Target( { TYPE, METHOD, FIELD, PARAMETER })
- at Documented
- at Retention(RUNTIME)
- at Inherited
-public @interface KSessionEventListener {
-
-}

Added: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java	                        (rev 0)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/KSessionEventListener.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,27 @@
+package org.jboss.seam.drools.qualifiers;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * 
+ * @author Tihomir Surdilovic
+ */
+ at Qualifier
+ at Target( TYPE )
+ at Documented
+ at Retention(RUNTIME)
+ at Inherited
+public @interface KSessionEventListener {
+
+}

Modified: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/TemplateData.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/TemplateData.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/TemplateData.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,8 +1,5 @@
 package org.jboss.seam.drools.qualifiers;
 
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
@@ -19,7 +16,7 @@
  * @author Tihomir Surdilovic
  */
 @Qualifier
- at Target( { TYPE, METHOD, FIELD, PARAMETER })
+ at Target( TYPE )
 @Documented
 @Retention(RUNTIME)
 @Inherited

Deleted: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,28 +0,0 @@
-package org.jboss.seam.drools.qualifiers;
-
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.PARAMETER;
-import static java.lang.annotation.ElementType.TYPE;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.Inherited;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import javax.enterprise.util.Nonbinding;
-import javax.inject.Qualifier;
-
-/**
- * 
- * @author Tihomir Surdilovic
- */
- at Qualifier
- at Target( { TYPE, METHOD, FIELD, PARAMETER })
- at Documented
- at Retention(RUNTIME)
- at Inherited
-public @interface WIHandler {
-	@Nonbinding String name();
-}

Added: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java	                        (rev 0)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/WIHandler.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,28 @@
+package org.jboss.seam.drools.qualifiers;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.enterprise.util.Nonbinding;
+import javax.inject.Qualifier;
+
+/**
+ * 
+ * @author Tihomir Surdilovic
+ */
+ at Qualifier
+ at Target( TYPE )
+ at Documented
+ at Retention(RUNTIME)
+ at Inherited
+public @interface WIHandler {
+	@Nonbinding String name();
+}

Modified: modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeBaseProducer.java
===================================================================
--- modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeBaseProducer.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeBaseProducer.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,15 +1,13 @@
 package org.jboss.seam.drools;
 
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
 import java.util.Iterator;
 import java.util.Properties;
-import java.util.Set;
 
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.Produces;
-import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
-import javax.enterprise.util.AnnotationLiteral;
 import javax.inject.Inject;
 
 import org.drools.KnowledgeBase;
@@ -23,11 +21,12 @@
 import org.drools.builder.ResourceType;
 import org.drools.event.knowledgebase.KnowledgeBaseEventListener;
 import org.drools.io.ResourceFactory;
+import org.drools.template.ObjectDataCompiler;
+import org.jboss.seam.drools.bootstrap.DroolsExtension;
 import org.jboss.seam.drools.config.DroolsConfiguration;
 import org.jboss.seam.drools.events.KnowledgeBuilderErrorsEvent;
 import org.jboss.seam.drools.events.RuleResourceAddedEvent;
 import org.jboss.seam.drools.qualifiers.KBaseConfigured;
-import org.jboss.seam.drools.qualifiers.KBaseEventListener;
 import org.jboss.seam.drools.utils.ConfigUtils;
 import org.jboss.weld.extensions.resources.ResourceProvider;
 import org.slf4j.Logger;
@@ -45,6 +44,8 @@
    BeanManager manager;
    @Inject
    ResourceProvider resourceProvider;
+   @Inject
+   DroolsExtension droolsExtension;
 
    @Produces
    @KBaseConfigured
@@ -70,19 +71,22 @@
       KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(getKnowledgeBaseConfiguration(kbaseConfig.getKnowledgeBaseConfigPath()));
       kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
 
-      Set<Bean<?>> allKBaseEventListeners = manager.getBeans(KnowledgeBaseEventListener.class, new AnnotationLiteral<KBaseEventListener>() {});
-      if (allKBaseEventListeners != null)
-      {         
-         Iterator<Bean<?>> iter = allKBaseEventListeners.iterator();
-         while (iter.hasNext())
-         {
-            addEventListener(kbase, iter.next());
-         }
-      }
+      addEventListeners(kbase);
 
       return kbase;
    }
 
+   private void addEventListeners(KnowledgeBase kbase)
+   {
+      Iterator<KnowledgeBaseEventListener> allKBaseEventListeners = droolsExtension.getKbaseEventListenerSet().iterator();
+      while (allKBaseEventListeners.hasNext())
+      {
+         KnowledgeBaseEventListener listener = allKBaseEventListeners.next();
+         kbase.addEventListener(listener);
+         log.info("Added KnowledgeBaseEventListener: " + listener);
+      }
+   }
+
    private KnowledgeBuilderConfiguration getKnowledgeBuilderConfiguration(String knowledgeBuilderConfigPath) throws Exception
    {
       KnowledgeBuilderConfiguration droolsKbuilderConfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
@@ -114,28 +118,53 @@
 
    private void addResource(KnowledgeBuilder kbuilder, String resource) throws Exception
    {
-      // TODO add support for drools templates definition!
       if (ConfigUtils.isValidResource(resource))
       {
          ResourceType resourceType = ResourceType.getResourceType(ConfigUtils.getResourceType(resource));
-         if (ConfigUtils.getResourcePath(resource).equals(ConfigUtils.RESOURCE_TYPE_URL))
+
+         if (ConfigUtils.isRuleTemplate(resource))
          {
-            kbuilder.add(ResourceFactory.newUrlResource(ConfigUtils.getRuleResource(resource)), resourceType);
-            manager.fireEvent(new RuleResourceAddedEvent(ConfigUtils.getRuleResource(resource)));
+            TemplateDataProvider templateDataProvider = droolsExtension.getTemplateDataProviders().get(ConfigUtils.getTemplateData(resource));
+            if (templateDataProvider != null)
+            {
+               InputStream templateStream = resourceProvider.loadResourceStream(ConfigUtils.getRuleResource(resource));
+               if (templateStream == null)
+               {
+                  throw new IllegalStateException("Could not load rule template: " + ConfigUtils.getRuleResource(resource));
+               }
+               ObjectDataCompiler converter = new ObjectDataCompiler();
+               String drl = converter.compile(templateDataProvider.getTemplateData(), templateStream);
+               log.info("Generated following rule from template and template data: \n" + drl);
+               templateStream.close();
+               Reader rdr = new StringReader(drl);
+               kbuilder.add(ResourceFactory.newReaderResource(rdr), resourceType);
+            }
+            else
+            {
+               throw new IllegalStateException("Requested template data provider: " + ConfigUtils.getTemplateData(resource) + " for resource " + resource + " has not been created. Check to make sure you have defined one.");
+            }
          }
-         else if (ConfigUtils.getResourcePath(resource).equals(ConfigUtils.RESOURCE_TYPE_FILE))
-         {
-            kbuilder.add(ResourceFactory.newFileResource(ConfigUtils.getRuleResource(resource)), resourceType);
-            manager.fireEvent(new RuleResourceAddedEvent(ConfigUtils.getRuleResource(resource)));
-         }
-         else if (ConfigUtils.getResourcePath(resource).equals(ConfigUtils.RESOURCE_TYPE_CLASSPATH))
-         {
-            kbuilder.add(ResourceFactory.newClassPathResource(ConfigUtils.getRuleResource(resource)), resourceType);
-            manager.fireEvent(new RuleResourceAddedEvent(ConfigUtils.getRuleResource(resource)));
-         }
          else
          {
-            log.error("Invalid resource: " + ConfigUtils.getResourcePath(resource));
+            if (ConfigUtils.getResourcePath(resource).equals(ConfigUtils.RESOURCE_TYPE_URL))
+            {
+               kbuilder.add(ResourceFactory.newUrlResource(ConfigUtils.getRuleResource(resource)), resourceType);
+               manager.fireEvent(new RuleResourceAddedEvent(ConfigUtils.getRuleResource(resource)));
+            }
+            else if (ConfigUtils.getResourcePath(resource).equals(ConfigUtils.RESOURCE_TYPE_FILE))
+            {
+               kbuilder.add(ResourceFactory.newFileResource(ConfigUtils.getRuleResource(resource)), resourceType);
+               manager.fireEvent(new RuleResourceAddedEvent(ConfigUtils.getRuleResource(resource)));
+            }
+            else if (ConfigUtils.getResourcePath(resource).equals(ConfigUtils.RESOURCE_TYPE_CLASSPATH))
+            {
+               kbuilder.add(ResourceFactory.newClassPathResource(ConfigUtils.getRuleResource(resource)), resourceType);
+               manager.fireEvent(new RuleResourceAddedEvent(ConfigUtils.getRuleResource(resource)));
+            }
+            else
+            {
+               log.error("Invalid resource: " + ConfigUtils.getResourcePath(resource));
+            }
          }
       }
       else
@@ -143,12 +172,4 @@
          log.error("Invalid resource definition: " + resource);
       }
    }
-
-   private void addEventListener(org.drools.KnowledgeBase kbase, Bean<?> listener)
-   {
-      CreationalContext<?> context = manager.createCreationalContext(listener);
-      KnowledgeBaseEventListener listenerInstance = (KnowledgeBaseEventListener) manager.getReference(listener, KnowledgeBaseEventListener.class, context);
-      kbase.addEventListener(listenerInstance);
-      log.debug("Added KnowledgeBaseEventListener: " + listener);
-    }
 }

Modified: modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeSessionProducer.java
===================================================================
--- modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeSessionProducer.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeSessionProducer.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -2,16 +2,12 @@
 
 import java.util.Iterator;
 import java.util.Properties;
-import java.util.Set;
 
-import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.Any;
 import javax.enterprise.inject.Disposes;
 import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.Produces;
-import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
-import javax.enterprise.util.AnnotationLiteral;
 import javax.inject.Inject;
 
 import org.drools.KnowledgeBase;
@@ -23,12 +19,10 @@
 import org.drools.runtime.KnowledgeSessionConfiguration;
 import org.drools.runtime.StatefulKnowledgeSession;
 import org.drools.runtime.StatelessKnowledgeSession;
-import org.drools.runtime.process.WorkItemHandler;
+import org.jboss.seam.drools.bootstrap.DroolsExtension;
 import org.jboss.seam.drools.config.DroolsConfiguration;
 import org.jboss.seam.drools.qualifiers.KAgentConfigured;
 import org.jboss.seam.drools.qualifiers.KBaseConfigured;
-import org.jboss.seam.drools.qualifiers.KSessionEventListener;
-import org.jboss.seam.drools.qualifiers.WIHandler;
 import org.jboss.seam.drools.utils.ConfigUtils;
 import org.jboss.weld.extensions.resources.ResourceProvider;
 import org.slf4j.Logger;
@@ -46,6 +40,8 @@
    BeanManager manager;
    @Inject
    ResourceProvider resourceProvider;
+   @Inject
+   DroolsExtension droolsExtension;
 
    @Produces
    @KBaseConfigured
@@ -89,7 +85,7 @@
       return ksession;
    }
 
-   void disposeStatefulSession(@Disposes @Any StatefulKnowledgeSession session)
+   public void disposeStatefulSession(@Disposes @Any StatefulKnowledgeSession session)
    {
       session.dispose();
    }
@@ -112,54 +108,37 @@
 
    private void addEventListeners(KnowledgeRuntimeEventManager ksession)
    {
-      Set<Bean<?>> allKSessionEventListeners = manager.getBeans(Object.class, new AnnotationLiteral<KSessionEventListener>()
+      Iterator<Object> iter = droolsExtension.getKsessionEventListenerSet().iterator();
+      while (iter.hasNext())
       {
-      });
-      if(allKSessionEventListeners != null) {
-         Iterator<Bean<?>> iter = allKSessionEventListeners.iterator();
-         while(iter.hasNext()) {
-            Bean<?> eventListener = iter.next();
-            CreationalContext<?> context = manager.createCreationalContext(eventListener);
-            Object eventListenerInstance = manager.getReference(eventListener, Object.class, context);
-            
-            if (eventListenerInstance instanceof WorkingMemoryEventListener)
-            {
-               ksession.addEventListener((WorkingMemoryEventListener) eventListenerInstance);
-            }
-            else if (eventListenerInstance instanceof AgendaEventListener)
-            {
-               ksession.addEventListener((AgendaEventListener) eventListenerInstance);
-            }
-            else if (eventListenerInstance instanceof ProcessEventListener)
-            {
-               ksession.addEventListener((ProcessEventListener) eventListenerInstance);
-            }
-            else
-            {
-               log.debug("Invalid Event Listener: " + eventListenerInstance);
-            }
+         Object eventListenerInstance = iter.next();
+
+         if (eventListenerInstance instanceof WorkingMemoryEventListener)
+         {
+            ksession.addEventListener((WorkingMemoryEventListener) eventListenerInstance);
          }
+         else if (eventListenerInstance instanceof AgendaEventListener)
+         {
+            ksession.addEventListener((AgendaEventListener) eventListenerInstance);
+         }
+         else if (eventListenerInstance instanceof ProcessEventListener)
+         {
+            ksession.addEventListener((ProcessEventListener) eventListenerInstance);
+         }
+         else
+         {
+            log.debug("Invalid Event Listener: " + eventListenerInstance);
+         }
       }
    }
 
    private void addWorkItemHandlers(StatefulKnowledgeSession ksession)
    {
-      Set<Bean<?>> allWorkItemHandlers = manager.getBeans(WorkItemHandler.class, new AnnotationLiteral<WIHandler>()
+      Iterator<String> iter = droolsExtension.getWorkItemHandlers().keySet().iterator();
+      while (iter.hasNext())
       {
-      });
-      if (allWorkItemHandlers != null)
-      {
-         Iterator<Bean<?>> iter = allWorkItemHandlers.iterator();
-         while (iter.hasNext())
-         {
-            Bean<?> handler = iter.next();
-            WIHandler handlerQualifier = (WIHandler) handler.getQualifiers().toArray()[0];
-            CreationalContext<?> context = manager.createCreationalContext(handler);
-            WorkItemHandler handlerInstance = (WorkItemHandler) manager.getReference(handler, WorkItemHandler.class, context);
-            
-            log.info("Registering new WorkItemHandler: " + handlerQualifier.name());
-            ksession.getWorkItemManager().registerWorkItemHandler(handlerQualifier.name(), handlerInstance);
-         }
+         String name = iter.next();
+         ksession.getWorkItemManager().registerWorkItemHandler(name, droolsExtension.getWorkItemHandlers().get(name));
       }
    }
 }

Modified: modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/bootstrap/DroolsExtension.java
===================================================================
--- modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/bootstrap/DroolsExtension.java	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/bootstrap/DroolsExtension.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,18 +1,129 @@
 package org.jboss.seam.drools.bootstrap;
 
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Any;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.util.AnnotationLiteral;
 
+import org.drools.event.knowledgebase.KnowledgeBaseEventListener;
+import org.drools.runtime.process.WorkItemHandler;
+import org.jboss.seam.drools.TemplateDataProvider;
+import org.jboss.seam.drools.qualifiers.KBaseEventListener;
+import org.jboss.seam.drools.qualifiers.KSessionEventListener;
+import org.jboss.seam.drools.qualifiers.TemplateData;
+import org.jboss.seam.drools.qualifiers.WIHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class DroolsExtension implements Extension
 {
    private static final Logger log = LoggerFactory.getLogger(DroolsExtension.class);
-
-   void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) {
+   private Set<KnowledgeBaseEventListener> kbaseEventListenerSet = new HashSet<KnowledgeBaseEventListener>();
+   private Set<Object> ksessionEventListenerSet = new HashSet<Object>();
+   private Map<String, WorkItemHandler> workItemHandlers = new HashMap<String, WorkItemHandler>();
+   private Map<String, TemplateDataProvider> templateDataProviders = new HashMap<String, TemplateDataProvider>();
+   
+   @SuppressWarnings("serial")
+   void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) {
+      //KnowledgeBase event listeners
+      log.info("Start creating knowledgebase event listeners");
+      Set<Bean<?>> allKBaseEventListeners = bm.getBeans(KnowledgeBaseEventListener.class, new AnnotationLiteral<KBaseEventListener>() {});
+      if(allKBaseEventListeners != null) {
+         Iterator<Bean<?>> kbaseEventListenerIterator = allKBaseEventListeners.iterator();
+         while (kbaseEventListenerIterator.hasNext()) {
+            Bean<?> listener = kbaseEventListenerIterator.next();
+            CreationalContext<?> context = bm.createCreationalContext(listener);
+            kbaseEventListenerSet.add((KnowledgeBaseEventListener) bm.getReference(listener, KnowledgeBaseEventListener.class, context));
+            
+         }
+      }
+      log.info("End creating [" + (allKBaseEventListeners == null ? 0 : allKBaseEventListeners.size())+ "] knowledgebase event listeners");      
       
+      //KnowledgeSession event listeners
+      log.info("Start creating knowledgeSession event listeners");
+      Set<Bean<?>> allKSessionEventListeners = bm.getBeans(Object.class, new AnnotationLiteral<KSessionEventListener>() {});
+      if(allKBaseEventListeners != null) {
+         Iterator<Bean<?>> ksessionEventListenerIterator = allKSessionEventListeners.iterator();
+         while (ksessionEventListenerIterator.hasNext()) {
+            Bean<?> listener = ksessionEventListenerIterator.next();
+            CreationalContext<?> context = bm.createCreationalContext(listener);
+            ksessionEventListenerSet.add(bm.getReference(listener, Object.class, context));        
+         }
+      }
+      log.info("End creating [" + (allKSessionEventListeners == null ? 0 : allKSessionEventListeners.size())+ "] knowledgesession event listeners");      
+ 
+      //WorkItemHandlers
+      log.info("Start creating workitem handlers");
+      Set<Bean<?>> allWorkItemHandlers = bm.getBeans(WorkItemHandler.class, new AnnotationLiteral<Any>(){});
+      if(allWorkItemHandlers != null) {
+         Iterator<Bean<?>> iter = allWorkItemHandlers.iterator();
+         while (iter.hasNext())
+         {
+            Bean<?> handler = iter.next();
+            WIHandler handlerAnnotation = handler.getBeanClass().getAnnotation(WIHandler.class);
+            String handlerName = handlerAnnotation.name();
+            if(handlerName.length() > 0) {
+               CreationalContext<?> context = bm.createCreationalContext(handler);
+               WorkItemHandler handlerInstance = (WorkItemHandler) bm.getReference(handler, WorkItemHandler.class, context);
+               workItemHandlers.put(handlerName, handlerInstance);
+            } else {
+               throw new IllegalStateException("WorkItemHandler name cannot be empty in class: " + handler.getBeanClass().getName());
+            }
+         }
+      }
+      log.info("End creating [" + (allWorkItemHandlers == null ? 0 : allWorkItemHandlers.size())+ "] workitem handlers");      
+      
+      //Template Data Providers
+      log.info("Start creating template providers");
+      Set<Bean<?>> allTemplateProviders = bm.getBeans(TemplateDataProvider.class, new AnnotationLiteral<Any>(){});
+      if(allTemplateProviders != null) {
+         Iterator<Bean<?>> iter = allTemplateProviders.iterator();
+         while (iter.hasNext())
+         {
+            Bean<?> templateDataProvider = iter.next();
+            TemplateData teamplateDataAnnotation = templateDataProvider.getBeanClass().getAnnotation(TemplateData.class);
+            String templateDataName = teamplateDataAnnotation.name();
+            if(templateDataName.length() > 0) {
+               CreationalContext<?> context = bm.createCreationalContext(templateDataProvider);
+               TemplateDataProvider templateDataProviderInstance = (TemplateDataProvider) bm.getReference(templateDataProvider, TemplateDataProvider.class, context);
+               templateDataProviders.put(templateDataName, templateDataProviderInstance);
+            } else {
+               throw new IllegalStateException("TemplateDataProvider name cannot be empty in class: " + templateDataProvider.getBeanClass().getName());
+            }
+         }
+      }
+      log.info("End creating [" + (allTemplateProviders == null ? 0 : allTemplateProviders.size())+ "] template data providers");      
+      
    }
 
+   public Set<KnowledgeBaseEventListener> getKbaseEventListenerSet()
+   {
+      return kbaseEventListenerSet;
+   }
+
+   public Map<String, WorkItemHandler> getWorkItemHandlers()
+   {
+      return workItemHandlers;
+   }
+
+   public Set<Object> getKsessionEventListenerSet()
+   {
+      return ksessionEventListenerSet;
+   }
+
+   public Map<String, TemplateDataProvider> getTemplateDataProviders()
+   {
+      return templateDataProviders;
+   }
+   
 }

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyDummyWorkItemHandler.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyDummyWorkItemHandler.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyDummyWorkItemHandler.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,24 @@
+package org.jboss.seam.drools.test.kbase;
+
+import org.drools.runtime.process.WorkItem;
+import org.drools.runtime.process.WorkItemHandler;
+import org.drools.runtime.process.WorkItemManager;
+import org.jboss.seam.drools.qualifiers.WIHandler;
+
+ at WIHandler(name="dummy1")
+public class MyDummyWorkItemHandler implements WorkItemHandler
+{
+
+   public void abortWorkItem(WorkItem arg0, WorkItemManager arg1)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void executeWorkItem(WorkItem arg0, WorkItemManager arg1)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+}

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyKnowledgeSessionEventListener.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyKnowledgeSessionEventListener.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyKnowledgeSessionEventListener.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,62 @@
+package org.jboss.seam.drools.test.kbase;
+
+import org.drools.event.process.ProcessCompletedEvent;
+import org.drools.event.process.ProcessEventListener;
+import org.drools.event.process.ProcessNodeLeftEvent;
+import org.drools.event.process.ProcessNodeTriggeredEvent;
+import org.drools.event.process.ProcessStartedEvent;
+import org.jboss.seam.drools.qualifiers.KSessionEventListener;
+
+ at KSessionEventListener
+public class MyKnowledgeSessionEventListener implements ProcessEventListener
+{
+
+   public void afterNodeLeft(ProcessNodeLeftEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void afterProcessCompleted(ProcessCompletedEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void afterProcessStarted(ProcessStartedEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void beforeNodeLeft(ProcessNodeLeftEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void beforeProcessCompleted(ProcessCompletedEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void beforeProcessStarted(ProcessStartedEvent arg0)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+   
+}

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyOtherDummyWorkItemHandler.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyOtherDummyWorkItemHandler.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MyOtherDummyWorkItemHandler.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,24 @@
+package org.jboss.seam.drools.test.kbase;
+
+import org.drools.runtime.process.WorkItem;
+import org.drools.runtime.process.WorkItemHandler;
+import org.drools.runtime.process.WorkItemManager;
+import org.jboss.seam.drools.qualifiers.WIHandler;
+
+ at WIHandler(name="dummy2")
+public class MyOtherDummyWorkItemHandler implements WorkItemHandler
+{
+
+   public void abortWorkItem(WorkItem arg0, WorkItemManager arg1)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+
+   public void executeWorkItem(WorkItem arg0, WorkItemManager arg1)
+   {
+      // TODO Auto-generated method stub
+      
+   }
+   
+}

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MySecondKnowledgeBaseEventListener.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MySecondKnowledgeBaseEventListener.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/MySecondKnowledgeBaseEventListener.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,13 @@
+package org.jboss.seam.drools.test.kbase;
+
+import org.drools.event.knowledgebase.DefaultKnowledgeBaseEventListener;
+import org.drools.event.knowledgebase.KnowledgeBaseEventListener;
+import org.jboss.seam.drools.qualifiers.KBaseEventListener;
+
+ at KBaseEventListener
+public class MySecondKnowledgeBaseEventListener extends DefaultKnowledgeBaseEventListener implements KnowledgeBaseEventListener
+{
+   public MySecondKnowledgeBaseEventListener() {
+      super();
+   }
+}
\ No newline at end of file

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/SimpleTemplateDataProvider.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/SimpleTemplateDataProvider.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/kbase/SimpleTemplateDataProvider.java	2010-04-13 06:23:47 UTC (rev 12440)
@@ -0,0 +1,31 @@
+package org.jboss.seam.drools.test.kbase;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.seam.drools.TemplateDataProvider;
+import org.jboss.seam.drools.qualifiers.TemplateData;
+
+ at TemplateData(name="forkbasetest")
+public class SimpleTemplateDataProvider implements TemplateDataProvider
+{
+   private static Collection<Map<String, Object>> templateData = new ArrayList<Map<String,Object>>();
+   static {
+      Map<String, Object> m1 = new HashMap<String, Object>();
+      m1.put("name", "Tihomir");
+      templateData.add(m1);
+      Map<String, Object> m2 = new HashMap<String, Object>();
+      m2.put("name", "Stuart");
+      templateData.add(m2);
+      Map<String, Object> m3 = new HashMap<String, Object>();
+      m3.put("name", "Dan");
+      templateData.add(m3);
+   }
+   public Collection<Map<String, Object>> getTemplateData()
+   {
+      return templateData;
+   }
+   
+}

Modified: modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/KBaseTest-beans.xml
===================================================================
--- modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/KBaseTest-beans.xml	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/KBaseTest-beans.xml	2010-04-13 06:23:47 UTC (rev 12440)
@@ -10,7 +10,7 @@
       <drools:knowledgeBuilderConfigPath>kbuilderconfig.properties</drools:knowledgeBuilderConfigPath>
       <drools:knowledgeBaseConfigPath>kbaseconfig.properties</drools:knowledgeBaseConfigPath>
       <drools:ruleResources>
-         <s:value>classpath;kbasetest.drl;DRL</s:value>
+         <s:value>classpath;kbasetest.drl;DRL;forkbasetest</s:value>
       </drools:ruleResources>
    </drools:DroolsConfiguration>
 

Modified: modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/kbasetest.drl
===================================================================
--- modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/kbasetest.drl	2010-04-12 22:51:45 UTC (rev 12439)
+++ modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/kbase/kbasetest.drl	2010-04-13 06:23:47 UTC (rev 12440)
@@ -1,6 +1,12 @@
+template header
+name
+
 package org.jboss.seam.drools.kbase
 
-rule "dummy"
-	then 
-		System.out.println("in dummy action");		
+template "kbasetesttemplate"
+rule "say hello to @{name}"
+then
+	System.out.println("Hello @{name}");
 end
+end template
+



More information about the seam-commits mailing list