非常基本的drools 5运行,基本设置和快速启动

时间:2010-04-21 23:41:46

标签: java jboss rules drools

drools是否有更全面的快速入门5.我试图运行简单的Hello World .drl规则,但我想通过一个ant脚本来完成,可能只使用javac / java:

我收到以下错误:注意:没有Eclipse或任何其他IDE,我没有完全运行:

drools是否有更全面的快速入门5.我试图运行简单的Hello World .drl规则,但我想通过一个ant脚本来完成,可能只使用javac / java:

我收到以下错误:注意:没有Eclipse或任何其他IDE,我没有完全运行:

test:
     [java] Exception in thread "main" org.drools.RuntimeDroolsException: Unable to load d
ialect 'org.drools.rule.builder.dialect.java.JavaDialectConfiguration:java:org.drools.rule
.builder.dialect.java.JavaDialectConfiguration'
     [java]     at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuild
erConfiguration.java:274)
     [java]     at org.drools.compiler.PackageBuilderConfiguration.buildDialectConfigurati
onMap(PackageBuilderConfiguration.java:259)
     [java]     at org.drools.compiler.PackageBuilderConfiguration.init(PackageBuilderConf
iguration.java:176)
     [java]     at org.drools.compiler.PackageBuilderConfiguration.<init>(PackageBuilderCo
nfiguration.java:153)
     [java]     at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:242)
     [java]     at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:142)
     [java]     at org.drools.builder.impl.KnowledgeBuilderProviderImpl.newKnowledgeBuilde
r(KnowledgeBuilderProviderImpl.java:29)
     [java]     at org.drools.builder.KnowledgeBuilderFactory.newKnowledgeBuilder(Knowledg
eBuilderFactory.java:29)
     [java]     at org.berlin.rpg.rules.Rules.rules(Rules.java:33)
     [java]     at org.berlin.rpg.rules.Rules.main(Rules.java:73)
     [java] Caused by: java.lang.RuntimeException: The Eclipse JDT Core jar is not in the
classpath
     [java]     at org.drools.rule.builder.dialect.java.JavaDialectConfiguration.setCompil
er(JavaDialectConfiguration.java:94)
     [java]     at org.drools.rule.builder.dialect.java.JavaDialectConfiguration.init(Java
DialectConfiguration.java:55)
     [java]     at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuild
erConfiguration.java:270)
     [java]     ... 9 more
     [java] Java Result: 1

...
...

我在javac和java target中包含以下库:

 <path id="classpath">
     <pathelement location="${lib.dir}" />
     <pathelement location="${lib.dir}/drools-api-5.0.1.jar" />
     <pathelement location="${lib.dir}/drools-compiler-5.0.1.jar" />
     <pathelement location="${lib.dir}/drools-core-5.0.1.jar" />
     <pathelement location="${lib.dir}/janino-2.5.15.jar" />
 </path>

这是抛出错误的Java代码。我注释掉了java.compiler代码,它也没有用。

public void rules(){

/*
final Properties properties = new Properties(); 
properties.setProperty( "drools.dialect.java.compiler", "JANINO" ); 
PackageBuilderConfiguration cfg = new PackageBuilderConfiguration( properties ); 
JavaDialectConfiguration javaConf = (JavaDialectConfiguration) 
cfg.getDialectConfiguration( "java" ); 
*/
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

// this will parse and compile in one step
kbuilder.add(ResourceFactory.newClassPathResource("HelloWorld.drl", Rules.class), ResourceType.DRL);

// Check the builder for errors
if (kbuilder.hasErrors()) {
    System.out.println(kbuilder.getErrors().toString());
    throw new RuntimeException("Unable to compile \"HelloWorld.drl\".");
}

// Get the compiled packages (which are serializable)
final Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();

// Add the packages to a knowledgebase (deploy the knowledge packages).
final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(pkgs);

final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
ksession.setGlobal("list", new ArrayList<Object>());

ksession.addEventListener(new DebugAgendaEventListener());
ksession.addEventListener(new DebugWorkingMemoryEventListener());

// Setup the audit logging
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/helloworld");

final Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
ksession.insert(message);

ksession.fireAllRules();
logger.close();
ksession.dispose();

}

...

这里我不认为Ant是相关的,因为我将fork设置为true:

 <target name="test" depends="compile">
     <java classname="org.berlin.rpg.rules.Rules" fork="true">
         <classpath refid="classpath.rt" />
         <classpath>
             <pathelement location="${basedir}" />
             <pathelement location="${build.classes.dir}" />
         </classpath>
     </java>
 </target>

错误在第1行引发。

基本上,我没有做任何事情,除了打电话

final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

我正在运行Windows XP,Java6和Ant.1.7。 Drools-Rules的最新版本(截至昨天)。

2 个答案:

答案 0 :(得分:9)

问题的关键是错误列表中的这一行: “引起:java.lang.RuntimeException:Eclipse JDT Core jar不在类路径中”

这个引用是由Eclipse Drools插件安装的库“core-3.4.2.v_883_R34x.jar”

如果将core-3.4.2.v_883_R34x.jar添加到库中,则不会获得运行时异常。

答案 1 :(得分:2)

我尝试了drools-5.0-examples HelloWorld,然后使用Maven编写了示例代码并设法使其正常工作。然后,我构建了一个Ant构建文件,其中包含您描述的特征,并得到与您完全相同的结果。

我注意到Maven版本包含更多依赖库。如果我将这些依赖项从Maven复制到Ant版“lib”目录并更新build.xml以包含这些,那么您的代码可以正常工作。

<path id="classpath.rt">
  <pathelement location="${lib.dir}/antlr-runtime-3.1.1.jar" />
  <pathelement location="${lib.dir}/core-3.4.2.v_883_R34x.jar" />
  <pathelement location="${lib.dir}/drools-api-5.0.1.jar" />
  <pathelement location="${lib.dir}/drools-compiler-5.0.1.jar" />
  <pathelement location="${lib.dir}/drools-core-5.0.1.jar" />
  <pathelement location="${lib.dir}/drools-transformer-xstream-5.0.1.jar" />
  <pathelement location="${lib.dir}/janino-2.5.15.jar" />
  <pathelement location="${lib.dir}/joda-time-1.6.jar" />
  <pathelement location="${lib.dir}/mvel2-2.0.10.jar" />
  <pathelement location="${lib.dir}/xpp3_min-1.1.4c.jar" />
  <pathelement location="${lib.dir}/xstream-1.3.1.jar" />      
</path>