有时候一个问题出现,你很难找到准确的关键字去描述它,所以就查询不到解决的方法。不过可以去CSDN问问题,把场景描述出来,没准会有专家来解答。怎去去解析超级BOM的逻辑表达式,我一直有疑问,结果上CSDN问了问题后,发现原来是很简单的事情。
面前有一堆逻辑表达式,每一行决定配置BOM中的物料是否匹配配置中的特征值,如果逻辑运算为真,该物料被选择。如果逻辑运算为假,配置BOM中不出现这个物料。
解题的关键在于找到可以运算字符串的逻辑表达式的类库,C#中mxparser类库可以做到字符串的逻辑运行。于是,我写了一个DEMO程序,成功解析了其中的一行:
全部代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Windows.Forms.VisualStyles;
using System.Security.Policy;
using org.mariuszgromada.math.mxparser;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace BomSyntax
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "(((LG00 = LG01 AND LC00 = LC01 AND LL00 = LL01 AND (UV00 = UV02 OR UV00 = UV03) AND UT00 = UT01 AND LF00 = LF01 AND LB00 = LB13 AND LE00 = LE01 AND PA00 = PA04 AND TC00 = TC01)))";
}
private void button1_Click(object sender, EventArgs e)
{
//按AND、OR分割出特征
string[] separatingStrings = { "AND", "OR" };
string[] words = textBox1.Text.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);
//整理每一个特征格式
for(int i = 0;i< words.Count(); i++)
{
words[i] = words[i].Replace("(", "").Replace(")", "").Replace(" ", "").Replace("="," = ").ToString();
}
string tmp = textBox1.Text;
foreach (string one in words)
{
listBox1.Items.Add(one.ToString());
//假设这些特征都在配置表中被选中
tmp = tmp.Replace(one, "1");
}
textBox2.Text = tmp;
//把 AND和OR 转成 mxparser的逻辑运算符 &、|
textBox3.Text = tmp.Replace("AND", "&").Replace("OR", "|");
Expression e1 = new Expression(textBox3.Text);
textBox4.Text = e1.calculate().ToString();
}
}
}
----------2025.1.8-----超级BOM解析逻辑举例------------
一、物料与语法
10002365: PNO9 = 0 AND AB00 = AB01
16668888: PNO9 = 2 AND AB00 = AB02
二、特征
车型A: PN09 = 0 , AB00 = AB01
车型B: PN09 = 2 , AB00 = AB02
车型C: PN09 = 0 , AB00 = AB02
车型D: PN09 = 7 , AB00 = AB02
三、结果
车型A---10002365
车型B---16668888
车型C--- 空
车型D--- 空
四、计算逻辑
1、车型A的特征,逐一带入物料语法中:
10002365: 真 AND 真
16668888: 假 AND 假