001    /*
002     * Created on Dec 4, 2004
003     *
004     */
005    package aima.logic.propositional.algorithms;
006    
007    import java.util.List;
008    import java.util.Set;
009    
010    import aima.logic.propositional.parsing.PEParser;
011    import aima.logic.propositional.parsing.ast.Sentence;
012    import aima.logic.propositional.parsing.ast.Symbol;
013    import aima.logic.propositional.visitors.SymbolCollector;
014    import aima.util.Converter;
015    import aima.util.SetOps;
016    import aima.util.Util;
017    
018    /**
019     * @author Ravi Mohan
020     * 
021     */
022    
023    public class TTEntails {
024            public boolean ttEntails(KnowledgeBase kb, String alpha) {
025                    Sentence kbSentence = kb.asSentence();
026                    Sentence querySentence = (Sentence) new PEParser().parse(alpha);
027                    SymbolCollector collector = new SymbolCollector();
028                    Set<Symbol> kbSymbols = collector.getSymbolsIn(kbSentence);
029                    Set<Symbol> querySymbols = collector.getSymbolsIn(querySentence);
030                    Set<Symbol> symbols = new SetOps<Symbol>().union(kbSymbols,
031                                    querySymbols);
032                    List<Symbol> symbolList = new Converter<Symbol>().setToList(symbols);
033                    return ttCheckAll(kbSentence, querySentence, symbolList, new Model());
034            }
035    
036            public boolean ttCheckAll(Sentence kbSentence, Sentence querySentence,
037                            List symbols, Model model) {
038                    if (symbols.isEmpty()) {
039                            if (model.isTrue(kbSentence)) {
040                                    // System.out.println("#");
041                                    return model.isTrue(querySentence);
042                            } else {
043                                    // System.out.println("0");
044                                    return true;
045                            }
046                    } else {
047                            Symbol symbol = (Symbol) Util.first(symbols);
048                            List rest = Util.rest(symbols);
049    
050                            Model trueModel = model.extend(new Symbol(symbol.getValue()), true);
051                            Model falseModel = model.extend(new Symbol(symbol.getValue()),
052                                            false);
053                            return (ttCheckAll(kbSentence, querySentence, rest, trueModel) && (ttCheckAll(
054                                            kbSentence, querySentence, rest, falseModel)));
055                    }
056            }
057    }