public void Unify(XPredicate src, XPredicate dest, IXExpression expression)
        {
            for (int i = 0; i < src.Signature.Arity; ++i)
            {
                var sArg = src.Vars[i];
                var dArg = dest.Vars[i];

                if (sArg.Type == XType.Var && dArg.Type == XType.Const)
                {
                    expression.Bind(sArg, dArg);
                }
            }
        }
        public bool Resolve(XPredicate target, IXExpression expression, out List <XPredicate> facts, out List <XPredicate> localFacts)
        {
            if (expression == null)
            {
                facts      = new List <XPredicate>();
                localFacts = new List <XPredicate>();

                return(true);
            }

            switch (expression.Type)
            {
            case XOperand.Predicate:
                var predicateExp = expression as XExpression;
                facts = new List <XPredicate>();

                if (Run(predicateExp.Predicate, out localFacts))
                {
                    foreach (var f in localFacts)
                    {
                        var clone = target.Clone();

                        for (int i = 0; i < f.Vars.Length; ++i)
                        {
                            var sArg = predicateExp.Predicate.Vars[i];
                            var dArg = f.Vars[i];

                            if (sArg.Type == XType.Var && dArg.Type == XType.Const)
                            {
                                clone.Bind(sArg, dArg);
                            }
                        }

                        facts.Add(clone);
                    }

                    return(true);
                }

                return(false);

            case XOperand.Not:
                var notExp = expression as XNot;
                return(!Resolve(target, notExp.Expression.Clone(), out facts, out localFacts));

            default:
                var andExp = expression as XAnd;
                return(Resolve2(target, andExp, out facts, out localFacts));
            }
        }
예제 #3
0
 public XNot(IXExpression expression)
 {
     Expression = expression;
 }
        private int ParseExpression(List <XToken <XTokenType> > tokens, int start, int end, XDatabase db, out IXExpression expression)
        {
            int position = start;

            expression = null;
            var expressions = new List <IXExpression>();

            for (; position < end; ++position)
            {
                position = ParseExpression2(tokens, position, end, db, out IXExpression child);
                expressions.Add(child);

                if (tokens[position].Type != XTokenType.Separator)
                {
                    break;
                }
            }

            if (expressions.Count == 0)
            {
                throw new ApplicationException("Clause has an empty body");
            }
            else if (expressions.Count == 1)
            {
                expression = expressions[0];
            }
            else
            {
                expression = new XAnd(expressions.ToArray());
            }

            return(position);
        }
        private int ParseExpression2(List <XToken <XTokenType> > tokens, int start, int end, XDatabase db, out IXExpression expression)
        {
            int position = start;

            if (tokens[position].Type == XTokenType.Not)
            {
                position   = ParsePredicate(tokens, position + 1, end, db, out XPredicate predicate);
                expression = new XNot(new XExpression(predicate));
            }
            else
            {
                position   = ParsePredicate(tokens, position, end, db, out XPredicate predicate);
                expression = new XExpression(predicate);
            }

            return(position);
        }
예제 #6
0
 public XClause(IXExpression body, XSignature signature, params XVar[] vars) : this(body, new XPredicate(signature, vars))
 {
 }
예제 #7
0
 public XClause(IXExpression body, XPredicate predicate)
 {
     Body      = body;
     Predicate = predicate;
 }