001 package aima.learning.knowledge; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import aima.learning.framework.Example; 007 import aima.logic.fol.Connectors; 008 import aima.logic.fol.parsing.ast.ConnectedSentence; 009 import aima.logic.fol.parsing.ast.Constant; 010 import aima.logic.fol.parsing.ast.NotSentence; 011 import aima.logic.fol.parsing.ast.Predicate; 012 import aima.logic.fol.parsing.ast.Sentence; 013 import aima.logic.fol.parsing.ast.Term; 014 015 /** 016 * @author Ciaran O'Reilly 017 * 018 */ 019 public class FOLExample { 020 private FOLDataSetDomain folDSDomain = null; 021 private Example example = null; 022 private int egNo = 0; 023 // 024 private Constant ithExampleConstant = null; 025 private Sentence classification = null; 026 private Sentence description = null; 027 028 // 029 // PUBLIC METHODS 030 // 031 public FOLExample(FOLDataSetDomain folDSDomain, Example example, int egNo) { 032 this.folDSDomain = folDSDomain; 033 this.example = example; 034 this.egNo = egNo; 035 constructFOLEg(); 036 } 037 038 public int getExampleNumber() { 039 return egNo; 040 } 041 042 public Sentence getClassification() { 043 return classification; 044 } 045 046 public Sentence getDescription() { 047 return description; 048 } 049 050 public String toString() { 051 return classification.toString() + " " + Connectors.AND + " " + description.toString(); 052 } 053 054 // 055 // PRIVATE METHODS 056 // 057 private void constructFOLEg() { 058 ithExampleConstant = new Constant(folDSDomain.getExampleConstant(egNo)); 059 060 List<Term> terms = new ArrayList<Term>(); 061 terms.add(ithExampleConstant); 062 // Create the classification sentence 063 classification = new Predicate(folDSDomain.getGoalPredicateName(), terms); 064 if (!example.getAttributeValueAsString( 065 folDSDomain.getDataSetTargetName()).equals( 066 folDSDomain.getTrueGoalValue())) { 067 // if not true then needs to be a Not sentence 068 classification = new NotSentence(classification); 069 } 070 071 // Create the description sentence 072 List<Sentence> descParts = new ArrayList<Sentence>(); 073 for (String dname : folDSDomain.getDescriptionDataSetNames()) { 074 String foldDName = folDSDomain.getFOLName(dname); 075 terms = new ArrayList<Term>(); 076 terms.add(ithExampleConstant); 077 // If multivalued becomes a two place predicate 078 // e.g: Patrons(X1, Some) 079 // otherwise: Hungry(X1) or ~ Hungry(X1) 080 // see pg 679 of AIMA 081 Sentence part = null; 082 if (folDSDomain.isMultivalued(dname)) { 083 terms.add(new Constant(folDSDomain.getFOLName(example.getAttributeValueAsString(dname)))); 084 part = new Predicate(foldDName, terms); 085 } else { 086 part = new Predicate(foldDName, terms); 087 // Need to determine if false 088 if (!folDSDomain.getTrueGoalValue().equals(example.getAttributeValueAsString(dname))) { 089 part = new NotSentence(part); 090 } 091 } 092 descParts.add(part); 093 } 094 if (descParts.size() == 1) { 095 description = descParts.get(0); 096 } else if (descParts.size() > 1) { 097 description = new ConnectedSentence(Connectors.AND, descParts.get(0), descParts.get(1)); 098 for (int i = 2; i < descParts.size(); i++) { 099 description = new ConnectedSentence(Connectors.AND, description, descParts.get(i)); 100 } 101 } 102 } 103 }