Example #1
0
        public override _Term Replace(_MGU s)
        {
            _Term result = new _Variable(this);

            for (int i = 0; i < s.Count; ++i)
            {
                if (s[i].Arg1.CompareTo(result))
                {
                    if (s[i].Arg2.GetType() != _TypeOfTerm.COMPOSITE)
                    {
                        result = _Term.arrPatternObject[(int)s[i].Arg2.GetType()].NewObject(s[i].Arg2);
                        if (result.GetType() == _TypeOfTerm.VARIABLE)
                        {
                            result = result.Replace(s);
                        }
                    }
                    else
                    {
                        result = s[i].Arg2.Replace(s);
                    }
                    break;
                }
            }

            return(result);
        }
Example #2
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
        }