/// <summary> /// Tests clauses in a randomized order (but still exhaustively). /// Uses Shuffler to generate a random permutation. /// </summary> IEnumerable <CutState> TestShuffledClauses(object[] args, PrologContext context, ushort myFrame) { entriesListUsed = true; var mark = context.MarkTrace(); var shuffler = new Shuffler((ushort)Entries.Count); var argIndexers = PredicateArgumentIndexer.ArglistIndexers(args); while (!shuffler.Done) { var entry = Entries[shuffler.Next()]; if (entry.Prematch(argIndexers)) { // This shouldn't be here... //context.PushGoalStack(Name, args, myFrame); context.SetCurrentRule(entry); foreach (var cutState in entry.Prove(args, context, myFrame)) { if (cutState == CutState.ForceFail) { if (KnowledgeBase.Trace || Trace) { context.TraceOutput("Cut: {0}", new Structure(Name, args)); } goto fail; } if (KnowledgeBase.Trace || Trace) { context.TraceOutput("Succeed: {0}", new Structure(Name, args)); } yield return(CutState.Continue); if (KnowledgeBase.Trace || Trace) { context.TraceOutput("Retry: {0}", new Structure(Name, args)); } } } } fail: context.RestoreVariables(mark); if (KnowledgeBase.Trace || Trace) { context.TraceOutput("Fail: {0}", new Structure(Name, args)); } //context.UnwindStack(Name, args); context.UnwindStack(myFrame); }
private static IEnumerable<CutState> RandomMemberImplementation(object[] args, PrologContext context) { if (args.Length != 2) throw new ArgumentCountException("random_member", args, "element", "list"); object objectArg = Term.Deref(args[0]); object listArg = Term.Deref(args[1]); if (listArg == null) yield break; var arg = listArg as LogicVariable; if (arg != null) throw new InstantiationException(arg, "List argument to random_member/2 must be instantiated to a proper list."); var length = Prolog.PrologListLength(listArg); var shuffler = new Shuffler((ushort)length); while (!shuffler.Done) #pragma warning disable 414, 168, 219 // ReSharper disable once UnusedVariable foreach (var ignore in Term.Unify(objectArg, Prolog.PrologListElement(listArg, shuffler.Next()))) #pragma warning restore 414, 168, 219 yield return CutState.Continue; }
/// <summary> /// Tests clauses in a randomized order (but still exhaustively). /// Uses Shuffler to generate a random permutation. /// </summary> IEnumerable<CutState> TestShuffledClauses(object[] args, PrologContext context, ushort myFrame) { entriesListUsed = true; var mark = context.MarkTrace(); var shuffler = new Shuffler((ushort)Entries.Count); var argIndexers = PredicateArgumentIndexer.ArglistIndexers(args); while (!shuffler.Done) { var entry = Entries[shuffler.Next()]; if (entry.Prematch(argIndexers)) { // This shouldn't be here... //context.PushGoalStack(Name, args, myFrame); context.SetCurrentRule(entry); foreach (var cutState in entry.Prove(args, context, myFrame)) { if (cutState == CutState.ForceFail) { if (KnowledgeBase.Trace || Trace) context.TraceOutput("Cut: {0}", new Structure(Name, args)); goto fail; } if (KnowledgeBase.Trace || Trace) context.TraceOutput("Succeed: {0}", new Structure(Name, args)); yield return CutState.Continue; if (KnowledgeBase.Trace || Trace) context.TraceOutput("Retry: {0}", new Structure(Name, args)); } } } fail: context.RestoreVariables(mark); if (KnowledgeBase.Trace || Trace) context.TraceOutput("Fail: {0}", new Structure(Name, args)); //context.UnwindStack(Name, args); context.UnwindStack(myFrame); }