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

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sat Sep 11 01:48:20 EDT 2010


Author: tsurdilovic
Date: 2010-09-11 01:48:19 -0400 (Sat, 11 Sep 2010)
New Revision: 13735

Added:
   modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/EvaluatorDef.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/CustomOperatorTest.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/MessageBean.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluator.java
   modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluatorDefinition.java
   modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/
   modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/CustomOperatorTest-beans.xml
   modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/customoperatortest.drl
Modified:
   modules/drools/trunk/impl/pom.xml
   modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeBaseProducer.java
   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/config/DroolsConfig.java
   modules/drools/trunk/impl/src/main/resources/META-INF/beans.xml
   modules/drools/trunk/pom.xml
Log:
SEAMDROOLS-4 - Add ability to automatically register custom operators


Added: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/EvaluatorDef.java
===================================================================
--- modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/EvaluatorDef.java	                        (rev 0)
+++ modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/EvaluatorDef.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -0,0 +1,25 @@
+package org.jboss.seam.drools.qualifiers;
+
+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 EvaluatorDef {
+	@Nonbinding String value();
+}
\ No newline at end of file


Property changes on: modules/drools/trunk/api/src/main/java/org/jboss/seam/drools/qualifiers/EvaluatorDef.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: modules/drools/trunk/impl/pom.xml
===================================================================
--- modules/drools/trunk/impl/pom.xml	2010-09-10 14:19:11 UTC (rev 13734)
+++ modules/drools/trunk/impl/pom.xml	2010-09-11 05:48:19 UTC (rev 13735)
@@ -78,6 +78,12 @@
 			<scope>test</scope>
 			<version>3.0.0-SNAPSHOT</version>
 		</dependency>
+		<dependency>
+	         <groupId>javax.servlet</groupId>
+	         <artifactId>servlet-api</artifactId>
+	         <version>2.5</version>
+	         <scope>test</scope>
+	    </dependency>
 		<!-- Test Dependencies -->
 		<dependency>
 			<groupId>junit</groupId>

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-09-10 14:19:11 UTC (rev 13734)
+++ modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/KnowledgeBaseProducer.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -18,7 +18,7 @@
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */ 
+ */
 package org.jboss.seam.drools;
 
 import java.io.InputStream;
@@ -26,6 +26,7 @@
 import java.io.Serializable;
 import java.io.StringReader;
 import java.util.Iterator;
+import java.util.Map.Entry;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.inject.Default;
@@ -35,11 +36,14 @@
 
 import org.drools.KnowledgeBase;
 import org.drools.KnowledgeBaseFactory;
+import org.drools.base.evaluators.EvaluatorDefinition;
 import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderConfiguration;
 import org.drools.builder.KnowledgeBuilderError;
 import org.drools.builder.KnowledgeBuilderErrors;
 import org.drools.builder.KnowledgeBuilderFactory;
 import org.drools.builder.ResourceType;
+import org.drools.builder.conf.EvaluatorOption;
 import org.drools.event.knowledgebase.KnowledgeBaseEventListener;
 import org.drools.io.ResourceFactory;
 import org.drools.template.ObjectDataCompiler;
@@ -63,10 +67,10 @@
 
    @Inject
    BeanManager manager;
-   
+
    @Inject
    ResourceProvider resourceProvider;
-   
+
    @Inject
    DroolsExtension droolsExtension;
 
@@ -75,13 +79,15 @@
    @ApplicationScoped
    public KnowledgeBase produceKnowledgeBase(DroolsConfig config) throws Exception
    {
-      KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config.getKnowledgeBuilderConfiguration());
+      KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(addCustomEvaluators(config.getKnowledgeBuilderConfiguration()));
 
