[jboss-svn-commits] JBL Code SVN: r20825 - in labs/jbossrules/trunk/drools-clips/src: main/java/org/drools/clips/functions and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 26 23:07:17 EDT 2008


Author: mark.proctor at jboss.com
Date: 2008-06-26 23:07:17 -0400 (Thu, 26 Jun 2008)
New Revision: 20825

Added:
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/LessThanOrEqFunction.java
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/MoreThanOrEqFunction.java
Removed:
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrLessThanFunction.java
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrMoreThanFunction.java
Modified:
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/ClipsShell.java
   labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clips/ClipsShellTest.java
Log:
JBRULES-720 Clips Parser 
-corrected and renaced >= and <= functions
-added more complex mixed test, mixign javabeans and deftemplates.

Modified: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/ClipsShell.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/ClipsShell.java	2008-06-27 03:06:42 UTC (rev 20824)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/ClipsShell.java	2008-06-27 03:07:17 UTC (rev 20825)
@@ -38,9 +38,11 @@
 import org.drools.clips.functions.GetFunction;
 import org.drools.clips.functions.IfFunction;
 import org.drools.clips.functions.LessThanFunction;
+import org.drools.clips.functions.LessThanOrEqFunction;
 import org.drools.clips.functions.MinusFunction;
 import org.drools.clips.functions.ModifyFunction;
 import org.drools.clips.functions.MoreThanFunction;
+import org.drools.clips.functions.MoreThanOrEqFunction;
 import org.drools.clips.functions.MultiplyFunction;
 import org.drools.clips.functions.NewFunction;
 import org.drools.clips.functions.PlusFunction;
@@ -117,7 +119,9 @@
         handlers.registerFunction( new PrognFunction() );
         handlers.registerFunction( new IfFunction() );
         handlers.registerFunction( new LessThanFunction() );
+        handlers.registerFunction( new LessThanOrEqFunction() );
         handlers.registerFunction( new MoreThanFunction() );
+        handlers.registerFunction( new MoreThanOrEqFunction() );
         handlers.registerFunction( new EqFunction() );
         handlers.registerFunction( new SwitchFunction() );
         //handlers.registerFunction( new DeffunctionFunction() );

Deleted: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrLessThanFunction.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrLessThanFunction.java	2008-06-27 03:06:42 UTC (rev 20824)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrLessThanFunction.java	2008-06-27 03:07:17 UTC (rev 20825)
@@ -1,14 +0,0 @@
-package org.drools.clips.functions;
-
-public class EqOrLessThanFunction  extends BaseInfixFunction  {
-    private static final String name = "<=";
-    private static final String mappedSymbol = "<=";
-    
-    public String getName() {
-        return name;
-    }
-    
-    public String getMappedSymbol() {
-        return mappedSymbol;
-    }    
-}

Deleted: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrMoreThanFunction.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrMoreThanFunction.java	2008-06-27 03:06:42 UTC (rev 20824)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrMoreThanFunction.java	2008-06-27 03:07:17 UTC (rev 20825)
@@ -1,14 +0,0 @@
-package org.drools.clips.functions;
-
-public class EqOrMoreThanFunction  extends BaseInfixFunction  {
-    private static final String name = ">=";
-    private static final String mappedSymbol = ">=";
-
-    public String getName() {
-        return name;
-    }
-    
-    public String getMappedSymbol() {
-        return mappedSymbol;
-    }    
-}

Copied: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/LessThanOrEqFunction.java (from rev 20820, labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrLessThanFunction.java)
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/LessThanOrEqFunction.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/LessThanOrEqFunction.java	2008-06-27 03:07:17 UTC (rev 20825)
@@ -0,0 +1,14 @@
+package org.drools.clips.functions;
+
+public class LessThanOrEqFunction  extends BaseInfixFunction  {
+    private static final String name = "<=";
+    private static final String mappedSymbol = "<=";
+    
+    public String getName() {
+        return name;
+    }
+    
+    public String getMappedSymbol() {
+        return mappedSymbol;
+    }    
+}

