public override Boolean IsAppear(_Term p) { Boolean flag = false; if (p.GetType() == _TypeOfTerm.COMPOSITE) { return(false); } for (int i = 0; i < mComponents.Count; ++i) { _Term temp = (_Term)mComponents[i]; if (temp.GetType() == _TypeOfTerm.COMPOSITE) { flag = temp.IsAppear(p); } else { flag = temp.CompareTo(p); } if (flag == true) { return(true); } } return(false); }
public override Boolean CompareTo(_Term p) { if (p.GetType() != _TypeOfTerm.COMPOSITE) { return(false); } if (Name != p.Name) { return(false); } _Composite temp = (_Composite)p; if (mComponents.Count != temp.mComponents.Count) { return(false); } for (int i = 0; i < mComponents.Count; ++i) { _Term temp1 = (_Term)mComponents[i]; _Term temp2 = (_Term)temp.mComponents[i]; if (temp1.CompareTo(temp2) == false) { return(false); } } return(true); }
public void Run(_Term goal) { mVariable = new ArrayList(); mResult = new ArrayList(); _MGU theta = new _MGU(); ArrayList _goals = new ArrayList(); // Tien xu ly if (goal.GetType() == _TypeOfTerm.COMPOSITE) { mG = new _Composite((_Composite)goal).ExtraName("?"); } else if (goal.GetType() == _TypeOfTerm.VARIABLE) { mG = new _Variable("?" + goal.Name); } else { mG = new _Constant(goal); } _goals.Add(mG); Run(_goals, theta); }
public _Horn(_Horn p) { mLeft = new ArrayList(); for (int i = 0; i < p.mLeft.Count; ++i) { _Term e = (_Term)p.mLeft[i]; mLeft.Add(_Term.arrPatternObject[(int)e.GetType()].NewObject(e)); } mRight = _Term.arrPatternObject[(int)p.mRight.GetType()].NewObject(p.mRight); }
public ArrayList GetArguments() { ArrayList result = new ArrayList(); for (int i = 0; i < mComponents.Count; ++i) { _Term p = (_Term)mComponents[i]; result.Add(_Term.arrPatternObject[(int)p.GetType()].NewObject(p)); } return(result); }
public _BackwardChaining(_KB p) { mKB = new _KB(); #region for (int i = 0; i < p.Count; ++i) for (int i = 0; i < p.Count; ++i) { _Horn horn = (_Horn)p[i]; ArrayList left = new ArrayList(); _Term right; _MGU tempMgu = new _MGU(); #region Lay tat ca cac bien o 1 dong ArrayList arrVal = new ArrayList(); arrVal = horn.Right.GetListOfVariable2(); for (int j = 0; j < horn.Left.Count; ++j) { _Term tempTerm = (_Term)horn.Left[j]; ArrayList arrValLeft = tempTerm.GetListOfVariable2(); for (int l = 0; l < arrValLeft.Count; ++l) { _Variable v1 = (_Variable)arrValLeft[l]; Boolean flag1 = false; for (int k = 0; k < arrVal.Count; ++k) { _Variable v2 = (_Variable)arrVal[k]; if (v1.CompareTo(v2)) { flag1 = true; break; } } if (flag1 == false) { arrVal.Add(arrValLeft[l]); } } } #endregion for (int j = 0; j < arrVal.Count; ++j) { _Variable t = (_Variable)arrVal[j]; _Term tempArg2 = new _Variable(t.Name + "." + i); tempMgu.Add(new _Element(t, tempArg2)); } for (int j = 0; j < horn.Left.Count; ++j) { _Term temp = (_Term)horn.Left[j]; left.Add(temp.Replace(tempMgu)); } right = horn.Right.Replace(tempMgu); mKB.Add(new _Horn(left, right)); } #endregion }
public override Boolean CompareTo(_Term p) { if (p.GetType() != _TypeOfTerm.VARIABLE) { return(false); } if (Name != p.Name) { return(false); } return(true); }
private ArrayList _GetListOfVariable2(_Composite p) { ArrayList result = new ArrayList(); for (int i = 0; i < p.mComponents.Count; ++i) { _Term temp = (_Term)p.mComponents[i]; if (temp.GetType() == _TypeOfTerm.VARIABLE) { Boolean flag = false; for (int j = 0; j < result.Count; ++j) { _Term te = (_Term)result[j]; if (temp.CompareTo(te)) { flag = true; break; } } if (flag == false) { result.Add(temp); } } else if (temp.GetType() == _TypeOfTerm.COMPOSITE) { ArrayList t = _GetListOfVariable2((_Composite)temp); for (int j1 = 0; j1 < t.Count; ++j1) { Boolean flag = false; _Term temp1 = (_Term)t[j1]; for (int j = 0; j < result.Count; ++j) { _Term te = (_Term)result[j]; if (temp1.CompareTo(te)) { flag = true; break; } } if (flag == false) { result.Add(temp1); } } } } return(result); }
public override _Term Replace(_MGU s) { _Term result = null; ArrayList temp = new ArrayList(); for (int i = 0; i < mComponents.Count; ++i) { temp.Add(this[i].Replace(s)); } result = new _Composite(Name, temp); return(result); }
private Boolean Run(_Term x, _Term y, ref _MGU s) { if (x.CompareTo(y)) { return(true); } else if (x.GetType() == _TypeOfTerm.VARIABLE) { return(Unify_var((_Variable)x, y, ref s)); } else if (y.GetType() == _TypeOfTerm.VARIABLE) { return(Unify_var((_Variable)y, x, ref s)); } else if (x.GetType() == _TypeOfTerm.COMPOSITE && y.GetType() == _TypeOfTerm.COMPOSITE) { if (x.Name != y.Name) { return(false); } _Composite comX = (_Composite)x; _Composite comY = (_Composite)y; if (comX.Count != comY.Count) // Khong cung toan tu { return(false); } for (int i = 0; i < comX.Count; ++i) { _Term tempArrX = comX[i].Replace(s); _Term tempArrY = comY[i].Replace(s); if (Run(tempArrX, tempArrY, ref s) == false) { return(false); } } return(true); } else // x hoac y la hang { return(false); } }
public _Element Find(_Term x) { _Element result = null; _Element temp; for (int i = 0; i < mContent.Count; ++i) { temp = (_Element)mContent[i]; if (temp.IsBelong(x) == true) { result = new _Element(temp); } } return(result); }
private void _ChangeName(String extra, _Composite p) { for (int i = 0; i < p.mComponents.Count; ++i) { _Term temp = (_Term)p.mComponents[i]; if (temp.GetType() == _TypeOfTerm.VARIABLE) { p.mComponents[i] = new _Variable(extra + temp.Name); } else if (temp.GetType() == _TypeOfTerm.COMPOSITE) { _ChangeName(extra, (_Composite)temp); } } }
private void Init(ArrayList Components) { if (Components == null) { mComponents = null; } else { mComponents = new ArrayList(); for (int i = 0; i < Components.Count; ++i) { _Term p = (_Term)Components[i]; mComponents.Add(_Term.arrPatternObject[(int)p.GetType()].NewObject(p)); } } }
private int _CountVariable(_Composite p) { int count = 0; for (int i = 0; i < p.mComponents.Count; ++i) { _Term temp = (_Term)p.mComponents[i]; if (temp.GetType() == _TypeOfTerm.VARIABLE) { count++; } else if (temp.GetType() == _TypeOfTerm.COMPOSITE) { count += _CountVariable((_Composite)temp); } } return(count); }
public _Horn(String str) { mLeft = new ArrayList(); String[] strTemp = str.Split(new String[] { "=>" }, StringSplitOptions.RemoveEmptyEntries); if (strTemp.Length == 2) // Co ve trai { String[] arrTerm = strTemp[0].Split(new String[] { ")," }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arrTerm.Length; ++i) { if (i < arrTerm.Length - 1) { arrTerm[i] = arrTerm[i] + ")"; } mLeft.Add(_Term.arrPatternObject[2].NewObject(arrTerm[i])); } str = strTemp[1]; } mRight = _Term.arrPatternObject[2].NewObject(str); }
public override string ToString() { String result = ""; if (mLeft.Count > 0) { for (int i = 0; i < mLeft.Count; ++i) { _Term temp = (_Term)mLeft[i]; result += temp.ToString(); if (i < mLeft.Count - 1) { result += ", "; } } result += " => "; } result += mRight.ToString(); return(result); }
public override String ToString() { String result = Name + "("; if (mComponents != null) { for (int i = 0; i < mComponents.Count; ++i) { _Term x = (_Term)mComponents[i]; result += x.ToString(); if (i < mComponents.Count - 1) { result += ", "; } } } result += ")"; return(result); }
private ArrayList _GetListOfVariable(_Composite p) { ArrayList result = new ArrayList(); for (int i = 0; i < p.mComponents.Count; ++i) { _Term temp = (_Term)p.mComponents[i]; if (temp.GetType() == _TypeOfTerm.VARIABLE) { result.Add(temp); } else if (temp.GetType() == _TypeOfTerm.COMPOSITE) { ArrayList t = _GetListOfVariable((_Composite)temp); for (int j = 0; j < t.Count; ++j) { result.Add(t[j]); } } } return(result); }
private Boolean Unify_var(_Variable var, _Term x, ref _MGU s) { _Element val; if ((val = s.Find(var)) != null) { return(Run(val.Arg2, x, ref s)); } else if ((val = s.Find(x)) != null) { return(Run(var, val.Arg1, ref s)); } else if (x.IsAppear(var) == true) { return(false); } else { s.Add(new _Element(var, x)); return(true); } }
public _Unify(_Term x, _Term y) { Init(x, y); }
// Chuyen chuoi thanh _Composite public override _Term NewObject(String str) { _Term result = null; Stack stack = new Stack(); int index; str = str.Trim(); while (str != "") { #region ... index = str.IndexOfAny(new char[] { '(', ',', ')' }); if (index == -1) // Chi xay ra khi do la bien hoac hang { #region ... str = str.Trim(); char c = str[0]; if (Char.IsUpper(c)) // La hang { stack.Push(new _Constant(str)); } else { stack.Push(new _Variable(str)); } str = ""; #endregion } else { #region ... String tempstr = str.Substring(0, index).Trim(); if (tempstr != "") { stack.Push(tempstr); } #region switch (str[index]) switch (str[index]) { case '(': stack.Push("("); break; case ')': ArrayList arr = new ArrayList(); Object temp; temp = stack.Pop(); while (temp.ToString() != "(") { if (temp.GetType() == "".GetType()) // Kieu chuoi { String strTemp = (String)temp; char c = strTemp[0]; if (Char.IsUpper(c)) // La hang { arr.Add(new _Constant(strTemp)); } else { arr.Add(new _Variable(strTemp)); } } else { arr.Add(temp); } temp = stack.Pop(); } // Chinh lai vi tri cua cac tham so theo dung thu tu ArrayList arrTemp = new ArrayList(); for (int i = arr.Count - 1; i >= 0; --i) { arrTemp.Add(arr[i]); } // Ten ham hoac ten vi tu temp = stack.Pop(); // Dua doi tuong moi vao stack stack.Push(new _Composite((String)temp, arrTemp)); break; } #endregion str = str.Substring(index + 1); #endregion } #endregion } result = (_Term)stack.Pop(); return(result); }
private void Init(_Term x, _Term y) { mArg1 = _Term.arrPatternObject[(int)x.GetType()].NewObject(x); mArg2 = _Term.arrPatternObject[(int)y.GetType()].NewObject(y); }
public override Boolean IsAppear(_Term p) { return(CompareTo(p)); }
public override _Term NewObject(_Term p) { return(new _Variable(p)); }
// Kiem tra 2 bieu thuc co bang nhau khong public abstract Boolean CompareTo(_Term p);
public _Term(_Term p) { mName = String.Copy(p.mName); }
public abstract _Term NewObject(_Term p);
public _Variable(_Term p) : base(p.Name.ToLower()) { }
// Kiem tra p co xuat hien trong bieu thuc hay khong public abstract Boolean IsAppear(_Term p);
public _Horn(ArrayList Left, _Term Right) { mLeft = Left; mRight = Right; }