예제 #1
0
        public SubstitutionSet Unify(IUnifiable p,
                                     SubstitutionSet s)
        {
            if (p is SimpleSentence)
            {
                SimpleSentence s1 = (SimpleSentence)p;

                if (terms.Length != s1.terms.Length)
                {
                    return(null);
                }

                SubstitutionSet sNew = new SubstitutionSet(s);

                for (int i = 0; i < terms.Length; i++)
                {
                    sNew = terms[i].Unify(s1.terms[i], sNew);

                    if (sNew == null)
                    {
                        return(null);
                    }
                }

                return(sNew);
            }

            if (p is Variable)
            {
                return(p.Unify(this, s));
            }

            return(null);
        }
예제 #2
0
        private void UnifyTester()
        {
            Constant          friend      = new Constant("friend");
            Constant          bill        = new Constant("bill");
            Constant          george      = new Constant("george");
            Constant          kate        = new Constant("kate");
            Constant          merry       = new Constant("merry");
            Variable          X           = new Variable("X");
            Variable          Y           = new Variable("Y");
            List <IUnifiable> expressions = new List <IUnifiable>();

            IUnifiable[] terms = new IUnifiable[2];

            terms[0] = bill;
            terms[1] = george;

            expressions.Add(new SimpleSentence(friend, terms));

            terms[1] = kate;

            expressions.Add(new SimpleSentence(friend, terms));

            terms[1] = merry;

            expressions.Add(new SimpleSentence(friend, terms));

            terms[0] = george;
            terms[1] = bill;

            expressions.Add(new SimpleSentence(friend, terms));

            terms[1] = kate;

            expressions.Add(new SimpleSentence(friend, terms));

            terms[0] = kate;
            terms[1] = merry;

            expressions.Add(new SimpleSentence(friend, terms));

            terms[0] = X;
            terms[1] = Y;

            IUnifiable      goal = new SimpleSentence(friend, terms);
            SubstitutionSet s    = null;

            textBox1.Text += "Goal: " + goal.ToString() + "\r\n";

            for (int i = 0; i < expressions.Count; i++)
            {
                IUnifiable next = expressions[i];

                s              = next.Unify(goal, new SubstitutionSet());
                textBox1.Text += next.ToString() + "\t";

                if (s != null)
                {
                    IPCExpression  ipcexp = goal.ReplaceVariables(s);
                    SimpleSentence sent   = (SimpleSentence)ipcexp;

                    textBox1.Text += sent.ToString() + "\r\n";
                }

                else
                {
                    textBox1.Text += "False\r\n";
                }
            }

            terms[0] = bill;
            terms[1] = Y;

            goal           = new SimpleSentence(friend, terms);
            textBox1.Text += "Goal: " + goal.ToString() + "\r\n";

            for (int i = 0; i < expressions.Count; i++)
            {
                IUnifiable next = expressions[i];

                s              = next.Unify(goal, new SubstitutionSet());
                textBox1.Text += next.ToString() + "\t";

                if (s != null)
                {
                    IPCExpression ipcexp = goal.ReplaceVariables(s);

                    textBox1.Text += ipcexp.ToString() + "\r\n";
                }

                else
                {
                    textBox1.Text += "False\r\n";
                }
            }
        }