static public RuleData ToRules(List <Token> input)
        {
            var output      = new RuleData();
            var currentTerm = new Term();

            output.Terms.Add(currentTerm);
            currentTerm.Id = output.Terms.Count;

            while (input.Count > 0)
            {
                var currentAtom = new Atom();
                currentTerm.Atoms.Add(currentAtom);

                //negativeness
                if (input[0].Type == TokenType.Negate)
                {
                    currentAtom.Truthfulness = false;
                    TokenPop(input);
                }

                //name
                var nameToken = TokenPop(input);
                Expect(nameToken, TokenType.String);
                currentAtom.Name = nameToken.Name;

                //arguments
                Expect(TokenPop(input), TokenType.ParenBegin);
                GetArguments(input, currentAtom.Arguments);

                var orOrPeriodOrQuestionMark = TokenPop(input);
                switch (orOrPeriodOrQuestionMark.Type)
                {
                case TokenType.Or:
                    continue;

                case TokenType.Period:
                    if (input.Count > 0)
                    {
                        currentTerm = new Term();
                        output.Terms.Add(currentTerm);
                        currentTerm.Id = output.Terms.Count;
                    }
                    continue;

                case TokenType.QuestionMark:
                    output.Terms.Remove(currentTerm);
                    output.Goal = currentTerm.Atoms[0];
                    if (input.Count > 0)
                    {
                        currentTerm = new Term();
                        output.Terms.Add(currentTerm);
                        currentTerm.Id = output.Terms.Count;
                    }
                    continue;
                }
                throw new Exception("Not the right input: " + orOrPeriodOrQuestionMark.Name);
            }

            return(output);
        }
        public RuleData Prune(int rulesLength)
        {
            var indexComparer = new IndexComparer();
            var output        = new RuleData();

            PruneAddParents(output.Terms, Terms.Last());
            output.Terms.Sort(indexComparer);
            for (var index = output.Terms.Count - 1; index >= 0; index--)
            {
                var term = output.Terms[index];
                PruneRenumber(output.Terms, term, rulesLength);
            }
            return(output);
        }
        public static List <Term> GetNewTerms(Term newestTerm, RuleData rules, List <Term> newTerms)
        {
            var output = new List <Term>();

            for (var i = 0; i < rules.Terms.Count - 1; i++)
            {
                var combinedTerm = CombineTerms(rules.Terms[i], newestTerm);
                if (!combinedTerm.SubMap.Failure &&
                    !combinedTerm.ExistsInList(rules.Terms) &&
                    !combinedTerm.ExistsInList(newTerms))
                {
                    output.Add(combinedTerm);
                }
            }

            return(output);
        }
        public static void Resolve(RuleData rules)
        {
            Console.WriteLine("Input clauses: ");
            rules.Print();
            Console.WriteLine("\nQuery:\n     " + rules.Goal.Stringify() + "?\n");

            var negatedGoal = new Atom(rules.Goal);

            negatedGoal.Truthfulness = !negatedGoal.Truthfulness;
            var negatedGoalTerm = new Term(negatedGoal)
            {
                FromGoal = 0
            };
            var newRules = new RuleData();

            newRules.Terms.Add(negatedGoalTerm);
            rules.Terms.Add(negatedGoalTerm);
            negatedGoalTerm.Id = rules.Terms.Count;
            var initialRulesLength = rules.Terms.Count;

            var newTerms = GetInitialNewTerms(rules);

            while (true)
            {
                if (newTerms.Count == 0)
                {
                    Console.WriteLine("Result: FALSE. No solution.");
                    break;
                }
                newTerms.Sort(termComparer);
                var topTerm = newTerms[0];
                newTerms.Remove(topTerm);
                newRules.Terms.Add(topTerm);
                rules.Terms.Add(topTerm);
                topTerm.Id = rules.Terms.Count;
                if (topTerm.Atoms[0].IsBool && !topTerm.Atoms[0].Truthfulness)
                {
                    Console.WriteLine("Result: TRUE.\n\nProof clauses added:");
                    newRules = newRules.Prune(initialRulesLength);
                    newRules.Print();
                    break;
                }
                var newestTerms = GetNewTerms(topTerm, rules, newTerms);
                newTerms.AddRange(newestTerms);
            }
        }
        public static List <Term> GetInitialNewTerms(RuleData rules)
        {
            var output = new List <Term>();

            for (var i = 0; i < rules.Terms.Count; i++)
            {
                for (var j = i + 1; j < rules.Terms.Count; j++)
                {
                    var combinedTerm = CombineTerms(rules.Terms[i], rules.Terms[j]);
                    if (!combinedTerm.SubMap.Failure &&
                        !combinedTerm.ExistsInList(rules.Terms) &&
                        !combinedTerm.ExistsInList(output))
                    {
                        output.Add(combinedTerm);
                    }
                }
            }

            return(output);
        }
Esempio n. 6
0
 private static bool ResolveGoal(RuleData rules)
 {
     Resolver.Resolve(rules);
     return(true);
 }