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 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 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 override Boolean CompareTo(_Term p) { if (p.GetType() != _TypeOfTerm.VARIABLE) { return(false); } if (Name != p.Name) { return(false); } return(true); }
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 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)); } } }
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 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 override _Term NewObject(_Term p) { return(new _Variable(p)); }
// 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); }
public _Variable(_Term p) : base(p.Name.ToLower()) { }
public override _Term NewObject(_Term p) { return(new _Constant(p)); }
public override _Term NewObject(_Term p) { return(new _Composite((_Composite)p)); }
public _Term(_Term p) { mName = String.Copy(p.mName); }
public _Constant(_Term p) : base(char.ToUpper(p.Name[0]) + p.Name.Substring(1).ToLower()) { }
public abstract _Term NewObject(_Term p);
// Kiem tra 2 bieu thuc co bang nhau khong public abstract Boolean CompareTo(_Term p);
// Kiem tra p co xuat hien trong bieu thuc hay khong public abstract Boolean IsAppear(_Term p);
public override Boolean IsAppear(_Term p) { return(CompareTo(p)); }
public _Element(_Variable var, _Term x) { Init(var, x); }
private void Init(_Variable var, _Term x) { mArg1 = new _Variable(var); mArg2 = _Term.arrPatternObject[(int)x.GetType()].NewObject(x); }
public _Unify(_Term x, _Term y) { Init(x, y); }
public Boolean IsBelong(_Term x) { return(mArg2.CompareTo(x)); }
private void Init(_Term x, _Term y) { mArg1 = _Term.arrPatternObject[(int)x.GetType()].NewObject(x); mArg2 = _Term.arrPatternObject[(int)y.GetType()].NewObject(y); }