private static ProbTriple JoinTwoTriple(ProbTriple tripleOne, ProbTriple tripleTwo, ProbAttribute attribute, string OperationNaturalJoin) { ProbTriple triple = new ProbTriple(); for (int i = 0; i < tripleOne.Value.Count; i++) { for (int j = 0; j < tripleTwo.Value.Count; j++) { if (SelectCondition.EQUAL(tripleOne.Value[i].ToString().Trim(), tripleTwo.Value[j].ToString().Trim(), attribute.Type.DataType)) { switch (OperationNaturalJoin) { case "in": triple.Value.Add(tripleOne.Value[i]); triple.MinProb.Add(tripleOne.MinProb[i] * tripleTwo.MinProb[j]); triple.MaxProb.Add(tripleOne.MaxProb[i] * tripleTwo.MaxProb[j]); break; case "ig": triple.Value.Add(tripleOne.Value[i]); triple.MinProb.Add(Math.Max(0, tripleOne.MinProb[i] + tripleTwo.MinProb[j] - 1)); triple.MaxProb.Add(Math.Min(tripleOne.MaxProb[i], tripleTwo.MaxProb[j])); break; case "me": triple.Value.Add(tripleOne.Value[i]); triple.MinProb.Add(0); triple.MaxProb.Add(0); break; default: break; } } } } return(triple.Value.Count <= 0 ? null : triple); }
internal bool ExecuteQuery() { try { if (!QueryAnalyze()) { return(false); } //got if (this.selectedRelations.Count == 2) // có 2 rel { if (flagNaturalJoin != true) //xe truong hop { this.selectedRelations[0] = Descartes(); } else { this.selectedRelations[0] = NaturalJoin(); } } else { foreach (ProbAttribute attr in this.selectedRelations[0].Scheme.Attributes) { if (!attr.AttributeName.Contains(".")) { attr.AttributeName = String.Format("{0}.{1}", this.selectedRelations[0].RelationName, attr.AttributeName); } } } if (!this.queryString.Contains("where")) //query ko co where { this.relationResult = getRelationBySelectAttribute(this.selectedRelations[0], this.selectedAttributes); // lay danh sach att return(true); } else { SelectCondition Condition = new SelectCondition(this.selectedRelations[0], this.conditionString); if (!Condition.CheckConditionString()) { this.MessageError = Condition.MessageError; return(false); } foreach (ProbTuple tuple in this.selectedRelations[0].tuples) { if (Condition.Satisfied(tuple)) { this.relationResult.tuples.Add(tuple); } } if (Condition.MessageError != string.Empty) { this.MessageError = Condition.MessageError; return(false); } if (Condition.conditionString == string.Empty) { this.MessageError = Condition.MessageError; return(false); } this.relationResult.Scheme = this.selectedRelations[0].Scheme; this.relationResult = getRelationBySelectAttribute(this.relationResult, this.selectedAttributes); } } catch { return(false); } return(true); }
private List <double> GetProbIntervalV3(string valueOne, string valueTwo, string operaterStr) { double minProb = 0, maxProb = 0; int indexOne, countTripleOne; ProbTuple tuple = this.tuple; try { if (SelectCondition.isCompareOperator(operaterStr)) // Biểu thức so sánh giữa một thuộc tính với một giá trị { indexOne = this.IndexOfAttribute(valueOne); // vị trí của thuộc tính trong ds các thuộc tính if (indexOne == -1) { return(null); } if (valueTwo.Contains("'")) { int count = valueTwo.Split(new char[] { '\'' }).Length - 1; if (valueTwo.Substring(0, 1) != "'") { MessageError = "Unclosed quotation mark before the character string " + valueTwo; return(null); } if (valueTwo.Substring(valueTwo.Length - 1, 1) != "'") { MessageError = "Unclosed quotation mark after the character string " + valueTwo; return(null); } if (count != 2) { MessageError = "Unclosed quotation mark at the character string " + valueTwo; return(null); } valueTwo = valueTwo.Remove(0, 1); valueTwo = valueTwo.Remove(valueTwo.Length - 1, 1); } #region ProbDataType ProbDataType dataType = new ProbDataType(); dataType.TypeName = Attributes[indexOne].Type.TypeName; dataType.DataType = Attributes[indexOne].Type.DataType; dataType.Domain = Attributes[indexOne].Type.Domain; dataType.DomainString = Attributes[indexOne].Type.DomainString; #endregion if (!dataType.CheckDataTypeOfVariables(valueTwo)) { MessageError = String.Format("Conversion failed when converting the varchar value {0} to data type {1}.", valueTwo, dataType.DataType); return(null); } #region ProbDataType countTripleOne = tuple.Triples[indexOne].Value.Count; // số lượng các cặp xác xuất trong thuộc tính var listValue = tuple.Triples[indexOne].Value; var minProbV = tuple.Triples[indexOne].MinProb; var maxProbV = tuple.Triples[indexOne].MaxProb; #endregion var result = listValue.Where(x => CompareTriple(x, valueTwo, operaterStr, dataType.TypeName)).Count(); if (result > 0) { minProb = (result / (float)countTripleOne) * minProbV; maxProb = (result / (float)countTripleOne) * maxProbV; return(new List <double> { minProb, maxProb }); } else { return(new List <double> { 0, 0 }); } } return(null); } catch { MessageError = "Incorrect syntax near 'where'."; return(null); } }
private bool GetProbIntervalV2(string valueOne, string valueTwo, string operaterStr, double?maxProbOfCon = null, double?minProbOfCon = null) { double minProb = 0, maxProb = 0; int indexOne, countTripleOne; ProbTuple tuple = this.tuple; try { if (SelectCondition.isCompareOperator(operaterStr)) // Biểu thức so sánh giữa một thuộc tính với một giá trị { indexOne = this.IndexOfAttribute(valueOne); // vị trí của thuộc tính trong ds các thuộc tính if (indexOne == -1) { return(false); } if (valueTwo.Contains("'")) { int count = valueTwo.Split(new char[] { '\'' }).Length - 1; if (valueTwo.Substring(0, 1) != "'") { MessageError = "Unclosed quotation mark before the character string " + valueTwo; return(false); } if (valueTwo.Substring(valueTwo.Length - 1, 1) != "'") { MessageError = "Unclosed quotation mark after the character string " + valueTwo; return(false); } if (count != 2) { MessageError = "Unclosed quotation mark at the character string " + valueTwo; return(false); } valueTwo = valueTwo.Remove(0, 1); valueTwo = valueTwo.Remove(valueTwo.Length - 1, 1); } #region ProbDataType ProbDataType dataType = new ProbDataType(); dataType.TypeName = Attributes[indexOne].Type.TypeName; dataType.DataType = Attributes[indexOne].Type.DataType; dataType.Domain = Attributes[indexOne].Type.Domain; dataType.DomainString = Attributes[indexOne].Type.DomainString; #endregion if (!dataType.CheckDataTypeOfVariables(valueTwo)) { MessageError = String.Format("Conversion failed when converting the varchar value {0} to data type {1}.", valueTwo, dataType.DataType); return(false); } #region ProbDataType countTripleOne = tuple.Triples[indexOne].Value.Count; // số lượng các cặp xác xuất trong thuộc tính var listValue = tuple.Triples[indexOne].Value; var minProbV = tuple.Triples[indexOne].MinProb; var maxProbV = tuple.Triples[indexOne].MaxProb; #endregion if (maxProbV == 1 && minProbV == 1 && !maxProbOfCon.HasValue && !minProbOfCon.HasValue) { var kp = listValue.Any(x => CompareTriple(x, valueTwo, operaterStr, dataType.TypeName)); return(kp); } else { var count = listValue.Where(x => CompareTriple(x, valueTwo, operaterStr, dataType.TypeName)).Count(); if (count > 0) { minProb = (count / (float)countTripleOne) * minProbV; maxProb = (count / (float)countTripleOne) * maxProbV; if (maxProbOfCon.HasValue && minProbOfCon.HasValue) { return(minProbOfCon.Value <= minProb && maxProb <= maxProbOfCon); } } return(false); } } else // Biểu thức kết hợp giữa hai khoảng xác suất { double minProbOne, minProbTwo, maxProbOne, maxProbTwo; string[] StrProb; valueOne = valueOne.Replace("[", ""); // [L,U] valueOne = valueOne.Replace("]", ""); StrProb = valueOne.Split(','); minProbOne = Convert.ToDouble(StrProb[0]); maxProbOne = Convert.ToDouble(StrProb[1]); valueTwo = valueTwo.Replace("[", ""); // [L,U] valueTwo = valueTwo.Replace("]", ""); StrProb = valueTwo.Split(','); minProbTwo = Convert.ToDouble(StrProb[0]); maxProbTwo = Convert.ToDouble(StrProb[1]); switch (operaterStr) { case "⊗_ig": minProb = Math.Max(0, minProbOne + minProbTwo - 1); maxProb = Math.Min(maxProbOne, maxProbTwo); break; case "⊗_in": minProb = minProbOne * minProbTwo; maxProb = maxProbOne * maxProbTwo; break; case "⊗_me": minProb = 0; maxProb = 0; break; case "⊕_ig": minProb = Math.Max(minProbOne, minProbTwo); maxProb = Math.Min(1, maxProbOne + maxProbTwo); break; case "⊕_in": minProb = minProbOne + minProbTwo - (minProbOne * minProbTwo); maxProb = maxProbOne + maxProbTwo - (maxProbOne * maxProbTwo); break; case "⊕_me": minProb = Math.Min(1, minProbOne + minProbTwo); maxProb = Math.Min(1, maxProbOne + maxProbTwo); break; default: MessageError = "Incorrect syntax near 'where'."; break; } } } catch { MessageError = "Incorrect syntax near 'where'."; return(false); } return(false); }
private string GetProbInterval(string valueOne, string valueTwo, string operaterStr) { double minProb = 0, maxProb = 0; int indexOne, indexTwo, countTripleOne, countTripleTwo; ProbTuple tuple = this.tuple; string typenameOne; string typenameTwo; try { if (operaterStr.Contains("equal_ig") || operaterStr.Contains("equal_in") || operaterStr.Contains("equal_me")) // Biểu thức so sánh bằng giữa hai thuộc tính trên cùng một bộ { indexOne = IndexOf(valueOne); indexTwo = IndexOf(valueTwo); if (indexOne == -1 || indexTwo == -1) { return(string.Empty); } countTripleOne = tuple.Triples[indexOne].Value.Count; countTripleTwo = tuple.Triples[indexTwo].Value.Count; typenameOne = Attributes[indexOne].Type.DataType; typenameTwo = Attributes[indexTwo].Type.DataType; if (typenameOne != typenameTwo) { //Attribute value does not match the data type ! MessageError = String.Format("Error :{0} and {1} must the same data type", valueOne, valueTwo); return(string.Empty); } for (int i = 0; i < countTripleOne; i++) { for (int j = 0; j < countTripleTwo; j++) { if (EQUAL(tuple.Triples[indexOne].Value[i].ToString().Trim(), tuple.Triples[indexTwo].Value[j].ToString().Trim(), typenameOne)) { switch (operaterStr) { case "equal_in": minProb += tuple.Triples[indexOne].MinProb[i] * tuple.Triples[indexTwo].MinProb[j]; maxProb = Math.Min(1, maxProb + tuple.Triples[indexOne].MaxProb[i] * tuple.Triples[indexTwo].MaxProb[j]); break; case "equal_ig": minProb += Math.Min(0, tuple.Triples[indexOne].MinProb[i] + tuple.Triples[indexTwo].MinProb[j] - 1); maxProb = Math.Min(1, maxProb + Math.Min(tuple.Triples[indexOne].MaxProb[i], tuple.Triples[indexTwo].MaxProb[j])); break; case "equal_me": minProb = 0; maxProb = Math.Min(1, maxProb + 0); break; default: break; } } } } } else if (SelectCondition.isCompareOperator(operaterStr)) // Biểu thức so sánh giữa một thuộc tính với một giá trị { indexOne = this.IndexOf(valueOne); // vị trí của thuộc tính trong ds các thuộc tính if (indexOne == -1) { return(string.Empty); } if (valueTwo.Contains("'")) { int count = valueTwo.Split(new char[] { '\'' }).Length - 1; if (valueTwo.Substring(0, 1) != "'") { MessageError = "Unclosed quotation mark before the character string " + valueTwo; return(string.Empty); } if (valueTwo.Substring(valueTwo.Length - 1, 1) != "'") { MessageError = "Unclosed quotation mark after the character string " + valueTwo; return(string.Empty); } if (count != 2) { MessageError = "Unclosed quotation mark at the character string " + valueTwo; return(string.Empty); } valueTwo = valueTwo.Remove(0, 1); valueTwo = valueTwo.Remove(valueTwo.Length - 1, 1); } countTripleOne = tuple.Triples[indexOne].Value.Count; // số lượng các cặp xác xuất trong thuộc tính typenameOne = Attributes[indexOne].Type.DataType; ProbDataType dataType = new ProbDataType(); dataType.TypeName = Attributes[indexOne].Type.TypeName; dataType.DataType = Attributes[indexOne].Type.DataType; dataType.Domain = Attributes[indexOne].Type.Domain; dataType.DomainString = Attributes[indexOne].Type.DomainString; if (!dataType.CheckDataTypeOfVariables(valueTwo)) { MessageError = String.Format("Conversion failed when converting the varchar value {0} to data type {1}.", valueTwo, typenameOne); return(string.Empty); } for (int i = 0; i < countTripleOne; i++) { if (this.CompareTriple(tuple.Triples[indexOne].Value[i].ToString().Trim(), valueTwo.Trim(), operaterStr, typenameOne)) // duyệt từng cặp xác xuất và so sánh { minProb += tuple.Triples[indexOne].MinProb[i]; maxProb += tuple.Triples[indexOne].MaxProb[i]; } } } else // Biểu thức kết hợp giữa hai khoảng xác suất { double minProbOne, minProbTwo, maxProbOne, maxProbTwo; string[] StrProb; valueOne = valueOne.Replace("[", ""); // [L,U] valueOne = valueOne.Replace("]", ""); StrProb = valueOne.Split(','); minProbOne = Convert.ToDouble(StrProb[0]); maxProbOne = Convert.ToDouble(StrProb[1]); valueTwo = valueTwo.Replace("[", ""); // [L,U] valueTwo = valueTwo.Replace("]", ""); StrProb = valueTwo.Split(','); minProbTwo = Convert.ToDouble(StrProb[0]); maxProbTwo = Convert.ToDouble(StrProb[1]); switch (operaterStr) { case "⊗_ig": minProb = Math.Max(0, minProbOne + minProbTwo - 1); maxProb = Math.Min(maxProbOne, maxProbTwo); break; case "⊗_in": minProb = minProbOne * minProbTwo; maxProb = maxProbOne * maxProbTwo; break; case "⊗_me": minProb = 0; maxProb = 0; break; case "⊕_ig": minProb = Math.Max(minProbOne, minProbTwo); maxProb = Math.Min(1, maxProbOne + maxProbTwo); break; case "⊕_in": minProb = minProbOne + minProbTwo - (minProbOne * minProbTwo); maxProb = maxProbOne + maxProbTwo - (maxProbOne * maxProbTwo); break; case "⊕_me": minProb = Math.Min(1, minProbOne + minProbTwo); maxProb = Math.Min(1, maxProbOne + maxProbTwo); break; default: MessageError = "Incorrect syntax near 'where'."; break; } } } catch { MessageError = "Incorrect syntax near 'where'."; return(string.Empty); } maxProb = 1 > maxProb ? maxProb : 1; // check maxProb return(String.Format("[{0},{1}]", minProb, maxProb)); }
private bool IsSelectionExpression(string conditionString) { if (conditionString.Contains(" and ") || conditionString.Contains(" or ") || conditionString.Contains(" not ")) { return(false); } string str = conditionString.Substring(conditionString.IndexOf("(") + 1, conditionString.IndexOf(")") - 1); if (str == string.Empty || str.Contains("(") || str.Contains(")")) { return(false); } if (SelectCondition.convertConditionStringToProbInterVal(conditionString) == null) { return(false); } List <string> arrayStr = new List <string>(); if (str.Contains("'")) { int index = -1; for (int i = 0; i < str.Length - 1; i++) { if (str[i] == ' ' && str[i + 1] == '\'') { index = i + 1; break; } } if (index != -1) { string tmp = str.Substring(index, str.Length - index); str = str.Substring(0, index - 1); arrayStr.AddRange(str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); arrayStr.Add(tmp); } else { arrayStr.AddRange(str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); int k = 0; for (int i = 0; i < arrayStr.Count; i++) { if (isCompareOperator(arrayStr[i])) { k = i + 1; break; } } string valueError = ""; for (int i = k; i < arrayStr.Count; i++) { valueError += arrayStr[i] + " "; } MessageError = "Unclosed quotation mark before the character string " + valueError; return(false); } } else { arrayStr.AddRange(str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); } for (int i = 1; i < arrayStr.Count - 1; i++) { if (Operator.Contains(arrayStr[i]) && (Operator.Contains(arrayStr[i - 1]) || Operator.Contains(arrayStr[i + 1]))) { MessageError = "Incorrect syntax near the keyword 'where'."; return(false); } if (!Operator.Contains(arrayStr[i]) && (!Operator.Contains(arrayStr[i - 1]) || !Operator.Contains(arrayStr[i + 1]))) { MessageError = "Incorrect syntax near the keyword 'where'."; return(false); } } return(true); }
internal bool ExcuteQuery() { try { if (!QueryAnalyze()) { return(false); } if (this.selectedRelations.Count == 1) { foreach (ProbAttribute attr in this.selectedRelations[0].Scheme.Attributes) { if (!attr.AttributeName.Contains(".")) { attr.AttributeName = String.Format("{0}.{1}", this.selectedRelations[0].RelationName, attr.AttributeName); } } } else { if (flagNaturalJoin != true) { var relationRes = this.selectedRelations[0]; for (int i = 1; i < this.selectedRelations.Count(); i++) { relationRes = Descartes(relationRes, this.selectedRelations[i]); } this.selectedRelations[0] = relationRes; } else { var relationRes = this.selectedRelations[0]; for (int i = 1; i < this.selectedRelations.Count(); i++) { relationRes = NaturalJoin(relationRes, this.selectedRelations[i], this.OperationNaturalJoin[i - 1]); } this.selectedRelations[0] = relationRes; } } if (!this.queryString.Contains(Common.Where)) { this.relationResult = getRelationBySelectAttributeV2(this.selectedRelations[0], this.selectedAttributes); return(true); } else { SelectCondition Condition = new SelectCondition(this.selectedRelations[0], this.conditionString); Condition.ProcessConditionString(); if (!string.IsNullOrEmpty(Condition.MessageError)) { MessageError = Condition.MessageError; return(false); } foreach (ProbTuple tuple in this.selectedRelations[0].tuples) { if (Condition.Satisfied(tuple)) { this.relationResult.tuples.Add(tuple); } else { if (!string.IsNullOrEmpty(Condition.MessageError)) { MessageError = Condition.MessageError; return(false); } } } if (Condition.MessageError != string.Empty) { this.MessageError = Condition.MessageError; return(false); } if (Condition.conditionString == string.Empty) { this.MessageError = Condition.MessageError; return(false); } this.relationResult.Scheme = this.selectedRelations[0].Scheme; this.relationResult = getRelationBySelectAttribute(this.relationResult, this.selectedAttributes); } } catch { return(false); } return(true); }