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
 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 _Element Find(_Variable 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);
                    break;
                }
            }
            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 Boolean IsBelong(_Variable x)
 {
     return(mArg1.CompareTo(x));
 }
 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);
 }
Example #8
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
        }