Ejemplo n.º 1
0
    public static InferenceRule Contrapose(InferenceRule rule)
    {
        Expression[] newConclusions = new Expression[rule.Premises.Length];
        for (int i = 0; i < rule.Premises.Length; i++)
        {
            newConclusions[i] = new Expression(Expression.NOT, rule.Premises[i]);
        }
        Expression[] newPremises = new Expression[rule.Conclusions.Length];
        for (int i = 0; i < rule.Conclusions.Length; i++)
        {
            newPremises[i] = new Expression(Expression.NOT, rule.Conclusions[i]);
        }

        return(new InferenceRule(newPremises, rule.Assumptions, newConclusions));
    }
Ejemplo n.º 2
0
        private void InitQuest()
        {
            var rule = new InferenceRule(new BoolExpression(new Information(_player, WorldAdjectives[Adjectives.King])))
                       .And(new BoolExpression(new Information(_player, GameObject.Find("Castle").GetComponent <Location>())));

            rule.AppliesToSelf = true;
            var info = new Information(GameObject.Find("Dragon").GetComponent <NPC>(), WorldAdjectives[Adjectives.Dead]);

            Quest q = new Quest(GameObject.Find("Queen").GetComponent <NPC>());

            q.GoalRules.Add(rule);
            q.GoalInfos.Add(info);

            Quest = q;
        }
Ejemplo n.º 3
0
        private void InitRules()
        {
            var rule = new InferenceRule(new BoolExpression(new Information(_player, WorldAdjectives[Adjectives.Armed])))
                       .And(new BoolExpression(new Information(_player, WorldAdjectives[Adjectives.Dangerous])));

            rule.Consequences = new List <Information> {
                new Information(_player, WorldAdjectives[Adjectives.Enemy])
            };
            rule.AppliesToSelf = false;

            rule = new InferenceRule(new BoolExpression(new Information(_player, WorldAdjectives[Adjectives.Enemy])));
            rule.Consequences = new List <Information> {
                new Information(_player, WorldAdjectives[Adjectives.Dangerous])
            };
            rule.AppliesToSelf = false;

            WorldRules.Add(rule);
        }
Ejemplo n.º 4
0
        public void ApplyInference(InferenceRule rule)
        {
            //get the array of queries parameters
            List<string[]>queries = rule.GetQueries();

            string[] bindings = null;
            string[] newTriple = null;

            foreach (string[] query in queries)
            {
                //get a set of parameter bindings (term:value). E.g. "?param:value1,param2:value2")
                bindings = Query(query);
                //for each binding set add a triple/statement expressing the rule
                foreach (string binding in bindings)
                {
                   newTriple = rule.MakeTriple(binding); //make a triple string passing binding values
                   Add(newTriple);
                }
            }
        }
Ejemplo n.º 5
0
 public void CreateRule(InferenceRule <ClueConstant> ir)
 {
     _rules.Add(ir);
 }
Ejemplo n.º 6
0
        /// <inheritdoc/>
        public void MarkInvalidInferrence(Configuration configuration, Theorem invalidConclusion, InferenceRule inferenceRule, Theorem[] negativeAssumptions, Theorem[] possitiveAssumptions)
        {
            // Prepare the file path for the rule with the name of the rule
            var filePath = Path.Combine(_settings.InvalidInferenceFolder, $"{inferenceRule.ToString().Replace(Path.DirectorySeparatorChar, '_')}.{_settings.FileExtension}");

            // If adding this inference would reach the maximal number of written inferences, we're done
            if (_invalidInferencesPerFile.GetValueOrDefault(filePath) + 1 > _settings.MaximalNumberOfInvalidInferencesPerFile)
            {
                return;
            }

            // Otherwise create or get the file in the invalid inference folder
            using var writer = new StreamWriter(filePath, append: true);

            // Prepare the formatter of the configuration
            var formatter = new OutputFormatter(configuration.AllObjects);

            // Write the configuration
            writer.WriteLine(formatter.FormatConfiguration(configuration));

            // An empty line
            writer.WriteLine();

            // Write the incorrect theorem
            writer.WriteLine($" {formatter.FormatTheorem(invalidConclusion)}");

            // Write its assumptions
            possitiveAssumptions.ForEach(assumption => writer.WriteLine($"  - {formatter.FormatTheorem(assumption)}"));

            // As well as negative ones
            negativeAssumptions.ForEach(assumption => writer.WriteLine($"  ! {formatter.FormatTheorem(assumption)}"));

            // Separator
            writer.WriteLine("--------------------------------------------------\n");

            // Mark that we've used this inference
            _invalidInferencesPerFile[filePath] = _invalidInferencesPerFile.GetValueOrDefault(filePath) + 1;
        }