-      if(config.getRuleResources().getResources() == null || config.getRuleResources().getResources().length == 0) {
+      if (config.getRuleResources().getResources() == null || config.getRuleResources().getResources().length == 0)
+      {
          throw new IllegalStateException("No rule resources are specified.");
       }
-      
-      for(String resourceEntry : config.getRuleResources().getResources()) {
+
+      for (String resourceEntry : config.getRuleResources().getResources())
+      {
          addResource(kbuilder, resourceEntry);
       }
 
@@ -103,6 +109,17 @@
       return kbase;
    }
 
+   private KnowledgeBuilderConfiguration addCustomEvaluators(KnowledgeBuilderConfiguration config)
+   {
+      Iterator<Entry<String, EvaluatorDefinition>> allCustomEvaluators = droolsExtension.getEvaluatorDefinitions().entrySet().iterator();
+      while (allCustomEvaluators.hasNext())
+      {
+         Entry<String, EvaluatorDefinition> nextEvalInfo = allCustomEvaluators.next();
+         config.setOption(EvaluatorOption.get(nextEvalInfo.getKey(), nextEvalInfo.getValue()));
+      }
+      return config;
+   }
+
    private void addEventListeners(KnowledgeBase kbase)
    {
       Iterator<KnowledgeBaseEventListener> allKBaseEventListeners = droolsExtension.getKbaseEventListenerSet().iterator();
@@ -117,7 +134,7 @@
    private void addResource(KnowledgeBuilder kbuilder, String entry) throws Exception
    {
       String[] entryParts = RuleResources.DIVIDER.split(entry.trim());
-      
+
       if (entryParts.length >= 3)
       {
          ResourceType resourceType = ResourceType.getResourceType(entryParts[RuleResources.RESOURCE_TYPE]);

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-09-10 14:19:11 UTC (rev 13734)
+++ modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/bootstrap/DroolsExtension.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -36,10 +36,12 @@
 import javax.enterprise.inject.spi.Extension;
 import javax.enterprise.util.AnnotationLiteral;
 
+import org.drools.base.evaluators.EvaluatorDefinition;
 import org.drools.event.knowledgebase.KnowledgeBaseEventListener;
 import org.drools.runtime.process.WorkItemHandler;
 import org.jboss.seam.drools.FactProvider;
 import org.jboss.seam.drools.TemplateDataProvider;
+import org.jboss.seam.drools.qualifiers.EvaluatorDef;
 import org.jboss.seam.drools.qualifiers.KBaseEventListener;
 import org.jboss.seam.drools.qualifiers.KSessionEventListener;
 import org.jboss.seam.drools.qualifiers.TemplateData;
@@ -55,6 +57,7 @@
    private Map<String, WorkItemHandler> workItemHandlers = new HashMap<String, WorkItemHandler>();
    private Map<String, TemplateDataProvider> templateDataProviders = new HashMap<String, TemplateDataProvider>();
    private Set<FactProvider> factProviderSet = new HashSet<FactProvider>();
+   private Map<String, EvaluatorDefinition> evaluatorDefinitions = new HashMap<String, EvaluatorDefinition>();
    
    @SuppressWarnings("serial")
    void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) {
@@ -106,6 +109,28 @@
       }
       log.info("End creating [" + (allWorkItemHandlers == null ? 0 : allWorkItemHandlers.size())+ "] workitem handlers");      
       
+      //EvaluatorDefinitions
+      log.info("Start creating evaluator definitions");
+      Set<Bean<?>> allEvaluatorDefinitions = bm.getBeans(EvaluatorDefinition.class, new AnnotationLiteral<Any>(){});
+      if(allEvaluatorDefinitions != null) {
+          Iterator<Bean<?>> iter = allEvaluatorDefinitions.iterator();
+          while (iter.hasNext())
+          {
+             Bean<?> evaluator = iter.next();
+             EvaluatorDef evaluatorAnnotation = evaluator.getBeanClass().getAnnotation(EvaluatorDef.class);
+             String evaluatorName = evaluatorAnnotation.value();
+             if(evaluatorName.length() > 0) {
+                CreationalContext<?> context = bm.createCreationalContext(evaluator);
+                EvaluatorDefinition evaluatorInstance = (EvaluatorDefinition) bm.getReference(evaluator, EvaluatorDefinition.class, context);
+                evaluatorDefinitions.put(evaluatorName, evaluatorInstance);
+             } else {
+                throw new IllegalStateException("Evaluator name cannot be empty in class: " + evaluator.getBeanClass().getName());
+             }
+          }
+       }
+      log.info("End creating [" + ( allEvaluatorDefinitions== null ? 0 : allEvaluatorDefinitions.size())+ "] evaluator definitions");      
+      
+      
       //Template Data Providers
       log.info("Start creating template providers");
       Set<Bean<?>> allTemplateProviders = bm.getBeans(TemplateDataProvider.class, new AnnotationLiteral<Any>(){});
@@ -166,5 +191,8 @@
    {
       return templateDataProviders;
    }
-   
+
+   public Map<String, EvaluatorDefinition> getEvaluatorDefinitions() {
+	   return evaluatorDefinitions;
+   }  
 }

Modified: modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/config/DroolsConfig.java
===================================================================
--- modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/config/DroolsConfig.java	2010-09-10 14:19:11 UTC (rev 13734)
+++ modules/drools/trunk/impl/src/main/java/org/jboss/seam/drools/config/DroolsConfig.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -41,6 +41,7 @@
 import org.drools.io.ResourceFactory;
 import org.drools.runtime.KnowledgeSessionConfiguration;
 import org.jboss.seam.drools.utils.ConfigUtils;
+import org.jboss.weld.extensions.bean.generic.GenericConfiguration;
 import org.jboss.weld.extensions.resourceLoader.ResourceProvider;
 
 import org.slf4j.Logger;
@@ -51,6 +52,7 @@
  * 
  * @author Tihomir Surdilovic
  */
+ at GenericConfiguration
 public class DroolsConfig
 {
    private static final Logger log = LoggerFactory.getLogger(DroolsConfig.class);

Modified: modules/drools/trunk/impl/src/main/resources/META-INF/beans.xml
===================================================================
--- modules/drools/trunk/impl/src/main/resources/META-INF/beans.xml	2010-09-10 14:19:11 UTC (rev 13734)
+++ modules/drools/trunk/impl/src/main/resources/META-INF/beans.xml	2010-09-11 05:48:19 UTC (rev 13735)
@@ -20,7 +20,7 @@
 	xmlns:d="urn:java:org.jboss.seam.drools:org.jboss.seam.drools.config:org.jboss.seam.drools.qualifiers.config"
 	xmlns:drools="urn:java:org.drools:org.drools.runtime">
 
-	<s:genericBean class="org.jboss.seam.drools.config.DroolsConfig">
+	<!--  <s:genericBean class="org.jboss.seam.drools.config.DroolsConfig">
 		<d:KnowledgeBaseProducer>
 			<s:modifies />
 			<d:produceKnowledgeBase>
@@ -275,7 +275,7 @@
 				</s:parameters>
 			</d:produceScannedQueryResults>
 		</d:QueryResultsProducer>
-	</s:genericBean>
+	</s:genericBean> -->
 
 	<d:DroolsConfig>
 		<s:modifies />

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/CustomOperatorTest.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/CustomOperatorTest.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/CustomOperatorTest.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -0,0 +1,76 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.drools.test.customoperator;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import javax.enterprise.inject.Default;
+import javax.inject.Inject;
+
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.drools.KnowledgeBaseProducer;
+import org.jboss.seam.drools.qualifiers.config.DefaultConfig;
+import org.jboss.seam.drools.test.DroolsModuleFilter;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.weld.extensions.resourceLoader.ResourceProvider;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+ at RunWith(Arquillian.class)
+public class CustomOperatorTest
+{
+   @Deployment
+   public static JavaArchive createTestArchive()
+   {
+      String pkgPath = CustomOperatorTest.class.getPackage().getName().replaceAll("\\.", "/");
+      JavaArchive archive = ShrinkWrap.create("test.jar", JavaArchive.class).addPackages(true, new DroolsModuleFilter("customoperator"), KnowledgeBaseProducer.class.getPackage()).addPackages(true, ResourceProvider.class.getPackage()).addClass(MessageBean.class).addClass(StrEvaluator.class).addClass(StrEvaluatorDefinition.class).addResource(pkgPath + "/customoperatortest.drl", ArchivePaths.create("customoperatortest.drl"))
+      // .addResource(pkgPath + "/kbuilderconfig.properties",
+      // ArchivePaths.create("kbuilderconfig.properties"))
+      // .addResource(pkgPath + "/kbaseconfig.properties",
+      // ArchivePaths.create("kbaseconfig.properties"))
+            .addManifestResource(pkgPath + "/CustomOperatorTest-beans.xml", ArchivePaths.create("beans.xml"));
+      // System.out.println(archive.toString(Formatters.VERBOSE));
+      return archive;
+   }
+
+   @Inject
+   @Default
+   @DefaultConfig
+   StatefulKnowledgeSession ksession;
+
+   @Test
+   public void testCustomOperator()
+   {
+      assertNotNull(ksession);
+      MessageBean mb = new MessageBean();
+      mb.setRoutingValue("R1:messageBody:R2");
+      org.drools.runtime.rule.FactHandle mbHandle = ksession.insert(mb);
+      ksession.fireAllRules();
+      MessageBean mbAfterEval = (MessageBean) ksession.getObject(mbHandle);
+      assertTrue(mbAfterEval.getResult().equals("Message starts with R1, ends with R2 and it's length is 17"));
+   }
+}


Property changes on: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/CustomOperatorTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/MessageBean.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/MessageBean.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/MessageBean.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.drools.test.customoperator;
+
+/**
+ * 
+ * @author Tihomir Surdilovic
+ */
+public class MessageBean
+{
+   private String routingValue;
+   private String result;
+
+   public String getRoutingValue()
+   {
+      return routingValue;
+   }
+
+   public void setRoutingValue(String routingValue)
+   {
+      this.routingValue = routingValue;
+   }
+
+   public String getResult()
+   {
+      return result;
+   }
+
+   public void setResult(String result)
+   {
+      this.result = result;
+   }
+   
+}


Property changes on: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/MessageBean.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluator.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluator.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluator.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -0,0 +1,139 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.drools.test.customoperator;
+
+import org.drools.base.BaseEvaluator;
+import org.drools.base.ValueType;
+import org.drools.base.evaluators.Operator;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
+import org.drools.rule.VariableRestriction.VariableContextEntry;
+import org.drools.spi.FieldValue;
+import org.drools.spi.InternalReadAccessor;
+import org.jboss.seam.drools.test.customoperator.StrEvaluatorDefinition.Operations;
+
+/**
+ * 
+ * @author Tihomir Surdilovic
+ */
+public class StrEvaluator extends BaseEvaluator {
+
+	public static final Operator STR_COMPARE = Operator.addOperatorToRegistry(
+			"str", false);
+	public static final Operator NOT_STR_COMPARE = Operator
+			.addOperatorToRegistry("str", true);
+	public static final String[] SUPPORTED_IDS = { STR_COMPARE
+			.getOperatorString() };
+	
+	private Operations parameter;
+
+	public void setParameterText(String parameterText) {
+		this.parameter = Operations.valueOf(parameterText);
+	}
+
+	public Operations getParameter() {
+		return parameter;
+	}
+
+	public StrEvaluator(final ValueType type, final boolean isNegated) {
+		super(type, isNegated ? NOT_STR_COMPARE : STR_COMPARE);
+	}
+
+	public boolean evaluate(InternalWorkingMemory workingMemory,
+			InternalReadAccessor extractor, Object object, FieldValue value) {
+		final Object objectValue = extractor
+.getValue(workingMemory, object);
+			switch (parameter) {
+			case startsWith:
+				return this.getOperator().isNegated() ^ (((String)objectValue).startsWith( (String)value.getValue() ));
+			case endsWith:
+				return this.getOperator().isNegated() ^ (((String)objectValue).endsWith( (String)value.getValue() ));
+			case length:
+				return this.getOperator().isNegated() ^ (((String)objectValue).length() == ((Long) value.getValue()).longValue() );
+			default:
+				throw new IllegalAccessError("Illegal str comparison parameter");
+			}
+	}
+
+	public boolean evaluate(InternalWorkingMemory workingMemory,
+			InternalReadAccessor leftExtractor, Object left,
+			InternalReadAccessor rightExtractor, Object right) {
+		final Object value1 = leftExtractor.getValue(workingMemory, left);
+		final Object value2 = rightExtractor.getValue(workingMemory, right);
+		
+			switch (parameter) {
+			case startsWith:
+				return this.getOperator().isNegated() ^ (((String)value1).startsWith( (String) value2 ));
+			case endsWith:
+				return this.getOperator().isNegated() ^ (((String)value1).endsWith( (String) value2 ));
+			case length:
+				return this.getOperator().isNegated() ^ (((String)value1).length() == ((Long) value2).longValue() );
+			default:
+				throw new IllegalAccessError("Illegal str comparison parameter");
+			}				
+		
+	}
+
+	public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
+			VariableContextEntry context, Object right) {
+		
+			switch (parameter) {
+			case startsWith:
+				return this.getOperator().isNegated() ^ (((String)right).startsWith( (String)((ObjectVariableContextEntry)
+						context).left) );
+			case endsWith:
+				return this.getOperator().isNegated() ^ (((String)right).endsWith( (String)((ObjectVariableContextEntry)
+						context).left));
+			case length:
+				return this.getOperator().isNegated() ^ (((String)right).length() ==  ((Long)((ObjectVariableContextEntry)
+						context).left).longValue());
+			default:
+				throw new IllegalAccessError("Illegal str comparison parameter");
+			}
+		
+	}
+
+	public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
+			VariableContextEntry context, Object left) {
+			switch (parameter) {
+			case startsWith:
+				return this.getOperator().isNegated() ^ (((String)left).startsWith((String)((ObjectVariableContextEntry)
+						context).right));
+			case endsWith:
+				return this.getOperator().isNegated() ^ (((String)left).endsWith((String)((ObjectVariableContextEntry)
+						context).right));
+			case length:
+				return this.getOperator().isNegated() ^ (((String)left).length() == ((Long)((ObjectVariableContextEntry)
+						context).right).longValue());
+			default:
+				throw new IllegalAccessError("Illegal str comparison parameter");
+			}
+		
+	}
+
+	@Override
+	public String toString() {
+		return "StrEvaluatorDefinition str";
+
+	}
+
+}


Property changes on: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluator.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluatorDefinition.java
===================================================================
--- modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluatorDefinition.java	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluatorDefinition.java	2010-09-11 05:48:19 UTC (rev 13735)
@@ -0,0 +1,100 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.drools.test.customoperator;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+import org.drools.base.ValueType;
+import org.drools.base.evaluators.EvaluatorDefinition;
+import org.drools.base.evaluators.Operator;
+import org.drools.spi.Evaluator;
+import org.jboss.seam.drools.qualifiers.EvaluatorDef;
+
+/**
+ * 
+ * @author Tihomir Surdilovic
+ */
+ at EvaluatorDef("str")
+public class StrEvaluatorDefinition implements EvaluatorDefinition
+{
+
+   public enum Operations
+   {
+      startsWith, endsWith, length;
+   }
+
+   private Evaluator[] evaluator;
+
+   public Evaluator getEvaluator(ValueType type, Operator operator)
+   {
+      return this.getEvaluator(type, operator.getOperatorString(), operator.isNegated(), null);
+   }
+
+   public Evaluator getEvaluator(ValueType type, Operator operator, String parameterText)
+   {
+      return this.getEvaluator(type, operator.getOperatorString(), operator.isNegated(), parameterText);
+   }
+
+   public Evaluator getEvaluator(ValueType type, String operatorId, boolean isNegated, String parameterText)
+   {
+      return getEvaluator(type, operatorId, isNegated, parameterText, Target.FACT, Target.FACT);
+   }
+
+   public Evaluator getEvaluator(ValueType type, String operatorId, boolean isNegated, String parameterText, Target leftTarget, Target rightTarget)
+   {
+      StrEvaluator evaluator = new StrEvaluator(type, isNegated);
+      evaluator.setParameterText(parameterText);
+      return evaluator;
+   }
+
+   public String[] getEvaluatorIds()
+   {
+      return StrEvaluator.SUPPORTED_IDS;
+   }
+
+   public Target getTarget()
+   {
+      return Target.FACT;
+   }
+
+   public boolean isNegatable()
+   {
+      return true;
+   }
+
+   public boolean supportsType(ValueType type)
+   {
+      return true;
+   }
+
+   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
+   {
+      evaluator = (Evaluator[]) in.readObject();
+   }
+
+   public void writeExternal(ObjectOutput out) throws IOException
+   {
+      out.writeObject(evaluator);
+   }
+}


Property changes on: modules/drools/trunk/impl/src/test/java/org/jboss/seam/drools/test/customoperator/StrEvaluatorDefinition.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/CustomOperatorTest-beans.xml
===================================================================
--- modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/CustomOperatorTest-beans.xml	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/CustomOperatorTest-beans.xml	2010-09-11 05:48:19 UTC (rev 13735)
@@ -0,0 +1,35 @@
+<!--
+JBoss, Home of Professional Open Source
+Copyright ${year}, Red Hat, Inc., and individual contributors
+by the @authors tag. See the copyright.txt in the distribution for a
+full listing of individual contributors.
+
+This is free software; you can redistribute it and/or modify it
+under the terms of the GNU Lesser General Public License as
+published by the Free Software Foundation; either version 2.1 of
+the License, or (at your option) any later version.
+
+This software is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this software; if not, write to the Free
+Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+02110-1301 USA, or see the FSF site: http://www.fsf.org.
+--> 
+<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xmlns:s="urn:java:ee" 
+	xmlns:d="urn:java:org.jboss.seam.drools:org.jboss.seam.drools.config"
+	xmlns:test="urn:java:org.jboss.seam.drools.test.ksession">
+
+	<d:DefaultRuleResources>
+  		<s:modifies/>
+  		<s:Exact>org.jboss.seam.drools.config.DefaultRuleResources</s:Exact>
+   		<d:resources>
+   			<s:value>classpath;customoperatortest.drl;DRL</s:value>
+   		</d:resources>
+	</d:DefaultRuleResources>
+	
+</beans>       


Property changes on: modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/CustomOperatorTest-beans.xml
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/customoperatortest.drl
===================================================================
--- modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/customoperatortest.drl	                        (rev 0)
+++ modules/drools/trunk/impl/src/test/resources/org/jboss/seam/drools/test/customoperator/customoperatortest.drl	2010-09-11 05:48:19 UTC (rev 13735)
@@ -0,0 +1,38 @@
+package org.jboss.seam.drools.test.customoperator
+
+import org.jboss.seam.drools.test.customoperator.MessageBean;
+/**
+rule teststartswith
+when
+	$m : MessageBean( routingValue str[startsWith] "R1" )
+then
+	System.out.println(" Message starts with R1 ");
+end
+
+rule testsendswith
+when
+	$m : MessageBean( routingValue str[endsWith] "R2" )
+then
+	System.out.println(" Message ends with R2 ");
+end
+
+rule testlength
+when
+	$m : MessageBean( routingValue str[length] 17 )
+then
+	System.out.println(" Message length is 17 ");
+end
+
+rule testnotstartswith
+when
+	$m : MessageBean( routingValue not str[startsWith] "R2" )
+then
+	System.out.println(" Message does not start with R2 ");
+end
+*/
+rule testmultiple
+when
+	$m : MessageBean( routingValue str[startsWith] "R1" && str[endsWith] "R2" && str[length] 17)
+then
+	$m.setResult("Message starts with R1, ends with R2 and it's length is 17");
+end

Modified: modules/drools/trunk/pom.xml
===================================================================
--- modules/drools/trunk/pom.xml	2010-09-10 14:19:11 UTC (rev 13734)
+++ modules/drools/trunk/pom.xml	2010-09-11 05:48:19 UTC (rev 13735)
@@ -48,6 +48,7 @@
 
 	<properties>
 		<seam.version>3.0.0.b01</seam.version>
+		<weld-extensions.version>1.0.0-SNAPSHOT</weld-extensions.version>
 	</properties>
 
 	<dependencyManagement>
@@ -73,6 +74,11 @@
 				<artifactId>seam-drools-impl</artifactId>
 				<version>${project.version}</version>
 			</dependency>
+			<dependency>
+            	<groupId>org.jboss.weld</groupId>
+            	<artifactId>weld-extensions</artifactId>
+            	<version>${weld-extensions.version}</version>
+         	</dependency>
 		</dependencies>
 	</dependencyManagement>
 



More information about the seam-commits mailing list