001 /* 002 * Created on Aug 2, 2005 003 * 004 */ 005 package aima.learning.inductive; 006 007 import java.util.ArrayList; 008 import java.util.Hashtable; 009 import java.util.List; 010 011 import aima.learning.framework.Example; 012 013 /** 014 * @author Ravi Mohan 015 * 016 */ 017 public class DecisionList { 018 private String positive, negative; 019 020 private List<DLTest> tests; 021 022 private Hashtable<DLTest, String> testOutcomes; 023 024 public DecisionList(String positive, String negative) { 025 this.positive = positive; 026 this.negative = negative; 027 this.tests = new ArrayList<DLTest>(); 028 testOutcomes = new Hashtable<DLTest, String>(); 029 } 030 031 public String predict(Example example) { 032 if (tests.size() == 0) { 033 return negative; 034 } 035 for (DLTest test : tests) { 036 if (test.matches(example)) { 037 return testOutcomes.get(test); 038 } 039 } 040 return negative; 041 } 042 043 public void add(DLTest test, String outcome) { 044 tests.add(test); 045 testOutcomes.put(test, outcome); 046 } 047 048 public DecisionList mergeWith(DecisionList dlist2) { 049 DecisionList merged = new DecisionList(positive, negative); 050 for (DLTest test : tests) { 051 merged.add(test, testOutcomes.get(test)); 052 } 053 for (DLTest test : dlist2.tests) { 054 merged.add(test, dlist2.testOutcomes.get(test)); 055 } 056 return merged; 057 } 058 059 @Override 060 public String toString() { 061 StringBuffer buf = new StringBuffer(); 062 for (DLTest test : tests) { 063 buf.append(test.toString() + " => " + testOutcomes.get(test) 064 + " ELSE \n"); 065 } 066 buf.append("END"); 067 return buf.toString(); 068 } 069 070 }