我无法找到正确的问题(堆栈溢出或谷歌)来查找可能已经存在的设计模式。我确实有一个有效的解决方案,但似乎过于复杂。我正在寻找寻求答案的地方的帮助,或类似问题的好例子。例如,在查询中处理多个表达式的GUI。
这是尝试解释这个问题:
最终,要解决的问题是处理多个表达式和多个逻辑运算,并通过从db读回表达式来实现。
我将表达式定义为左侧,操作符和右侧。就像你在声明中看到的那样:
(a==b)
因此可以使用多个表达式:
((a==b) && (c==d))
甚至
(((a==b) && (c==d)) || (e==f))
我可以很容易地将左侧,操作员和右侧存储在数据库中:
EXPID LEFTID RIGHTID LEFT OPERATOR RIGHT
1 'STATE' == 'NH'
2 'TYPE' == 'FUEL'
3 1 2 &&
将以
的形式回读((STATE==NH) && (TYPE==FUEL))
完美。除了它很快变得非常复杂。举个例子:
(ZIPCODE=='84770') AND (DEALER==NULL) AND (CAR_MODEL=='GTI') AND (CAR_BRAND=='VW') AND (LEGAL_OWNER==329778) AND (CAR_OWNER==459) AND ((STATE==UT) || (STATE==UT))
这需要像
这样的疯狂EXPID LEFTID RIGHTID LEFT OPERATOR RIGHT
1 ZIP == 84770
2 DEALER == NULL
3 CAR_MODEL == VW
4 CAR_BRAND == GTI
5 STATE == UT
6 STATE == NH
7 1 2 &&
8 3 4 &&
9 5 6 ||
10 7 8 &&
11 9 10 &&
如果想要将所有这些映射到GUI,问题就会变得更糟。
不确定它是否对这个问题有帮助,但这里是处理多个表达式逻辑的输出和代码:
Output:
$ javac Expression.java & java Expression
exp1: Expression [(A EQ A)] evaluates to [true]
exp2: Expression [(C EQ D)] evaluates to [false]
expOr (exp1 or exp2): Expression [((A EQ A) OR (C EQ D))] evaluates to [true]
expAnd (exp1 and exp2): Expression [((A EQ A) AND (C EQ D))] evaluates to [false]
complex (exp1 NEQ expAnd): Expression [((A EQ A) NEQ ((A EQ A) AND (C EQ D)))] evaluates to [true]
n1: Expression [(5 EQ 5)] evaluates to [true]
n2: Expression [(6 NEQ 5)] evaluates to [true]
n3: Expression [(5 NEQ 5)] evaluates to [false]
n4 (n1 EQ complex): Expression [((5 EQ 5) EQ ((A EQ A) NEQ ((A EQ A) AND (C EQ D))))] evaluates to [true]
public class Expression
{
enum Op {EQ,NEQ,OR,AND};
Object left;
Object right;
Op op;
public Expression(Object l,Op o,Object r)
{
left = l;
right = r;
op = o;
}
public void test()
{
test("");
}
public void test(String msg)
{
System.out.println(msg + ": Expression ["+toString()+"] evaluates to ["+evaluate()+"]");
}
public boolean evaluate()
{
if(Op.EQ == op) return equals();
if(Op.NEQ == op) return !equals();
if(Op.OR == op) return or();
if(Op.AND == op) return and();
return false;
}
//logical equals of expression with passed in expression
public boolean equals(Object o)
{
if(o instanceof Expression)
{
Expression exp = (Expression)o;
return evaluate() && exp.evaluate();
}
return false;
}
//compaire left and right for equality
public boolean equals()
{
return left.equals(right);
}
//logical OR between left and right Expressions
public boolean or()
{
if(left instanceof Expression && right instanceof Expression)
{
Expression expLeft = (Expression)left;
Expression expRight = (Expression)right;
return expLeft.evaluate() || expRight.evaluate();
}
return false;
}
//logical AND between left and right Expressions
public boolean and()
{
if(left instanceof Expression && right instanceof Expression)
{
Expression expLeft = (Expression)left;
Expression expRight = (Expression)right;
return expLeft.evaluate() && expRight.evaluate();
}
return false;
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("("+left.toString() + " " + op.toString() + " " + right.toString()+")");
return sb.toString();
}
public static void main(String[] args) throws Exception
{
//compare some strings
Expression exp1 = new Expression("A",Op.EQ,"A");
exp1.test("exp1");
Expression exp2 = new Expression("C",Op.EQ,"D");
exp2.test("exp2");
Expression expOr = new Expression(exp1,Op.OR,exp2);
expOr.test("expOr (exp1 or exp2)");
Expression expAnd = new Expression(exp1,Op.AND,exp2);
expAnd.test("expAnd (exp1 and exp2)");
Expression complex = new Expression(exp1,Op.NEQ,expAnd);
complex.test("complex (exp1 NEQ expAnd)");
//numeric comparisons
Expression n1 = new Expression(5,Op.EQ,5);
n1.test("n1");
Expression n2 = new Expression(6,Op.NEQ,5);
n2.test("n2");
Expression n3 = new Expression(5,Op.NEQ,5);
n3.test("n3");
//numeric and strings
Expression n4 = new Expression(n1,Op.EQ,complex);
n4.test("n4 (n1 EQ complex)");
}
}