1

我正在尝试运行一个使用 drool.net 的简单应用程序。每当我执行时,应用程序在builder.AddPackageFromDrl("DroolsApp.Rules.drl", stream);错误处崩溃,指出 NullReferenceException 未处理并且对象引用未设置对象的实例。我试图更改 drl 文件,但错误仍然存​​在。我错过了什么吗?还有没有任何网站提供使用 drools.net 的详细教程,我是否需要任何特定的程序来创建和编辑 drl 文件,因为我使用的是一个简单的记事本。

提前致谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.drools.dotnet.compiler;
using org.drools.dotnet.rule;
using org.drools.dotnet;
using System.IO;
using System.Reflection;

namespace DroolsApp
{
    class Program
    {   
        static void Main(string[] args)
        {            
            PackageBuilder builder = new PackageBuilder();
            Stream stream = Assembly.GetExecutingAssembly()
            .GetManifestResourceStream("DroolsApp.Rules.drl");
            builder.AddPackageFromDrl("DroolsApp.Rules.drl", stream);
            Package pkg = builder.GetPackage();
            RuleBase ruleBase = RuleBaseFactory.NewRuleBase();
            ruleBase.AddPackage(pkg);            
            executeRules(ruleBase, 5);
            executeRules(ruleBase, 6);
            executeRules(ruleBase, 7);
        }

        public static void executeRules(RuleBase ruleBase, int amount)
        {
            WorkingMemory workingMemory = ruleBase.NewWorkingMemory();
            Quest quest = new Quest();
            quest.Value = amount;
            workingMemory.assertObject(quest);
            workingMemory.fireAllRules();
        }
    }
}   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DroolsApp
{
    class Quest
    {
        public int Value { set; get; }
    }
}

DRL File:    

package DroolsApp

rule "NumberFive"
    when
        quest : Quest( Value == 5 )
    then
            Console.WriteLine("FIVE");
end 

rule "NumberSix"
    when
        quest : Quest( Value == 6 )
    then
            Console.WriteLine("SIX");
end

堆栈跟踪:

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=drools.dotnet
  StackTrace:
       at org.drools.dotnet.semantics.DotnetClassTypeResolver.resolveType(String className)
       at org.drools.semantics.java.RuleBuilder.build(ColumnDescr columnDescr)
       at org.drools.semantics.java.RuleBuilder.build(RuleDescr ruleDescr)
       at org.drools.semantics.java.RuleBuilder.build(Package pkg, RuleDescr ruleDescr)
       at org.drools.compiler.PackageBuilder.addRule(RuleDescr ruleDescr)
       at org.drools.compiler.PackageBuilder.addPackage(PackageDescr packageDescr)
       at org.drools.compiler.PackageBuilder.addPackageFromDrl(String fileName, Reader reader)
       at org.drools.dotnet.compiler.PackageBuilder.AddPackageFromDrl(String fileName, Stream drlStream)
       at MinimalDroolsForm.Program.Main(String[] args) in C:\Users\Carl\Desktop\DroolsApp\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
4

2 回答 2

3

这肯定是 drl 文件中的错误。尝试删除所有规则和导入,编译它们,然后添加单行并重复(它必须验证)。Drools.net 对带有 NullReferenceException 的编译错误或加载 CompiledRules0.dll 的错误做出反应(因为它没有生成)。添加一个 Pre-build Build Event 删除之前生成的 CompiledRules0.dll 也是一件好事(当 Drools.net 无法生成文件时,它使用前一个)。这是代码:

cd $(TargetDir)
del CompiledRules0.*
于 2012-12-06T09:47:31.213 回答
0

两个提示: 1) 在调用 GetManifestResourceStream 方法时,一定要传递命名空间和文件名作为参数。2)在Visual Studio(如果您正在使用它)中,规则文件属性,将构建操作设置为嵌入式资源。

于 2012-04-22T13:18:12.917 回答