コード例 #1
0
        abstract public String EvaluateQuery(QueryClass Query, Boolean Debug); //?????????

        // Function to check whether the KB entails the Query
        // Returns a string response: YES or NO.
        // If the answer is YES, it is follwed by a colon (:)
        // and the number of models of KB
        protected void LoadSymbolsList(QueryClass Query)
        {
            // First build a list of symbols in the KB and query
            List <String> AllSymbols    = new List <string>(); // List of symbols contained within the KB and Query
            List <String> ClauseSymbols = new List <string>();
            List <String> QuerySymbols  = new List <string>();

            // Check the KB clauses for symbols
            foreach (HornClauseClass Clause in this.KnowledgeBase)
            {
                ClauseSymbols = Clause.GetSymbols();

                foreach (String Symbol in ClauseSymbols)
                {
                    if (!AllSymbols.Contains(Symbol))
                    {
                        // only add symbols that aren't already contained
                        AllSymbols.Add(Symbol);
                        //Console.WriteLine("New Symbol found in KB: " + Symbol);
                    }
                    else
                    {
                        //Console.WriteLine("Repeated Symbol found in KB: " + Symbol);
                    }
                }
            }

            // Add the query symbols

            QuerySymbols = Query.QueryClause.GetSymbols();

            foreach (String Symbol in QuerySymbols)
            {
                if (!AllSymbols.Contains(Symbol))
                {
                    // only add symbols that aren't already contained
                    AllSymbols.Add(Symbol);
                    //Console.WriteLine("New Symbol found in Query: " + Symbol);
                }
                else
                {
                    //Console.WriteLine("Repeated Symbol found in Query: " + Symbol);
                }
            }
            // Save symbols to KB
            this.Symbols = AllSymbols;
        }
コード例 #2
0
        public override string EvaluateQuery(QueryClass Query, Boolean Debug)
        {
            string Result = "YES:";
            // initialise our goals list to the symbol(s) contained within the query
            List <string> goals    = Query.QueryClause.GetSymbols();
            List <string> searched = new List <string>();

            // starting from the symbol query, iteratively check if each symbol in the goals list
            // is a conclusion of another KB clause, or is a fact.
            // if the symbol is a conclusion of another KB clause, then we add the symbols of
            // the premise of that clause to the goals symbols list
            while (goals.Count > 0)
            {
                String goal = goals[0];
                goals.RemoveAt(0);
                searched.Add(goal);
                HornClauseClass ex = isConclusionOrFact(goal);//Not sure why I called it ex. name can be changed - Thomas
                if (ex == null)
                {
                    //null should say that it wasn't a conclusion or a fact in the knowledge base
                    //and therefore should return null
                    return("NO");
                }
                else if (ex.GetType().Name == "HornClauseImplicationClass")
                {
                    //probably should keep the clauses
                    List <String> temp = ex.GetPremiseSymbols();
                    foreach (String str in temp)
                    {
                        if (!goals.Contains(str) && !searched.Contains(str))
                        {
                            goals.Add(str);
                        }
                    }
                }
            }
            searched.Reverse();
            for (int i = 0; i < searched.Count; i++)
            {
                Result += " " + searched[i];
                if (i < searched.Count - 1)
                {
                    Result += ",";
                }
            }
            return(Result);
        }