Copied: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/MoreThanOrEqFunction.java (from rev 20820, labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/EqOrMoreThanFunction.java)
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/MoreThanOrEqFunction.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clips/functions/MoreThanOrEqFunction.java	2008-06-27 03:07:17 UTC (rev 20825)
@@ -0,0 +1,14 @@
+package org.drools.clips.functions;
+
+public class MoreThanOrEqFunction  extends BaseInfixFunction  {
+    private static final String name = ">=";
+    private static final String mappedSymbol = ">=";
+
+    public String getName() {
+        return name;
+    }
+    
+    public String getMappedSymbol() {
+        return mappedSymbol;
+    }    
+}

Modified: labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clips/ClipsShellTest.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clips/ClipsShellTest.java	2008-06-27 03:06:42 UTC (rev 20824)
+++ labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clips/ClipsShellTest.java	2008-06-27 03:07:17 UTC (rev 20825)
@@ -3,17 +3,12 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintStream;
-import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
 
 import junit.framework.TestCase;
 
 import org.drools.Person;
 import org.drools.WorkingMemory;
-import org.drools.clips.FunctionHandlers;
-import org.drools.clips.ClipsShell;
 import org.drools.clips.functions.AssertFunction;
 import org.drools.clips.functions.BindFunction;
 import org.drools.clips.functions.CallFunction;
@@ -22,9 +17,11 @@
 import org.drools.clips.functions.GetFunction;
 import org.drools.clips.functions.IfFunction;
 import org.drools.clips.functions.LessThanFunction;
+import org.drools.clips.functions.LessThanOrEqFunction;
 import org.drools.clips.functions.MinusFunction;
 import org.drools.clips.functions.ModifyFunction;
 import org.drools.clips.functions.MoreThanFunction;
+import org.drools.clips.functions.MoreThanOrEqFunction;
 import org.drools.clips.functions.MultiplyFunction;
 import org.drools.clips.functions.NewFunction;
 import org.drools.clips.functions.PlusFunction;
@@ -40,7 +37,7 @@
 public class ClipsShellTest extends TestCase {
     private ByteArrayOutputStream baos;
 
-    ClipsShell                         shell;
+    ClipsShell                    shell;
 
     public void setUp() {
         FunctionHandlers handlers = FunctionHandlers.getInstance();
@@ -53,7 +50,9 @@
         handlers.registerFunction( new PrognFunction() );
         handlers.registerFunction( new IfFunction() );
         handlers.registerFunction( new LessThanFunction() );
+        handlers.registerFunction( new LessThanOrEqFunction() );
         handlers.registerFunction( new MoreThanFunction() );
+        handlers.registerFunction( new MoreThanOrEqFunction() );
         handlers.registerFunction( new EqFunction() );
         handlers.registerFunction( new SwitchFunction() );
         //handlers.registerFunction( new DeffunctionFunction() );
@@ -112,6 +111,10 @@
 
         assertEquals( "hellohello",
                       new String( baos.toByteArray() ) );
+
+        if ( 1 <= 10 ) {
+
+        }
     }
 
     public void testIfElse() {
@@ -196,7 +199,6 @@
                       new String( this.baos.toByteArray() ) );
     }
 
