⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dotnetblockconsequence.cs

📁 drools 一个开放源码的规则引擎
💻 CS
字号:
using org.drools.spi;
using org.drools.rule;
using System.Reflection;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections.Generic;
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Text;

namespace org.drools.semantics.dotnet
{
	public class DotNetBlockConsequence : Consequence
	{
		private int _id;
		private Rule _rule;
		private string _expression;
		private Assembly _assembly;
		private string _className;
		private string _methodName = "Invoke";

		public DotNetBlockConsequence(Rule rule, int id, string expression)
		{
			_id = id;
			_rule = rule;
			_expression = expression;
			_className = "Consequence_" + id;
			_assembly = Compile();
		}

		public Assembly Assembly
		{
			get { return _assembly; }
		}

		public void invoke(Tuple t)
		{
			try
			{
				List<object> parameters = new List<object>();
				foreach (Declaration d in _rule.getParameterDeclarations().toArray(new Declaration[] { }))
				{
					parameters.Add(t.get(d));
				}
				parameters.Add(new DefaultKnowledgeHelper(_rule, t));
				object o = _assembly.CreateInstance(this.GetType().Namespace + "." + _className);
				if (o == null) throw new ConsequenceException("Unable to find class: " + _className + " in " + this.ToString(), _rule);
				o.GetType().GetMethod(_methodName).Invoke(o, parameters.ToArray());
			}
			catch (Exception e)
			{
				throw new ConsequenceException(e.GetBaseException(), _rule, "Error executing " + this.ToString());
			}
		}

		private Assembly Compile()
		{
			//Generate Code
			CodeCompileUnit code = CodeGenerator.CreateConsequence(this.GetType().Namespace,
				_className, _methodName, (Declaration[]) 
				_rule.getParameterDeclarations().toArray(new Declaration[]{}),
				_expression, (DotNetImporter)_rule.getImporter(),
				(DotNetFunctions)_rule.getRuleSet().getFunctions("dotnet"));

			//Generate IL
			return CodeCompiler.Compile(code);
		}

		public override string ToString()
		{
			return "[Consequence: " + _expression + "]";
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -