Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
        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);
            }
        }
Exemple #4
0
        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 Boolean IsBelong(_Term x)
 {
     return(mArg2.CompareTo(x));
 }
Exemple #6
0
        private void Run(ArrayList goals, _MGU theta)
        {
            #region if (goals.Count == 0)
            if (goals.Count == 0)
            {
                mResult.Add(theta);
                ArrayList ListVariable = mG.GetListOfVariable();
                _MGU      result       = new _MGU();
                #region for (int j = 0; j < ListVariable.Count; ++j) : thay tap the thanh ket qua cuoi cung
                for (int j = 0; j < ListVariable.Count; ++j)
                {
                    _Variable val = (_Variable)ListVariable[j];
                    result.Add(new _Element(new _Variable(val.Name.Replace("?", "")), val.Replace(theta)));
                }
                #endregion
                #region Kiem tra ket qua vua tim duoc da co hay chu
                Boolean flag = false;
                for (int i = 0; i < mResult.Count; ++i)
                {
                    _MGU temp = (_MGU)mResult[i];
                    if (temp.CompareTo(result) == true)
                    {
                        flag = true;
                        break;
                    }
                }
                #endregion
                if (flag == false) // Chi nhan cac ket qua chua co
                {
                    mResult.Add(result);
                }

                return;
            }
            #endregion
            _Term firstGoals = (_Term)goals[0];
            _Term q1         = firstGoals.Replace(theta);
            #region for (int i = 0; i < mKB.Count; ++i)
            for (int i = 0; i < mKB.Count; ++i)
            {
                _Term  q = mKB[i].Right;
                _Unify u = new _Unify(q, q1);
                _MGU   Temptheta1;           // Chua phep the cua viec dong nhat q1 voi q, co dang {x/y, y/A)
                _MGU   tempMGU = new _MGU(); // Chua phep the o dang trung gian, chi dung de the vao p1,...,pn, dang {x/x0, y/y0}
                _MGU   theta1  = new _MGU(); // Phep the that su (da thay doi ten bien) cua phep dong nhat q1 voi q, co dang {x0/y0, y0/A}

                #region if (u.Run(out Temptheta1) == true)
                if (u.Run(out Temptheta1) == true)
                {
                    #region for (int j = 0; j < Temptheta1.Count; ++j) doi ten bien trong tap the de co tap the moi la theta1
                    ArrayList arrIndex = new ArrayList(); // Ghi nhan nhung bien nao xuat hien (tuc la bien count tang)
                    for (int j = 0; j < Temptheta1.Count; ++j)
                    {
                        #region if (theta.Find((_Term)Temptheta1[j].Arg1) != null)
                        // Neu bien da co trong theta (tuc la bien co lien quan den cac phep the khac),
                        // thi khong doi ten
                        if (theta.Find((_Term)Temptheta1[j].Arg1) != null)
                        {
                            theta1.Add(Temptheta1[j]);
                            continue;
                        }
                        #endregion
                        #region for (k = 0; k < mVariable.Count; ++k)
                        // Kiem tra xem bien cua Temptheta1[j] da xuat hien chua
                        int     k;
                        Boolean flag = false;
                        for (k = 0; k < mVariable.Count; ++k)
                        {
                            _Item tempItem = (_Item)mVariable[k];
                            if (Temptheta1[j].Arg1.CompareTo(tempItem.val))
                            {
                                flag = true;
                                break;
                            }
                        }
                        #endregion
                        #region if (flag == false)
                        // Neu chua co bien thi them vao tap hop, con neu bien da xuat hien thi tang so lan xuat hien len 1 don vi
                        _Item ItemTemp;
                        if (flag == false)
                        {
                            ItemTemp       = new _Item();
                            ItemTemp.val   = new _Variable(Temptheta1[j].Arg1);
                            ItemTemp.Count = 0;
                            mVariable.Add(ItemTemp);
                        }
                        else
                        {
                            ItemTemp = (_Item)mVariable[k];
                            ItemTemp.Count++;
                            mVariable[k] = ItemTemp;
                            arrIndex.Add(k);
                            // Doi ten bien va dua vao tap theta1
                        }
//                        theta1.Add(new _Element(new _Variable(ItemTemp.val.Name + "_" + ItemTemp.Count.ToString()), Temptheta1[j].Arg2));
                        _Term tempArg2 = new _Variable(ItemTemp.val.Name + "_" + ItemTemp.Count.ToString());
                        theta1.Add(new _Element(Temptheta1[j].Arg1, tempArg2));
                        theta1.Add(new _Element((_Variable)tempArg2, Temptheta1[j].Arg2));
                        tempMGU.Add(new _Element(Temptheta1[j].Arg1, theta1[theta1.Count - 1].Arg1));
                        #endregion
                    }
                    #endregion
                    _MGU tempTheta = theta + theta1;
                    #region tempGoals = [p1,...,pn] | Rest(goals)
                    ArrayList tempGoals = new ArrayList();
                    #region for (int j = 0; j < mKB[i].Left.Count; ++j)
                    // Them p1,...,pn vao tempGoals, cac phan tu truoc khi them vao da duoc doi ten bien
                    for (int j = 0; j < mKB[i].Left.Count; ++j)
                    {
                        _Term t = (_Term)mKB[i].Left[j];
//                        tempGoals.Add(t.Replace(tempMGU));
                        tempGoals.Add(t.Replace(theta1));
                    }
                    #endregion
                    #region for (int j = 1; j < goals.Count; ++j)
                    // Them cac rest(goals) vao tempGoals, loai bo cac phan tu trung
                    for (int j = 1; j < goals.Count; ++j)
                    {
                        Boolean flag = false;
                        _Term   t2   = (_Term)goals[j];
                        for (int k = 0; k < tempGoals.Count; ++k)
                        {
                            _Term t1 = (_Term)tempGoals[k];

                            if (t1.CompareTo(t2))
                            {
                                flag = true;
                                break;
                            }
                        }

                        if (flag == false)
                        {
                            tempGoals.Add(goals[j]);
                        }
                    }
                    #endregion
                    #endregion
                    Run(tempGoals, tempTheta);
                    #region for (int j = 0; j < arrIndex.Count; ++j)
                    // Giam bien count, chi ap dung cho nhung bien ma count thay doi
                    for (int j = 0; j < arrIndex.Count; ++j)
                    {
                        _Item ItemTemp;
                        int   k = (int)arrIndex[j];
                        ItemTemp = (_Item)mVariable[k];
                        ItemTemp.Count--;
                        mVariable[k] = ItemTemp;
                    }
                    #endregion
                }
                #endregion
            }
            #endregion
        }