-
     public void testExplicitCall() {
         String t = "(import org.drools.*) (bind ?p (new Person mark cheddar) ) (call ?p setFields bob stilton 35)  (printout t (call ?p toLongString))";
         this.shell.eval( t );
@@ -245,26 +247,26 @@
     }
 
     public void FIXME_testTemplateCreation2() throws Exception {
-    	this.shell.eval( "(deftemplate PersonTemplate (slot name (type String) ) (slot age (type int) ) )" );
+        this.shell.eval( "(deftemplate PersonTemplate (slot name (type String) ) (slot age (type int) ) )" );
         this.shell.eval( "(defrule xxx (PersonTemplate (name ?name&bob) (age 30) ) (PersonTemplate  (name ?name) (age 35)) => (printout t xx \" \" (eq 1 1) ) )" );
-        this.shell.eval( "(assert (PersonTemplate (name 'mike') (age 34)))");
+        this.shell.eval( "(assert (PersonTemplate (name 'mike') (age 34)))" );
 
         Class personClass = this.shell.getStatefulSession().getRuleBase().getPackage( "MAIN" ).getPackageScopeClassLoader().loadClass( "MAIN.PersonTemplate" );
-        assertNotNull(personClass);
+        assertNotNull( personClass );
     }
 
     public void testTemplateCreation() throws Exception {
         this.shell.eval( "(deftemplate Person (slot name (type String) ) (slot age (type int) ) )" );
 
         this.shell.eval( "(defrule xxx (Person (name ?name&bob) (age 30) ) => (printout t hello bob ) )" );
-        
+
         this.shell.eval( "(assert (Person (name bob) (age 30) ) )" );
         this.shell.eval( "(run)" );
 
         assertEquals( "hellobob",
-                      new String( this.baos.toByteArray() ) );          
-    }    
-    
+                      new String( this.baos.toByteArray() ) );
+    }
+
     public void testTemplateCreationWithJava() throws Exception {
         this.shell.eval( "(deftemplate Person (slot name (type String) ) (slot age (type int) ) )" );
 
@@ -287,22 +289,26 @@
         WorkingMemory wm = shell.getStatefulSession();
         Class personClass = this.shell.getStatefulSession().getRuleBase().getPackage( "MAIN" ).getPackageScopeClassLoader().loadClass( "MAIN.Person" );
 
-        Method nameMethod = personClass.getMethod( "setName", new Class[] { String.class } );
-        Method ageMethod = personClass.getMethod( "setAge", new Class[] { int.class } );
+        Method nameMethod = personClass.getMethod( "setName",
+                                                   new Class[]{String.class} );
+        Method ageMethod = personClass.getMethod( "setAge",
+                                                  new Class[]{int.class} );
 
         Object bob1 = personClass.newInstance();
-        nameMethod.invoke( bob1, "bob" );
-        ageMethod.invoke( bob1, 30 );
+        nameMethod.invoke( bob1,
+                           "bob" );
+        ageMethod.invoke( bob1,
+                          30 );
 
-
         Object bob2 = personClass.newInstance();
-        nameMethod.invoke( bob2, "bob" );
-        ageMethod.invoke( bob2, 35 );
+        nameMethod.invoke( bob2,
+                           "bob" );
+        ageMethod.invoke( bob2,
+                          35 );
         //Constructor constructor = personClass.getConstructor( new Class[] { String.class,String.class, int.class} );
         wm.insert( bob1 );
         wm.insert( bob2 );
 
-
         wm.fireAllRules();
         assertEquals( "yy truexx true",
                       new String( this.baos.toByteArray() ) );
@@ -391,4 +397,36 @@
         this.shell.eval( "(run)" );
     }
 
+    public void testMixed() {
+        this.shell.eval( "(import org.drools.Cheese)" );
+        String str ="";
+        str += "(deftemplate Person ";
+        str += "  (slot name ";
+        str += "    (type String) ) ";
+        str += "  (slot age";
+        str += "    (type String) ) ";
+        str += "  (slot location";
+        str += "    (type String) ) ";        
+        str += "  (slot cheese";
+        str += "    (type String) ) ";
+        str += ")";
+        this.shell.eval( str );
+        this.shell.eval( "(deffunction max (?a ?b) (if (> ?a ?b) then (return ?a) else (return ?b) ) )" );
+
+        str = "";
+        str += "(defrule sendsomecheese ";
+        str += "  (Person (name ?name) (age ?personAge) (cheese ?cheeseType) (location \"london\") ) ";
+        str += "  (Cheese (type ?cheeseType) (price ?cheesePrice&:(eq (max ?personAge ?cheesePrice) ?cheesePrice)  ) )";
+        str += "\n=>\n";
+        str += "  (printout t \"send some \" ?cheeseType \" \" to \" \" ?name) ";
+        str += ")";
+        this.shell.eval( str );
+        
+        this.shell.eval( "(assert (Person (name mark) (location \"london\") (cheese \"cheddar\") (age 25) ) )" );
+        this.shell.eval( "(assert (Cheese (type \"cheddar\") (price  30) ) ) " );
+        this.shell.eval( "(run)" );
+        
+        assertEquals( "send some cheddar to mark",
+                      new String( this.baos.toByteArray() ) );        
+    }
 }




More information about the jboss-svn-commits mailing list