论坛首页 Java企业应用论坛

用Groovy动态编译的对象作为Drools的Fact遇到的问题

浏览 4300 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-09-26  
最近,希望实现将Groovy和Drools结合起来实现,全动态的Model和Rule的实现方案。发现Groovy动态生成的类在Drools中使用有一些兼容性的问题。

这是我的Unittest:

java 代码
 
  1. package com.dg;  
  2.   
  3. import groovy.lang.GroovyClassLoader;  
  4.   
  5. import java.io.IOException;  
  6. import java.io.StringReader;  
  7.   
  8. import org.drools.RuleBase;  
  9. import org.drools.RuleBaseConfiguration;  
  10. import org.drools.StatelessSession;  
  11. import org.drools.compiler.DroolsParserException;  
  12. import org.drools.compiler.PackageBuilder;  
  13. import org.drools.compiler.PackageBuilderErrors;  
  14. import org.drools.reteoo.ReteooRuleBase;  
  15. import org.junit.*;  
  16.   
  17.   
  18. public class DgTest {  
  19.     final static String ruleCode = "package com.test;\n" +  
  20.                                     "import com.dg.DgTest;\n" +  
  21.                                    "rule \"hello\"\n" +  
  22.                                    "  when \n" +   
  23.                                    "    x: DgTest();\n" +  
  24.                                    "  then \n" +  
  25.                                    "    x.fire(null);\n" +   
  26.                                    "end\n"+  
  27.                                    "";  
  28.     final static String ruleCode1 = "package com.test;\n" +  
  29.                                     "import com.dg.DgTest;\n" +  
  30.                                    "import com.test.DynamicModel;\n" +  
  31.                                    "rule \"hello\"\n" +  
  32.                                    "  when \n" +   
  33.                                    "    d: DynamicModel();\n" +  
  34.                                    "    x: DgTest();\n" +                                    
  35.                                    "  then \n" +  
  36.                                    "    x.fire(d);\n" +   
  37.                                    "end\n"+  
  38.                                    "";  
  39.     private boolean fired = false;  
  40.     RuleBase setupRule(ClassLoader cl, String code) {  
  41.         System.setProperty("drools.compiler""JANINO");  
  42.         if( cl != null )  
  43.             Thread.currentThread().setContextClassLoader( cl );  
  44.         PackageBuilder builder = new PackageBuilder();  
  45.   
  46.         StringReader reader = new StringReader( code );  
  47.         try {  
  48.             builder.addPackageFromDrl( reader );  
  49.         } catch (DroolsParserException e) {  
  50.             e.printStackTrace();  
  51.         } catch (IOException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.           
  55.         if( builder.hasErrors() ) {   
  56.             PackageBuilderErrors errors = builder.getErrors();  
  57.               
  58.             System.out.println( errors );  
  59.             Assert.assertFalse( builder.hasErrors() );  
  60.         }  
  61.           
  62.         RuleBaseConfiguration conf = new RuleBaseConfiguration();  
  63.           
  64.         RuleBase ruleBase = new ReteooRuleBase( conf );  
  65.           
  66.         try {  
  67.             ruleBase.addPackage( builder.getPackage());  
  68.         } catch (Exception e) {  
  69.             PackageBuilderErrors errs = builder.getErrors();  
  70.               
  71.             for( Object err : errs.getErrors()) {  
  72.             }  
  73.   
  74.             Assert.assertTrue( false );  
  75.         }  
  76.         return ruleBase;  
  77.           
  78.     }  
  79.   
  80.     void runRuleWithObject( RuleBase rule, Object obj) {  
  81.         StatelessSession wm = rule.newStatelessSession();  
  82.         Object objs[] = new Object[2];  
  83.         objs[0] = this;  
  84.         objs[1] = obj;  
  85.         wm.execute(objs);  
  86.         Assert.assertTrue( fired );  
  87.           
  88.     }  
  89.     /** 
  90.      * simple test 
  91.      * 
  92.      */  
  93.     @Test public void drools() {  
  94.         RuleBase rb = setupRule(null, ruleCode);  
  95.         Assert.assertNotNull( rb );  
  96.           
  97.         runRuleWithObject( rb , null );  
  98.     }  
  99.       
  100.     /** 
  101.      * groovy dynamic test 
  102.      * 
  103.      */  
  104.     @Test public void groovy() {  
  105.         GroovyClassLoader gcl = new GroovyClassLoader();  
  106.         Assert.assertNotNull( gcl );  
  107.         Class greetingClass = gcl.parseClass("package com.test;\n" +  
  108.                                             "class DynamicModel{\n" +  
  109.                                             "}\n"  
  110.                                             );  
  111.         Assert.assertNotNull( greetingClass );  
  112.           
  113.         RuleBase rb = setupRule(gcl, ruleCode1);  
  114.   
  115.         try {  
  116.             runRuleWithObject( rb , greetingClass.newInstance() );  
  117.         } catch (InstantiationException e) {  
  118.             // TODO Auto-generated catch block  
  119.             e.printStackTrace();  
  120.         } catch (IllegalAccessException e) {  
  121.             // TODO Auto-generated catch block  
  122.             e.printStackTrace();  
  123.         }  
  124.           
  125.     }  
  126.       
  127.     public void fire(Object obj) {  
  128.         fired = true;  
  129.         if( obj != null)  
  130.             System.out.println(obj);  
  131.     }  
  132. }  

在使用Groovy动态类时,编译Rule的时候总是报
Rule Compilation error : [Rule name=hello, agendaGroup=MAIN, salience=0, no-loop=false]
    com/test/Rule_hello_0.java (3:49) : Only a type can be imported. com.test.DynamicModel resolves to a package
    com/test/Rule_hello_0.java (7:231) : com.test.DynamicModel cannot be resolved to a type

那位能够给些建议呢?
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics