001    package aima.logic.propositional.algorithms;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import aima.logic.propositional.parsing.PEParser;
007    import aima.logic.propositional.parsing.ast.Sentence;
008    import aima.logic.propositional.visitors.CNFTransformer;
009    import aima.util.LogicUtils;
010    
011    /**
012     * @author Ravi Mohan
013     */
014    
015    public class KnowledgeBase {
016            private List<Sentence> sentences;
017    
018            private PEParser parser;
019    
020            public KnowledgeBase() {
021                    sentences = new ArrayList<Sentence>();
022                    parser = new PEParser();
023            }
024    
025            public void tell(String aSentence) {
026                    Sentence sentence = (Sentence) parser.parse(aSentence);
027                    if (!(sentences.contains(sentence))) {
028                            sentences.add(sentence);
029                    }
030            }
031    
032            public void tellAll(String[] percepts) {
033                    for (int i = 0; i < percepts.length; i++) {
034                            tell(percepts[i]);
035                    }
036    
037            }
038    
039            public int size() {
040                    return sentences.size();
041            }
042    
043            public Sentence asSentence() {
044                    return LogicUtils.chainWith("AND", sentences);
045            }
046    
047            public boolean askWithDpll(String queryString) {
048                    Sentence query = null, cnfForm = null;
049                    try {
050                            // just a check to see that the query is well formed
051                            query = (Sentence) parser.parse(queryString);
052                    } catch (Exception e) {
053                            System.out.println("error parsing query" + e.getMessage());
054                    }
055    
056                    Sentence kbSentence = asSentence();
057                    Sentence kbPlusQuery = null;
058                    if (kbSentence != null) {
059                            kbPlusQuery = (Sentence) parser.parse(" ( " + kbSentence.toString()
060                                            + " AND " + queryString + " )");
061                    } else {
062                            kbPlusQuery = query;
063                    }
064                    try {
065                            cnfForm = new CNFTransformer().transform(kbPlusQuery);
066                            // System.out.println(cnfForm.toString());
067                    } catch (Exception e) {
068                            System.out.println("error converting kb +  query to CNF"
069                                            + e.getMessage());
070    
071                    }
072                    return new DPLL().dpllSatisfiable(cnfForm);
073            }
074    
075            public boolean askWithTTEntails(String queryString) {
076    
077                    return new TTEntails().ttEntails(this, queryString);
078            }
079    
080            @Override
081            public String toString() {
082                    if (sentences.size() == 0) {
083                            return "";
084                    } else
085                            return asSentence().toString();
086            }
087    
088            public List getSentences() {
089                    return sentences;
090            }
091    }