コード例 #3
0
        public List <List <Boolean> > Table = new List <List <Boolean> >();     // The actual TT

        // Methods
        public TruthTableClass(List <HornClauseClass> KB, QueryClass Query)
        {
            KnowledgeBase = KB;               // Assign the KB to the object being constructed
            LoadSymbolsList(Query);
            // create 2^n Rows, with n + 1 collumns (+1 column for the query result)
            int NumSymbols = Symbols.Count;  // The order of this list is the order of columns in the table
            int NumClauses = KnowledgeBase.Count;

            List <Boolean> TempRow = new List <bool>();   // used to build rows before they are put into the table

            for (int RowNum = 0; RowNum < Math.Pow(2, NumSymbols); RowNum++)
            {
                TempRow = new List <bool>(); // Reset TempRow at the start of every row
                for (int ColNum = 0; ColNum < NumSymbols + NumClauses + 2; ColNum++)
                {
                    // set alternating boolean value for first NumSymbols" rows
                    if (ColNum < NumSymbols)
                    {
                        if ((RowNum % Math.Pow(2, NumSymbols - ColNum)) < Math.Pow(2, NumSymbols - ColNum - 1))
                        {
                            TempRow.Add(false);
                        }
                        else
                        {
                            TempRow.Add(true);
                        }
                    }
                    else if (ColNum >= NumSymbols && ColNum < (NumSymbols + NumClauses + 1))
                    {
                        // These are the Clause evaluation cells
                        // Set to false by default
                        TempRow.Add(false);
                    }
                    else
                    {
                        // These are the Query evaluation cells
                        // Set to false by default
                        TempRow.Add(false);
                    }
                }
                Table.Add(TempRow); // Add row to the table
            }
            EvaluateClauses();
            //Console.WriteLine("TT World Computation Done");
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: BenskiBoy/InferenceEngine
        public static void Main(string[] args)
        {
            // Main Input Arguments      //
            // args[0] inference method	 //
            // args[1] filename			 //

            String  fileName  = "default";
            String  inference = "default";
            Boolean Debug     = false;

            if (args.Length < 2)
            {
                Console.WriteLine("ERROR: NOT ENOUGH INPUT PARAMETERS");
                return;
            }
            else if (args.Length == 2)
            {
                inference = args [0];
                fileName  = args [1];
            }
            else if (args.Length == 3)
            {
                inference = args [0];
                fileName  = args [1];
                if (args[2] == "DEBUG")
                {
                    Debug = true;
                }
            }
            else
            {
                Console.WriteLine("ERROR: TOO MANY INPUT PARAMETERS");
                return;
            }

            if (Debug)
            {
                Console.WriteLine("Program begin");
            }



            //String inference = args[0];
            //String fileName = args [1];

            // debugging stuff for 3rd argument, error checking in input?

            Parser        P = new Parser();
            InferenceType iEngine;

            List <HornClauseClass> parsedKB    = P.GetKB(fileName);
            QueryClass             parsedQuery = new QueryClass(P.GetQuery(fileName));

            switch (inference)
            {
            case "TT":
                iEngine = new TruthTableClass(parsedKB, parsedQuery);
                break;

            case "FC":
                iEngine = new ForwardChaining(parsedKB);
                break;

            case "BC":
                iEngine = new BackwardChaining(parsedKB);
                break;

            default:
                Console.WriteLine("Inference Type code invalid");
                return;
            }
            String Result = iEngine.EvaluateQuery(parsedQuery, Debug);

            Console.WriteLine(Result);
        }
コード例 #5
0
        // Method to evaluate the querey collumn of the TT
        // return value shows number of times query enatils KB...
        override public string EvaluateQuery(QueryClass Query, Boolean Debug)
        {
            // first fill in the collumn, then check if query is a subset of
            // the valid worlds of the TT (the rows where SUM clauses = true)
            LoadSymbolsList(Query);
            List <String>      ClauseSymbols   = new List <String>();
            List <SymbolValue> SymbolValues    = new List <SymbolValue>();
            SymbolValue        TempSymbolValue = new SymbolValue();


            ClauseSymbols = Query.QueryClause.GetSymbols();
            foreach (String clausey in ClauseSymbols)
            {
                TempSymbolValue.SymbolName = clausey;
                TempSymbolValue.Value      = false; //Set to false by default

                SymbolValues.Add(TempSymbolValue);
            }
            //SymbolValues now contains all symbols but still need to set values
            foreach (List <Boolean> Row in Table)
            {
                for (int ColNum = 0; ColNum < Symbols.Count; ColNum++)
                {
                    for (int ClauseSymbolNum = 0; ClauseSymbolNum < SymbolValues.Count; ClauseSymbolNum++)
                    {
                        if (Symbols[ColNum] == SymbolValues[ClauseSymbolNum].SymbolName)
                        {
                            TempSymbolValue               = SymbolValues[ClauseSymbolNum];
                            TempSymbolValue.Value         = Row[ColNum];
                            SymbolValues[ClauseSymbolNum] = TempSymbolValue;
                        }
                    }
                }

                Row[Symbols.Count + KnowledgeBase.Count + 1] = Query.QueryClause.Evaluate(SymbolValues);
            }

            // Check if query is a subset of the valid worlds of the TT
            // (the rows where SUM clauses = true)

            Boolean QueryResult    = true;
            int     NumValidWorlds = 0;

            foreach (List <Boolean> Row in Table)
            {
                if (Row[Symbols.Count + KnowledgeBase.Count] == true)
                {
                    // if the SUM clauses entry is true, check if the Query entry is true
                    if (Row[Symbols.Count + KnowledgeBase.Count + 1] == true)
                    {
                        // if it's untrue, then change the QueryResult to false.
                        // as the valid worlds / KB is NOT a subset of the query
                        NumValidWorlds++;
                    }
                    else
                    {
                        // if it's false, then don't change the temp bool
                        QueryResult = false;
                    }
                }
            }

            // Format the Result string
            if (Debug)
            {
                this.PrintTT();
            }                                        // for debugging
            if (QueryResult)
            {
                // "When the method is TT and the answer is YES,
                // it should be followed by a colon (:) and
                // the number of models of KB"
                return("YES: " + NumValidWorlds);
            }
            else
            {
                return("NO");
            }
        }
コード例 #6
0
        override public string EvaluateQuery(QueryClass Query, Boolean Debug)
        {
            // first load in all of the symbols (clauses and query)
            LoadSymbolsList(Query);
            string Result;
            // create a list to keep track of the number of unknown premises
            // remaining for each clause
            // Indexed by clause number
            // Initialised to the number of premises in the clause
            List <int> NumPremisesRemaining = new List <int>();
            int        TempInt = 0;

            for (int ClauseNum = 0; ClauseNum < this.KnowledgeBase.Count; ClauseNum++)
            {
                TempInt = this.KnowledgeBase[ClauseNum].GetPremiseSymbols().Count;
                NumPremisesRemaining.Add(TempInt);
            }

            // create a list to keep track of which symbols have been inferred
            List <Boolean> SymbolIsInferred = new List <Boolean>();

            // initialise this list
            for (int SymbolNum = 0; SymbolNum < this.Symbols.Count; SymbolNum++)
            {
                SymbolIsInferred.Add(false);
            }

            // create the agenda of symbols which are to be tested
            List <String> Agenda = new List <String>();
            String        TempString;

            // initially, we populate the agenda with any known symbols

            foreach (HornClauseClass Clause in this.KnowledgeBase)
            {
                if (Clause.Type == HornClauseClassType.Fact)
                {
                    TempString = Clause.GetSymbols()[0];
                    Agenda.Add(TempString);
                }
            }

            String        P;
            List <String> CPremiseSymbols = new List <String>();
            Boolean       TempBool        = false;

            String ResultSymbols = "";

            while (Agenda.Count > 0)
            {
                // while agenda is not empty
                // assign the first item of the agenda to P
                P = Agenda[0];
                // then remove it from the Agenda
                Agenda.RemoveAt(0);

                // return true if P = Q
                if (P == Query.QueryClause.GetPremiseSymbols()[0])
                {
                    Result = "YES: " + ResultSymbols + P;
                    return(Result);
                }

                int SymbolIndex = 0;
                // find the corresponding index for the symbol P
                for (int SymbolNum = 0; SymbolNum < this.Symbols.Count; SymbolNum++)
                {
                    if (this.Symbols[SymbolNum] == P)
                    {
                        SymbolIndex = SymbolNum;
                    }
                }

                // if inferred[p] = false
                // ie. if the symbol P is known to be inferred
                if (SymbolIsInferred[SymbolIndex] == false)
                {
                    // set the inferred[p] to true;
                    SymbolIsInferred[SymbolIndex] = true;
                    ResultSymbols += Symbols[SymbolIndex] + ", ";

                    for (int ClauseNum = 0; ClauseNum < this.KnowledgeBase.Count; ClauseNum++)
                    {
                        // check if P is in C.premise
                        CPremiseSymbols = new List <String>();
                        CPremiseSymbols = this.KnowledgeBase[ClauseNum].GetPremiseSymbols();
                        TempBool        = false;
                        foreach (String Symbol in CPremiseSymbols)
                        {
                            if (Symbol == P)
                            {
                                TempBool = true;
                            }
                        }
                        if (TempBool == true)
                        {
                            // we've found P in this clause
                            // decrement count[ClauseNum]
                            NumPremisesRemaining[ClauseNum] = NumPremisesRemaining[ClauseNum] - 1;
                            // if count[c] = 0 then add c.CONCLUSION to agenda
                            if (NumPremisesRemaining[ClauseNum] == 0)
                            {
                                Agenda.Add(this.KnowledgeBase[ClauseNum].GetConclusionSymbol());
                            }
                        }
                    }
                }
            }
            Result = "NO";
            return(Result);
            // end of FC procedure
        }
コード例 #7
0
 public List <String> Symbols;                                          // List of symbols contained within the KB
 abstract public String EvaluateQuery(QueryClass Query, Boolean Debug); //?????????