NReco.LambdaParser使用案例
使用案例集合:
private async void RuleEngine_Click(object sender, EventArgs e)
{
#region 获取变量
string expression = this.Rule.Text.Trim();
string pattern = @"\$(.*?)\$";
MatchCollection matches = Regex.Matches(expression, pattern);
foreach (Match match in matches)
{
string variableName = match.Groups[1].Value;
}
#endregion
#region 执行器初始化
var lambdaParser = new LambdaParser();
lambdaParser.AllowVars = true;
#endregion
#region 变量初始化
var tagsql = string.Empty;
var dictLast = new Dictionary<string, object>();
#endregion
#region 常用方法与数据定义
dictLast["PI"] = 3.14M;
dictLast["Now"] = DateTime.Now;
dictLast["Not"] = (Func<bool, bool>)((t) => !t);
dictLast["Max"] = (Func<decimal, decimal, decimal>)((a, b) => Math.Max(a, b));
dictLast["Yes"] = true;
dictLast["No"] = false;
dictLast["arr"] = new double[] { 1.5, 2.5 };
dictLast["dic"] = new Dictionary<string, int>()
{
{"apple", 1},
{"banana", 2},
{"orange", 3}
};
dictLast["Alert"] = (Func<string, string>)((msg) => AlertAsync(msg));
#endregion
#region 判断字符是否正确输入
string input = "Now.ToString(\"yyyy-MM-dd/HH\")";
string backslash = @"(?<!\\)"""; // 匹配没有被转义的双引号
MatchCollection matches2 = Regex.Matches(input, backslash);
if (matches.Count > 0)
Console.WriteLine("双引号没有被正确转义");
else
Console.WriteLine("双引号都有正确转义");
#endregion
#region test
Console.WriteLine(lambdaParser.Eval("Now.ToString(\"yyyy-MM-dd/HH\")", dictLast));
Console.WriteLine(lambdaParser.Eval("Max(Max(3,4),2)", dictLast));
Console.WriteLine(lambdaParser.Eval("arr[0]+arr[1]", dictLast)); // -> 4
Console.WriteLine(lambdaParser.Eval("dic[\"apple\"]+arr[1]", dictLast)); // -> 3.5
Console.WriteLine(lambdaParser.Eval("(new[]{1,2})[1]", dictLast)); // 2
#endregion
int i = 0;
foreach (var v in this.Variables.Text.Trim().Split(','))
{
if (i == 0)
tagsql = this.DataSet1.Text.Trim();
else
tagsql = this.DataSet2.Text.Trim();
var rtTagInfos = await _dbInstance.DBOperation.QueryAsync<dynamic>(tagsql);
if (rtTagInfos.Count() == 1)
{
var dictSingle = ((IDictionary<string, object>)rtTagInfos.FirstOrDefault())
.ToDictionary(kvp => kvp.Key.Split(new[] { '(', ')' }).LastOrDefault(), kvp => kvp.Value);
dictLast = dictLast.Union(dictSingle).ToDictionary(x => x.Key, x => x.Value);
}
else
{
#region 没有去重
//var keyValuePairs = new List<KeyValuePair<string, object>>();
//foreach (var record in rtTagInfos)
//{
// int i = 0;
// string propertyName = string.Empty;
// foreach (var property in (IDictionary<string, object>)record)
// {
// if (i == 0)
// propertyName = Convert.ToString(property.Value);
// else
// keyValuePairs.Add(new KeyValuePair<string, object>(propertyName, property.Value));
// i = i + 1;
// }
//}
//var dictlist = keyValuePairs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
#endregion
var keyValuePairs = new HashSet<KeyValuePair<string, object>>();
Parallel.ForEach(rtTagInfos, (record) =>
{
int i = 0;
string propertyName = string.Empty;
foreach (var property in (IDictionary<string, object>)record)
{
if (i == 0)
{
propertyName = Convert.ToString(property.Value);
}
else
{
var keyValuePair = new KeyValuePair<string, object>(propertyName, property.Value);
lock (keyValuePairs)
{
if (!keyValuePairs.Contains(keyValuePair))
{
if (!keyValuePairs.Any(kvp => kvp.Key == keyValuePair.Key))
{
keyValuePairs.Add(keyValuePair);
}
}
}
}
i = i + 1;
}
});
var dictlist = keyValuePairs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
dictLast = dictLast.Union(dictlist).ToDictionary(x => x.Key, x => x.Value);
}
i = i + 1;
}
var res = lambdaParser.Eval(this.Rule.Text.Trim().Replace("$", ""), dictLast);
MessageBox.Show(res?.ToString());
}
public string AlertAsync(string msg)
{
var time = "SELECT NOW() as now";
var rtTagInfos = AsyncHelper.RunSync(() => _dbInstance.DBOperation.QueryFirstOrDefaultAsync<DateTime>(time));
return msg + rtTagInfos.ToString();
}
$reportvalue$<1234?"1":Alert("告警:")