|
| 1 | +package aima.learning.knowledge; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import aima.learning.framework.Example; |
| 7 | +import aima.logic.fol.Connectors; |
| 8 | +import aima.logic.fol.parsing.ast.ConnectedSentence; |
| 9 | +import aima.logic.fol.parsing.ast.Constant; |
| 10 | +import aima.logic.fol.parsing.ast.NotSentence; |
| 11 | +import aima.logic.fol.parsing.ast.Predicate; |
| 12 | +import aima.logic.fol.parsing.ast.Sentence; |
| 13 | +import aima.logic.fol.parsing.ast.Term; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Ciaran O'Reilly |
| 17 | + * |
| 18 | + */ |
| 19 | +public class FOLExample { |
| 20 | + private FOLDataSetDomain folDSDomain = null; |
| 21 | + private Example example = null; |
| 22 | + private int egNo = 0; |
| 23 | + // |
| 24 | + private Constant ithExampleConstant = null; |
| 25 | + private Sentence classification = null; |
| 26 | + private Sentence description = null; |
| 27 | + |
| 28 | + // |
| 29 | + // PUBLIC METHODS |
| 30 | + // |
| 31 | + public FOLExample(FOLDataSetDomain folDSDomain, Example example, int egNo) { |
| 32 | + this.folDSDomain = folDSDomain; |
| 33 | + this.example = example; |
| 34 | + this.egNo = egNo; |
| 35 | + constructFOLEg(); |
| 36 | + } |
| 37 | + |
| 38 | + public int getExampleNumber() { |
| 39 | + return egNo; |
| 40 | + } |
| 41 | + |
| 42 | + public Sentence getClassification() { |
| 43 | + return classification; |
| 44 | + } |
| 45 | + |
| 46 | + public Sentence getDescription() { |
| 47 | + return description; |
| 48 | + } |
| 49 | + |
| 50 | + public String toString() { |
| 51 | + return classification.toString() + " " + Connectors.AND + " " + description.toString(); |
| 52 | + } |
| 53 | + |
| 54 | + // |
| 55 | + // PRIVATE METHODS |
| 56 | + // |
| 57 | + private void constructFOLEg() { |
| 58 | + ithExampleConstant = new Constant(folDSDomain.getExampleConstant(egNo)); |
| 59 | + |
| 60 | + List<Term> terms = new ArrayList<Term>(); |
| 61 | + terms.add(ithExampleConstant); |
| 62 | + // Create the classification sentence |
| 63 | + classification = new Predicate(folDSDomain.getGoalPredicateName(), terms); |
| 64 | + if (!example.getAttributeValueAsString( |
| 65 | + folDSDomain.getGoalPredicateName()).equals( |
| 66 | + folDSDomain.getTrueGoalValue())) { |
| 67 | + // if not true then needs to be a Not sentence |
| 68 | + classification = new NotSentence(classification); |
| 69 | + } |
| 70 | + |
| 71 | + // Create the description sentence |
| 72 | + List<Sentence> descParts = new ArrayList<Sentence>(); |
| 73 | + for (String dname : folDSDomain.getDescriptionPredicateNames()) { |
| 74 | + terms = new ArrayList<Term>(); |
| 75 | + terms.add(ithExampleConstant); |
| 76 | + // If multivalued becomes a two place predicate |
| 77 | + // e.g: Patrons(X1, Some) |
| 78 | + // otherwise: Hungry(X1) or ~ Hungry(X1) |
| 79 | + // see pg 679 of AIMA |
| 80 | + Sentence part = null; |
| 81 | + if (folDSDomain.isMultivalued(dname)) { |
| 82 | + terms.add(new Constant(example.getAttributeValueAsString(dname))); |
| 83 | + part = new Predicate(dname, terms); |
| 84 | + } else { |
| 85 | + part = new Predicate(dname, terms); |
| 86 | + // Need to determine if false |
| 87 | + if (!folDSDomain.getTrueGoalValue().equals(example.getAttributeValueAsString(dname))) { |
| 88 | + part = new NotSentence(part); |
| 89 | + } |
| 90 | + } |
| 91 | + descParts.add(part); |
| 92 | + } |
| 93 | + if (descParts.size() == 1) { |
| 94 | + description = descParts.get(0); |
| 95 | + } else if (descParts.size() > 1) { |
| 96 | + description = new ConnectedSentence(Connectors.AND, descParts.get(0), descParts.get(1)); |
| 97 | + for (int i = 2; i < descParts.size(); i++) { |
| 98 | + description = new ConnectedSentence(Connectors.AND, description, descParts.get(i)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments