drools.net阅读DRL文件

时间:2012-04-20 21:07:11

标签: c# drools

我正在尝试运行一个使用drool.net的简单应用程序。每当我执行时,应用程序崩溃在builder.AddPackageFromDrl("DroolsApp.Rules.drl", stream);错误声明NullReferenceException未处理且Object引用未设置对象的实例。我试图更改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: 

2 个答案:

答案 0 :(得分:3)

这确实是drl文件中的错误。尝试删除所有规则和导入,编译它们,然后添加单行和重复(它必须验证)。 Drools.net使用NullReferenceException响应编译错误或加载CompiledRules0.dll时出错(因为它未生成)。添加预构建构建事件以删除先前生成的CompiledRules0.dll(当Drools.net无法生成它使用前一个文件的文件时)也是一件好事。这是代码:

cd $(TargetDir)
del CompiledRules0.*

答案 1 :(得分:0)

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