最近,希望实现将Groovy和Drools结合起来实现,全动态的Model和Rule的实现方案。发现Groovy动态生成的类在Drools中使用有一些兼容性的问题。
这是我的Unittest:
在使用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
那位能够给些建议呢?
这是我的Unittest:
java 代码
- package com.dg;
- import groovy.lang.GroovyClassLoader;
- import java.io.IOException;
- import java.io.StringReader;
- import org.drools.RuleBase;
- import org.drools.RuleBaseConfiguration;
- import org.drools.StatelessSession;
- import org.drools.compiler.DroolsParserException;
- import org.drools.compiler.PackageBuilder;
- import org.drools.compiler.PackageBuilderErrors;
- import org.drools.reteoo.ReteooRuleBase;
- import org.junit.*;
- public class DgTest {
- final static String ruleCode = "package com.test;\n" +
- "import com.dg.DgTest;\n" +
- "rule \"hello\"\n" +
- " when \n" +
- " x: DgTest();\n" +
- " then \n" +
- " x.fire(null);\n" +
- "end\n"+
- "";
- final static String ruleCode1 = "package com.test;\n" +
- "import com.dg.DgTest;\n" +
- "import com.test.DynamicModel;\n" +
- "rule \"hello\"\n" +
- " when \n" +
- " d: DynamicModel();\n" +
- " x: DgTest();\n" +
- " then \n" +
- " x.fire(d);\n" +
- "end\n"+
- "";
- private boolean fired = false;
- RuleBase setupRule(ClassLoader cl, String code) {
- System.setProperty("drools.compiler", "JANINO");
- if( cl != null )
- Thread.currentThread().setContextClassLoader( cl );
- PackageBuilder builder = new PackageBuilder();
- StringReader reader = new StringReader( code );
- try {
- builder.addPackageFromDrl( reader );
- } catch (DroolsParserException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if( builder.hasErrors() ) {
- PackageBuilderErrors errors = builder.getErrors();
- System.out.println( errors );
- Assert.assertFalse( builder.hasErrors() );
- }
- RuleBaseConfiguration conf = new RuleBaseConfiguration();
- RuleBase ruleBase = new ReteooRuleBase( conf );
- try {
- ruleBase.addPackage( builder.getPackage());
- } catch (Exception e) {
- PackageBuilderErrors errs = builder.getErrors();
- for( Object err : errs.getErrors()) {
- }
- Assert.assertTrue( false );
- }
- return ruleBase;
- }
- void runRuleWithObject( RuleBase rule, Object obj) {
- StatelessSession wm = rule.newStatelessSession();
- Object objs[] = new Object[2];
- objs[0] = this;
- objs[1] = obj;
- wm.execute(objs);
- Assert.assertTrue( fired );
- }
- /**
- * simple test
- *
- */
- @Test public void drools() {
- RuleBase rb = setupRule(null, ruleCode);
- Assert.assertNotNull( rb );
- runRuleWithObject( rb , null );
- }
- /**
- * groovy dynamic test
- *
- */
- @Test public void groovy() {
- GroovyClassLoader gcl = new GroovyClassLoader();
- Assert.assertNotNull( gcl );
- Class greetingClass = gcl.parseClass("package com.test;\n" +
- "class DynamicModel{\n" +
- "}\n"
- );
- Assert.assertNotNull( greetingClass );
- RuleBase rb = setupRule(gcl, ruleCode1);
- try {
- runRuleWithObject( rb , greetingClass.newInstance() );
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void fire(Object obj) {
- fired = true;
- if( obj != null)
- System.out.println(obj);
- }
- }
在使用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
那位能够给些建议呢?