Esempio n. 1
0
            public override Proposition Resolve(string[] key, SymbolIs[] state)
            {
                //Resolve child prepositions.
                _requirement = _requirement.Resolve(key, state);
                _implication = _implication.Resolve(key, state);

                SymbolIs solution = CheckSolvable(key, state);

                if (solution == SymbolIs.True)
                {
                    return(new True());
                }
                else if (solution == SymbolIs.False)
                {
                    return(new Not(new True()));
                }
                else
                {
                    //If the requirement [A in A=>B] is true, return the implication [B].
                    SymbolIs req = _requirement.CheckSolvable(key, state);

                    if (req == SymbolIs.True)
                    {
                        return(_implication);
                    }
                    else
                    {
                        return(this);
                    }
                }
            }
Esempio n. 2
0
            public override Proposition Resolve(string[] key, SymbolIs[] state)
            {
                //Resolve child prepositions.
                _fact1 = _fact1.Resolve(key, state);
                _fact2 = _fact2.Resolve(key, state);

                SymbolIs sol1 = _fact1.CheckSolvable(key, state);
                SymbolIs sol2 = _fact2.CheckSolvable(key, state);

                //If both are false resolve to Not-True, if either is true resolve to True.
                if (sol1 == SymbolIs.False && sol2 == SymbolIs.False)
                {
                    return(new Not(new True()));
                }
                else if (sol1 == SymbolIs.True || sol2 == SymbolIs.True)
                {
                    return(new True());
                }
                else
                {
                    //If one of the facts can be proved false, it can be removed from the logic.
                    if (sol1 == SymbolIs.False)
                    {
                        return(_fact2);
                    }
                    else if (sol2 == SymbolIs.False)
                    {
                        return(_fact1);
                    }
                    else
                    {
                        return(this);
                    }
                }
            }
Esempio n. 3
0
        protected override int QueryUsingRule(Rule query)
        {
            List <HornRule> checkedRules = new List <HornRule>();

            bool stateChanged = true;

            while (stateChanged)
            {
                stateChanged = false;
                //Try to solve each of the rules given the current knowledge.
                foreach (HornRule HR in KB)
                {
                    if (checkedRules.Contains(HR))
                    {
                        continue;
                    }
                    //Resolve the rule. If it was fully resolved:
                    HR.Resolve(literalsKey, literalsSolutions);
                    if (HR.IsFullyResolved)
                    {
                        //If this rule proves a literal (i.e. doesn't return Unknown), add the literal to the knowledge and remove the rule.
                        HornRule.Literal newLiteral = HR.ProveLiteral(literalsKey, literalsSolutions);
                        if (newLiteral.state != SymbolIs.Unknown)
                        {
                            stateChanged = true;
                            checkedRules.Add(HR);

                            //Loop through the knowledge table and add the new information at the correct index.
                            for (int i = 0; i < literalsKey.Length; ++i)
                            {
                                if (literalsKey[i] == newLiteral.symbol)
                                {
                                    if (literalsSolutions[i] == SymbolIs.Unknown)
                                    {
                                        literalsSolutions[i] = newLiteral.state;
                                    }
                                    else if (literalsSolutions[i] != newLiteral.state)
                                    {
                                        throw new Exception("KnowledgeBase is contradictory");
                                    }
                                }
                            }
                        }
                    }
                }

                //If the query has been proven, return either true or false based on the outcome.
                SymbolIs solution = query.CheckSolved(literalsKey, literalsSolutions);
                if (solution == SymbolIs.True)
                {
                    return(1);
                }
                else if (solution == SymbolIs.False)
                {
                    return(0);
                }
            } //End of While loop
            return(0);
        }
Esempio n. 4
0
 public Literal(string Symbol, bool Negated)
 {
     symbol = Symbol;
     if (Negated)
     {
         state = SymbolIs.False;
     }
     else
     {
         state = SymbolIs.True;
     }
 }
