Exemple #1
0
        internal BacktrackingPredicate Add(Functor functor, BacktrackingPredicateDelegate backtrackingPredicateDelegate)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (backtrackingPredicateDelegate == null)
            {
                throw new ArgumentNullException("backtrackingPredicateDelegate");
            }

            BacktrackingPredicate backtrackingPredicate = Methods.Add(functor, backtrackingPredicateDelegate);

            return(backtrackingPredicate);
        }
Exemple #2
0
        internal BacktrackingPredicate Add(Functor functor, BacktrackingPredicateDelegate backtrackingPredicateDelegate)
        {
            if (functor == null)
            {
                throw new ArgumentNullException("functor");
            }
            if (backtrackingPredicateDelegate == null)
            {
                throw new ArgumentNullException("backtrackingPredicateDelegate");
            }
            if (Contains(functor))
            {
                throw new ArgumentException("Item already exists.", "functor");
            }
            var predicate = new BacktrackingPredicate(this, functor, backtrackingPredicateDelegate);

            Items.Add(predicate);
            Library.Touch();

            return(predicate);
        }
        private ExecutionResults OnLibraryCallBacktrackingPredicate(WamInstruction instruction, BacktrackingPredicate predicate)
        {
            WamReferenceTarget[] arguments = new WamReferenceTarget[instruction.Functor.Arity];
            for (int index = 0; index < instruction.Functor.Arity; ++index)
            {
                arguments[index] = ArgumentRegisters[index];
            }

            IEnumerable<bool> enumerable;
            try
            {
                enumerable = predicate.BacktrackingPredicateDelegate(this, arguments);
            }
            catch
            {
                // Backtrack on exception.
                //
                return ExecutionResults.Backtrack;
            }

            IEnumerator<bool> enumerator = enumerable.GetEnumerator();

            ChoicePoint = new WamChoicePoint(ChoicePoint, Environment, StackIndex, ReturnInstructionPointer, ArgumentRegisters, CutChoicePoint);
            ChoicePoint.BacktrackInstructionPointer = InstructionPointer.GetNext();
            ChoicePoint.PredicateEnumerator = enumerator;

            InstructionPointer = ChoicePoint.BacktrackInstructionPointer;
            PredicateEnumerator = ChoicePoint.PredicateEnumerator;

            return ExecutionResults.None;
        }
Exemple #4
0
        ExecutionResults OnLibraryCallBacktrackingPredicate(WamInstruction instruction, BacktrackingPredicate predicate)
        {
            var arguments = new WamReferenceTarget[instruction.Functor.Arity];

            for (var index = 0; index < instruction.Functor.Arity; ++index)
            {
                arguments[index] = ArgumentRegisters[index];
            }

            IEnumerable <bool> enumerable;

            try
            {
                enumerable = predicate.BacktrackingPredicateDelegate(this, arguments);
            }
            catch
            {
                // Backtrack on exception.
                //
                return(ExecutionResults.Backtrack);
            }

            var enumerator = enumerable.GetEnumerator();

            ChoicePoint = new WamChoicePoint(ChoicePoint, Environment, StackIndex, ReturnInstructionPointer, ArgumentRegisters, CutChoicePoint)
            {
                BacktrackInstructionPointer = InstructionPointer.GetNext(),
                PredicateEnumerator         = enumerator
            };
            InstructionPointer  = ChoicePoint.BacktrackInstructionPointer;
            PredicateEnumerator = ChoicePoint.PredicateEnumerator;
            return(ExecutionResults.None);
        }