Ejemplo n.º 7
0
        public bool CompleteInfer(List <Fact> inputs, List <Request> outputs)
        {
            // TODO: replace this global variable
            InferenceRule Rule = null;

            foreach (Fact input in inputs)
            {
                Known.Add(input);
            }

            bool       completeFlag = false; // duyet het luat chua
            List <int> usedIndexes  = new List <int>();

            // luu lai danh sach cac luat da di qua va duoc ap dung de tranh lap lai

            while (!completeFlag)        // neu chuat duyet het luat
            {
                bool usableFlag = false; // luat co ap dung dung khong
                int  index      = 0;     // vi tri cua luat hien tai

                // duyet tung gia thiet trong tap luat
                foreach (InferenceRule rule in RuleBase.InferenceRules)
                {
                    IEnumerable <Fact> hypotheses = rule.Hypotheses;

                    // Tim cach luu lai nhung luat ma da duyet co trong Know nhung chua co result

                    usableFlag = CheckHypothesesInKnown(hypotheses, Known); // Kiem tra luat co ap dung duoc khong
                    if (usableFlag)
                    {
                        // Kiem tra xem luat nay da ap dung chua va dua su kien vao Known chua de tranh lap lai
                        if (!usedIndexes.Contains(index))
                        {
                            // Neu chua ap dung
                            IEnumerable <Fact> conclusions = rule.Conclusions;
                            if (CheckRequestsInConclusions(outputs, conclusions))
                            // Kiem tra xem su kien ket luan co phai la ket qua khong
                            {
                                foreach (CrispFact fact in hypotheses)
                                {
                                    Console.Write(fact.ToString() + "\t");
                                }
                                Console.Write(" -> ");
                                foreach (CrispFact fact in conclusions)
                                {
                                    Console.Write(fact.ToString() + "\t");
                                }
                                Console.WriteLine();
                            }
                            else // Su kien ket luan khong la ket qua
                            {
                                foreach (Fact conclusion in conclusions)
                                {
                                    // Them tat ca su kien ket luan vao Known
                                    Known.Add(conclusion);
                                }
                            }

                            usedIndexes.Add(index); // luat o vi tri index da duyet va cap nhat vao Known
                            break;
                        }
                    }

                    index++;
                }

                // Neu duyet het luat ma chua tim duoc ket luan
                if (index == RuleBase.InferenceRules.Count)
                {
                    completeFlag = true;
                }


                foreach (RelationRule rule in RuleBase.RelationRules)
                {
                    for (int i = 0; i < Known.Count; ++i)
                    {
                        Fact fact = Known[i];

                        IndividualCrispFact individualFact = fact as IndividualCrispFact;
                        if (individualFact == null)
                        {
                            continue;
                        }

                        if (individualFact.Class == rule.Domain)
                        {
                            Tuple <IEnumerable <string>, IEnumerable <string> > result =
                                _knowledgeManager.RunQuery(rule.Query, "autogen0:" + individualFact.Individual, rule.QueryRange);
                            foreach (string rangeIndividual in result.Item2)
                            {
                                string individual = rangeIndividual.Replace("autogen0:", "");
                                Console.WriteLine(individual); // workar5ound

                                Fact rangeFact = new IndividualCrispFact
                                {
                                    Class      = rule.Range,
                                    Individual = individual
                                };

                                if (!Known.Contains(rangeFact))
                                {
                                    Known.Add(rangeFact);
                                }
                            }
                        }
                        else if (individualFact.Class == rule.Range)
                        {
                            Tuple <IEnumerable <string>, IEnumerable <string> > result =
                                _knowledgeManager.RunQuery(rule.Query, rule.QueryDomain, "autogen0:" + individualFact.Individual);
                            foreach (string domainIndividual in result.Item1)
                            {
                                string individual = domainIndividual.Replace("autogen0:", "");
                                Console.WriteLine(individual);

                                Fact domainFact = new IndividualCrispFact
                                {
                                    Class      = rule.Range,
                                    Individual = individual
                                };

                                if (!Known.Contains(domainFact))
                                {
                                    Known.Add(domainFact);
                                }
                            }
                        }
                    }
                }
            }

            // TODO:

            return(false);
        }