Esempio n. 5
0
 protected void FindDefinitions()
 {
     for (int i = 0; i < literalsKey.Length; ++i)
     {
         literalsSolutions[i] = SymbolIs.Unknown;
         foreach (HornRule HR in KB)
         {
             SymbolIs solution = HR.DefinesLiteral(literalsKey[i]);
             if (solution != SymbolIs.Unknown)
             {
                 literalsSolutions[i] = solution;
                 break;
             }
         }
     }
 }
Esempio n. 6
0
            public override SymbolIs CheckSolvable(string[] key, SymbolIs[] state)
            {
                SymbolIs solution = _fact.CheckSolvable(key, state);

                if (solution == SymbolIs.False)
                {
                    return(SymbolIs.True);
                }
                else if (solution == SymbolIs.True)
                {
                    return(SymbolIs.False);
                }
                else
                {
                    return(SymbolIs.Unknown);
                }
            }
Esempio n. 7
0
            /// <summary> Using the given knowledge, return the most simple solvable version of this preposition chain. Fully solvable chain will result in True(). </summary>
            public virtual Proposition Resolve(string[] key, SymbolIs[] state)
            {
                SymbolIs solution = CheckSolvable(key, state);

                if (solution == SymbolIs.True)
                {
                    return(new True());
                }
                else if (solution == SymbolIs.False)
                {
                    return(new Not(new True()));
                }
                else
                {
                    return(this);
                }
            }
Esempio n. 8
0
        public Literal ProveLiteral(string[] key, SymbolIs[] knowledge)
        {
            SymbolIs solution = _rule.CheckSolvable(key, knowledge);

            if (solution == SymbolIs.True)
            {
                return(_literalVal);
            }
            else if (solution == SymbolIs.False)
            {
                return(new Literal(_literalVal.symbol, _literalVal.Inverse));
            }
            else
            {
                return(new Literal(_literalVal.symbol, SymbolIs.Unknown));
            }
        }
Esempio n. 9
0
            public override SymbolIs CheckSolvable(string[] key, SymbolIs[] state)
            {
                SymbolIs sol1 = _fact1.CheckSolvable(key, state);
                SymbolIs sol2 = _fact2.CheckSolvable(key, state);

                if (sol1 == SymbolIs.False && sol2 == SymbolIs.False)
                {
                    return(SymbolIs.False);
                }
                else if (sol1 == SymbolIs.True || sol2 == SymbolIs.True)
                {
                    return(SymbolIs.True);
                }
                else
                {
                    return(SymbolIs.Unknown);
                }
            }
Esempio n. 10
0
            public override SymbolIs CheckSolvable(string[] key, SymbolIs[] state)
            {
                SymbolIs req = _requirement.CheckSolvable(key, state);
                SymbolIs imp = _implication.CheckSolvable(key, state);

                if (req == SymbolIs.False)
                {
                    return(SymbolIs.True);
                }
                else if (imp == SymbolIs.True)
                {
                    return(SymbolIs.True);
                }
                else if (req == SymbolIs.True && imp == SymbolIs.False)
                {
                    return(SymbolIs.False);
                }
                else
                {
                    return(SymbolIs.Unknown);
                }
            }
Esempio n. 11
0
            public override Proposition Resolve(string[] key, SymbolIs[] state)
            {
                //Resolve child prepositions.
                _fact1 = _fact1.Resolve(key, state);
                _fact2 = _fact2.Resolve(key, state);

                SymbolIs sol1 = _fact1.CheckSolvable(key, state);
                SymbolIs sol2 = _fact2.CheckSolvable(key, state);

                //If the either fact is Unknown return this, otherwise resolve to True if they are the same, or false if they are different.
                if (sol1 == sol2 && sol1 != SymbolIs.Unknown)
                {
                    return(new True());
                }
                else if (sol1 != SymbolIs.Unknown && sol2 != SymbolIs.Unknown)
                {
                    return(new Not(new True()));
                }
                else
                {
                    return(this);
                }
            }
