public void TestMethodLiteralVariableNoMatch() { var variableDataTypeMap = new Dictionary <string, DataType>(); variableDataTypeMap.Add("testVariable1", DataType.Boolean); variableDataTypeMap.Add("testVariable2", DataType.Integer); variableDataTypeMap.Add("testVariable3", DataType.Decimal); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("testVariable1", new Variable(true)); variableMap.Add("testVariable2", new Variable(222)); variableMap.Add("testVariable3", new Variable(333.33)); var literalVariable = new LiteralVariable(); literalVariable.value = "testVariable"; Assert.AreEqual("Var[testVariable]", literalVariable.getRPolish()); Assert.AreEqual(DataType.None, literalVariable.getDataType(variableDataTypeMap)); try { literalVariable.eval(variableMap); Assert.Fail(); } catch (Exception e) { Assert.IsTrue(e.GetType() == typeof(ArgumentException)); } }
public void TestMethodLiteralVariableMultiVariable() { var variableDataTypeMap = new Dictionary <string, DataType>(); variableDataTypeMap.Add("testVariable1", DataType.Boolean); variableDataTypeMap.Add("testVariable2", DataType.Integer); variableDataTypeMap.Add("testVariable3", DataType.Decimal); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("testVariable1", new Variable(true)); variableMap.Add("testVariable2", new Variable(222)); variableMap.Add("testVariable3", new Variable(333.33)); var literalVariable = new LiteralVariable(); literalVariable.value = "testVariable1"; Assert.AreEqual("Var[testVariable1]", literalVariable.getRPolish()); Assert.AreEqual(DataType.Boolean, literalVariable.getDataType(variableDataTypeMap)); UnitTestUtil.AssertMathTreeNodeValue(true, literalVariable.eval(variableMap)); literalVariable.value = "testVariable2"; Assert.AreEqual("Var[testVariable2]", literalVariable.getRPolish()); Assert.AreEqual(DataType.Integer, literalVariable.getDataType(variableDataTypeMap)); UnitTestUtil.AssertMathTreeNodeValue(222, literalVariable.eval(variableMap)); literalVariable.value = "testVariable3"; Assert.AreEqual("Var[testVariable3]", literalVariable.getRPolish()); Assert.AreEqual(DataType.Decimal, literalVariable.getDataType(variableDataTypeMap)); UnitTestUtil.AssertMathTreeNodeValue(333.33, literalVariable.eval(variableMap)); }
public void TestMethodLiteralVariableBoolean() { var variableDataTypeMap = new Dictionary <string, DataType>(); variableDataTypeMap.Add("testVariable", DataType.Boolean); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("testVariable", new Variable(true)); var literalVariable = new LiteralVariable(); literalVariable.value = "testVariable"; Assert.AreEqual("Var[testVariable]", literalVariable.getRPolish()); Assert.AreEqual(DataType.Boolean, literalVariable.getDataType(variableDataTypeMap)); UnitTestUtil.AssertMathTreeNodeValue(true, literalVariable.eval(variableMap)); }
public void TestMethodDecimalVariable() { string expr = "var1 - var2 / 2.1"; Variable var1 = new Variable(2.3); Variable var2 = new Variable(12.6); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("var1", var1); variableMap.Add("var2", var2); var functionMap = new Dictionary <string, Function>(); // 終端記号化 List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(5, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[0].type); Assert.AreEqual("var1", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.OpDiff, terminalSymbolList[1].type); Assert.AreEqual("-", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[2].type); Assert.AreEqual("var2", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.OpDivide, terminalSymbolList[3].type); Assert.AreEqual("/", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[4].type); Assert.AreEqual("2.1", terminalSymbolList[4].value); // 品詞化 List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(5, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[1].GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(lexicalList[2].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[3].GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(lexicalList[4].GetType() == typeof(LiteralDecimal)); LiteralVariable l0 = (LiteralVariable)lexicalList[0]; Assert.AreEqual("var1", l0.value); LiteralVariable l2 = (LiteralVariable)lexicalList[2]; Assert.AreEqual("var2", l2.value); LiteralDecimal l4 = (LiteralDecimal)lexicalList[4]; Assert.AreEqual("2.1", l4.value); Operator op1 = (Operator)lexicalList[1]; Assert.AreEqual(5, op1.getPriority()); Operator op3 = (Operator)lexicalList[3]; Assert.AreEqual(6, op3.getPriority()); // 数式ツリー化 MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal0 = (LiteralVariable)tree.root.left.lex; Assert.AreEqual("var1", literal0.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal2 = (LiteralVariable)tree.root.right.left.lex; Assert.AreEqual("var2", literal2.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal4 = (LiteralDecimal)tree.root.right.right.lex; Assert.AreEqual("2.1", literal4.value); // データ型評価 DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree, variableMap, functionMap); Assert.AreEqual(dataType, DataType.Decimal); // 評価値評価 MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree, variableMap, functionMap); Assert.AreEqual(value.type, DataType.Decimal); Assert.AreEqual(value.valueDecimal, -3.7); }
public void TestMethodIntegerVariable() { string expr = "var1 + var2 * 5"; Variable var1 = new Variable(13); Variable var2 = new Variable(29); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("var1", var1); variableMap.Add("var2", var2); var functionMap = new Dictionary <string, Function>(); // 終端記号化 List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(5, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[0].type); Assert.AreEqual("var1", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.OpAdd, terminalSymbolList[1].type); Assert.AreEqual("+", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[2].type); Assert.AreEqual("var2", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.OpProd, terminalSymbolList[3].type); Assert.AreEqual("*", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[4].type); Assert.AreEqual("5", terminalSymbolList[4].value); // 品詞化 List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(5, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[1].GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(lexicalList[2].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[3].GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(lexicalList[4].GetType() == typeof(LiteralInteger)); LiteralVariable l0 = (LiteralVariable)lexicalList[0]; Assert.AreEqual("var1", l0.value); LiteralVariable l2 = (LiteralVariable)lexicalList[2]; Assert.AreEqual("var2", l2.value); LiteralInteger l4 = (LiteralInteger)lexicalList[4]; Assert.AreEqual("5", l4.value); Operator op1 = (Operator)lexicalList[1]; Assert.AreEqual(5, op1.getPriority()); Operator op3 = (Operator)lexicalList[3]; Assert.AreEqual(6, op3.getPriority()); // 数式ツリー化 MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal0 = (LiteralVariable)tree.root.left.lex; Assert.AreEqual("var1", literal0.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal2 = (LiteralVariable)tree.root.right.left.lex; Assert.AreEqual("var2", literal2.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal4 = (LiteralInteger)tree.root.right.right.lex; Assert.AreEqual("5", literal4.value); // データ型評価 DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree, variableMap, functionMap); Assert.AreEqual(dataType, DataType.Integer); // 評価値評価 MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree, variableMap, functionMap); Assert.AreEqual(value.type, DataType.Integer); Assert.AreEqual(value.valueInteger, 158); }
/// <summary> /// 逆ポーランド記法で表現された終端記号から、品詞を作成する。 /// </summary> /// <param name="rPolish">終端記号の逆ポーランド記法表現</param> /// <returns>品詞。ただし、演算子の括弧深さは0とする。</returns> public static Lexical createFromRPolish(string rPolish) { switch (rPolish) { case "+": return(new BinaryOperatorAdd()); case "--": return(new BinaryOperatorDiff()); case "*": return(new BinaryOperatorProd()); case "/": return(new BinaryOperatorDivide()); case "%": return(new BinaryOperatorMod()); case "^": return(new BinaryOperatorPow()); case "-": return(new UnaryOperatorNegative()); case "<": return(new BinaryOperatorLt()); case "<=": return(new BinaryOperatorLtEq()); case ">": return(new BinaryOperatorGt()); case ">=": return(new BinaryOperatorGtEq()); case "==": return(new BinaryOperatorEqual()); case "!=": return(new BinaryOperatorNotEqual()); case "!": return(new UnaryOperatorNot()); case "&&": return(new BinaryOperatorAnd()); case "||": return(new BinaryOperatorOr()); case ",": return(new BinaryOperatorComma()); case "true": var literalTrue = new LiteralTrue(); literalTrue.value = "true"; return(literalTrue); case "false": var literalFalse = new LiteralFalse(); literalFalse.value = "false"; return(literalFalse); } if (rPolish.StartsWith("Int")) { //Int[value]の余計な文字「Int[」と「]」を取り除く。 string value = rPolish.Substring(4, rPolish.Length - 5); var literalInteger = new LiteralInteger(); literalInteger.value = value; return(literalInteger); } else if (rPolish.StartsWith("Dec")) { //Dec[value]の余計な文字「Dec[」と「]」を取り除く。 string value = rPolish.Substring(4, rPolish.Length - 5); var literalDecimal = new LiteralDecimal(); literalDecimal.value = value; return(literalDecimal); } else if (rPolish.StartsWith("Var")) { //Var[value]の余計な文字「Var[」と「]」を取り除く。 string value = rPolish.Substring(4, rPolish.Length - 5); var literalVariable = new LiteralVariable(); literalVariable.value = value; return(literalVariable); } else if (rPolish.StartsWith("Func")) { //Func[value]の余計な文字「Func[」と「]」を取り除く。 string value = rPolish.Substring(5, rPolish.Length - 6); var unaryOperatorFunction = new UnaryOperatorFunction(); unaryOperatorFunction.functionName = value; return(unaryOperatorFunction); } else { throw new ArgumentException("逆ポーランド記法表現で対応する品詞がありません。"); } }
public static Lexical create(TerminalSymbol terminalSymbol, int parenthesisDepth) { switch (terminalSymbol.type) { case TerminalSymbolType.Integer: var literalInteger = new LiteralInteger(); literalInteger.value = terminalSymbol.value; return(literalInteger); case TerminalSymbolType.Decimal: var literalDecimal = new LiteralDecimal(); literalDecimal.value = terminalSymbol.value; return(literalDecimal); case TerminalSymbolType.LogicTrue: var literalTrue = new LiteralTrue(); literalTrue.value = terminalSymbol.value; return(literalTrue); case TerminalSymbolType.LogicFalse: var literalFalse = new LiteralFalse(); literalFalse.value = terminalSymbol.value; return(literalFalse); case TerminalSymbolType.Variable: var literalVariable = new LiteralVariable(); literalVariable.value = terminalSymbol.value; return(literalVariable); case TerminalSymbolType.OpAdd: var binaryOperatorAdd = new BinaryOperatorAdd(); binaryOperatorAdd.parenthesisDepth = parenthesisDepth; return(binaryOperatorAdd); case TerminalSymbolType.OpDiff: var binaryOperatorDiff = new BinaryOperatorDiff(); binaryOperatorDiff.parenthesisDepth = parenthesisDepth; return(binaryOperatorDiff); case TerminalSymbolType.OpProd: var binaryOperatorProd = new BinaryOperatorProd(); binaryOperatorProd.parenthesisDepth = parenthesisDepth; return(binaryOperatorProd); case TerminalSymbolType.OpDivide: var binaryOperatorDivide = new BinaryOperatorDivide(); binaryOperatorDivide.parenthesisDepth = parenthesisDepth; return(binaryOperatorDivide); case TerminalSymbolType.OpMod: var binaryOperatorMod = new BinaryOperatorMod(); binaryOperatorMod.parenthesisDepth = parenthesisDepth; return(binaryOperatorMod); case TerminalSymbolType.OpPow: var binaryOperatorPow = new BinaryOperatorPow(); binaryOperatorPow.parenthesisDepth = parenthesisDepth; return(binaryOperatorPow); case TerminalSymbolType.OpNeg: var unaryOperatorNegative = new UnaryOperatorNegative(); unaryOperatorNegative.parenthesisDepth = parenthesisDepth; return(unaryOperatorNegative); case TerminalSymbolType.OpLt: var binaryOperatorLt = new BinaryOperatorLt(); binaryOperatorLt.parenthesisDepth = parenthesisDepth; return(binaryOperatorLt); case TerminalSymbolType.OpLtEq: var binaryOperatorLtEq = new BinaryOperatorLtEq(); binaryOperatorLtEq.parenthesisDepth = parenthesisDepth; return(binaryOperatorLtEq); case TerminalSymbolType.OpGt: var binaryOperatorGt = new BinaryOperatorGt(); binaryOperatorGt.parenthesisDepth = parenthesisDepth; return(binaryOperatorGt); case TerminalSymbolType.OpGtEq: var binaryOperatorGtEq = new BinaryOperatorGtEq(); binaryOperatorGtEq.parenthesisDepth = parenthesisDepth; return(binaryOperatorGtEq); case TerminalSymbolType.OpEq: var binaryOperatorEqual = new BinaryOperatorEqual(); binaryOperatorEqual.parenthesisDepth = parenthesisDepth; return(binaryOperatorEqual); case TerminalSymbolType.OpNotEq: var binaryOperatorNotEqual = new BinaryOperatorNotEqual(); binaryOperatorNotEqual.parenthesisDepth = parenthesisDepth; return(binaryOperatorNotEqual); case TerminalSymbolType.OpNot: var unaryOperatorNot = new UnaryOperatorNot(); unaryOperatorNot.parenthesisDepth = parenthesisDepth; return(unaryOperatorNot); case TerminalSymbolType.OpAnd: var binaryOperatorAnd = new BinaryOperatorAnd(); binaryOperatorAnd.parenthesisDepth = parenthesisDepth; return(binaryOperatorAnd); case TerminalSymbolType.OpOr: var binaryOperatorOr = new BinaryOperatorOr(); binaryOperatorOr.parenthesisDepth = parenthesisDepth; return(binaryOperatorOr); case TerminalSymbolType.Function: var unaryOperatorFunction = new UnaryOperatorFunction(); // 演算子の中で、関数だけ変数名を登録する必要がある。 unaryOperatorFunction.functionName = terminalSymbol.value; unaryOperatorFunction.parenthesisDepth = parenthesisDepth; return(unaryOperatorFunction); case TerminalSymbolType.Comma: var binaryOperatorComma = new BinaryOperatorComma(); binaryOperatorComma.parenthesisDepth = parenthesisDepth; return(binaryOperatorComma); default: throw new ApplicationException("品詞に変換できない終端記号が指定されました。"); } }