Example #1
0
 private static void VisitLiteral(Expression literal, GdlVisitor visitor)
 {
     visitor.VisitLiteral(literal);
     var fact = literal as Fact;
     if (fact != null)
     {
         if (fact.RelationName == GameContainer.Parser.TokDistinct)
             VisitDistinct(fact, visitor);
         else
             VisitSentence(fact, visitor);
     }
     else if (literal is Negation)
         VisitNot((Negation) literal, visitor);
     else if (literal is Disjunction)
         VisitOr((Disjunction) literal, visitor);
     else
         throw new Exception("Unexpected GdlLiteral type " + literal.GetType());
 }
Example #2
0
        /// <summary>
        /// Unification will try to find a mapping that will go from the second expression to the first.
        /// </summary>
        /// <param name="exp1">Expression to map to</param>
        /// <param name="exp2">Expression to map from</param>
        /// <returns>List of mappigs that satify. If none exist returns null</returns>
        public static Substitution Mgu(Expression exp1, Expression exp2)
        {
            if (exp1.GetType() != exp2.GetType())
                return null;

            var fact1 = exp1 as Fact;
            if (fact1 != null)
                return Mgu(fact1, (Fact) exp2);

            var dis1 = exp1 as Disjunction;
            if (dis1 != null)
                return Mgu(dis1, (Disjunction) exp2);

            var neg1 = exp1 as Negation;
            if (neg1 != null)
                return Mgu(neg1, (Negation)exp2);

            throw new Exception("Unhandled type in unifier Mgu");
        }
Example #3
0
        private static bool Mgu(Expression exp1, Expression exp2, Substitution subsSoFar)
        {
            if (exp1.GetType() != exp2.GetType())
                return false;

            var fact1 = exp1 as Fact;
            if (fact1 != null)
                return Mgu(fact1, (Fact)exp2, subsSoFar);

            var dis1 = exp1 as Disjunction;
            if (dis1 != null)
                return Mgu(dis1, (Disjunction)exp2, subsSoFar);

            var neg1 = exp1 as Negation;
            if (neg1 != null)
                return Mgu(neg1, (Negation)exp2, subsSoFar);

            throw new Exception("Unhandled type in unifier Mgu");
        }
Example #4
0
        private static Expression ReplaceRelationInLiteral(Expression literal, ISentenceForm trueForm)
        {
            var sentence = literal as Fact;
            if (sentence != null)
                if (trueForm.Matches(sentence))
                {
                    var terms = sentence.GetTerms();
                    var function = terms[0] as TermFunction;
                    Debug.Assert(function != null);
                    return new VariableFact(true, function.FunctionName, function.Arguments);
                }
                else
                    return literal;

            var not = literal as Negation;
            if (not != null)
                return new Negation(ReplaceRelationInLiteral(not.Negated, trueForm));

            var or = literal as Disjunction;
            if (or != null)
            {
                var newOrBody = new List<Expression>();
                for (int i = 0; i < or.GetDisjuncts().Count(); i++)
                    newOrBody.Add(ReplaceRelationInLiteral(or.Constituents[i], trueForm));
                return new Disjunction(newOrBody.ToArray());
            }
            throw new Exception(string.Format("Unanticipated GDL literal type {0} encountered in Relationizer", literal.GetType()));
        }