Esempio n. 12
0
        //UNUSED! Simplifies the KB by substituting proven literals into rules. Created to understand how to solve the KB.
        public void SolveKB()
        {
            bool ruleChanged = true;

            while (ruleChanged)
            {
                ruleChanged = false;
                foreach (HornRule R in KB)
                {
                    if (R.Resolve(literalsKey, literalsSolutions))
                    {
                        ruleChanged = true;
                        for (int i = 0; i < literalsKey.Length; ++i)
                        {
                            SymbolIs solution = R.DefinesLiteral(literalsKey[i]);
                            if (solution != SymbolIs.Unknown && literalsSolutions[i] == SymbolIs.Unknown)
                            {
                                literalsSolutions[i] = solution;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 13
0
 public Literal(string Symbol, SymbolIs State)
 {
     symbol = Symbol;
     state  = State;
 }
Esempio n. 14
0
        protected override int QueryUsingRule(Rule query)
        {
            //Initialize
            List <string> literalsToSolve = new List <string>();

            foreach (string symbol in query.symbols)
            {
                literalsToSolve.Add(symbol);
            }
            List <HornRule> rulesToSolve = new List <HornRule>();
            List <HornRule> checkedRules = new List <HornRule>();
            bool            stateChanged = true;

            while (stateChanged == true)
            {
                stateChanged = false;

                //Add the the list  of rules to solve any rules which are relevant to our query (I.e. are relevant to symbols which are relevant).
                //Then add each symbol used by those rules to the list of literals to solve.
                foreach (HornRule HR in KB)
                {
                    if (checkedRules.Contains(HR))
                    {
                        continue;
                    }
                    for (int i = 0; i < literalsToSolve.Count; ++i)
                    {
                        if (HR.LiteralIsImplication(literalsToSolve[i]))
                        {
                            stateChanged = true;
                            rulesToSolve.Add(HR);
                            checkedRules.Add(HR);
                            foreach (string s in HR.symbols)
                            {
                                if (!literalsToSolve.Contains(s))
                                {
                                    literalsToSolve.Add(s);
                                }
                            }
                            break;
                        }
                    }
                }

                //Try to solve each of the rules given the current knowledge.
                //If a rule is solved, remove it from the list, and add any symbols it entails to the KB.
                for (int r = 0; r < rulesToSolve.Count; ++r)
                {
                    //Resolve the rule. If it was fully resolved:
                    rulesToSolve[r].Resolve(literalsKey, literalsSolutions);
                    if (rulesToSolve[r].IsFullyResolved)
                    {
                        //If this rule proves a literal (i.e. doesn't return Unknown), add the literal to the knowledge and remove the rule.
                        HornRule.Literal newLiteral = rulesToSolve[r].ProveLiteral(literalsKey, literalsSolutions);
                        if (newLiteral.state != SymbolIs.Unknown)
                        {
                            stateChanged = true;

                            //Loop through the knowledge table and add the new information at the correct index.
                            for (int i = 0; i < literalsKey.Length; ++i)
                            {
                                if (literalsKey[i] == newLiteral.symbol)
                                {
                                    if (literalsSolutions[i] == SymbolIs.Unknown)
                                    {
                                        literalsSolutions[i] = newLiteral.state;
                                    }
                                    else if (literalsSolutions[i] != newLiteral.state)
                                    {
                                        throw new Exception("KnowledgeBase is contradictory");
                                    }
                                }
                            }

                            //Stop trying to find rules relevant to the proven literal.
                            literalsToSolve.Remove(newLiteral.symbol);

                            //Remove the solved rule from the list, and go back one index.
                            rulesToSolve.RemoveAt(r);
                            --r;
                        }
                    }
                }

                //If the query has been proven, return either true or false based on the outcome.
                SymbolIs solution = query.CheckSolved(literalsKey, literalsSolutions);
                if (solution == SymbolIs.True)
                {
                    return(1);
                }
                else if (solution == SymbolIs.False)
                {
                    return(0);
                }
            } //End of While loop.

            return(0);
        }