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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri May 4 22:29:42 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-05-04 22:29:41 -0400 (Fri, 04 May 2007)
New Revision: 11697

Added:
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/ParserHandler.java
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/Shell.java
Modified:
   labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/BlockExecutionEngine.java
   labs/jbossrules/trunk/drools-clips/src/main/resources/org/drools/clp/CLP.g
   labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/CompiledFunctionsTest.java
   labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/DeffunctionTest.java
Log:
-start of shell

Modified: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/BlockExecutionEngine.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/BlockExecutionEngine.java	2007-05-05 00:25:25 UTC (rev 11696)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/BlockExecutionEngine.java	2007-05-05 02:29:41 UTC (rev 11697)
@@ -13,8 +13,6 @@
     implements
     Consequence,
     ExecutionEngine {
-    //private Map variableMap = new HashMap();
-
     private FunctionCaller[] functions;
 
     private int              index;

Added: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/ParserHandler.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/ParserHandler.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/ParserHandler.java	2007-05-05 02:29:41 UTC (rev 11697)
@@ -0,0 +1,12 @@
+package org.drools.clp;
+
+import org.drools.lang.descr.RuleDescr;
+
+public interface ParserHandler {       
+    
+    //public void functionHandler(RuleDescr ruleDescr);
+    
+    public void ruleDescrHandler(RuleDescr ruleDescr);
+    
+    public void lispFormHandler(ExecutionEngine engine);
+}

Added: labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/Shell.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/Shell.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-clips/src/main/java/org/drools/clp/Shell.java	2007-05-05 02:29:41 UTC (rev 11697)
@@ -0,0 +1,103 @@
+package org.drools.clp;
+
+import java.io.Reader;
+import java.util.Iterator;
+import java.util.List;
+
+import org.antlr.runtime.ANTLRReaderStream;
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.Lexer;
+import org.antlr.runtime.TokenStream;
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.WorkingMemory;
+import org.drools.compiler.PackageBuilder;
+import org.drools.compiler.SwitchingCommonTokenStream;
+import org.drools.lang.DRLLexer;
+import org.drools.lang.descr.AttributeDescr;
+import org.drools.lang.descr.PackageDescr;
+import org.drools.lang.descr.RuleDescr;
+
+public class Shell implements ParserHandler{
+    private FunctionRegistry registry;
+    //private Map packageBulders;
+    
+    private RuleBase ruleBase;
+    private WorkingMemory workingMemory;
+    
+    public Shell() {
+        this.ruleBase = RuleBaseFactory.newRuleBase();
+        this.workingMemory = ruleBase.newWorkingMemory();
+    }
+    
+    public void evalReader(Reader reader)  {
+        try {
+            CLPParser parser = new CLPParser( new SwitchingCommonTokenStream( new DRLLexer( new ANTLRReaderStream( reader ) ) ) );
+            evalParser( parser );    
+        } catch (Exception e) {
+            throw new RuntimeException( "Unable to parser Reader", e);
+        }      
+    }
+    
+    public void evalString(String text) {
+        CLPParser parser =  new CLPParser( newTokenStream( newLexer( newCharStream( text ) ) ) ) ;
+        evalParser( parser );
+    }
+    
+    private void evalParser(CLPParser parser) {
+        parser.setFunctionRegistry( this.registry );
+        //parser.setF
+    }
+       
+//    public void functionHandler(FunctionDescr ruleDescr) {
+//        throw new RuntimeException( "Drools Clips does not support FunctionDescr" );
+//    }    
+
+    public void lispFormHandler(LispForm lispForm) {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public void ruleDescrHandler(RuleDescr ruleDescr) {
+        String module = getModuleName( ruleDescr.getAttributes() );
+        
+        PackageDescr pkg = new PackageDescr(module);
+        
+        PackageBuilder builder = new PackageBuilder();
+        builder.addPackage( pkg );     
+    }  
+    
+    public String getModuleName(List list) {
+        for ( Iterator it = list.iterator(); it.hasNext(); ) {
+            AttributeDescr attr = ( AttributeDescr ) it.next();
+            if ( attr.getName().equals( "agenda-group" ) ) {
+                return attr.getValue();
+            }
+        }
+        return "MAIN";
+    }    
+    
+//    public PackageBuilder getBuilder(String namespace) {
+//        PackageBuilder builder = (PackageBuilder) this.packageBulders.get( namespace );
+//        if ( builder == null ) {
+//            builder = new PackageBuilder();
+//            this.packageBulders.put( namespace, builder );
+//        }
+//        return builder;
+//        
+//    }
+
+    private CharStream newCharStream(final String text) {
+        return new ANTLRStringStream( text );
+    }
+
+    private CLPLexer newLexer(final CharStream charStream) {
+        return new CLPLexer( charStream );
+    }
+
+    private TokenStream newTokenStream(final Lexer lexer) {
+        return new SwitchingCommonTokenStream( lexer );
+    }
+
+}

Modified: labs/jbossrules/trunk/drools-clips/src/main/resources/org/drools/clp/CLP.g
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/main/resources/org/drools/clp/CLP.g	2007-05-05 00:25:25 UTC (rev 11696)
+++ labs/jbossrules/trunk/drools-clips/src/main/resources/org/drools/clp/CLP.g	2007-05-05 02:29:41 UTC (rev 11697)
@@ -20,10 +20,8 @@
 	private int lineOffset = 0;
 	private DescrFactory factory = new DescrFactory();
 	private boolean parserDebug = false;
-	private FunctionRegistry functionRegistry;
+	private FunctionRegistry functionRegistry;	
 	
-    FunctionRegistry factoryRegistry;
-	
 	public void setFunctionRegistry(FunctionRegistry functionRegistry) {
 		this.functionRegistry = functionRegistry;
 	}
@@ -222,6 +220,13 @@
 	;	
 */
 
+eval_script[ParserHandler parserHandler]
+	:	(		r=defrule { parserHandler.ruleDescrHandler( r ); }
+			|	e=execution_block { parserHandler.lispFormHandler( e ); }
+		)
+	;
+	
+
 deffunction
 	:
 	;
@@ -272,7 +277,7 @@
 		
 		'=>'
 		
-		engine=rhs { rule.setConsequence( engine ); }
+		engine=execution_block { rule.setConsequence( engine ); }
 		
 		RIGHT_PAREN
 	;
@@ -311,7 +316,7 @@
 		)
 	;
 
-rhs returns[ExecutionEngine engine]
+execution_block returns[ExecutionEngine engine]
 	@init {
 	        engine = new BlockExecutionEngine();
 			ExecutionBuildContext context = new ExecutionBuildContext( engine, functionRegistry );  	

Modified: labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/CompiledFunctionsTest.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/CompiledFunctionsTest.java	2007-05-05 00:25:25 UTC (rev 11696)
+++ labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/CompiledFunctionsTest.java	2007-05-05 02:29:41 UTC (rev 11697)
@@ -262,6 +262,11 @@
         
         assertEquals( "416256", new String( bais.toByteArray() ) );          
     }
+    
+    public void testFactorial() throws Exception {
+        BlockExecutionEngine engine = (BlockExecutionEngine) parse( "(deffunction factorial (?n) (if (>= ?n 1) then (* ?n (factorial (- ?n 1))) else 1))" ).rhs();
+        //engine.ex
+    }
 
     private CLPParser parse(final String text) throws Exception {
         this.parser = newParser( newTokenStream( newLexer( newCharStream( text ) ) ) );

Modified: labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/DeffunctionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/DeffunctionTest.java	2007-05-05 00:25:25 UTC (rev 11696)
+++ labs/jbossrules/trunk/drools-clips/src/test/java/org/drools/clp/DeffunctionTest.java	2007-05-05 02:29:41 UTC (rev 11697)
@@ -34,7 +34,5 @@
         w.setValue( context,  new LongValueHandler( 7 ) );
         
         assertEquals( new BigDecimal( 17 ), function.execute( new ValueHandler[] { q, w }, context ).getBigDecimalValue( context ) );
-    }
-    
-    
+    }      
 }




More information about the jboss-svn-commits mailing list