001 package aima.logic.propositional.parsing; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import aima.logic.propositional.parsing.ast.BinarySentence; 007 import aima.logic.propositional.parsing.ast.FalseSentence; 008 import aima.logic.propositional.parsing.ast.MultiSentence; 009 import aima.logic.propositional.parsing.ast.Sentence; 010 import aima.logic.propositional.parsing.ast.Symbol; 011 import aima.logic.propositional.parsing.ast.TrueSentence; 012 import aima.logic.propositional.parsing.ast.UnarySentence; 013 014 /** 015 * @author Ravi Mohan 016 * 017 */ 018 019 public class AbstractPLVisitor implements PLVisitor { 020 private PEParser parser = new PEParser(); 021 022 public Object visitSymbol(Symbol s, Object arg) { 023 return new Symbol(s.getValue()); 024 } 025 026 public Object visitTrueSentence(TrueSentence ts, Object arg) { 027 return new TrueSentence(); 028 } 029 030 public Object visitFalseSentence(FalseSentence fs, Object arg) { 031 return new FalseSentence(); 032 } 033 034 public Object visitNotSentence(UnarySentence fs, Object arg) { 035 return new UnarySentence((Sentence) fs.getNegated().accept(this, arg)); 036 } 037 038 public Object visitBinarySentence(BinarySentence fs, Object arg) { 039 return new BinarySentence(fs.getOperator(), (Sentence) fs.getFirst() 040 .accept(this, arg), (Sentence) fs.getSecond().accept(this, arg)); 041 } 042 043 public Object visitMultiSentence(MultiSentence fs, Object arg) { 044 List terms = fs.getSentences(); 045 List<Sentence> newTerms = new ArrayList<Sentence>(); 046 for (int i = 0; i < terms.size(); i++) { 047 Sentence s = (Sentence) terms.get(i); 048 Sentence subsTerm = (Sentence) s.accept(this, arg); 049 newTerms.add(subsTerm); 050 } 051 return new MultiSentence(fs.getOperator(), newTerms); 052 } 053 054 protected Sentence recreate(Object ast) { 055 return (Sentence) parser.parse(((Sentence) ast).toString()); 056 } 057 058 }