static public List <string> SetupFacts(List <string> true_facts, string provable) { facts = Parser.getFactsInput("..\\..\\FACTS.txt"); rules = Parser.getRulesInput("..\\..\\RULES.txt"); List <Fact> trueFacts = facts.FindAll(x => true_facts.Any(x.description.ToLower().Contains)); if (provable != "") { ProofNodeCF proven = directProofCF(provable, trueFacts, new HashSet <Rule>()); List <string> proofstrings = new List <string>(); printProofNodeCF(new List <ProofNodeCF> { proven }, proofstrings); return(proofstrings); } else { List <ProofNodeCF> proofNodeCFs = directProofAllCF(trueFacts, new HashSet <Rule>()); List <string> proofstrings = new List <string>(); foreach (ProofNodeCF node in proofNodeCFs) { foreach (var fact in node.nextFacts) { if (fact.id.Contains("Игра")) { proofstrings.Add(fact.description); } } //printProofNodeCF(proofNodeCFs, proofstrings); } return(proofstrings); } }
static ProofNodeCF directProofCF(string provable, List <Fact> trueFacts, HashSet <Rule> appliedRules, int depth = 0) { HashSet <Rule> iterationAppliedRules = new HashSet <Rule>(); foreach (Rule rule in rules) { if (!appliedRules.Contains(rule)) { List <string> trueFactsIds = trueFacts.Select(x => x.id).ToList(); var intersection = trueFactsIds.Intersect(rule.premises).ToList(); intersection.Sort(); if (rule.premises.SequenceEqual(intersection)) { if (rule.conclusions.Contains(provable)) { List <Fact> premises = trueFacts.FindAll(x => rule.premises.Contains(x.id)); string desc = facts.Find(x => x.id == rule.conclusions[0]).description; List <Fact> nextFacts = new List <Fact> { new Fact(rule.conclusions[0], desc, calcCF(trueFacts, rule)) }; return(new ProofNodeCF(rule, premises, nextFacts, null, depth)); } iterationAppliedRules.Add(rule); appliedRules.Add(rule); } } } if (iterationAppliedRules.Count == 0) { return(null); } foreach (Rule rule in rules) { if (iterationAppliedRules.Contains(rule)) { double CF = 0.0; Fact possible = trueFacts.Find(x => x.id == rule.conclusions[0]); if (possible == null) { CF = calcCF(trueFacts, rule); } else { double CF1 = calcCF(trueFacts, rule); double CF2 = possible.CF; if (CF1 >= 0 && CF2 >= 0) { CF = CF1 + CF2 - CF1 * CF2; } else if (CF1 < 0 && CF2 < 0) { CF = CF1 + CF2 + CF1 * CF2; } else { CF = (CF1 + CF2) / (1 - Math.Min(Math.Abs(CF1), Math.Abs(CF2))); } } string desc = facts.Find(x => x.id == rule.conclusions[0]).description; Fact conclusion = new Fact(rule.conclusions[0], desc, CF); List <Fact> premises = trueFacts.FindAll(x => rule.premises.Contains(x.id)); ProofNodeCF res = directProofCF(provable, trueFacts.Append(conclusion).ToList(), appliedRules, depth + 1); if (res != null) { return(new ProofNodeCF(rule, premises, new List <Fact> { conclusion }, new List <ProofNodeCF> { res }, depth)); } } } return(null); }