*/
public class Context {
private Map
public void assign(Variable var, boolean value) {
map.put(var, new Boolean(value));
}
public boolean lookup(Variable var) throws IllegalArgumentException {
Boolean value = map.get(var);
if (value == null) {
throw new IllegalArgumentException();
}
return value.booleanValue();
}
}
package com.bankht.Interpreter;
import java.util.HashMap;
import java.util.Map;
/**
* @author: ÌØÖÖ±ø¡ªAK47
* @´´½¨Ê±¼ä£º2012-7-3 ÏÂÎç03:11:54
*
* @Àà˵Ã÷ £º»·¾³(Context)ÀඨÒå³ö´Ó±äÁ¿µ½²¼¶ûÖµµÄÒ»¸öÓ³Éä
*/
public class Context {
private Map
public void assign(Variable var, boolean value) {
map.put(var, new Boolean(value));
}
public boolean lookup(Variable var) throws IllegalArgumentException {
Boolean value = map.get(var);
if (value == null) {
throw new IllegalArgumentException();
}
return value.booleanValue();
}
}
¡¡¡¡¿Í»§¶ËÀà
[java]
package com.bankht.Interpreter;
/**
* @author: ÌØÖÖ±ø¡ªAK47
* @´´½¨Ê±¼ä£º2012-7-3 ÏÂÎç03:12:17
*
* @Àà˵Ã÷ £º¿Í»§¶ËÀà
*/
public class Client {
public static void main(String[] args) {
Context ctx = new Context();
Variable x = new Variable("x");
Variable y = new Variable("y");
Constant c = new Constant(true);
ctx.assign(x, false);
ctx.assign(y, true);
Expression exp = new Or(new And(c, x), new And(y, new Not(x)));
System.out.println("x=" + x.interpret(ctx));
System.out.println("y=" + y.interpret(ctx));
System.out.println(exp.toString() + "=" + exp.interpret(ctx));
}
}
package com.bankht.Interpreter;
/**
* @author: ÌØÖÖ±ø¡ªAK47
* @´´½¨Ê±¼ä£º2012-7-3 ÏÂÎç03:12:17
*
* @Àà˵Ã÷ £º¿Í»§¶ËÀà
*/
public class Client {
public static void main(String[] args) {
Context ctx = new Context();
Variable x = new Variable("x");
Variable y = new Variable("y");
Constant c = new Constant(true);
ctx.assign(x, false);
ctx.assign(y, true);
Expression exp = new Or(new And(c, x), new And(y, new Not(x)));
System.out.println("x=" + x.interpret(ctx));
System.out.println("y=" + y.interpret(ctx));
System.out.println(exp.toString() + "=" + exp.interpret(ctx));
}
}
¡¡¡¡ÔËÐнá¹ûÈçÏ£º
[html]
x=false
y=true
((true AND x) OR (y AND (Not x)))=true
¡¡ ×÷Õߣºm13666368773