import java.util.ArrayList;
import java.util.List;
public class LogicalMdxTest {
public static void main(String[] args) {
String[] input = new String[] { "a and b or c and d or e", "a", "a and b", "a or b", "a and b and c",
"a or b or c", "a and b and c or d", "a or b and c and d" };
for (String item : input) {
List<LogicalMdx> mdx = getMdx(item);
System.out.println(item);
String mdxStr = getMdxStr(mdx);
System.out.println(mdxStr);
if (item.equals(mdxStr)) {
System.out.println("Success-----");
} else {
System.out.println("Failed----");
}
}
}
private static String getMdxStr(List<LogicalMdx> mdx) {
StringBuilder sb = new StringBuilder();
for (LogicalMdx item : mdx) {
sb.append(item.getPrefix());
sb.append(item.getContent());
}
return sb.toString();
}
private static List<LogicalMdx> getMdx(String input) {
List<LogicalMdx> result = new ArrayList<LogicalMdx>();
String[] ands = input.split(" and ");
LogicalMdx item = new LogicalMdx();
for (int i = 0; i < ands.length; i++) {
String and = ands[i];
if (i > 0) {
item.setPrefix(" and ");
}
String[] ors = and.split(" or ");
for (int j = 0; j < ors.length; j++) {
String or = ors[j];
if (j > 0) {
item.setPrefix(" or ");
}
item.setContent(or);
result.add(item);
item = new LogicalMdx();
}
}
return result;
}
}
逻辑拼接
最新推荐文章于 2025-08-06 16:23:50 发布