public void BooleanExpressionComplexSerializeTest() { ItemFilter filter1 = new CategoryFilter("cat"); IBooleanExpression leaf1 = new MaxAmount(10, filter1); ItemFilter filter2 = new AllProductsFilter(); IBooleanExpression leaf2 = new UserCountry("Wakanda forever", filter2); IBooleanExpression complex = new XorExpression(); leaf1.id = 1; leaf2.id = 2; complex.addChildren(leaf1, leaf2); string json = JsonConvert.SerializeObject(complex, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); IBooleanExpression result = JsonConvert.DeserializeObject <IBooleanExpression>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); Assert.IsInstanceOfType(result, typeof(XorExpression)); Assert.IsInstanceOfType(((XorExpression)result).firstChild, typeof(MaxAmount)); Assert.IsInstanceOfType(((XorExpression)result).secondChild, typeof(UserCountry)); }
public void DiscountSerializeTest() { ItemFilter filter1 = new CategoryFilter("cat"); IBooleanExpression leaf1 = new MaxAmount(10, filter1); ItemFilter filter2 = new AllProductsFilter(); IBooleanExpression leaf2 = new UserCountry("Wakanda forever", filter2); IBooleanExpression complex = new XorExpression(); //we add manually ids for the leaves becuase there is no stub for them at the moment. //Not adding these ids will cause the json serializer to falsly think it has a loop (parent and children have the same id -> .Equals() = true) leaf2.id = 10; leaf1.id = 11; complex.addChildren(leaf1, leaf2); //TODO: when there are concrete Outcomes, we can test this //IOutcome outcome = new //Discount discount = new Discount(complex,) string json = JsonConvert.SerializeObject(complex, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); IBooleanExpression result = JsonConvert.DeserializeObject <IBooleanExpression>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); Assert.IsInstanceOfType(result, typeof(XorExpression)); Assert.IsInstanceOfType(((XorExpression)result).firstChild, typeof(MaxAmount)); Assert.IsInstanceOfType(((XorExpression)result).secondChild, typeof(UserCountry)); }
public static void SerializeXorExpression() { var v = new BooleanLiteral(true); var a = new XorExpression(v, v); var b = SerializationUtil.Reserialize(a); Assert.AreEqual(a, b); }
void IExpressionVisitor.Visit(XorExpression expression) { Label labelFalse = generator.DefineLabel(); Label labelEnd = generator.DefineLabel(); expression.LeftChildExpression.AcceptInternal(this); expression.RightChildExpression.AcceptInternal(this); generator.Emit(OpCodes.Ceq); generator.IfFalseGoto(labelFalse); expression.FalseValue.AcceptInternal(this); generator.Goto(labelEnd); generator.MarkLabel(labelFalse); expression.TrueValue.AcceptInternal(this); generator.MarkLabel(labelEnd); }
public static int Evaluate(Expression exp, int var) { if (exp is ConstantExpression) { ConstantExpression tExp = exp as ConstantExpression; return((int)(exp as ConstantExpression).Value); } else if (exp is VariableExpression) { return(var); } else if (exp is AddExpression) { AddExpression nExp = (AddExpression)exp; return(Evaluate(nExp.OperandA, var) + Evaluate(nExp.OperandB, var)); } else if (exp is SubExpression) { SubExpression nExp = (SubExpression)exp; return(Evaluate(nExp.OperandA, var) - Evaluate(nExp.OperandB, var)); } else if (exp is MulExpression) { MulExpression nExp = (MulExpression)exp; return(Evaluate(nExp.OperandA, var) * Evaluate(nExp.OperandB, var)); } else if (exp is DivExpression) { DivExpression nExp = (DivExpression)exp; return(Evaluate(nExp.OperandA, var) / Evaluate(nExp.OperandB, var)); } else if (exp is NegExpression) { NegExpression nExp = (NegExpression)exp; return(-Evaluate(nExp.Value, var)); } else if (exp is InvExpression) { InvExpression nExp = (InvExpression)exp; return(~Evaluate(nExp.Value, var)); } else if (exp is XorExpression) { XorExpression nExp = (XorExpression)exp; return(Evaluate(nExp.OperandA, var) ^ Evaluate(nExp.OperandB, var)); } throw new NotSupportedException(); }
public void XORevaluateTest() { FalseCondition falseCondition = new FalseCondition(); TrueCondition trueCondition = new TrueCondition(); XorExpression xor = new XorExpression(); xor.addChildren(trueCondition, trueCondition); Assert.IsFalse(xor.evaluate(list, user)); xor.addChildren(trueCondition, falseCondition); Assert.IsTrue(xor.evaluate(list, user)); xor.addChildren(falseCondition, trueCondition); Assert.IsTrue(xor.evaluate(list, user)); xor.addChildren(falseCondition, falseCondition); Assert.IsFalse(xor.evaluate(list, user)); }
public static bool?Evaluate(XorExpression exp, PredicateList state) { bool?lhs = ExpressionExecutive.Evaluate(exp.lhs, state); bool?rhs = ExpressionExecutive.Evaluate(exp.rhs, state); if (lhs == true && rhs == true) { return(false); } if (lhs == true && rhs == false) { return(true); } if (lhs == true && rhs == null) { return(null); } if (lhs == false && rhs == true) { return(true); } if (lhs == false && rhs == false) { return(false); } if (lhs == false && rhs == null) { return(null); } if (lhs == null && rhs == true) { return(null); } if (lhs == null && rhs == false) { return(null); } if (lhs == null && rhs == null) { return(null); } throw new NotImplementedException(); }
private IBooleanExpression createComplexExpression(JObject info) { string primeType = (string)info["type"]; IBooleanExpression firstChild = null, secondChild = null, toReturn; switch (primeType) { case "leaf": return(createLeaf((JObject)info["data"])); case "and": firstChild = createComplexExpression((JObject)info["firstChild"]); secondChild = createComplexExpression((JObject)info["secondChild"]); toReturn = new AndExpression(); break; case "or": firstChild = createComplexExpression((JObject)info["firstChild"]); secondChild = createComplexExpression((JObject)info["secondChild"]); toReturn = new OrExpression(); break; case "xor": firstChild = createComplexExpression((JObject)info["firstChild"]); secondChild = createComplexExpression((JObject)info["secondChild"]); toReturn = new XorExpression(); break; default: throw new Exception("unknon purchasing type"); } if (firstChild != null && secondChild != null) { toReturn.addChildren(firstChild, secondChild); return(toReturn); } else { throw new Exception("can't create children in policy type"); } }
private void addDiscountPolicy() { bridge.Login(getStoreOwner1(), password); ItemFilter filter1 = new AllProductsFilter(); IBooleanExpression leaf1 = new MinAmount(5, filter1); ItemFilter filter2 = new AllProductsFilter(); IBooleanExpression leaf2 = new MinAmount(10, filter2); IBooleanExpression complex = new XorExpression(); //we add manually ids for the leaves becuase there is no stub for them at the moment. //Not adding these ids will cause the json serializer to falsly think it has a loop (parent and children have the same id -> .Equals() = true) leaf2.id = 20; leaf1.id = 21; complex.addChildren(leaf1, leaf2); IOutcome outcome = new FreeProduct(productId, 1); Discount discount = new Discount(complex, outcome); string json = JsonHandler.SerializeObject(discount); policyId = bridge.addDiscountPolicy(storeId, json); Assert.IsTrue(policyId >= 0); }
private bool ParseXorIfExists(Lexer lexer, Token token, Attributes attributes) { var successfullyParsed = true; successfullyParsed &= ParseOrIfExists(lexer, token, attributes); if (lexer.GetCurrentToken().Is(TokenType.XorOperator)) { var retTree = attributes["EXP"] as Expression; // SEM: Sólo puede aplicarse un XOR entre booleanos if (retTree.Type != DataType.Boolean) { LogTypeExpressionInvalid(lexer.GetCurrentToken(), DataType.Boolean, retTree.Type); successfullyParsed = false; } do { var tree = new XorExpression(); tree.LeftOperand = retTree; successfullyParsed &= ParseOrIfExists(lexer, lexer.GetNextToken(), attributes); tree.RightOperand = attributes["EXP"] as Expression; // SEM: Sólo puede aplicarse un XOR entre booleanos if (tree.RightOperand.Type != DataType.Boolean) { LogTypeExpressionInvalid(lexer.GetCurrentToken(), DataType.Boolean, tree.RightOperand.Type); successfullyParsed = false; } retTree = tree; } while (lexer.GetCurrentToken().Is(TokenType.XorOperator)); attributes["EXP"] = retTree; } return(successfullyParsed); }
public void Visit(XorExpression x) { }
public AbstractType Visit(XorExpression x) { return(OpExpressionType(x)); }
// TODO: Implement operator precedence (see http://forum.dlang.org/thread/[email protected] ) public ISymbolValue Visit(XorExpression x) { return E_MathOp(x); }
IExpression XorExpression(IBlockNode Scope = null) { var left = AndExpression(Scope); if (laKind != Xor) return left; Step(); var ae = new XorExpression(); ae.LeftOperand = left; ae.RightOperand = XorExpression(Scope); return ae; }
public virtual void VisitXorExpression(XorExpression xorExpression) { DefaultVisit(xorExpression); }
public ISymbolValue Visit(XorExpression x) { return(E_MathOp(x)); }
IExpression ParseAsmXorExpression(IBlockNode Scope, IStatement Parent) { var left = ParseAsmAndExpression(Scope, Parent); while (laKind == Xor) { Step(); var e = new XorExpression(); e.LeftOperand = left; e.RightOperand = ParseAsmAndExpression(Scope, Parent); left = e; } return left; }
public override void VisitPostOrder(Expression exp) { x86Register reg = GetFreeRegister(exp); if (exp is ConstantExpression) { insts.Add(new x86Instruction() { OpCode = x86OpCode.MOV, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = reg }, new x86ImmediateOperand() { Immediate = (exp as ConstantExpression).Value } } }); SetRegister(exp, reg); } else if (exp is VariableExpression) { insts.AddRange(args(reg)); SetRegister(exp, reg); } else if (exp is AddExpression) { AddExpression _exp = exp as AddExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.ADD, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } else if (exp is SubExpression) { SubExpression _exp = exp as SubExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.SUB, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } else if (exp is MulExpression) { throw new NotSupportedException(); } else if (exp is DivExpression) { throw new NotSupportedException(); } else if (exp is NegExpression) { NegExpression _exp = exp as NegExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.NEG, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.Value) } } }); SetRegister(exp, GetRegister(_exp.Value)); } else if (exp is InvExpression) { InvExpression _exp = exp as InvExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.NOT, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.Value) } } }); SetRegister(exp, GetRegister(_exp.Value)); } else if (exp is XorExpression) { XorExpression _exp = exp as XorExpression; insts.Add(new x86Instruction() { OpCode = x86OpCode.XOR, Operands = new Ix86Operand[] { new x86RegisterOperand() { Register = GetRegister(_exp.OperandA) }, new x86RegisterOperand() { Register = GetRegister(_exp.OperandB) } } }); SetRegister(exp, GetRegister(_exp.OperandA)); ReleaseRegister(_exp.OperandB); } }